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