Search in sources :

Example 1 with Properties

use of com.baidu.hugegraph.computer.core.graph.properties.Properties in project hugegraph-computer by hugegraph.

the class EdgesInputTest method addEdgeBuffer.

private static void addEdgeBuffer(Consumer<ManagedBuffer> consumer, EdgeFrequency freq) throws IOException {
    for (long i = 0L; i < 200L; i++) {
        Vertex vertex = graphFactory().createVertex();
        vertex.id(BytesId.of(i));
        int count = (int) i;
        if (count == 0) {
            continue;
        }
        Edges edges = graphFactory().createEdges(count);
        for (long j = 0; j < count; j++) {
            Edge edge = graphFactory().createEdge();
            switch(freq) {
                case SINGLE:
                    edge.targetId(BytesId.of(j));
                    break;
                case SINGLE_PER_LABEL:
                    edge.label(String.valueOf(j));
                    edge.targetId(BytesId.of(j));
                    break;
                case MULTIPLE:
                    edge.name(String.valueOf(j));
                    edge.label(String.valueOf(j));
                    edge.targetId(BytesId.of(j));
                    break;
                default:
                    throw new ComputerException("Illegal edge frequency %s", freq);
            }
            Properties properties = graphFactory().createProperties();
            properties.put("p1", new LongValue(i));
            edge.properties(properties);
            edges.add(edge);
        }
        vertex.edges(edges);
        ReceiverUtil.consumeBuffer(writeEdges(vertex, freq), consumer);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 2 with Properties

use of com.baidu.hugegraph.computer.core.graph.properties.Properties in project hugegraph-computer by hugegraph.

the class EdgesInput method readEdges.

// TODO: use one reused Edges instance to read batches for each vertex.
private Edges readEdges(RandomAccessInput in) {
    try {
        int count = in.readFixedInt();
        Edges edges = this.graphFactory.createEdges(count);
        if (this.frequency == EdgeFrequency.SINGLE) {
            for (int i = 0; i < count; i++) {
                Edge edge = this.graphFactory.createEdge();
                // Only use targetId as subKey, use props as subValue
                edge.targetId(StreamGraphInput.readId(in));
                // Read subValue
                Properties props = this.graphFactory.createProperties();
                props.read(in);
                edge.properties(props);
                edges.add(edge);
            }
        } else if (this.frequency == EdgeFrequency.SINGLE_PER_LABEL) {
            for (int i = 0; i < count; i++) {
                Edge edge = this.graphFactory.createEdge();
                // Use label + targetId as subKey, use props as subValue
                edge.label(StreamGraphInput.readLabel(in));
                edge.targetId(StreamGraphInput.readId(in));
                // Read subValue
                Properties props = this.graphFactory.createProperties();
                props.read(in);
                edge.properties(props);
                edges.add(edge);
            }
        } else {
            assert this.frequency == EdgeFrequency.MULTIPLE;
            for (int i = 0; i < count; i++) {
                Edge edge = this.graphFactory.createEdge();
                /*
                     * Use label + sortValues + targetId as subKey,
                     * use properties as subValue
                     */
                edge.label(StreamGraphInput.readLabel(in));
                edge.name(StreamGraphInput.readLabel(in));
                edge.targetId(StreamGraphInput.readId(in));
                // Read subValue
                Properties props = this.graphFactory.createProperties();
                props.read(in);
                edge.properties(props);
                edges.add(edge);
            }
        }
        return edges;
    } catch (IOException e) {
        throw new ComputerException("Failed to read edges from input '%s'", e, this.edgeFile.getAbsoluteFile());
    }
}
Also used : IOException(java.io.IOException) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 3 with Properties

use of com.baidu.hugegraph.computer.core.graph.properties.Properties 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 4 with Properties

use of com.baidu.hugegraph.computer.core.graph.properties.Properties in project hugegraph-computer by hugegraph.

the class EdgeMessageRecvPartitionTest method checkTenEdgesWithCombinedProperties.

private static void checkTenEdgesWithCombinedProperties(Iterator<KvEntry> it) throws IOException {
    for (long i = 0L; i < 10L; i++) {
        Assert.assertTrue(it.hasNext());
        KvEntry entry = it.next();
        Id id = ReceiverUtil.readId(entry.key());
        Assert.assertEquals(BytesId.of(i), id);
        EntryIterator subKvIt = EntriesUtil.subKvIterFromEntry(entry);
        for (long j = i + 1; j < i + 3; j++) {
            Assert.assertTrue(subKvIt.hasNext());
            KvEntry subKv = subKvIt.next();
            Id targetId = ReceiverUtil.readId(subKv.key());
            Assert.assertEquals(BytesId.of(j), targetId);
            Properties properties = graphFactory().createProperties();
            ReceiverUtil.readValue(subKv.value(), properties);
            Assert.assertEquals(2, properties.size());
            LongValue v1 = properties.get("p1");
            Assert.assertEquals(new LongValue(i), v1);
            LongValue v2 = properties.get("p2");
            Assert.assertEquals(new LongValue(2L * i), v2);
        }
    }
    Assert.assertFalse(it.hasNext());
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) EntryIterator(com.baidu.hugegraph.computer.core.store.EntryIterator) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties)

Example 5 with Properties

use of com.baidu.hugegraph.computer.core.graph.properties.Properties in project hugegraph-computer by hugegraph.

the class EdgeMessageRecvPartitionTest method checkTenEdges.

public static void checkTenEdges(PeekableIterator<KvEntry> it) throws IOException {
    for (long i = 0L; i < 10L; i++) {
        Assert.assertTrue(it.hasNext());
        KvEntry entry = it.next();
        Id id = ReceiverUtil.readId(entry.key());
        Assert.assertEquals(BytesId.of(i), id);
        EntryIterator subKvIt = EntriesUtil.subKvIterFromEntry(entry);
        for (long j = i + 1; j < i + 3; j++) {
            Assert.assertTrue(subKvIt.hasNext());
            KvEntry subKv = subKvIt.next();
            Id targetId = ReceiverUtil.readId(subKv.key());
            Assert.assertEquals(BytesId.of(j), targetId);
            Properties properties = graphFactory().createProperties();
            ReceiverUtil.readValue(subKv.value(), properties);
            Assert.assertEquals(1, properties.size());
            LongValue v1 = properties.get("p1");
            Assert.assertEquals(new LongValue(i), v1);
        }
    }
    Assert.assertFalse(it.hasNext());
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) EntryIterator(com.baidu.hugegraph.computer.core.store.EntryIterator) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties)

Aggregations

Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)30 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)20 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)15 Test (org.junit.Test)12 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)11 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)9 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)7 Id (com.baidu.hugegraph.computer.core.graph.id.Id)6 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)5 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)5 Value (com.baidu.hugegraph.computer.core.graph.value.Value)5 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)4 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)3 Config (com.baidu.hugegraph.computer.core.config.Config)3 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)3 Pointer (com.baidu.hugegraph.computer.core.store.entry.Pointer)3 IOException (java.io.IOException)3 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)2 ComputerOptions (com.baidu.hugegraph.computer.core.config.ComputerOptions)2 EdgeFrequency (com.baidu.hugegraph.computer.core.config.EdgeFrequency)2