Search in sources :

Example 26 with Query

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);
}
Also used : Intervals(org.apache.druid.java.util.common.Intervals) Logger(org.apache.druid.java.util.common.logger.Logger) JacksonUtils(org.apache.druid.java.util.common.jackson.JacksonUtils) Iterator(java.util.Iterator) Intervals(org.apache.druid.java.util.common.Intervals) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringUtils(org.apache.druid.java.util.common.StringUtils) Query(org.skife.jdbi.v2.Query) Collectors(java.util.stream.Collectors) Iterators(com.google.common.collect.Iterators) Interval(org.joda.time.Interval) List(java.util.List) CloseableIterators(org.apache.druid.java.util.common.CloseableIterators) Handle(org.skife.jdbi.v2.Handle) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) DataSegment(org.apache.druid.timeline.DataSegment) ResultIterator(org.skife.jdbi.v2.ResultIterator) PreparedBatch(org.skife.jdbi.v2.PreparedBatch) IAE(org.apache.druid.java.util.common.IAE) SegmentId(org.apache.druid.timeline.SegmentId) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Collections(java.util.Collections) Map(java.util.Map) DataSegment(org.apache.druid.timeline.DataSegment) Interval(org.joda.time.Interval)

Example 27 with Query

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;
}
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 28 with Query

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();
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 29 with Query

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");
}
Also used : Query(org.jpl7.Query)

Example 30 with Query

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");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Aggregations

Query (org.jpl7.Query)88 Term (org.jpl7.Term)52 Variable (org.jpl7.Variable)32 Query (org.hypertrace.entity.data.service.v1.Query)31 Map (java.util.Map)28 Test (org.junit.jupiter.api.Test)23 Atom (org.jpl7.Atom)21 Compound (org.jpl7.Compound)18 Test (org.junit.Test)18 Filter (org.hypertrace.core.documentstore.Filter)16 Query (com.google.datastore.v1.Query)14 AttributeFilter (org.hypertrace.entity.data.service.v1.AttributeFilter)14 GqlQuery (com.google.datastore.v1.GqlQuery)7 ArrayList (java.util.ArrayList)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Entity (org.hypertrace.entity.data.service.v1.Entity)6 Integer (org.jpl7.Integer)6 Query (org.molgenis.emx2.Query)6 IOException (java.io.IOException)5 Collections (java.util.Collections)5