use of org.molgenis.emx2.Query in project druid by druid-io.
the class SqlSegmentsMetadataQuery method retrieveSegments.
private CloseableIterator<DataSegment> retrieveSegments(final String dataSource, final Collection<Interval> intervals, final IntervalMode matchMode, final boolean used) {
// Check if the intervals all support comparing as strings. If so, bake them into the SQL.
final boolean compareAsString = intervals.stream().allMatch(Intervals::canCompareEndpointsAsStrings);
final StringBuilder sb = new StringBuilder();
sb.append("SELECT payload FROM %s WHERE used = :used AND dataSource = :dataSource");
if (compareAsString && !intervals.isEmpty()) {
sb.append(" AND (");
for (int i = 0; i < intervals.size(); i++) {
sb.append(matchMode.makeSqlCondition(connector.getQuoteString(), StringUtils.format(":start%d", i), StringUtils.format(":end%d", i)));
if (i == intervals.size() - 1) {
sb.append(")");
} else {
sb.append(" OR ");
}
}
}
final Query<Map<String, Object>> sql = handle.createQuery(StringUtils.format(sb.toString(), dbTables.getSegmentsTable())).setFetchSize(connector.getStreamingFetchSize()).bind("used", used).bind("dataSource", dataSource);
if (compareAsString) {
final Iterator<Interval> iterator = intervals.iterator();
for (int i = 0; iterator.hasNext(); i++) {
Interval interval = iterator.next();
sql.bind(StringUtils.format("start%d", i), interval.getStart().toString()).bind(StringUtils.format("end%d", i), interval.getEnd().toString());
}
}
final ResultIterator<DataSegment> resultIterator = sql.map((index, r, ctx) -> JacksonUtils.readValue(jsonMapper, r.getBytes(1), DataSegment.class)).iterator();
return CloseableIterators.wrap(Iterators.filter(resultIterator, dataSegment -> {
if (intervals.isEmpty()) {
return true;
} else {
// segment interval like "20010/20011".)
for (Interval interval : intervals) {
if (matchMode.apply(interval, dataSegment.getInterval())) {
return true;
}
}
return false;
}
}), resultIterator);
}
use of org.molgenis.emx2.Query 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 org.molgenis.emx2.Query in project packages-jpl by SWI-Prolog.
the class FamilyMT method run.
public void run() {
Map<String, Term> solution;
// Variable X = new Variable("X");
//
Query q2 = new Query("child_of(joe,ralf)");
System.err.println("child_of(joe,ralf) is " + (q2.hasSolution() ? "provable" : "not provable"));
new Query("sleep(?)", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
//
Query q3 = new Query("descendent_of(steve,ralf)");
System.err.println("descendent_of(steve,ralf) is " + (q3.hasSolution() ? "provable" : "not provable"));
delay();
//
Query q4 = new Query("descendent_of(X, ralf)");
solution = q4.oneSolution();
System.err.println("first solution of descendent_of(X, ralf)");
System.err.println("X = " + solution.get("X"));
delay();
//
Map<String, Term>[] solutions = q4.allSolutions();
System.err.println("all solutions of descendent_of(X, ralf)");
for (int i = 0; i < solutions.length; i++) {
System.err.println("X = " + solutions[i].get("X"));
}
delay();
//
System.err.println("each solution of descendent_of(X, ralf)");
while (q4.hasMoreSolutions()) {
solution = q4.nextSolution();
System.err.println("X = " + solution.get("X"));
}
delay();
//
Query q5 = new Query("descendent_of(X, Y)");
System.err.println(id + ": each solution of descendent_of(X, Y)");
while (q5.hasMoreSolutions()) {
solution = q5.nextSolution();
System.err.println(id + ": X = " + solution.get("X") + ", Y = " + solution.get("Y"));
delay();
}
}
use of org.molgenis.emx2.Query in project packages-jpl by SWI-Prolog.
the class Test method test_3.
static void test_3() {
System.out.print("test 3...");
Query q3 = new Query("p", new Term[] { pair_a_b });
if (!q3.hasSolution()) {
System.out.println("p(a-b) failed");
// System.exit(1);
}
System.out.println("passed");
}
use of org.molgenis.emx2.Query in project packages-jpl by SWI-Prolog.
the class Test method test_6.
static void test_6() {
System.out.print("test 6...");
Variable X = new Variable("X");
Query q6 = new Query("p", new Term[] { X, X });
Term[] x_target = new Term[] { a };
Map<String, Term>[] solutions = q6.allSolutions();
if (solutions.length != 1) {
System.out.println("p(X, X) failed:");
System.out.println("\tExpected: 1 solution");
System.out.println("\tGot: " + solutions.length);
// System.exit(1);
}
for (int i = 0; i < solutions.length; ++i) {
Object x_binding = solutions[i].get("X");
if (!x_binding.equals(x_target[i])) {
System.out.println("p(X, X) failed:");
System.out.println("\tExpected: " + x_target[i]);
System.out.println("\tGot: " + x_binding);
// System.exit(1);
}
}
System.out.println("passed");
}
Aggregations