Search in sources :

Example 11 with BytesBuffer

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));
}
Also used : Calendar(java.util.Calendar) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Point(java.awt.Point) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 12 with BytesBuffer

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));
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 13 with BytesBuffer

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());
    }
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) BackendAction(com.baidu.hugegraph.backend.store.BackendAction) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Test(org.junit.Test)

Example 14 with BytesBuffer

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);
        }
    });
}
Also used : StoreType(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) StoreAction(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 15 with BytesBuffer

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();
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

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