Search in sources :

Example 16 with Query

use of com.baidu.hugegraph.backend.query.Query 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 17 with Query

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

the class SortByCountIdHolderList method add.

@Override
public boolean add(IdHolder holder) {
    if (this.paging()) {
        return super.add(holder);
    }
    this.mergedHolders.add(holder);
    if (super.isEmpty()) {
        Query parent = holder.query().originQuery();
        super.add(new SortByCountIdHolder(parent));
    }
    SortByCountIdHolder sortHolder = (SortByCountIdHolder) this.get(0);
    sortHolder.merge(holder);
    return true;
}
Also used : Query(com.baidu.hugegraph.backend.query.Query)

Example 18 with Query

use of com.baidu.hugegraph.backend.query.Query 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 19 with Query

use of com.baidu.hugegraph.backend.query.Query 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 20 with Query

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

the class AbstractTransaction method queryNumber.

@Watched(prefix = "tx")
public Number queryNumber(Query query) {
    LOG.debug("Transaction queryNumber: {}", query);
    E.checkArgument(query.aggregate() != null, "The aggregate must be set for number query: %s", query);
    Query squery = this.serializer.writeQuery(query);
    this.beforeRead();
    try {
        return this.store.queryNumber(squery);
    } finally {
        this.afterRead();
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

Query (com.baidu.hugegraph.backend.query.Query)31 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)24 Id (com.baidu.hugegraph.backend.id.Id)11 HugeGraph (com.baidu.hugegraph.HugeGraph)9 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)9 HugeType (com.baidu.hugegraph.type.HugeType)7 Iterator (java.util.Iterator)7 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)6 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)6 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)5 Edge (org.apache.tinkerpop.gremlin.structure.Edge)5 Test (org.junit.Test)5 HugeException (com.baidu.hugegraph.HugeException)4 BackendException (com.baidu.hugegraph.backend.BackendException)4 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)4 ArrayList (java.util.ArrayList)4 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3