use of com.baidu.hugegraph.computer.core.io.BytesOutput 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"));
}
}
use of com.baidu.hugegraph.computer.core.io.BytesOutput in project hugegraph-computer by hugegraph.
the class PointerCombinerTest method testCombineEdgePropertiesFail.
@Test
public void testCombineEdgePropertiesFail() throws IOException {
Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.WORKER_COMBINER_CLASS, DoubleValueSumCombiner.class.getName(), ComputerOptions.WORKER_EDGE_PROPERTIES_COMBINER_CLASS, MergeOldPropertiesCombiner.class.getName());
Combiner<Properties> valueCombiner = config.createObject(ComputerOptions.WORKER_EDGE_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));
// Only write count.
bytesOutput1.writeInt(1);
value2.write(bytesOutput2);
Pointer pointer1 = new InlinePointer(bytesOutput1.buffer(), bytesOutput1.position());
Pointer pointer2 = new InlinePointer(bytesOutput2.buffer(), bytesOutput2.position());
Assert.assertThrows(ComputerException.class, () -> {
combiner.combine(pointer1, pointer2);
}, e -> {
Assert.assertContains("Failed to combine pointer", e.getMessage());
});
}
}
use of com.baidu.hugegraph.computer.core.io.BytesOutput in project hugegraph-computer by hugegraph.
the class ComputeManagerTest method writeVertex.
private static byte[] writeVertex(Vertex vertex) throws IOException {
BytesOutput bytesOutput = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
EntryOutput entryOutput = new EntryOutputImpl(bytesOutput);
GraphComputeOutput output = new StreamGraphOutput(context(), entryOutput);
output.writeVertex(vertex);
return bytesOutput.toByteArray();
}
use of com.baidu.hugegraph.computer.core.io.BytesOutput in project hugegraph-computer by hugegraph.
the class UnitTestBase method assertIdEqualAfterWriteAndRead.
public static void assertIdEqualAfterWriteAndRead(Id oldId) throws IOException {
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
oldId.write(bao);
bytes = bao.toByteArray();
}
Id newId = IdFactory.createId(oldId.idType());
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
newId.read(bai);
Assert.assertEquals(oldId, newId);
}
}
use of com.baidu.hugegraph.computer.core.io.BytesOutput in project hugegraph-computer by hugegraph.
the class ReceiverUtil method writeMessage.
public static byte[] writeMessage(Id id, Writable message) throws IOException {
BytesOutput bytesOutput = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
EntryOutput entryOutput = new EntryOutputImpl(bytesOutput);
entryOutput.writeEntry(out -> {
id.write(out);
}, message);
return bytesOutput.toByteArray();
}
Aggregations