Search in sources :

Example 26 with BytesBuffer

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

the class BytesBufferTest method testPropertyWithSet.

@Test
public void testPropertyWithSet() {
    BytesBuffer buf = BytesBuffer.allocate(0);
    PropertyKey pkey = genSetPkey(DataType.BOOLEAN);
    Object value = ImmutableSet.of(true, false);
    byte[] bytes = genBytes("020100");
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.BYTE);
    value = ImmutableSet.of();
    bytes = genBytes("00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.BYTE);
    value = ImmutableSet.of((byte) 127, (byte) 128);
    bytes = genBytes("027f8fffffff00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.INT);
    value = ImmutableSet.of(127, 128);
    bytes = genBytes("027f8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.FLOAT);
    value = ImmutableSet.of(1.0f, 3.14f);
    bytes = genBytes("023f8000004048f5c3");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.LONG);
    value = ImmutableSet.of(127L, 128L);
    bytes = genBytes("027f8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.DOUBLE);
    value = ImmutableSet.of(1.0d, 3.14d);
    bytes = genBytes("023ff000000000000040091eb851eb851f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.DATE);
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Beijing"));
    c.setTimeInMillis(1565851529514L);
    value = ImmutableSet.of(c.getTime(), c.getTime());
    bytes = genBytes("01adc9a098e22a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.TEXT);
    value = ImmutableSet.of("abc", "123");
    bytes = genBytes("020361626303313233");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.BLOB);
    value = ImmutableSet.of(genBytes("001199aabbcc"), genBytes("5566"));
    bytes = genBytes("0206001199aabbcc025566");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Set<?> set = (Set<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    Iterator<?> iterator = set.iterator();
    Assert.assertEquals(Blob.wrap(genBytes("001199aabbcc")), iterator.next());
    Assert.assertEquals(Blob.wrap(genBytes("5566")), iterator.next());
    pkey = genSetPkey(DataType.UUID);
    UUID uuid = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
    value = ImmutableSet.of(uuid, uuid);
    bytes = genBytes("013cfcafc879064ab7a2074ded056f58de");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.OBJECT);
    value = ImmutableSet.of(new Point(3, 8), new Point(3, 9));
    bytes = genBytes("021301006a6176612e6177742e506f696ef4010610" + "1301006a6176612e6177742e506f696ef4010612");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.OBJECT);
    value = ImmutableSet.of(new int[] { 1, 3 }, new int[] { 2, 5 });
    bytes = genBytes("020801005bc9010302060801005bc90103040a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    set = (Set<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    iterator = set.iterator();
    Assert.assertArrayEquals(new int[] { 1, 3 }, (int[]) iterator.next());
    Assert.assertArrayEquals(new int[] { 2, 5 }, (int[]) iterator.next());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Calendar(java.util.Calendar) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Point(java.awt.Point) UUID(java.util.UUID) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

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