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;
}
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;
}
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();
}
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\"}");
}
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}");
}
Aggregations