Search in sources :

Example 1 with JsonDBException

use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.

the class JsonRecordConsumer method accept.

@Override
@SuppressWarnings("PMD.CyclomaticComplexity")
public void accept(JsonRecord record) {
    try {
        String path = record.getPath();
        path = Strings.trimSuffix(path.substring(base.length()), "/");
        // Lets see how much of the path we match compared to
        // when we last got called.
        List<String> newPath = new ArrayList<>(Arrays.asList(path.split("/")));
        if (newPath.size() == 1 && newPath.get(0).isEmpty()) {
            newPath.clear();
        }
        // Handle limit options.
        if (this.options.limitToFirst() != null) {
            if (!newPath.get(0).equals(currentRootField)) {
                this.entriesAdded++;
                currentRootField = newPath.get(0);
            }
            if (this.entriesAdded > this.options.limitToFirst()) {
                close();
                return;
            }
        }
        // should we skip over deep records?
        if (this.options.depth() != null && this.options.depth() < newPath.size()) {
            shallowObjects.add(newPath.get(0));
            return;
        }
        int pathMatches = getPathMatches(newPath);
        // we might need to close objects down...
        closeDownStructs(pathMatches);
        // or open some new ones up...
        openUpStructs(newPath, pathMatches);
        if (!currentPath.isEmpty() && jg.getOutputContext().inArray()) {
            JsonRecordSupport.PathPart pathPart = currentPath.getLast();
            String last = newPath.get(newPath.size() - 1);
            int idx = JsonRecordSupport.toArrayIndex(last);
            while (idx > pathPart.getIdx()) {
                // to track the nulls that lead up to the next value.
                pathPart.incrementIdx();
                jg.writeNull();
            }
            // to track the value that we are about to write.
            pathPart.incrementIdx();
        }
        writeValue(record);
    } catch (IOException e) {
        throw new JsonDBException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) JsonDBException(io.syndesis.server.jsondb.JsonDBException) IOException(java.io.IOException)

Example 2 with JsonDBException

use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.

the class SqlJsonDB method getAsStreamingOutput.

@Override
@SuppressWarnings({ "PMD.ExcessiveMethodLength", "PMD.NPathComplexity" })
public Consumer<OutputStream> getAsStreamingOutput(String path, GetOptions options) {
    GetOptions o;
    if (options != null) {
        o = options;
    } else {
        o = new GetOptions();
    }
    // Lets normalize the path a bit
    String baseDBPath = JsonRecordSupport.convertToDBPath(path);
    String like = baseDBPath + "%";
    GetOptions.Order order = o.order();
    if (order == null) {
        order = GetOptions.Order.ASC;
    }
    Consumer<OutputStream> result = null;
    final Handle h = dbi.open();
    try {
        StringBuilder sql = new StringBuilder(250);
        // Creating the iterator could fail with a runtime exception,
        ArrayList<Consumer<Query<Map<String, Object>>>> binds = new ArrayList<>();
        if (o.filter() == null) {
            sql.append("select path,value,ovalue from jsondb where path LIKE :like");
        } else {
            sql.append("SELECT path,value,ovalue FROM jsondb A INNER JOIN (");
            SqlExpressionBuilder.create(this, o.filter(), baseDBPath).build(sql, binds);
            sql.append(") B ON A.path LIKE B.match_path||'%'");
        }
        if (o.startAfter() != null) {
            String startAfter = validateKey(o.startAfter());
            if (o.order() == GetOptions.Order.DESC) {
                sql.append(" and path <= :startAfter");
                binds.add(query -> {
                    String bindPath = baseDBPath + startAfter;
                    query.bind("startAfter", bindPath);
                });
            } else {
                sql.append(" and path >= :startAfter");
                binds.add(query -> {
                    String bindPath = baseDBPath + incrementKey(startAfter);
                    query.bind("startAfter", bindPath);
                });
            }
        }
        if (o.startAt() != null) {
            String startAt = validateKey(o.startAt());
            if (o.order() == GetOptions.Order.DESC) {
                sql.append(" and path < :startAt");
                binds.add(query -> {
                    String bindPath = baseDBPath + incrementKey(startAt);
                    query.bind("startAt", bindPath);
                });
            } else {
                sql.append(" and path >= :startAt");
                binds.add(query -> {
                    String bindPath = baseDBPath + startAt;
                    query.bind("startAt", bindPath);
                });
            }
        }
        if (o.endAt() != null) {
            String endAt = validateKey(o.endAt());
            if (o.order() == GetOptions.Order.DESC) {
                sql.append(" and path > :endAt");
                binds.add(query -> {
                    String value = baseDBPath + endAt;
                    query.bind("endAt", value);
                });
            } else {
                sql.append(" and path < :endAt");
                binds.add(query -> {
                    String bindPath = baseDBPath + incrementKey(endAt);
                    query.bind("endAt", bindPath);
                });
            }
        }
        if (o.endBefore() != null) {
            String endBefore = validateKey(o.endBefore());
            if (o.order() == GetOptions.Order.DESC) {
                sql.append(" and path >= :endBefore");
                binds.add(query -> {
                    String value = baseDBPath + incrementKey(endBefore);
                    query.bind("endBefore", value);
                });
            } else {
                sql.append(" and path < :endBefore");
                binds.add(query -> {
                    String value = baseDBPath + endBefore;
                    query.bind("endBefore", value);
                });
            }
        }
        sql.append(" order by path ").append(order);
        Query<Map<String, Object>> query = h.createQuery(sql.toString()).bind("like", like);
        for (Consumer<Query<Map<String, Object>>> bind : binds) {
            bind.accept(query);
        }
        ResultIterator<JsonRecord> iterator = query.map(JsonRecordMapper.INSTANCE).iterator();
        try {
            // At this point we know if we can produce results..
            if (iterator.hasNext()) {
                result = output -> {
                    try (JsonRecordConsumer toJson = new JsonRecordConsumer(baseDBPath, output, o)) {
                        while (!toJson.isClosed() && iterator.hasNext()) {
                            toJson.accept(iterator.next());
                        }
                    } catch (IOException e) {
                        throw new JsonDBException(e);
                    } finally {
                        iterator.close();
                        h.close();
                    }
                };
            }
        } finally {
            // if we are producing results, then defer closing the iterator
            if (result == null) {
                iterator.close();
            }
        }
    } finally {
        // if we are producing results, then defer closing the handle
        if (result == null) {
            h.close();
        }
    }
    return result;
}
Also used : Query(org.skife.jdbi.v2.Query) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) JsonDBException(io.syndesis.server.jsondb.JsonDBException) IOException(java.io.IOException) GetOptions(io.syndesis.server.jsondb.GetOptions) Handle(org.skife.jdbi.v2.Handle) Consumer(java.util.function.Consumer) Map(java.util.Map)

Example 3 with JsonDBException

use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.

the class PodLogMonitor method processLine.

@SuppressWarnings("PMD.CyclomaticComplexity")
private void processLine(byte[] line) throws IOException {
    // 2018-01-12T21:22:02.068338027Z { ..... }
    if (// not long enough
    line.length < 32 || // expecting space
    line[30] != ' ' || // expecting the json data starting here.
    line[31] != '{') {
        return;
    }
    String time = new String(line, 0, 30, StandardCharsets.US_ASCII);
    try {
        @SuppressWarnings("unchecked") Map<String, Object> // NOPMD
        json = Json.reader().forType(HashMap.class).readValue(line, 31, line.length - 31);
        // are the required fields set?
        String id = validate((String) json.remove("id"));
        String exchange = validate((String) json.remove("exchange"));
        InflightData inflightData = getInflightData(exchange, time);
        String step = (String) json.remove("step");
        if (step == null) {
            // Looks like an exchange level logging event.
            Boolean failed = (Boolean) json.remove("failed");
            if (failed != null) {
                inflightData.activity.setFailed(failed);
            }
            String status = (String) json.remove("status");
            inflightData.metadata.putAll(json);
            if (status != null) {
                inflightData.activity.setStatus(status);
                if ("done".equals(status)) {
                    inflightData.activity.setSteps(new ArrayList<>(inflightData.steps.values()));
                    if (!inflightData.metadata.isEmpty()) {
                        inflightData.activity.setMetadata(toJsonNode(inflightData.metadata));
                    }
                    String activityAsString = Json.writer().writeValueAsString(inflightData.activity);
                    String transactionPath = format("/exchanges/%s/%s", integrationId, exchange);
                    inflightActivities.remove(exchange);
                    logsController.eventQueue.put(batch -> {
                        // Do as little as possible in here, single thread processes the event queue.
                        batch.put(transactionPath, activityAsString);
                        trackState(time, batch);
                    });
                }
            }
        } else {
            // Looks like a step level logging event.
            ActivityStep as = inflightData.getStep(step, id);
            String message = (String) json.remove("message");
            if (message != null) {
                if (as.getMessages() == null) {
                    as.setMessages(new ArrayList<>());
                }
                as.getMessages().add(message);
            }
            String failure = (String) json.remove("failure");
            if (failure != null) {
                as.setFailure(failure);
            }
            Number duration = (Number) json.remove("duration");
            if (duration != null) {
                as.setDuration(duration.longValue());
            }
            if (!json.isEmpty()) {
                if (as.getEvents() == null) {
                    as.setEvents(new ArrayList<>());
                }
                as.getEvents().add(toJsonNode(json));
            }
        }
    } catch (JsonDBException | ClassCastException | IOException ignored) {
    // / log record not in the expected format.
    } catch (InterruptedException e) {
        final InterruptedIOException rethrow = new InterruptedIOException(e.getMessage());
        rethrow.initCause(e);
        throw rethrow;
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonDBException(io.syndesis.server.jsondb.JsonDBException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ActivityStep(io.syndesis.server.endpoint.v1.handler.activity.ActivityStep) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 4 with JsonDBException

use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.

the class MemorySqlJsonDB method create.

public static CloseableJsonDB create(Collection<Index> indexes) {
    JdbcDataSource ds = new JdbcDataSource();
    DBI dbi = new DBI(ds);
    ds.setURL("jdbc:h2:mem:" + KeyGenerator.createKey() + ";MODE=PostgreSQL");
    try {
        Connection keepsDBOpen = ds.getConnection();
        ClosableSqlJsonDB result = new ClosableSqlJsonDB(keepsDBOpen, dbi, indexes);
        result.createTables();
        return result;
    } catch (SQLException e) {
        throw new JsonDBException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) DBI(org.skife.jdbi.v2.DBI) JsonDBException(io.syndesis.server.jsondb.JsonDBException)

Example 5 with JsonDBException

use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.

the class SqlJsonDB method set.

@Override
public void set(String path, InputStream body) {
    withTransaction(dbi -> {
        BatchManager mb = new BatchManager(dbi);
        String baseDBPath = JsonRecordSupport.convertToDBPath(path);
        mb.deleteRecordsForSet(baseDBPath);
        try {
            JsonRecordSupport.jsonStreamToRecords(indexPaths, baseDBPath, body, mb.createSetConsumer());
        } catch (IOException e) {
            throw new JsonDBException(e);
        }
        mb.flush();
    });
    if (bus != null) {
        bus.broadcast("jsondb-updated", prefix(trimSuffix(path, "/"), "/"));
    }
}
Also used : JsonDBException(io.syndesis.server.jsondb.JsonDBException) IOException(java.io.IOException)

Aggregations

JsonDBException (io.syndesis.server.jsondb.JsonDBException)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 ActivityStep (io.syndesis.server.endpoint.v1.handler.activity.ActivityStep)1 GetOptions (io.syndesis.server.jsondb.GetOptions)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 DBI (org.skife.jdbi.v2.DBI)1