Search in sources :

Example 16 with BytesBuffer

use of com.baidu.hugegraph.backend.serializer.BytesBuffer 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 17 with BytesBuffer

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

the class StoreSerializer method writeIncrCounter.

public static byte[] writeIncrCounter(IncrCounter incrCounter) {
    // The first two bytes are reserved for StoreType and StoreAction
    BytesBuffer buffer = BytesBuffer.allocate(StoreCommand.HEADER_SIZE + 1 + BytesBuffer.LONG_LEN);
    StoreCommand.writeHeader(buffer);
    buffer.write(incrCounter.type().code());
    buffer.writeVLong(incrCounter.increment());
    return buffer.bytes();
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 18 with BytesBuffer

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

the class StoreSerializer method readMutations.

public static List<BackendMutation> readMutations(BytesBuffer buffer) {
    int size = buffer.readVInt();
    List<BackendMutation> mutations = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        BytesBuffer buf = BytesBuffer.wrap(buffer.readBigBytes());
        mutations.add(readMutation(buf));
    }
    return mutations;
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) ArrayList(java.util.ArrayList) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 19 with BytesBuffer

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

the class StoreStateMachine method onApplyLeader.

private Future<?> onApplyLeader(RaftStoreClosure closure) {
    // Leader just take the command out from the closure
    StoreCommand command = closure.command();
    BytesBuffer buffer = BytesBuffer.wrap(command.data());
    // The first two bytes are StoreType and StoreAction
    StoreType type = StoreType.valueOf(buffer.read());
    StoreAction action = StoreAction.valueOf(buffer.read());
    boolean forwarded = command.forwarded();
    // Let the producer thread to handle it, and wait for it
    CompletableFuture<Object> future = new CompletableFuture<>();
    closure.complete(Status.OK(), () -> {
        Object result;
        try {
            result = this.applyCommand(type, action, buffer, forwarded);
        } catch (Throwable e) {
            future.completeExceptionally(e);
            throw e;
        }
        future.complete(result);
        return result;
    });
    return future;
}
Also used : StoreType(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType) CompletableFuture(java.util.concurrent.CompletableFuture) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) StoreAction(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction)

Example 20 with BytesBuffer

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

the class HugeIndex method formatIndexId.

public static Id formatIndexId(HugeType type, Id indexLabelId, Object fieldValues) {
    if (type.isStringIndex()) {
        String value = "";
        if (fieldValues instanceof Id) {
            value = IdGenerator.asStoredString((Id) fieldValues);
        } else if (fieldValues != null) {
            value = fieldValues.toString();
        }
        /*
             * Modify order between index label and field-values to put the
             * index label in front(hugegraph-1317)
             */
        String strIndexLabelId = IdGenerator.asStoredString(indexLabelId);
        return SplicingIdGenerator.splicing(strIndexLabelId, value);
    } else {
        assert type.isRangeIndex();
        int length = type.isRange4Index() ? 4 : 8;
        BytesBuffer buffer = BytesBuffer.allocate(4 + length);
        buffer.writeInt(SchemaElement.schemaId(indexLabelId));
        if (fieldValues != null) {
            E.checkState(fieldValues instanceof Number, "Field value of range index must be number:" + " %s", fieldValues.getClass().getSimpleName());
            byte[] bytes = number2bytes((Number) fieldValues);
            buffer.write(bytes);
        }
        return buffer.asId();
    }
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) 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