Search in sources :

Example 11 with HugeIndex

use of com.baidu.hugegraph.structure.HugeIndex in project incubator-hugegraph by apache.

the class SchemaIndexTransaction method updateNameIndex.

@Watched(prefix = "index")
public void updateNameIndex(SchemaElement element, boolean removed) {
    if (!this.needIndexForName()) {
        return;
    }
    IndexLabel indexLabel = IndexLabel.label(element.type());
    // Update name index if backend store not supports name-query
    HugeIndex index = new HugeIndex(this.graph(), indexLabel);
    index.fieldValues(element.name());
    index.elementIds(element.id());
    if (removed) {
        this.doEliminate(this.serializer.writeIndex(index));
    } else {
        this.doAppend(this.serializer.writeIndex(index));
    }
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 12 with HugeIndex

use of com.baidu.hugegraph.structure.HugeIndex in project incubator-hugegraph by apache.

the class SchemaIndexTransaction method queryByName.

@Watched(prefix = "index")
private QueryResults<BackendEntry> queryByName(ConditionQuery query) {
    if (!this.needIndexForName()) {
        return super.query(query);
    }
    IndexLabel il = IndexLabel.label(query.resultType());
    String name = (String) query.condition(HugeKeys.NAME);
    E.checkState(name != null, "The name in condition can't be null " + "when querying schema by name");
    ConditionQuery indexQuery;
    indexQuery = new ConditionQuery(HugeType.SECONDARY_INDEX, query);
    indexQuery.eq(HugeKeys.FIELD_VALUES, name);
    indexQuery.eq(HugeKeys.INDEX_LABEL_ID, il.id());
    IdQuery idQuery = new IdQuery(query.resultType(), query);
    Iterator<BackendEntry> entries = super.query(indexQuery).iterator();
    try {
        while (entries.hasNext()) {
            HugeIndex index = this.serializer.readIndex(graph(), indexQuery, entries.next());
            idQuery.query(index.elementIds());
            Query.checkForceCapacity(idQuery.idsSize());
        }
    } finally {
        CloseableIterator.closeIterator(entries);
    }
    if (idQuery.ids().isEmpty()) {
        return QueryResults.empty();
    }
    assert idQuery.idsSize() == 1 : idQuery.ids();
    if (idQuery.idsSize() > 1) {
        LOG.warn("Multiple ids are found with same name '{}': {}", name, idQuery.ids());
    }
    return super.query(idQuery);
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 13 with HugeIndex

use of com.baidu.hugegraph.structure.HugeIndex in project incubator-hugegraph by apache.

the class GraphIndexTransaction method updateIndex.

private void updateIndex(IndexLabel indexLabel, Object propValue, Id elementId, long expiredTime, boolean removed) {
    HugeIndex index = new HugeIndex(this.graph(), indexLabel);
    index.fieldValues(propValue);
    index.elementIds(elementId, expiredTime);
    if (removed) {
        this.doEliminate(this.serializer.writeIndex(index));
    } else {
        this.doAppend(this.serializer.writeIndex(index));
    }
}
Also used : HugeIndex(com.baidu.hugegraph.structure.HugeIndex)

Example 14 with HugeIndex

use of com.baidu.hugegraph.structure.HugeIndex 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)

Aggregations

HugeIndex (com.baidu.hugegraph.structure.HugeIndex)14 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)5 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)5 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)4 Id (com.baidu.hugegraph.backend.id.Id)3 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 LockUtil (com.baidu.hugegraph.util.LockUtil)2 HugeGraphParams (com.baidu.hugegraph.HugeGraphParams)1 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)1 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)1 PageIds (com.baidu.hugegraph.backend.page.PageIds)1 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)1 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)1 Metadatable (com.baidu.hugegraph.iterator.Metadatable)1 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)1 IdWithExpiredTime (com.baidu.hugegraph.structure.HugeIndex.IdWithExpiredTime)1 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1