Search in sources :

Example 1 with BytesBuffer

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

the class PageState method toBytes.

private byte[] toBytes() {
    assert this.position.length > 0;
    int length = 2 + this.position.length + 2 * BytesBuffer.INT_LEN;
    BytesBuffer buffer = BytesBuffer.allocate(length);
    buffer.writeBytes(this.position);
    buffer.writeInt(this.offset);
    buffer.writeInt(this.total);
    return buffer.bytes();
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 2 with BytesBuffer

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

the class IdUtil method writeBinString.

public static Object writeBinString(Id id) {
    int len = id.edge() ? BytesBuffer.BUF_EDGE_ID : id.length() + 1;
    BytesBuffer buffer = BytesBuffer.allocate(len).writeId(id);
    buffer.forReadWritten();
    return buffer.asByteBuffer();
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 3 with BytesBuffer

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

the class PageInfo method toBytes.

public byte[] toBytes() {
    byte[] pageState = PageState.toBytes(this.page);
    int length = 2 + BytesBuffer.INT_LEN + pageState.length;
    BytesBuffer buffer = BytesBuffer.allocate(length);
    buffer.writeInt(this.offset);
    buffer.writeBytes(pageState);
    return buffer.bytes();
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 4 with BytesBuffer

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

the class PageInfo method fromBytes.

public static PageInfo fromBytes(byte[] bytes) {
    if (bytes.length == 0) {
        // The first page
        return new PageInfo(0, PAGE_NONE);
    }
    try {
        BytesBuffer buffer = BytesBuffer.wrap(bytes);
        int offset = buffer.readInt();
        byte[] pageState = buffer.readBytes();
        String page = PageState.toString(pageState);
        return new PageInfo(offset, page);
    } catch (Exception e) {
        throw new HugeException("Invalid page: '0x%s'", e, Bytes.toHex(bytes));
    }
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) HugeException(com.baidu.hugegraph.HugeException) HugeException(com.baidu.hugegraph.HugeException)

Example 5 with BytesBuffer

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

the class HugeIndex method parseIndexId.

public static HugeIndex parseIndexId(HugeGraph graph, HugeType type, byte[] id) {
    Object values;
    IndexLabel indexLabel;
    if (type.isStringIndex()) {
        Id idObject = IdGenerator.of(id, IdType.STRING);
        String[] parts = SplicingIdGenerator.parse(idObject);
        E.checkState(parts.length == 2, "Invalid secondary index id");
        Id label = IdGenerator.ofStoredString(parts[0], IdType.LONG);
        indexLabel = IndexLabel.label(graph, label);
        values = parts[1];
    } else {
        assert type.isRange4Index() || type.isRange8Index();
        final int labelLength = 4;
        E.checkState(id.length > labelLength, "Invalid range index id");
        BytesBuffer buffer = BytesBuffer.wrap(id);
        Id label = IdGenerator.of(buffer.readInt());
        indexLabel = IndexLabel.label(graph, label);
        List<Id> fields = indexLabel.indexFields();
        E.checkState(fields.size() == 1, "Invalid range index fields");
        DataType dataType = graph.propertyKey(fields.get(0)).dataType();
        E.checkState(dataType.isNumber() || dataType.isDate(), "Invalid range index field type");
        Class<?> clazz = dataType.isNumber() ? dataType.clazz() : DataType.LONG.clazz();
        values = bytes2number(buffer.read(id.length - labelLength), clazz);
    }
    HugeIndex index = new HugeIndex(graph, indexLabel);
    index.fieldValues(values);
    return index;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) DataType(com.baidu.hugegraph.type.define.DataType) Id(com.baidu.hugegraph.backend.id.Id)

Aggregations

BytesBuffer (com.baidu.hugegraph.backend.serializer.BytesBuffer)26 Test (org.junit.Test)9 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)8 BackendException (com.baidu.hugegraph.backend.BackendException)4 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)4 Point (java.awt.Point)4 BackendMutation (com.baidu.hugegraph.backend.store.BackendMutation)3 Calendar (java.util.Calendar)3 Id (com.baidu.hugegraph.backend.id.Id)2 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)2 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)2 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)2 StoreAction (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction)2 StoreType (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType)2 IOException (java.io.IOException)2 Random (java.util.Random)2 UUID (java.util.UUID)2 LZ4Factory (net.jpountz.lz4.LZ4Factory)2 HugeException (com.baidu.hugegraph.HugeException)1 BackendColumn (com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn)1