Search in sources :

Example 11 with GraphFactory

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());
        });
    }
}
Also used : GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) Config(com.baidu.hugegraph.computer.core.config.Config) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Test(org.junit.Test)

Example 12 with GraphFactory

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);
}
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 13 with GraphFactory

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();
}
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 14 with GraphFactory

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();
}
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 15 with GraphFactory

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);
    }
}
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)

Aggregations

GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)18 Test (org.junit.Test)17 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)15 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)11 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)9 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)9 Id (com.baidu.hugegraph.computer.core.graph.id.Id)9 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)9 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)6 File (java.io.File)6 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)4 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 Config (com.baidu.hugegraph.computer.core.config.Config)3 Value (com.baidu.hugegraph.computer.core.graph.value.Value)3 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)2 BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)2 InlinePointer (com.baidu.hugegraph.computer.core.store.entry.InlinePointer)2 Pointer (com.baidu.hugegraph.computer.core.store.entry.Pointer)2