Search in sources :

Example 16 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class BinarySerializer method writeQueryCondition.

@Override
protected Query writeQueryCondition(Query query) {
    HugeType type = query.resultType();
    if (!type.isIndex()) {
        return query;
    }
    ConditionQuery cq = (ConditionQuery) query;
    if (type.isNumericIndex()) {
        // Convert range-index/shard-index query to id range query
        return this.writeRangeIndexQuery(cq);
    } else {
        assert type.isSearchIndex() || type.isSecondaryIndex() || type.isUniqueIndex();
        // Convert secondary-index or search-index query to id query
        return this.writeStringIndexQuery(cq);
    }
}
Also used : ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) HugeType(com.baidu.hugegraph.type.HugeType)

Example 17 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class BinarySerializer method writeRangeIndexQuery.

private Query writeRangeIndexQuery(ConditionQuery query) {
    Id index = query.condition(HugeKeys.INDEX_LABEL_ID);
    E.checkArgument(index != null, "Please specify the index label");
    List<Condition> fields = query.syspropConditions(HugeKeys.FIELD_VALUES);
    E.checkArgument(!fields.isEmpty(), "Please specify the index field values");
    HugeType type = query.resultType();
    Id start = null;
    if (query.paging() && !query.page().isEmpty()) {
        byte[] position = PageState.fromString(query.page()).position();
        start = new BinaryId(position, null);
    }
    RangeConditions range = new RangeConditions(fields);
    if (range.keyEq() != null) {
        Id id = formatIndexId(type, index, range.keyEq(), true);
        if (start == null) {
            return new IdPrefixQuery(query, id);
        }
        E.checkArgument(Bytes.compare(start.asBytes(), id.asBytes()) >= 0, "Invalid page out of lower bound");
        return new IdPrefixQuery(query, start, id);
    }
    Object keyMin = range.keyMin();
    Object keyMax = range.keyMax();
    boolean keyMinEq = range.keyMinEq();
    boolean keyMaxEq = range.keyMaxEq();
    if (keyMin == null) {
        E.checkArgument(keyMax != null, "Please specify at least one condition");
        // Set keyMin to min value
        keyMin = NumericUtil.minValueOf(keyMax.getClass());
        keyMinEq = true;
    }
    Id min = formatIndexId(type, index, keyMin, false);
    if (!keyMinEq) {
        /*
             * Increase 1 to keyMin, index GT query is a scan with GT prefix,
             * inclusiveStart=false will also match index started with keyMin
             */
        increaseOne(min.asBytes());
        keyMinEq = true;
    }
    if (start == null) {
        start = min;
    } else {
        E.checkArgument(Bytes.compare(start.asBytes(), min.asBytes()) >= 0, "Invalid page out of lower bound");
    }
    if (keyMax == null) {
        keyMax = NumericUtil.maxValueOf(keyMin.getClass());
        keyMaxEq = true;
    }
    Id max = formatIndexId(type, index, keyMax, false);
    if (keyMaxEq) {
        keyMaxEq = false;
        increaseOne(max.asBytes());
    }
    return new IdRangeQuery(query, start, keyMinEq, max, keyMaxEq);
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeType(com.baidu.hugegraph.type.HugeType) RangeConditions(com.baidu.hugegraph.backend.query.Condition.RangeConditions) IdRangeQuery(com.baidu.hugegraph.backend.query.IdRangeQuery) IdPrefixQuery(com.baidu.hugegraph.backend.query.IdPrefixQuery)

Example 18 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class AbstractSerializer method writeQuery.

@Override
public Query writeQuery(Query query) {
    HugeType type = query.resultType();
    // Serialize edge condition query (TODO: add VEQ(for EOUT/EIN))
    if (type.isEdge() && query.conditionsSize() > 0) {
        if (query.idsSize() > 0) {
            throw new BackendException("Not supported query edge by id " + "and by condition at the same time");
        }
        Query result = this.writeQueryEdgeCondition(query);
        if (result != null) {
            return result;
        }
    }
    // Serialize id in query
    if (query.idsSize() == 1 && query instanceof IdQuery.OneIdQuery) {
        IdQuery.OneIdQuery result = (IdQuery.OneIdQuery) query.copy();
        result.resetId(this.writeQueryId(type, result.id()));
        query = result;
    } else if (query.idsSize() > 0 && query instanceof IdQuery) {
        IdQuery result = (IdQuery) query.copy();
        result.resetIds();
        for (Id id : query.ids()) {
            result.query(this.writeQueryId(type, id));
        }
        query = result;
    }
    // Serialize condition(key/value) in query
    if (query instanceof ConditionQuery && query.conditionsSize() > 0) {
        query = this.writeQueryCondition(query);
    }
    return query;
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 19 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class BinarySerializer method formatIndexName.

protected byte[] formatIndexName(HugeIndex index) {
    BytesBuffer buffer;
    Id elemId = index.elementId();
    if (!this.indexWithIdPrefix) {
        int idLen = 1 + elemId.length();
        buffer = BytesBuffer.allocate(idLen);
    } else {
        Id indexId = index.id();
        HugeType type = index.type();
        if (!type.isNumericIndex() && indexIdLengthExceedLimit(indexId)) {
            indexId = index.hashId();
        }
        int idLen = 1 + elemId.length() + 1 + indexId.length();
        buffer = BytesBuffer.allocate(idLen);
        // Write index-id
        buffer.writeIndexId(indexId, type);
    }
    // Write element-id
    buffer.writeId(elemId);
    // Write expired time if needed
    if (index.hasTtl()) {
        buffer.writeVLong(index.expiredTime());
    }
    return buffer.bytes();
}
Also used : BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeType(com.baidu.hugegraph.type.HugeType)

Example 20 with HugeType

use of com.baidu.hugegraph.type.HugeType 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);
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Aggregations

HugeType (com.baidu.hugegraph.type.HugeType)34 Id (com.baidu.hugegraph.backend.id.Id)16 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)5 Query (com.baidu.hugegraph.backend.query.Query)4 ExistedException (com.baidu.hugegraph.exception.ExistedException)4 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)4 ArrayList (java.util.ArrayList)4 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)3 Condition (com.baidu.hugegraph.backend.query.Condition)3 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)3 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)3 BinaryId (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId)3 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)3 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)3 Lock (java.util.concurrent.locks.Lock)3 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)3 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)3 HugeException (com.baidu.hugegraph.HugeException)2 BackendException (com.baidu.hugegraph.backend.BackendException)2 BinaryEntryIterator (com.baidu.hugegraph.backend.serializer.BinaryEntryIterator)2