Search in sources :

Example 6 with BytesBuffer

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

the class CassandraSerializer method writeProperty.

@Override
protected Object writeProperty(PropertyKey propertyKey, Object value) {
    BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_PROPERTY);
    if (propertyKey == null) {
        /*
             * Since we can't know the type of the property value in some
             * scenarios so need to construct a fake property key to
             * serialize to reuse code.
             */
        propertyKey = new PropertyKey(null, IdGenerator.of(0L), "fake");
        propertyKey.dataType(DataType.fromClass(value.getClass()));
    }
    buffer.writeProperty(propertyKey, value);
    buffer.forReadWritten();
    return buffer.asByteBuffer();
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 7 with BytesBuffer

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

the class LZ4Util method decompress.

public static BytesBuffer decompress(byte[] bytes, int blockSize, float bufferRatio) {
    float ratio = bufferRatio <= 0.0F ? DEFAULT_BUFFER_RATIO : bufferRatio;
    LZ4Factory factory = LZ4Factory.fastestInstance();
    LZ4FastDecompressor decompressor = factory.fastDecompressor();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    int initBufferSize = Math.min(Math.round(bytes.length * ratio), BytesBuffer.MAX_BUFFER_CAPACITY);
    BytesBuffer buf = new BytesBuffer(initBufferSize);
    LZ4BlockInputStream lzInput = new LZ4BlockInputStream(bais, decompressor);
    int count;
    byte[] buffer = new byte[blockSize];
    try {
        while ((count = lzInput.read(buffer)) != -1) {
            buf.write(buffer, 0, count);
        }
        lzInput.close();
    } catch (IOException e) {
        throw new BackendException("Failed to decompress", e);
    }
    /*
         * If need to perform reading outside the method,
         * remember to call forReadWritten()
         */
    return buf;
}
Also used : LZ4FastDecompressor(net.jpountz.lz4.LZ4FastDecompressor) ByteArrayInputStream(java.io.ByteArrayInputStream) LZ4Factory(net.jpountz.lz4.LZ4Factory) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) IOException(java.io.IOException) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 8 with BytesBuffer

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

the class BytesBufferTest method testAllocate.

@Test
public void testAllocate() {
    Assert.assertEquals(0, BytesBuffer.allocate(0).array().length);
    Assert.assertEquals(0, BytesBuffer.allocate(0).bytes().length);
    Assert.assertEquals(4, BytesBuffer.allocate(4).array().length);
    Assert.assertEquals(0, BytesBuffer.allocate(4).bytes().length);
    BytesBuffer buf4 = BytesBuffer.allocate(4);
    buf4.write(new byte[4]);
    Assert.assertArrayEquals(new byte[] { 0, 0, 0, 0 }, buf4.bytes());
    BytesBuffer buf2 = BytesBuffer.allocate(2);
    buf2.write(new byte[4]);
    Assert.assertArrayEquals(new byte[] { 0, 0, 0, 0 }, buf2.bytes());
    BytesBuffer buf0 = BytesBuffer.allocate(0);
    buf0.write(new byte[4]);
    Assert.assertArrayEquals(new byte[] { 0, 0, 0, 0 }, buf0.bytes());
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 9 with BytesBuffer

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

the class BytesBufferTest method testString.

@Test
public void testString() {
    BytesBuffer buf = BytesBuffer.allocate(0);
    buf.writeStringRaw("any");
    byte[] bytes = genBytes("616e79");
    Assert.assertArrayEquals(bytes, buf.bytes());
    buf.forReadWritten();
    Assert.assertEquals("any", buf.readStringFromRemaining());
    bytes = genBytes("61626364");
    buf = BytesBuffer.allocate(0);
    Assert.assertArrayEquals(bytes, buf.writeStringToRemaining("abcd").bytes());
    Assert.assertEquals("abcd", BytesBuffer.wrap(bytes).readStringFromRemaining());
    bytes = genBytes("0461626364");
    buf = BytesBuffer.allocate(0);
    Assert.assertArrayEquals(bytes, buf.writeString("abcd").bytes());
    Assert.assertEquals("abcd", BytesBuffer.wrap(bytes).readString());
    bytes = genBytes("6162636400");
    buf = BytesBuffer.allocate(0);
    Assert.assertArrayEquals(bytes, buf.writeStringWithEnding("abcd").bytes());
    Assert.assertEquals("abcd", BytesBuffer.wrap(bytes).readStringWithEnding());
    bytes = genBytes("61620100");
    buf = BytesBuffer.allocate(0);
    Assert.assertArrayEquals(bytes, buf.writeStringWithEnding("ab\u0001").bytes());
    Assert.assertEquals("ab\u0001", BytesBuffer.wrap(bytes).readStringWithEnding());
    bytes = genBytes("61627f00");
    buf = BytesBuffer.allocate(0);
    Assert.assertArrayEquals(bytes, buf.writeStringWithEnding("ab\u007f").bytes());
    Assert.assertEquals("ab\u007f", BytesBuffer.wrap(bytes).readStringWithEnding());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        BytesBuffer.allocate(0).writeStringWithEnding("ab\u0000");
    }, e -> {
        Assert.assertContains("Can't contains byte '0x00' in string", e.getMessage());
    });
    buf = BytesBuffer.allocate(0);
    buf.writeStringWithEnding("ab\uffff");
    buf.forReadWritten();
    Assert.assertEquals("ab\uffff", buf.readStringWithEnding());
}
Also used : BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 10 with BytesBuffer

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

the class BytesBufferTest method testVarLong.

@Test
public void testVarLong() {
    Assert.assertArrayEquals(new byte[] { 0 }, BytesBuffer.allocate(5).writeVLong(0).bytes());
    Assert.assertArrayEquals(new byte[] { 1 }, BytesBuffer.allocate(5).writeVLong(1).bytes());
    Assert.assertArrayEquals(new byte[] { (byte) 0x7f }, BytesBuffer.allocate(5).writeVLong(127).bytes());
    Assert.assertArrayEquals(new byte[] { (byte) 0x81, 0 }, BytesBuffer.allocate(5).writeVLong(128).bytes());
    Assert.assertArrayEquals(new byte[] { (byte) 0xff, (byte) 0x7f }, BytesBuffer.allocate(5).writeVLong(16383).bytes());
    Assert.assertArrayEquals(new byte[] { (byte) 0x81, (byte) 0x80, 0 }, BytesBuffer.allocate(5).writeVLong(16384).bytes());
    Assert.assertArrayEquals(new byte[] { (byte) 0x81, (byte) 0x80, 1 }, BytesBuffer.allocate(5).writeVLong(16385).bytes());
    Assert.assertArrayEquals(new byte[] { -127, -1, -1, -1, -1, -1, -1, -1, -1, 127 }, BytesBuffer.allocate(5).writeVLong(-1).bytes());
    Assert.assertArrayEquals(new byte[] { -121, -1, -1, -1, 127 }, BytesBuffer.allocate(5).writeVLong(Integer.MAX_VALUE).bytes());
    Assert.assertArrayEquals(new byte[] { -127, -1, -1, -1, -1, -8, -128, -128, -128, 0 }, BytesBuffer.allocate(5).writeVLong(Integer.MIN_VALUE).bytes());
    Assert.assertArrayEquals(new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, 127 }, BytesBuffer.allocate(5).writeVLong(Long.MAX_VALUE).bytes());
    Assert.assertArrayEquals(new byte[] { -127, -128, -128, -128, -128, -128, -128, -128, -128, 0 }, BytesBuffer.allocate(5).writeVLong(Long.MIN_VALUE).bytes());
    for (long i = Short.MIN_VALUE; i < Short.MAX_VALUE; i++) {
        BytesBuffer buf = BytesBuffer.allocate(10).writeVLong(i);
        Assert.assertEquals(i, buf.forReadWritten().readVLong());
    }
    Random random = new Random();
    for (long i = Long.MIN_VALUE; i < Long.MAX_VALUE; ) {
        BytesBuffer buf = BytesBuffer.allocate(10).writeVLong(i);
        Assert.assertEquals(i, buf.forReadWritten().readVLong());
        long old = i;
        i += (random.nextLong() >>> 8);
        if (old > 0 && i < 0) {
            // overflow
            break;
        }
    }
}
Also used : Random(java.util.Random) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) 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