use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class WriteBuffersTest method testWriteVertex.
@Test
public void testWriteVertex() throws IOException {
GraphFactory graphFactory = context().graphFactory();
// NOTE: need ensure the buffer size can hold follow writed bytes
WriteBuffers buffers = new WriteBuffers(context(), 100, 110);
Vertex vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
buffers.writeVertex(vertex);
WriteBuffer buffer = Whitebox.getInternalState(buffers, "writingBuffer");
long position1 = buffer.output().position();
Assert.assertGt(0L, position1);
vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
Properties properties = graphFactory.createProperties();
properties.put("name", BytesId.of("marko"));
properties.put("age", new IntValue(18));
properties.put("city", new ListValue<>(ValueType.ID, ImmutableList.of(BytesId.of("wuhan"), BytesId.of("xian"))));
vertex.properties(properties);
buffers.writeVertex(vertex);
buffer = Whitebox.getInternalState(buffers, "writingBuffer");
long position2 = buffer.output().position();
Assert.assertGt(position1, position2);
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);
buffer = Whitebox.getInternalState(buffers, "writingBuffer");
long position3 = buffer.output().position();
Assert.assertGt(position2, position3);
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class WriteBuffersTest method wrapForRead.
@Test
public void wrapForRead() throws IOException {
GraphFactory graphFactory = context().graphFactory();
WriteBuffers buffers = new WriteBuffers(context(), 10, 20);
Vertex vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
buffers.writeVertex(vertex);
buffers.prepareSorting();
try (RandomAccessInput input = buffers.wrapForRead()) {
EntryInput entryInput = new EntryInputImpl(input);
StreamGraphInput graphInput = new StreamGraphInput(context(), entryInput);
vertex.value(null);
Assert.assertEquals(vertex, graphInput.readVertex());
}
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class WriteBufferTest method testWriteVertex.
@Test
public void testWriteVertex() throws IOException {
GraphFactory graphFactory = context.graphFactory();
// NOTE: need ensure the buffer size can hold follow writed bytes
WriteBuffer buffer = new WriteBuffer(context, 100, 110);
Vertex vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
buffer.writeVertex(vertex);
long position1 = buffer.output().position();
Assert.assertGt(0L, position1);
vertex = graphFactory.createVertex(BytesId.of(1L), new DoubleValue(0.5d));
Properties properties = graphFactory.createProperties();
properties.put("name", BytesId.of("marko"));
properties.put("age", new IntValue(18));
properties.put("city", new ListValue<>(ValueType.ID, ImmutableList.of(BytesId.of("wuhan"), BytesId.of("xian"))));
vertex.properties(properties);
buffer.writeVertex(vertex);
long position2 = buffer.output().position();
Assert.assertGt(position1, position2);
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)));
buffer.writeEdges(vertex);
long position3 = buffer.output().position();
Assert.assertGt(position2, position3);
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class ComputerContextUtil method initContext.
public static Config initContext(Map<String, String> params) {
// Set algorithm's parameters
String algorithmParamsName = params.get(ComputerOptions.ALGORITHM_PARAMS_CLASS.name());
AlgorithmParams algorithmParams;
try {
algorithmParams = (AlgorithmParams) Class.forName(algorithmParamsName).newInstance();
} catch (Exception e) {
throw new ComputerException("Can't create algorithmParams, " + "algorithmParamsName = {}", algorithmParamsName);
}
algorithmParams.setAlgorithmParameters(params);
Config config = new DefaultConfig(params);
GraphFactory graphFactory = new BuiltinGraphFactory();
Allocator allocator = new DefaultAllocator(config, graphFactory);
ComputerContext.initContext(config, graphFactory, allocator);
return config;
}
use of com.baidu.hugegraph.computer.core.graph.GraphFactory in project hugegraph-computer by hugegraph.
the class JsonStructGraphOutputTest method testWriteReadVertexOnlyIdAndValue.
@Test
public void testWriteReadVertexOnlyIdAndValue() throws IOException {
UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", 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);
Value value = BytesId.of(999L);
Vertex vertex = factory.createVertex(longId, value);
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\":999}" + System.lineSeparator(), json);
} finally {
FileUtils.deleteQuietly(file);
}
}
Aggregations