use of com.baidu.hugegraph.computer.core.graph.value.Value in project hugegraph-computer by hugegraph.
the class DefaultPropertiesTest method testOverwrite.
@Test
public void testOverwrite() {
DefaultProperties properties = new DefaultProperties(graphFactory());
Assert.assertEquals(0, properties.get().size());
properties.put("p1", new LongValue(1L));
properties.put("p2", new DoubleValue(2.0D));
Assert.assertEquals(new LongValue(1L), properties.get("p1"));
properties.put("p1", new LongValue(2L));
Assert.assertEquals(new LongValue(2L), properties.get("p1"));
Map<String, Value> props = properties.get();
Assert.assertEquals(2, props.size());
Assert.assertEquals(new LongValue(2L), props.get("p1"));
Assert.assertEquals(new DoubleValue(2.0D), props.get("p2"));
}
use of com.baidu.hugegraph.computer.core.graph.value.Value in project hugegraph-computer by hugegraph.
the class JsonStructGraphOutputTest method testWriteReadVertexOnlyIdAndValue.
@Test
public void testWriteReadVertexOnlyIdAndValue() throws IOException {
UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "false", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false", ComputerOptions.OUTPUT_RESULT_NAME, "rank");
ComputerContext context = context();
GraphFactory factory = context.graphFactory();
Id longId = BytesId.of(100L);
Value value = BytesId.of(999L);
Vertex vertex = factory.createVertex(longId, value);
String fileName = "output.json";
File file = new File(fileName);
try {
BufferedFileOutput dos = new BufferedFileOutput(file);
StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.JSON, dos);
output.writeVertex(vertex);
dos.close();
@SuppressWarnings("deprecation") String json = FileUtils.readFileToString(file);
Assert.assertEquals("{\"id\":100,\"rank\":999}" + System.lineSeparator(), json);
} finally {
FileUtils.deleteQuietly(file);
}
}
use of com.baidu.hugegraph.computer.core.graph.value.Value 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.graph.value.Value 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.graph.value.Value in project hugegraph-computer by hugegraph.
the class StreamGraphOutputInputTest method testWriteReadMessage.
@Test
public void testWriteReadMessage() throws IOException {
UnitTestBase.updateOptions(ComputerOptions.ALGORITHM_MESSAGE_CLASS, DoubleValue.class.getName());
Id id = BytesId.of(999L);
Value value = new DoubleValue(0.85D);
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
StreamGraphOutput output = newStreamGraphOutput(bao);
output.writeMessage(id, value);
bytes = bao.toByteArray();
System.out.println(Arrays.toString(bytes));
}
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
StreamGraphInput input = newStreamGraphInput(bai);
Assert.assertEquals(Pair.of(id, value), input.readMessage());
}
}
Aggregations