use of com.baidu.hugegraph.computer.core.io.BytesInput in project hugegraph-computer by hugegraph.
the class PointerCombinerTest method testMessageCombiner.
@Test
public void testMessageCombiner() throws IOException {
Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.WORKER_COMBINER_CLASS, DoubleValueSumCombiner.class.getName());
Combiner<DoubleValue> valueCombiner = config.createObject(ComputerOptions.WORKER_COMBINER_CLASS);
PointerCombiner combiner = SorterTestUtil.createPointerCombiner(DoubleValue::new, new DoubleValueSumCombiner());
try (BytesOutput bytesOutput1 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
BytesOutput bytesOutput2 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
DoubleValue value1 = new DoubleValue(1.0D);
DoubleValue value2 = new DoubleValue(2.0D);
value1.write(bytesOutput1);
value2.write(bytesOutput2);
Pointer pointer1 = new InlinePointer(bytesOutput1.buffer(), bytesOutput1.position());
Pointer pointer2 = new InlinePointer(bytesOutput2.buffer(), bytesOutput2.position());
Pointer pointer = combiner.combine(pointer1, pointer2);
BytesInput input = IOFactory.createBytesInput(pointer.bytes());
DoubleValue combinedValue = new DoubleValue();
combinedValue.read(input);
Assert.assertEquals(new DoubleValue(3.0D), combinedValue);
}
}
use of com.baidu.hugegraph.computer.core.io.BytesInput in project hugegraph-computer by hugegraph.
the class UnitTestBase method assertValueEqualAfterWriteAndRead.
public static void assertValueEqualAfterWriteAndRead(Value oldValue) throws IOException {
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
oldValue.write(bao);
bytes = bao.toByteArray();
}
Value newValue = graphFactory().createValue(oldValue.valueType());
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
newValue.read(bai);
Assert.assertEquals(oldValue, newValue);
}
}
use of com.baidu.hugegraph.computer.core.io.BytesInput in project hugegraph-computer by hugegraph.
the class UnitTestBase method assertEqualAfterWriteAndRead.
public static void assertEqualAfterWriteAndRead(Writable writeObj, Readable readObj) throws IOException {
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
writeObj.write(bao);
bytes = bao.toByteArray();
}
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
readObj.read(bai);
Assert.assertEquals(writeObj, readObj);
}
}
use of com.baidu.hugegraph.computer.core.io.BytesInput in project hugegraph-computer by hugegraph.
the class SerializeUtil method fromBytes.
public static <V extends Readable> List<V> fromBytes(byte[] bytes, Supplier<V> supplier) {
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
int size = bai.readInt();
List<V> list = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
V obj = supplier.get();
obj.read(bai);
list.add(obj);
}
return list;
} catch (IOException e) {
throw new ComputerException("Failed to read from byte array", e);
}
}
use of com.baidu.hugegraph.computer.core.io.BytesInput in project hugegraph-computer by hugegraph.
the class PointerCombinerTest method testVertexPropertiesCombiner.
@Test
public void testVertexPropertiesCombiner() throws IOException {
Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.WORKER_COMBINER_CLASS, DoubleValueSumCombiner.class.getName(), ComputerOptions.WORKER_VERTEX_PROPERTIES_COMBINER_CLASS, MergeOldPropertiesCombiner.class.getName());
Combiner<Properties> valueCombiner = config.createObject(ComputerOptions.WORKER_VERTEX_PROPERTIES_COMBINER_CLASS);
GraphFactory graphFactory = graphFactory();
PointerCombiner combiner = SorterTestUtil.createPointerCombiner(graphFactory::createProperties, valueCombiner);
try (BytesOutput bytesOutput1 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
BytesOutput bytesOutput2 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
Properties value1 = graphFactory.createProperties();
value1.put("p1", new LongValue(1L));
Properties value2 = graphFactory.createProperties();
value2.put("p2", new LongValue(2L));
value1.write(bytesOutput1);
value2.write(bytesOutput2);
Pointer pointer1 = new InlinePointer(bytesOutput1.buffer(), bytesOutput1.position());
Pointer pointer2 = new InlinePointer(bytesOutput2.buffer(), bytesOutput2.position());
Pointer pointer = combiner.combine(pointer1, pointer2);
BytesInput input = IOFactory.createBytesInput(pointer.bytes());
Properties combinedValue = graphFactory.createProperties();
combinedValue.read(input);
Map<String, Value> map = combinedValue.get();
Assert.assertEquals(2, map.size());
Assert.assertEquals(new LongValue(1L), map.get("p1"));
Assert.assertEquals(new LongValue(2L), map.get("p2"));
}
}
Aggregations