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