Search in sources :

Example 36 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class AbstractSerializer method writeQuery.

@Override
public Query writeQuery(Query query) {
    HugeType type = query.resultType();
    // Serialize edge condition query (TODO: add VEQ(for EOUT/EIN))
    if (type.isEdge() && query.conditionsSize() > 0) {
        if (query.idsSize() > 0) {
            throw new BackendException("Not supported query edge by id " + "and by condition at the same time");
        }
        Query result = this.writeQueryEdgeCondition(query);
        if (result != null) {
            return result;
        }
    }
    // Serialize id in query
    if (query.idsSize() == 1 && query instanceof IdQuery.OneIdQuery) {
        IdQuery.OneIdQuery result = (IdQuery.OneIdQuery) query.copy();
        result.resetId(this.writeQueryId(type, result.id()));
        query = result;
    } else if (query.idsSize() > 0 && query instanceof IdQuery) {
        IdQuery result = (IdQuery) query.copy();
        result.resetIds();
        for (Id id : query.ids()) {
            result.query(this.writeQueryId(type, id));
        }
        query = result;
    }
    // Serialize condition(key/value) in query
    if (query instanceof ConditionQuery && query.conditionsSize() > 0) {
        query = this.writeQueryCondition(query);
    }
    return query;
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 37 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class InMemoryDBTable method query.

@Override
public Iterator<BackendEntry> query(BackendSession session, Query query) {
    String page = query.page();
    if (page != null && !page.isEmpty()) {
        throw new NotSupportException("paging by InMemoryDBStore");
    }
    Map<Id, BackendEntry> rs = this.store;
    if (query instanceof IdPrefixQuery) {
        IdPrefixQuery pq = (IdPrefixQuery) query;
        rs = this.queryByIdPrefix(pq.start(), pq.inclusiveStart(), pq.prefix(), rs);
    }
    if (query instanceof IdRangeQuery) {
        IdRangeQuery rq = (IdRangeQuery) query;
        rs = this.queryByIdRange(rq.start(), rq.inclusiveStart(), rq.end(), rq.inclusiveEnd(), rs);
    }
    // Query by id(s)
    if (query.idsSize() > 0) {
        rs = this.queryById(query.ids(), rs);
    }
    // Query by condition(s)
    if (query.conditionsSize() > 0) {
        ConditionQuery condQuery = (ConditionQuery) query;
        if (condQuery.containsScanRelation()) {
            return this.queryByRange(condQuery);
        }
        rs = this.queryByFilter(query.conditions(), rs);
    }
    Iterator<BackendEntry> iterator = rs.values().iterator();
    long offset = query.offset() - query.actualOffset();
    if (offset >= rs.size()) {
        query.goOffset(rs.size());
        return QueryResults.emptyIterator();
    }
    if (offset > 0L) {
        query.goOffset(offset);
        iterator = this.skipOffset(iterator, offset);
    }
    if (!query.noLimit() && query.total() < rs.size()) {
        iterator = this.dropTails(iterator, query.limit());
    }
    return iterator;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Id(com.baidu.hugegraph.backend.id.Id) IdRangeQuery(com.baidu.hugegraph.backend.query.IdRangeQuery) IdPrefixQuery(com.baidu.hugegraph.backend.query.IdPrefixQuery)

Example 38 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class GraphTransaction method traverseByLabel.

private <T> void traverseByLabel(SchemaLabel label, Function<Query, Iterator<T>> fetcher, Consumer<T> consumer, boolean deleting) {
    HugeType type = label.type() == HugeType.VERTEX_LABEL ? HugeType.VERTEX : HugeType.EDGE;
    Query query = label.enableLabelIndex() ? new ConditionQuery(type) : new Query(type);
    query.capacity(Query.NO_CAPACITY);
    query.limit(Query.NO_LIMIT);
    if (this.store().features().supportsQueryByPage()) {
        query.page(PageInfo.PAGE_NONE);
    }
    if (label.hidden()) {
        query.showHidden(true);
    }
    query.showDeleting(deleting);
    query.showExpired(deleting);
    if (label.enableLabelIndex()) {
        // Support label index, query by label index by paging
        assert query instanceof ConditionQuery;
        ((ConditionQuery) query).eq(HugeKeys.LABEL, label.id());
        Iterator<T> iter = fetcher.apply(query);
        try {
            // Fetch by paging automatically
            while (iter.hasNext()) {
                consumer.accept(iter.next());
                /*
                     * Commit per batch to avoid too much data in single commit,
                     * especially for Cassandra backend
                     */
                this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
            }
            // Commit changes if exists
            this.commit();
        } finally {
            CloseableIterator.closeIterator(iter);
        }
    } else {
        // Not support label index, query all and filter by label
        if (query.paging()) {
            query.limit(this.pageSize);
        }
        String page = null;
        do {
            Iterator<T> iter = fetcher.apply(query);
            try {
                while (iter.hasNext()) {
                    T e = iter.next();
                    SchemaLabel elemLabel = ((HugeElement) e).schemaLabel();
                    if (label.equals(elemLabel)) {
                        consumer.accept(e);
                        /*
                             * Commit per batch to avoid too much data in single
                             * commit, especially for Cassandra backend
                             */
                        this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
                    }
                }
                // Commit changes of every page before next page query
                this.commit();
                if (query.paging()) {
                    page = PageInfo.pageState(iter).toString();
                    query.page(page);
                }
            } finally {
                CloseableIterator.closeIterator(iter);
            }
        } while (page != null);
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 39 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class GraphTransaction method optimizeQueries.

private <R> QueryList<R> optimizeQueries(Query query, QueryResults.Fetcher<R> fetcher) {
    boolean supportIn = this.storeFeatures().supportsQueryWithInCondition();
    QueryList<R> queries = new QueryList<>(query, fetcher);
    for (ConditionQuery cq : ConditionQueryFlatten.flatten((ConditionQuery) query, supportIn)) {
        // Optimize by sysprop
        Query q = this.optimizeQuery(cq);
        /*
             * NOTE: There are two possibilities for this query:
             * 1.sysprop-query, which would not be empty.
             * 2.index-query result(ids after optimization), which may be empty.
             */
        if (q == null) {
            queries.add(this.indexQuery(cq), this.batchSize);
        } else if (!q.empty()) {
            queries.add(q);
        }
    }
    return queries;
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) QueryList(com.baidu.hugegraph.backend.page.QueryList)

Example 40 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class GraphTransaction method constructEdgesQuery.

/**
 * Construct one edge condition query based on source vertex, direction and
 * edge labels
 * @param sourceVertex source vertex of edge
 * @param direction only be "IN", "OUT" or "BOTH"
 * @param edgeLabels edge labels of queried edges
 * @return constructed condition query
 */
@Watched
public static ConditionQuery constructEdgesQuery(Id sourceVertex, Directions direction, Id... edgeLabels) {
    E.checkState(sourceVertex != null, "The edge query must contain source vertex");
    E.checkState(direction != null, "The edge query must contain direction");
    ConditionQuery query = new ConditionQuery(HugeType.EDGE);
    // Edge source vertex
    query.eq(HugeKeys.OWNER_VERTEX, sourceVertex);
    // Edge direction
    if (direction == Directions.BOTH) {
        query.query(Condition.or(Condition.eq(HugeKeys.DIRECTION, Directions.OUT), Condition.eq(HugeKeys.DIRECTION, Directions.IN)));
    } else {
        assert direction == Directions.OUT || direction == Directions.IN;
        query.eq(HugeKeys.DIRECTION, direction);
    }
    // Edge labels
    if (edgeLabels.length == 1) {
        query.eq(HugeKeys.LABEL, edgeLabels[0]);
    } else if (edgeLabels.length > 1) {
        query.query(Condition.in(HugeKeys.LABEL, Arrays.asList(edgeLabels)));
    } else {
        assert edgeLabels.length == 0;
    }
    return query;
}
Also used : ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)59 Id (com.baidu.hugegraph.backend.id.Id)19 Test (org.junit.Test)19 Condition (com.baidu.hugegraph.backend.query.Condition)17 HugeGraph (com.baidu.hugegraph.HugeGraph)13 ArrayList (java.util.ArrayList)12 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)10 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)10 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)8 Collection (java.util.Collection)8 Query (com.baidu.hugegraph.backend.query.Query)7 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)7 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)7 Map (java.util.Map)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)7 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)6 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)5 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)5 HugeType (com.baidu.hugegraph.type.HugeType)5 ImmutableMap (com.google.common.collect.ImmutableMap)5