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