Search in sources :

Example 21 with BytesBuffer

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

the class GZipUtil method compress.

public static BytesBuffer compress(byte[] data) {
    int estimateSize = data.length >> 3;
    BytesBuffer output = BytesBuffer.allocate(estimateSize);
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.finish();
    byte[] buffer = new byte[BUF_SIZE];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        output.write(buffer, 0, count);
    }
    output.forReadWritten();
    return output;
}
Also used : Deflater(java.util.zip.Deflater) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 22 with BytesBuffer

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

the class GZipUtil method decompress.

public static BytesBuffer decompress(byte[] data) {
    int estimateSize = data.length << 3;
    BytesBuffer output = BytesBuffer.allocate(estimateSize);
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    byte[] buffer = new byte[BUF_SIZE];
    while (!inflater.finished()) {
        try {
            int count = inflater.inflate(buffer);
            output.write(buffer, 0, count);
        } catch (DataFormatException e) {
            throw new BackendException("Failed to decompress", e);
        }
    }
    output.forReadWritten();
    return output;
}
Also used : DataFormatException(java.util.zip.DataFormatException) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Inflater(java.util.zip.Inflater) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 23 with BytesBuffer

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

the class LZ4Util method compress.

public static BytesBuffer compress(byte[] bytes, int blockSize, float bufferRatio) {
    float ratio = bufferRatio <= 0.0F ? DEFAULT_BUFFER_RATIO : bufferRatio;
    LZ4Factory factory = LZ4Factory.fastestInstance();
    LZ4Compressor compressor = factory.fastCompressor();
    int initBufferSize = Math.round(bytes.length / ratio);
    BytesBuffer buf = new BytesBuffer(initBufferSize);
    LZ4BlockOutputStream lz4Output = new LZ4BlockOutputStream(buf, blockSize, compressor);
    try {
        lz4Output.write(bytes);
        lz4Output.close();
    } catch (IOException e) {
        throw new BackendException("Failed to compress", e);
    }
    /*
         * If need to perform reading outside the method,
         * remember to call forReadWritten()
         */
    return buf;
}
Also used : LZ4Compressor(net.jpountz.lz4.LZ4Compressor) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) LZ4Factory(net.jpountz.lz4.LZ4Factory) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) IOException(java.io.IOException) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 24 with BytesBuffer

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

the class BytesBufferTest method testPropertyWithList.

@Test
public void testPropertyWithList() {
    BytesBuffer buf = BytesBuffer.allocate(0);
    PropertyKey pkey = genListPkey(DataType.BOOLEAN);
    Object value = ImmutableList.of(true, false);
    byte[] bytes = genBytes("020100");
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.BYTE);
    value = ImmutableList.of();
    bytes = genBytes("00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.BYTE);
    value = ImmutableList.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 = genListPkey(DataType.INT);
    value = ImmutableList.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 = genListPkey(DataType.FLOAT);
    value = ImmutableList.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 = genListPkey(DataType.LONG);
    value = ImmutableList.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 = genListPkey(DataType.DOUBLE);
    value = ImmutableList.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 = genListPkey(DataType.DATE);
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Beijing"));
    c.setTimeInMillis(1565851529514L);
    value = ImmutableList.of(c.getTime(), c.getTime());
    bytes = genBytes("02adc9a098e22aadc9a098e22a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.TEXT);
    value = ImmutableList.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 = genListPkey(DataType.BLOB);
    value = ImmutableList.of(genBytes("001199aabbcc"), genBytes("5566"));
    bytes = genBytes("0206001199aabbcc025566");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    List<?> list = (List<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    Assert.assertEquals(Blob.wrap(genBytes("001199aabbcc")), list.get(0));
    Assert.assertEquals(Blob.wrap(genBytes("5566")), list.get(1));
    pkey = genListPkey(DataType.UUID);
    UUID uuid = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
    value = ImmutableList.of(uuid, uuid);
    bytes = genBytes("023cfcafc879064ab7a2074ded056f58de" + "3cfcafc879064ab7a2074ded056f58de");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.OBJECT);
    value = ImmutableList.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 = genListPkey(DataType.OBJECT);
    value = ImmutableList.of(new int[] { 1, 3 }, new int[] { 2, 5 });
    bytes = genBytes("020801005bc9010302060801005bc90103040a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    list = (List<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    Assert.assertArrayEquals(new int[] { 1, 3 }, (int[]) list.get(0));
    Assert.assertArrayEquals(new int[] { 2, 5 }, (int[]) list.get(1));
}
Also used : Calendar(java.util.Calendar) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Point(java.awt.Point) UUID(java.util.UUID) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 25 with BytesBuffer

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

the class BytesBufferTest method testVarInt.

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