Search in sources :

Example 1 with GraphFactory

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);
}
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) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) Test(org.junit.Test)

Example 2 with GraphFactory

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

Example 3 with GraphFactory

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);
}
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) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) Test(org.junit.Test)

Example 4 with GraphFactory

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;
}
Also used : BuiltinGraphFactory(com.baidu.hugegraph.computer.core.graph.BuiltinGraphFactory) Allocator(com.baidu.hugegraph.computer.core.allocator.Allocator) DefaultAllocator(com.baidu.hugegraph.computer.core.allocator.DefaultAllocator) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) BuiltinGraphFactory(com.baidu.hugegraph.computer.core.graph.BuiltinGraphFactory) AlgorithmParams(com.baidu.hugegraph.computer.algorithm.AlgorithmParams) Config(com.baidu.hugegraph.computer.core.config.Config) DefaultConfig(com.baidu.hugegraph.computer.core.config.DefaultConfig) DefaultConfig(com.baidu.hugegraph.computer.core.config.DefaultConfig) DefaultAllocator(com.baidu.hugegraph.computer.core.allocator.DefaultAllocator) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 5 with GraphFactory

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);
    }
}
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) FloatValue(com.baidu.hugegraph.computer.core.graph.value.FloatValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) BooleanValue(com.baidu.hugegraph.computer.core.graph.value.BooleanValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) File(java.io.File) 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