use of com.baidu.hugegraph.perf.PerfUtil.Watched 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.perf.PerfUtil.Watched in project incubator-hugegraph by apache.
the class GraphIndexTransaction method collectMatchedIndex.
/**
* Collect matched IndexLabel(s) in a SchemaLabel for a query
* @param schemaLabel find indexLabels of this schemaLabel
* @param query conditions container
* @return MatchedLabel object contains schemaLabel and matched indexLabels
*/
@Watched(prefix = "index")
private MatchedIndex collectMatchedIndex(SchemaLabel schemaLabel, ConditionQuery query) {
SchemaTransaction schema = this.params().schemaTransaction();
Set<IndexLabel> ils = InsertionOrderUtil.newSet();
for (Id il : schemaLabel.indexLabels()) {
IndexLabel indexLabel = schema.getIndexLabel(il);
/*
* Method schema#getIndexLabel may return null here
* because the indexLabel is being created at this time
* and has not been saved to the backend storage
*/
if (indexLabel == null || indexLabel.indexType().isUnique()) {
continue;
}
ils.add(indexLabel);
}
if (this.graph().readMode().showOlap()) {
for (IndexLabel il : schema.getIndexLabels()) {
if (il.olap()) {
ils.add(il);
}
}
}
if (ils.isEmpty()) {
return null;
}
// Try to match single or composite index
Set<IndexLabel> matchedILs = matchSingleOrCompositeIndex(query, ils);
if (matchedILs.isEmpty()) {
// Try to match joint indexes
matchedILs = matchJointIndexes(query, ils);
}
if (!matchedILs.isEmpty()) {
return new MatchedIndex(schemaLabel, matchedILs);
}
return null;
}
use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.
the class GraphIndexTransaction method doIndexQueryOnce.
@Watched(prefix = "index")
private PageIds doIndexQueryOnce(IndexLabel indexLabel, ConditionQuery query) {
// Query all or one page
Iterator<BackendEntry> entries = null;
LockUtil.Locks locks = new LockUtil.Locks(this.graphName());
try {
locks.lockReads(LockUtil.INDEX_LABEL_DELETE, indexLabel.id());
locks.lockReads(LockUtil.INDEX_LABEL_REBUILD, indexLabel.id());
Set<Id> ids = InsertionOrderUtil.newSet();
entries = super.query(query).iterator();
while (entries.hasNext()) {
HugeIndex index = this.serializer.readIndex(graph(), query, entries.next());
this.removeExpiredIndexIfNeeded(index, query.showExpired());
ids.addAll(index.elementIds());
if (query.reachLimit(ids.size())) {
break;
}
Query.checkForceCapacity(ids.size());
this.recordIndexValue(query, index);
}
// If there is no data, the entries is not a Metadatable object
if (ids.isEmpty()) {
return PageIds.EMPTY;
}
// NOTE: Memory backend's iterator is not Metadatable
if (!query.paging()) {
return new PageIds(ids, PageState.EMPTY);
}
E.checkState(entries instanceof Metadatable, "The entries must be Metadatable when query " + "in paging, but got '%s'", entries.getClass().getName());
return new PageIds(ids, PageInfo.pageState(entries));
} finally {
locks.unlock();
CloseableIterator.closeIterator(entries);
}
}
use of com.baidu.hugegraph.perf.PerfUtil.Watched 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.perf.PerfUtil.Watched in project incubator-hugegraph by apache.
the class AbstractTransaction method commit.
@Watched(prefix = "tx")
@Override
public void commit() throws BackendException {
LOG.debug("Transaction commit() [auto: {}]...", this.autoCommit);
this.checkOwnerThread();
if (this.closed) {
throw new BackendException("Transaction has been closed");
}
if (this.committing) {
// It is not allowed to recursively commit in a transaction
return;
}
if (!this.hasUpdate()) {
LOG.debug("Transaction has no data to commit({})", store());
return;
}
// Do rate limit if needed
RateLimiter rateLimiter = this.graph.writeRateLimiter();
if (rateLimiter != null) {
int size = this.mutationSize();
double time = size > 0 ? rateLimiter.acquire(size) : 0.0;
if (time > 0) {
LOG.debug("Waited for {}s to mutate {} item(s)", time, size);
}
BackendEntryIterator.checkInterrupted();
}
// Do commit
assert !this.committing : "Not allowed to commit when it's committing";
this.committing = true;
try {
this.commit2Backend();
} finally {
this.committing = false;
this.reset();
}
}
Aggregations