Search in sources :

Example 31 with Vertex

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();
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 32 with Vertex

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();
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Test(org.junit.Test)

Example 33 with Vertex

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);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex)

Example 34 with Vertex

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);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) File(java.io.File) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 35 with Vertex

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());
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Test(org.junit.Test)

Aggregations

Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)46 Test (org.junit.Test)21 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)18 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)16 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)15 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)15 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)13 Id (com.baidu.hugegraph.computer.core.graph.id.Id)12 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)11 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)11 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)10 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)7 Value (com.baidu.hugegraph.computer.core.graph.value.Value)7 IOException (java.io.IOException)7 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)6 File (java.io.File)6 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)4 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)4 ComputerOptions (com.baidu.hugegraph.computer.core.config.ComputerOptions)2