Search in sources :

Example 6 with BinaryId

use of com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId in project incubator-hugegraph by apache.

the class BinarySerializer method formatIndexId.

protected static BinaryId formatIndexId(HugeType type, Id indexLabel, Object fieldValues, boolean equal) {
    boolean withEnding = type.isRangeIndex() || equal;
    Id id = HugeIndex.formatIndexId(type, indexLabel, fieldValues);
    if (!type.isNumericIndex() && indexIdLengthExceedLimit(id)) {
        id = HugeIndex.formatIndexHashId(type, indexLabel, fieldValues);
    }
    BytesBuffer buffer = BytesBuffer.allocate(1 + id.length());
    byte[] idBytes = buffer.writeIndexId(id, type, withEnding).bytes();
    return new BinaryId(idBytes, id);
}
Also used : 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)

Example 7 with BinaryId

use of com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId in project incubator-hugegraph by apache.

the class BinarySerializer method parse.

public BackendEntry parse(BackendEntry originEntry) {
    byte[] bytes = originEntry.id().asBytes();
    BinaryBackendEntry parsedEntry = new BinaryBackendEntry(originEntry.type(), bytes, this.enablePartition);
    if (this.enablePartition) {
        bytes = Arrays.copyOfRange(bytes, parsedEntry.id().length() + 2, bytes.length);
    } else {
        bytes = Arrays.copyOfRange(bytes, parsedEntry.id().length(), bytes.length);
    }
    BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
    buffer.write(parsedEntry.id().asBytes());
    buffer.write(bytes);
    parsedEntry = new BinaryBackendEntry(originEntry.type(), new BinaryId(buffer.bytes(), BytesBuffer.wrap(buffer.bytes()).readEdgeId()));
    for (BackendEntry.BackendColumn col : originEntry.columns()) {
        parsedEntry.column(buffer.bytes(), col.value);
    }
    return parsedEntry;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) BackendColumn(com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn)

Example 8 with BinaryId

use of com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId 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 9 with BinaryId

use of com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId in project incubator-hugegraph by apache.

the class BinarySerializer method writeQueryEdgeRangeCondition.

private Query writeQueryEdgeRangeCondition(ConditionQuery cq) {
    List<Condition> sortValues = cq.syspropConditions(HugeKeys.SORT_VALUES);
    E.checkArgument(sortValues.size() >= 1 && sortValues.size() <= 2, "Edge range query must be with sort-values range");
    // Would ignore target vertex
    Id vertex = cq.condition(HugeKeys.OWNER_VERTEX);
    Directions direction = cq.condition(HugeKeys.DIRECTION);
    if (direction == null) {
        direction = Directions.OUT;
    }
    Id label = cq.condition(HugeKeys.LABEL);
    BytesBuffer start = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
    writePartitionedId(HugeType.EDGE, vertex, start);
    start.write(direction.type().code());
    start.writeId(label);
    BytesBuffer end = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
    end.copyFrom(start);
    RangeConditions range = new RangeConditions(sortValues);
    if (range.keyMin() != null) {
        start.writeStringRaw((String) range.keyMin());
    }
    if (range.keyMax() != null) {
        end.writeStringRaw((String) range.keyMax());
    }
    // Sort-value will be empty if there is no start sort-value
    Id startId = new BinaryId(start.bytes(), null);
    // Set endId as prefix if there is no end sort-value
    Id endId = new BinaryId(end.bytes(), null);
    boolean includeStart = range.keyMinEq();
    if (cq.paging() && !cq.page().isEmpty()) {
        includeStart = true;
        byte[] position = PageState.fromString(cq.page()).position();
        E.checkArgument(Bytes.compare(position, startId.asBytes()) >= 0, "Invalid page out of lower bound");
        startId = new BinaryId(position, null);
    }
    if (range.keyMax() == null) {
        return new IdPrefixQuery(cq, startId, includeStart, endId);
    }
    return new IdRangeQuery(cq, startId, includeStart, endId, range.keyMaxEq());
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) Directions(com.baidu.hugegraph.type.define.Directions) BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) RangeConditions(com.baidu.hugegraph.backend.query.Condition.RangeConditions) IdRangeQuery(com.baidu.hugegraph.backend.query.IdRangeQuery) IdPrefixQuery(com.baidu.hugegraph.backend.query.IdPrefixQuery)

Example 10 with BinaryId

use of com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId in project incubator-hugegraph by apache.

the class BinarySerializer method formatILDeletion.

private BinaryBackendEntry formatILDeletion(HugeIndex index) {
    Id id = index.indexLabelId();
    BinaryId bid = new BinaryId(id.asBytes(), id);
    BinaryBackendEntry entry = new BinaryBackendEntry(index.type(), bid);
    if (index.type().isStringIndex()) {
        byte[] idBytes = IdGenerator.of(id.asString()).asBytes();
        BytesBuffer buffer = BytesBuffer.allocate(idBytes.length);
        buffer.write(idBytes);
        entry.column(buffer.bytes(), null);
    } else {
        assert index.type().isRangeIndex();
        BytesBuffer buffer = BytesBuffer.allocate(4);
        buffer.writeInt((int) id.asLong());
        entry.column(buffer.bytes(), null);
    }
    return entry;
}
Also used : 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)

Aggregations

BinaryId (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId)10 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)7 Id (com.baidu.hugegraph.backend.id.Id)6 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3 Condition (com.baidu.hugegraph.backend.query.Condition)2 RangeConditions (com.baidu.hugegraph.backend.query.Condition.RangeConditions)2 Directions (com.baidu.hugegraph.type.define.Directions)2 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)1 Query (com.baidu.hugegraph.backend.query.Query)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 BackendColumn (com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn)1 HugeType (com.baidu.hugegraph.type.HugeType)1 HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)1