Search in sources :

Example 1 with GetOptions

use of io.syndesis.server.jsondb.GetOptions 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 2 with GetOptions

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

the class DBActivityTrackingService method getActivities.

@Override
public List<Activity> getActivities(String integrationId, String from, Integer requestedLimit) throws IOException {
    String path = "/activity/exchanges/" + integrationId;
    int limit = 10;
    if (requestedLimit != null) {
        limit = requestedLimit;
    }
    if (limit > 1000) {
        // max out to 1000 per request.
        limit = 1000;
    }
    GetOptions options = new GetOptions().order(// reverse the order since we want most recent exchanges first.
    GetOptions.Order.DESC).startAfter(from).limitToFirst(// allow paging
    limit);
    byte[] data = jsondb.getAsByteArray(path, options);
    if (data == null) {
        return new ArrayList<>();
    }
    JsonNode map = Json.reader().readTree(new ByteArrayInputStream(data));
    List<Activity> rc = new ArrayList<>();
    Iterator<Map.Entry<String, JsonNode>> i = map.fields();
    while (i.hasNext()) {
        Map.Entry<String, JsonNode> entry = i.next();
        try {
            String value = entry.getValue().textValue();
            Activity activity = Json.reader().forType(Activity.class).readValue(value);
            if (activity.getSteps() == null) {
                activity.setSteps(new ArrayList<ActivityStep>());
            }
            rc.add(activity);
        } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException ignored) {
            // We could get stuff like class cast exceptions..
            LOG.debug("Could convert entry: {}", entry, ignored);
        }
    }
    return rc;
}
Also used : ArrayList(java.util.ArrayList) Activity(io.syndesis.server.endpoint.v1.handler.activity.Activity) JsonNode(com.fasterxml.jackson.databind.JsonNode) GetOptions(io.syndesis.server.jsondb.GetOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) ActivityStep(io.syndesis.server.endpoint.v1.handler.activity.ActivityStep) Map(java.util.Map)

Example 3 with GetOptions

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

the class JsonDBResource method get.

@Produces({ APPLICATION_JSON, APPLICATION_JAVASCRIPT })
@Path("/{path: .*}.json")
@GET
public Response get(@PathParam("path") String path, @QueryParam("print") String print, @QueryParam("shallow") Boolean shallow, @QueryParam("callback") String callback) {
    GetOptions options = new GetOptions();
    if ("pretty".equals(print)) {
        options.prettyPrint(true);
    } else if ("silent".equals(print)) {
        if (jsondb.exists(path)) {
            return Response.noContent().build();
        }
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    if (shallow != null) {
        options.depth(1);
    }
    String contentType = APPLICATION_JSON;
    if (callback != null) {
        contentType = APPLICATION_JAVASCRIPT;
        options.callback(callback);
    }
    Consumer<OutputStream> stream = jsondb.getAsStreamingOutput(path, options);
    if (stream == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    StreamingOutput streamingOutput = x -> stream.accept(x);
    return Response.ok(streamingOutput).header(CONTENT_TYPE, contentType).build();
}
Also used : OutputStream(java.io.OutputStream) CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) PathParam(javax.ws.rs.PathParam) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) StreamingOutput(javax.ws.rs.core.StreamingOutput) JsonDB(io.syndesis.server.jsondb.JsonDB) Consumer(java.util.function.Consumer) GetOptions(io.syndesis.server.jsondb.GetOptions) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) Map(java.util.Map) PUT(javax.ws.rs.PUT) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) InputStream(java.io.InputStream) DELETE(javax.ws.rs.DELETE) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) GetOptions(io.syndesis.server.jsondb.GetOptions) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with GetOptions

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

the class JsonDBTest method testGetStartAtEndAt.

@Test
public void testGetStartAtEndAt() throws IOException {
    jsondb.set("/test", mapper.writeValueAsString(map("user1", "1", "user2:1", "2", "user2:2", "3", "user2:3", "4", "user4", "5")));
    String json = jsondb.getAsString("/test", new GetOptions().startAt("user2:").endAt("user2:"));
    assertThat(json).isEqualTo("{\"user2:1\":\"2\",\"user2:2\":\"3\",\"user2:3\":\"4\"}");
}
Also used : GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Example 5 with GetOptions

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

the class JsonDBTest method testGetDepth1.

@Test
public void testGetDepth1() throws IOException {
    jsondb.set("/test", mapper.writeValueAsString(map("name", "Hiram Chirino", "props", map("city", "Tampa", "state", "FL"))));
    String json = jsondb.getAsString("/test", new GetOptions().depth(1));
    assertThat(json).isEqualTo("{\"name\":\"Hiram Chirino\",\"props\":true}");
    jsondb.delete("/test");
    jsondb.set("/test/a1/b1/c1", "1");
    jsondb.set("/test/a1/b2/c1", "2");
    jsondb.set("/test/a2/b3/c1", "3");
    jsondb.set("/test/a3/b4/c1", "4");
    jsondb.set("/test/a4/b5/c1", "5");
    json = jsondb.getAsString("/test", new GetOptions().depth(1));
    assertThat(json).isEqualTo("{\"a1\":true,\"a2\":true,\"a3\":true,\"a4\":true}");
}
Also used : GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Aggregations

GetOptions (io.syndesis.server.jsondb.GetOptions)13 Test (org.junit.Test)9 Map (java.util.Map)3 Consumer (java.util.function.Consumer)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 MapType (com.fasterxml.jackson.databind.type.MapType)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 PodListBuilder (io.fabric8.kubernetes.api.model.PodListBuilder)1 ListResult (io.syndesis.common.model.ListResult)1 IdPrefixFilter (io.syndesis.server.dao.manager.operators.IdPrefixFilter)1 Activity (io.syndesis.server.endpoint.v1.handler.activity.Activity)1 ActivityStep (io.syndesis.server.endpoint.v1.handler.activity.ActivityStep)1 JsonDB (io.syndesis.server.jsondb.JsonDB)1 JsonDBException (io.syndesis.server.jsondb.JsonDBException)1 InputStream (java.io.InputStream)1