Search in sources :

Example 36 with BackendEntry

use of com.baidu.hugegraph.backend.store.BackendEntry 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;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Id(com.baidu.hugegraph.backend.id.Id) IdRangeQuery(com.baidu.hugegraph.backend.query.IdRangeQuery) IdPrefixQuery(com.baidu.hugegraph.backend.query.IdPrefixQuery)

Example 37 with BackendEntry

use of com.baidu.hugegraph.backend.store.BackendEntry 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 38 with BackendEntry

use of com.baidu.hugegraph.backend.store.BackendEntry 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 39 with BackendEntry

use of com.baidu.hugegraph.backend.store.BackendEntry in project incubator-hugegraph by apache.

the class StoreSerializer method writeMutation.

public static byte[] writeMutation(BackendMutation mutation) {
    BytesBuffer buffer = BytesBuffer.allocate(MUTATION_SIZE);
    // write mutation size
    buffer.writeVInt(mutation.size());
    for (Iterator<BackendAction> items = mutation.mutation(); items.hasNext(); ) {
        BackendAction item = items.next();
        // write Action
        buffer.write(item.action().code());
        BackendEntry entry = item.entry();
        // write HugeType
        buffer.write(entry.type().code());
        // write id
        buffer.writeBytes(entry.id().asBytes());
        // write subId
        if (entry.subId() != null) {
            buffer.writeId(entry.subId());
        } else {
            buffer.writeId(IdGenerator.ZERO);
        }
        // write ttl
        buffer.writeVLong(entry.ttl());
        // write columns
        buffer.writeVInt(entry.columns().size());
        for (BackendColumn column : entry.columns()) {
            buffer.writeBytes(column.name);
            buffer.writeBytes(column.value);
        }
    }
    return buffer.bytes();
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) BackendColumn(com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn) BackendAction(com.baidu.hugegraph.backend.store.BackendAction) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 40 with BackendEntry

use of com.baidu.hugegraph.backend.store.BackendEntry in project incubator-hugegraph by apache.

the class SchemaTransaction method getAllSchema.

protected <T extends SchemaElement> List<T> getAllSchema(HugeType type) {
    List<T> results = new ArrayList<>();
    Query query = new Query(type);
    Iterator<BackendEntry> entries = this.query(query).iterator();
    try {
        while (entries.hasNext()) {
            BackendEntry entry = entries.next();
            if (entry == null) {
                continue;
            }
            results.add(this.deserialize(entry, type));
            Query.checkForceCapacity(results.size());
        }
    } finally {
        CloseableIterator.closeIterator(entries);
    }
    return results;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList)

Aggregations

BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)54 Test (org.junit.Test)27 TextBackendEntry (com.baidu.hugegraph.backend.serializer.TextBackendEntry)26 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)26 BackendMutation (com.baidu.hugegraph.backend.store.BackendMutation)21 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)7 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)7 Id (com.baidu.hugegraph.backend.id.Id)6 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)6 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)5 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)5 HugeIndex (com.baidu.hugegraph.structure.HugeIndex)5 Query (com.baidu.hugegraph.backend.query.Query)4 BinarySerializer (com.baidu.hugegraph.backend.serializer.BinarySerializer)4 HugeConfig (com.baidu.hugegraph.config.HugeConfig)4 ArrayList (java.util.ArrayList)4 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)3 LockUtil (com.baidu.hugegraph.util.LockUtil)3 BackendException (com.baidu.hugegraph.backend.BackendException)2 Condition (com.baidu.hugegraph.backend.query.Condition)2