use of com.baidu.hugegraph.computer.core.graph.GraphFactory 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.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class WriteBufferTest method testWriteVertexWithEdgeFreq.
@Test
public void testWriteVertexWithEdgeFreq() throws IOException {
GraphFactory graphFactory = context.graphFactory();
Vertex vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
WriteBuffer buffer;
UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "SINGLE");
buffer = new WriteBuffer(ComputerContext.instance(), 100, 110);
buffer.writeEdges(vertex);
long position1 = buffer.output().position();
Assert.assertGt(0L, position1);
UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "SINGLE_PER_LABEL");
// Pass a new context object, because config has updated
buffer = new WriteBuffer(ComputerContext.instance(), 100, 110);
buffer.writeEdges(vertex);
long position2 = buffer.output().position();
Assert.assertGte(position1, position2);
UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "MULTIPLE");
// Pass a new context object, because config has updated
buffer = new WriteBuffer(ComputerContext.instance(), 100, 110);
buffer.writeEdges(vertex);
long position3 = buffer.output().position();
Assert.assertGte(position2, position3);
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class WriteBuffersTest method testSwitchAndFinishSorting.
@Test
public void testSwitchAndFinishSorting() throws IOException, InterruptedException {
GraphFactory graphFactory = context().graphFactory();
WriteBuffers buffers = new WriteBuffers(context(), 50, 100);
Vertex vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
buffers.writeEdges(vertex);
// Reached threshold, the position is 76
Assert.assertTrue(buffers.reachThreshold());
/*
* When reached threshold, switchForSorting will exchange writing buffer
* and sorting buffer, so the writing buffer become clean
*/
buffers.switchForSorting();
Assert.assertFalse(buffers.reachThreshold());
Assert.assertTrue(buffers.isEmpty());
// Nothing changed
buffers.switchForSorting();
Assert.assertFalse(buffers.reachThreshold());
Assert.assertTrue(buffers.isEmpty());
// The writing buffer reached threshold again, position is 76
buffers.writeEdges(vertex);
AtomicInteger counter = new AtomicInteger(0);
Thread thread1 = new Thread(() -> {
// Await until finishSorting method called
buffers.switchForSorting();
Assert.assertEquals(2, counter.get());
});
Thread thread2 = new Thread(() -> {
while (counter.get() < 2) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Assert.fail(e.getMessage());
}
counter.incrementAndGet();
}
// counter is 2
buffers.finishSorting();
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class WriteBuffersTest method testPrepareSorting.
@Test
public void testPrepareSorting() throws IOException, InterruptedException {
GraphFactory graphFactory = context().graphFactory();
WriteBuffers buffers = new WriteBuffers(context(), 50, 100);
Vertex vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
buffers.writeEdges(vertex);
// Reached threshold, the position is 76
Assert.assertTrue(buffers.reachThreshold());
Assert.assertFalse(buffers.isEmpty());
// Exchange writing buffer and sorting buffer
buffers.prepareSorting();
Assert.assertFalse(buffers.reachThreshold());
Assert.assertTrue(buffers.isEmpty());
Thread thread1 = new Thread(() -> {
Assert.assertThrows(ComputerException.class, () -> {
buffers.prepareSorting();
}, e -> {
Assert.assertTrue(e.getMessage().contains("Interrupted"));
});
});
thread1.start();
Thread.sleep(100);
thread1.interrupt();
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class JsonStructGraphOutputTest method testWriteReadVertexWithEdges.
@Test
public void testWriteReadVertexWithEdges() throws IOException {
UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "true", 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);
IdList idList = new IdList();
idList.add(BytesId.of(998L));
idList.add(BytesId.of(999L));
Vertex vertex = factory.createVertex(longId, idList);
vertex.addEdge(factory.createEdge("knows", BytesId.of(200)));
vertex.addEdge(factory.createEdge("watch", "1111", BytesId.of(300)));
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\":[998,999]," + "\"adjacent_edges\":[{\"target_id\":200," + "\"label\":\"knows\",\"name\":\"\"}," + "{\"target_id\":300,\"label\":\"watch\"," + "\"name\":\"1111\"}]}" + System.lineSeparator(), json);
} finally {
FileUtils.deleteQuietly(file);
}
}
Aggregations