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();
}
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();
}
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;
}
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;
}
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();
}
}
Aggregations