use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex 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.vertex.Vertex 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.vertex.Vertex in project hugegraph-computer by hugegraph.
the class ComputeManagerTest method add200VertexBuffer.
private static void add200VertexBuffer(Consumer<NetworkBuffer> consumer) throws IOException {
for (long i = 0L; i < 200L; i += 2) {
Vertex vertex = graphFactory().createVertex();
vertex.id(BytesId.of(i));
vertex.properties(graphFactory().createProperties());
ReceiverUtil.consumeBuffer(writeVertex(vertex), consumer);
}
}
use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex 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);
}
}
use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.
the class StreamGraphOutputInputTest method testWriteReadVertex.
@Test
public void testWriteReadVertex() throws Exception {
Id longId = BytesId.of(100L);
LongValue longValue = new LongValue(999L);
Vertex vertex = graphFactory().createVertex(longId, longValue);
Properties properties = graphFactory().createProperties();
properties.put("age", new LongValue(20L));
vertex.properties(properties);
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
StreamGraphOutput output = newStreamGraphOutput(bao);
output.writeVertex(vertex);
bytes = bao.toByteArray();
}
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
StreamGraphInput input = newStreamGraphInput(bai);
assertVertexEqualWithoutValue(vertex, input.readVertex());
}
}
Aggregations