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();
}
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;
}
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());
}
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());
}
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;
}
}
}
Aggregations