Search in sources :

Example 1 with Edges

use of com.baidu.hugegraph.computer.core.graph.edge.Edges in project hugegraph-computer by hugegraph.

the class FileGraphPartition method compute1.

private long compute1(ComputationContext context, Computation<M> computation, int superstep) {
    Value result = this.context.config().createObject(ComputerOptions.ALGORITHM_RESULT_CLASS);
    long activeVertexCount = 0L;
    while (this.vertexInput.hasNext()) {
        Vertex vertex = this.vertexInput.next();
        this.readVertexStatusAndValue(vertex, result);
        Iterator<M> messageIter = this.messageInput.iterator(this.vertexInput.idPointer());
        if (messageIter.hasNext()) {
            vertex.reactivate();
        }
        /*
             * If the vertex is inactive, it's edges will be skipped
             * automatically at the next vertex.
             */
        if (vertex.active()) {
            Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
            vertex.edges(edges);
            computation.compute(context, vertex, messageIter);
        }
        // The vertex status may be changed after computation
        if (vertex.active()) {
            activeVertexCount++;
        }
        try {
            this.saveVertexStatusAndValue(vertex);
        } catch (IOException e) {
            throw new ComputerException("Error occurred when saveVertex", e);
        }
    }
    return activeVertexCount;
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IOException(java.io.IOException) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 2 with Edges

use of com.baidu.hugegraph.computer.core.graph.edge.Edges in project hugegraph-computer by hugegraph.

the class FileGraphPartition method compute0.

private long compute0(ComputationContext context, Computation<M> computation) {
    long activeVertexCount = 0L;
    while (this.vertexInput.hasNext()) {
        Vertex vertex = this.vertexInput.next();
        vertex.reactivate();
        Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
        vertex.edges(edges);
        computation.compute0(context, vertex);
        if (vertex.active()) {
            activeVertexCount++;
        }
        try {
            this.saveVertexStatusAndValue(vertex);
        } catch (IOException e) {
            throw new ComputerException("Error occurred when saveVertex: %s", e, vertex);
        }
    }
    return activeVertexCount;
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) IOException(java.io.IOException) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 3 with Edges

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

use of com.baidu.hugegraph.computer.core.graph.edge.Edges in project hugegraph-computer by hugegraph.

the class FileGraphPartition method output.

protected PartitionStat output() {
    ComputerOutput output = this.context.config().createObject(ComputerOptions.OUTPUT_CLASS);
    output.init(this.context.config(), this.partition);
    try {
        this.beforeOutput();
    } catch (IOException e) {
        throw new ComputerException("Error occurred when beforeOutput", e);
    }
    Value result = this.context.config().createObject(ComputerOptions.ALGORITHM_RESULT_CLASS);
    while (this.vertexInput.hasNext()) {
        Vertex vertex = this.vertexInput.next();
        this.readVertexStatusAndValue(vertex, result);
        Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
        vertex.edges(edges);
        output.write(vertex);
    }
    try {
        this.afterOutput();
    } catch (IOException e) {
        throw new ComputerException("Error occurred when afterOutput", e);
    }
    output.close();
    return new PartitionStat(this.partition, this.vertexCount, this.edgeCount, 0L);
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) PartitionStat(com.baidu.hugegraph.computer.core.graph.partition.PartitionStat) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IOException(java.io.IOException) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) ComputerOutput(com.baidu.hugegraph.computer.core.output.ComputerOutput)

Example 5 with Edges

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

Aggregations

Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)18 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)13 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)11 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)10 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)9 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)8 IOException (java.io.IOException)7 Value (com.baidu.hugegraph.computer.core.graph.value.Value)3 Id (com.baidu.hugegraph.computer.core.graph.id.Id)2 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)1 PartitionStat (com.baidu.hugegraph.computer.core.graph.partition.PartitionStat)1 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)1 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)1 ComputerOutput (com.baidu.hugegraph.computer.core.output.ComputerOutput)1 HashSet (java.util.HashSet)1