use of com.baidu.hugegraph.backend.serializer.BytesBuffer in project incubator-hugegraph by apache.
the class BytesBufferTest method testProperty.
@Test
public void testProperty() {
BytesBuffer buf = BytesBuffer.allocate(0);
PropertyKey pkey = genPkey(DataType.BOOLEAN);
Object value = true;
byte[] bytes = genBytes("01");
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
value = false;
bytes = genBytes("00");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.BYTE);
value = (byte) 127;
bytes = genBytes("7f");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.INT);
value = 127;
bytes = genBytes("7f");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.INT);
value = 128;
bytes = genBytes("8100");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.FLOAT);
value = 1.0f;
bytes = genBytes("3f800000");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.FLOAT);
value = 3.14f;
bytes = genBytes("4048f5c3");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.FLOAT);
value = -1.0f;
bytes = genBytes("bf800000");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.FLOAT);
value = Float.MAX_VALUE;
bytes = genBytes("7f7fffff");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.LONG);
value = 127L;
bytes = genBytes("7f");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.DOUBLE);
value = 3.14d;
bytes = genBytes("40091eb851eb851f");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.DATE);
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Beijing"));
c.setTimeInMillis(1565851529514L);
value = c.getTime();
bytes = genBytes("adc9a098e22a");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.TEXT);
value = "abc";
bytes = genBytes("03616263");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.BLOB);
value = genBytes("001199aabbcc");
bytes = genBytes("06001199aabbcc");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(Blob.wrap((byte[]) value), BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.UUID);
value = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
bytes = genBytes("3cfcafc879064ab7a2074ded056f58de");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.OBJECT);
value = new Point(3, 8);
bytes = genBytes("1301006a6176612e6177742e506f696ef4010610");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.OBJECT);
value = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
bytes = genBytes("2101006a6176612e7574696c2e555549c401" + "3cfcafc879064ab7a2074ded056f58de");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
pkey = genPkey(DataType.OBJECT);
value = new int[] { 1, 3, 8 };
bytes = genBytes("0901005bc90104020610");
buf.forReadWritten();
Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
Assert.assertArrayEquals((int[]) value, (int[]) BytesBuffer.wrap(bytes).readProperty(pkey));
}
use of com.baidu.hugegraph.backend.serializer.BytesBuffer in project incubator-hugegraph by apache.
the class BytesBufferTest method testWrap.
@Test
public void testWrap() {
BytesBuffer buf4 = BytesBuffer.wrap(new byte[] { 1, 2, 3, 4 });
Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4 }, buf4.array());
Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4 }, buf4.read(4));
}
use of com.baidu.hugegraph.backend.serializer.BytesBuffer in project incubator-hugegraph by apache.
the class StoreSerializerTest method testSerializeBackendMutation.
@Test
public void testSerializeBackendMutation() {
BinaryBackendEntry entry = new BinaryBackendEntry(HugeType.VERTEX, new byte[] { 1, 2 });
entry.column(new byte[] { 1 }, new byte[] { 1 });
entry.column(new byte[] { 2 }, new byte[] { 2 });
entry.column(new byte[] { 127 }, new byte[] { 127 });
BackendMutation origin = new BackendMutation();
origin.add(entry, Action.INSERT);
byte[] bytes = StoreSerializer.writeMutation(origin);
BytesBuffer buffer = BytesBuffer.wrap(bytes);
BackendMutation actual = StoreSerializer.readMutation(buffer);
Assert.assertEquals(1, actual.size());
Iterator<BackendAction> iter = actual.mutation();
while (iter.hasNext()) {
BackendAction item = iter.next();
Assert.assertEquals(Action.INSERT, item.action());
BackendEntry e = item.entry();
Assert.assertEquals(entry.type(), e.type());
Assert.assertEquals(entry.id(), e.id());
Assert.assertEquals(entry.subId(), e.subId());
Assert.assertEquals(entry.ttl(), e.ttl());
Assert.assertEquals(entry.columnsSize(), e.columnsSize());
Assert.assertEquals(entry.columns(), e.columns());
}
}
use of com.baidu.hugegraph.backend.serializer.BytesBuffer in project incubator-hugegraph by apache.
the class StoreStateMachine method onApplyFollower.
private Future<?> onApplyFollower(ByteBuffer data) {
// Follower need to read mutation data
byte[] bytes = data.array();
// Let the backend thread do it directly
return this.context.backendExecutor().submit(() -> {
BytesBuffer buffer = LZ4Util.decompress(bytes, RaftSharedContext.BLOCK_SIZE);
buffer.forReadWritten();
StoreType type = StoreType.valueOf(buffer.read());
StoreAction action = StoreAction.valueOf(buffer.read());
try {
return this.applyCommand(type, action, buffer, false);
} catch (Throwable e) {
String title = "Failed to execute backend command";
LOG.error("{}: {}", title, action, e);
throw new BackendException(title, e);
}
});
}
use of com.baidu.hugegraph.backend.serializer.BytesBuffer in project incubator-hugegraph by apache.
the class StoreSerializer method writeMutations.
public static byte[] writeMutations(List<BackendMutation> mutations) {
int estimateSize = mutations.size() * MUTATION_SIZE;
// The first two bytes are reserved for StoreType and StoreAction
BytesBuffer buffer = BytesBuffer.allocate(StoreCommand.HEADER_SIZE + 4 + estimateSize);
StoreCommand.writeHeader(buffer);
buffer.writeVInt(mutations.size());
for (BackendMutation mutation : mutations) {
buffer.writeBigBytes(writeMutation(mutation));
}
return buffer.bytes();
}
Aggregations