Search in sources :

Example 41 with Watched

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;
}
Also used : SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 42 with Watched

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;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 43 with Watched

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);
    }
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Metadatable(com.baidu.hugegraph.iterator.Metadatable) LockUtil(com.baidu.hugegraph.util.LockUtil) PageIds(com.baidu.hugegraph.backend.page.PageIds) Id(com.baidu.hugegraph.backend.id.Id) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 44 with Watched

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;
}
Also used : NoIndexException(com.baidu.hugegraph.exception.NoIndexException) SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType) HugeException(com.baidu.hugegraph.HugeException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 45 with Watched

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();
    }
}
Also used : RateLimiter(com.google.common.util.concurrent.RateLimiter) BackendException(com.baidu.hugegraph.backend.BackendException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)65 Id (com.baidu.hugegraph.backend.id.Id)30 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)9 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)8 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)8 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)7 ArrayList (java.util.ArrayList)7 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)6 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)5 HugeIndex (com.baidu.hugegraph.structure.HugeIndex)5 IdHolder (com.baidu.hugegraph.backend.page.IdHolder)4 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)4 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)4 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)4 Query (com.baidu.hugegraph.backend.query.Query)4 SchemaJob (com.baidu.hugegraph.job.schema.SchemaJob)4 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)4 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)4 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4