Search in sources :

Example 31 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class WriteBufferTest method testWriteMessage.

@Test
public void testWriteMessage() throws IOException {
    WriteBuffer buffer = new WriteBuffer(context, 50, 100);
    buffer.writeMessage(BytesId.of(1L), new DoubleValue(0.85D));
    long position1 = buffer.output().position();
    Assert.assertGt(0L, position1);
    buffer.writeMessage(BytesId.of(2L), new DoubleValue(0.15D));
    long position2 = buffer.output().position();
    Assert.assertGt(position1, position2);
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Test(org.junit.Test)

Example 32 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class WriteBufferTest method testClear.

@Test
public void testClear() throws IOException {
    WriteBuffer buffer = new WriteBuffer(context, 10, 20);
    Assert.assertTrue(buffer.isEmpty());
    Vertex vertex = context.graphFactory().createVertex(BytesId.of(1L), new DoubleValue(0.5d));
    buffer.writeVertex(vertex);
    Assert.assertFalse(buffer.isEmpty());
    buffer.clear();
    Assert.assertTrue(buffer.isEmpty());
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Test(org.junit.Test)

Example 33 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class WriteBuffersTest method testWriteMessage.

@Test
public void testWriteMessage() throws IOException {
    WriteBuffers buffers = new WriteBuffers(context(), 50, 100);
    WriteBuffer buffer = Whitebox.getInternalState(buffers, "writingBuffer");
    buffers.writeMessage(BytesId.of(1L), new DoubleValue(0.85D));
    long position1 = buffer.output().position();
    Assert.assertGt(0L, position1);
    buffers.writeMessage(BytesId.of(2L), new DoubleValue(0.15D));
    long position2 = buffer.output().position();
    Assert.assertGt(position1, position2);
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Test(org.junit.Test)

Example 34 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue 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 35 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue 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)

Aggregations

DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)49 Test (org.junit.Test)28 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)21 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)14 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)13 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)12 Id (com.baidu.hugegraph.computer.core.graph.id.Id)10 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)8 DefaultProperties (com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties)7 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)6 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)5 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)3 Value (com.baidu.hugegraph.computer.core.graph.value.Value)3 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)2 DefaultEdge (com.baidu.hugegraph.computer.core.graph.edge.DefaultEdge)2 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)2 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)2 ListValue (com.baidu.hugegraph.computer.core.graph.value.ListValue)2 File (java.io.File)2