use of com.baidu.hugegraph.schema.SchemaLabel 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.schema.SchemaLabel in project incubator-hugegraph by apache.
the class GraphIndexTransaction method collectMatchedIndexes.
@Watched(prefix = "index")
private Set<MatchedIndex> collectMatchedIndexes(ConditionQuery query) {
SchemaTransaction schema = this.params().schemaTransaction();
Id label = query.condition(HugeKeys.LABEL);
List<? extends SchemaLabel> schemaLabels;
if (label != null) {
// Query has LABEL condition
SchemaLabel schemaLabel;
if (query.resultType().isVertex()) {
schemaLabel = schema.getVertexLabel(label);
} else if (query.resultType().isEdge()) {
schemaLabel = schema.getEdgeLabel(label);
} else {
throw new AssertionError(String.format("Unsupported index query type: %s", query.resultType()));
}
schemaLabels = ImmutableList.of(schemaLabel);
} else {
// Query doesn't have LABEL condition
if (query.resultType().isVertex()) {
schemaLabels = schema.getVertexLabels();
} else if (query.resultType().isEdge()) {
schemaLabels = schema.getEdgeLabels();
} else {
throw new AssertionError(String.format("Unsupported index query type: %s", query.resultType()));
}
}
// Collect MatchedIndex for each SchemaLabel
Set<MatchedIndex> matchedIndexes = InsertionOrderUtil.newSet();
for (SchemaLabel schemaLabel : schemaLabels) {
MatchedIndex index = this.collectMatchedIndex(schemaLabel, query);
if (index != null) {
matchedIndexes.add(index);
}
}
return matchedIndexes;
}
use of com.baidu.hugegraph.schema.SchemaLabel in project incubator-hugegraph by apache.
the class GraphIndexTransaction method queryByLabel.
@Watched(prefix = "index")
private IdHolderList queryByLabel(ConditionQuery query) {
HugeType queryType = query.resultType();
IndexLabel il = IndexLabel.label(queryType);
validateIndexLabel(il);
Id label = query.condition(HugeKeys.LABEL);
assert label != null;
HugeType indexType;
SchemaLabel schemaLabel;
if (queryType.isVertex()) {
indexType = HugeType.VERTEX_LABEL_INDEX;
schemaLabel = this.graph().vertexLabel(label);
} else if (queryType.isEdge()) {
indexType = HugeType.EDGE_LABEL_INDEX;
schemaLabel = this.graph().edgeLabel(label);
} else {
throw new HugeException("Can't query %s by label", queryType);
}
if (!this.store().features().supportsQueryByLabel() && !schemaLabel.enableLabelIndex()) {
throw new NoIndexException("Don't accept query by label '%s', " + "label index is disabled", schemaLabel);
}
ConditionQuery indexQuery = new ConditionQuery(indexType, query);
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, il.id());
indexQuery.eq(HugeKeys.FIELD_VALUES, label);
/*
* We can avoid redundant element ids if set limit, but if there are
* label index overridden by other vertices with different label,
* query with limit like g.V().hasLabel('xx').limit(10) may lose some
* results, so can't set limit here. But in this case, the following
* query results may be still different:
* g.V().hasLabel('xx').count() // label index count
* g.V().hasLabel('xx').limit(-1).count() // actual vertices count
* It’s a similar situation for the offset, like:
* g.V().hasLabel('xx').range(26, 27)
* g.V().hasLabel('xx').range(27, 28)
* we just reset limit here, but don't reset offset due to performance
* optimization with index+offset query, see Query.skipOffsetIfNeeded().
* NOTE: if set offset the backend itself will skip the offset
*/
indexQuery.copyBasic(query);
indexQuery.limit(Query.NO_LIMIT);
IdHolder idHolder = this.doIndexQuery(il, indexQuery);
IdHolderList holders = new IdHolderList(query.paging());
holders.add(idHolder);
return holders;
}
use of com.baidu.hugegraph.schema.SchemaLabel in project incubator-hugegraph by apache.
the class IndexLabelBuilder method build.
@Override
public IndexLabel build() {
Id id = this.validOrGenerateId(HugeType.INDEX_LABEL, this.id, this.name);
this.checkBaseType();
this.checkIndexType();
HugeGraph graph = this.graph();
this.checkFields4Range();
IndexLabel indexLabel = new IndexLabel(graph, id, this.name);
indexLabel.baseType(this.baseType);
SchemaLabel schemaLabel = this.loadBaseLabel();
indexLabel.baseValue(schemaLabel.id());
indexLabel.indexType(this.indexType);
for (String field : this.indexFields) {
PropertyKey propertyKey = graph.propertyKey(field);
indexLabel.indexField(propertyKey.id());
}
indexLabel.userdata(this.userdata);
return indexLabel;
}
use of com.baidu.hugegraph.schema.SchemaLabel in project incubator-hugegraph by apache.
the class IndexLabelBuilder method eliminate.
@Override
public IndexLabel eliminate() {
IndexLabel indexLabel = this.indexLabelOrNull(this.name);
if (indexLabel == null) {
throw new NotFoundException("Can't update index label '%s' " + "since it doesn't exist", this.name);
}
this.checkStableVars();
Userdata.check(this.userdata, Action.ELIMINATE);
indexLabel.removeUserdata(this.userdata);
SchemaLabel schemaLabel = indexLabel.baseLabel();
this.graph().addIndexLabel(schemaLabel, indexLabel);
return indexLabel;
}
Aggregations