com.tinkerpop.gremlin.giraph.structure.util.GiraphInternalVertex Maven / Gradle / Ivy
package com.tinkerpop.gremlin.giraph.structure.util;
import com.tinkerpop.gremlin.giraph.Constants;
import com.tinkerpop.gremlin.giraph.process.computer.GiraphGraphComputerSideEffects;
import com.tinkerpop.gremlin.giraph.process.computer.GiraphMessenger;
import com.tinkerpop.gremlin.giraph.process.computer.util.ConfUtil;
import com.tinkerpop.gremlin.giraph.process.computer.util.KryoWritable;
import com.tinkerpop.gremlin.giraph.structure.io.EmptyOutEdges;
import com.tinkerpop.gremlin.process.computer.VertexProgram;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.Graph;
import com.tinkerpop.gremlin.structure.io.kryo.KryoReader;
import com.tinkerpop.gremlin.structure.io.kryo.KryoWriter;
import com.tinkerpop.gremlin.structure.util.ElementHelper;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex;
import org.apache.giraph.graph.Vertex;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class GiraphInternalVertex extends Vertex {
//TODO: Dangerous that the underlying TinkerGraph Vertex can have edges written to it.
private static final String VERTEX_ID = Graph.Key.hide("vertexId");
private VertexProgram vertexProgram;
private TinkerGraph tinkerGraph;
private TinkerVertex tinkerVertex;
private GiraphGraphComputerSideEffects sideEffects;
public GiraphInternalVertex() {
}
public GiraphInternalVertex(final TinkerVertex tinkerVertex) {
this.tinkerGraph = TinkerGraph.open();
this.tinkerVertex = tinkerVertex;
this.tinkerGraph.variables().set(VERTEX_ID, this.tinkerVertex.id());
final TinkerVertex vertex = (TinkerVertex) this.tinkerGraph.addVertex(Element.ID, this.tinkerVertex.id(), Element.LABEL, this.tinkerVertex.label());
this.tinkerVertex.properties().forEach((k, v) -> vertex.property(k, v.value()));
this.tinkerVertex.outE().forEach(edge -> {
final TinkerVertex otherVertex = (TinkerVertex) ElementHelper.getOrAddVertex(this.tinkerGraph, edge.inV().id().next(), edge.inV().label().next());
final TinkerEdge tinkerEdge = (TinkerEdge) vertex.addEdge(edge.label(), otherVertex, Element.ID, edge.id());
edge.properties().forEach((k, v) -> tinkerEdge.property(k, v.value()));
});
this.tinkerVertex.inE().forEach(edge -> {
final TinkerVertex otherVertex = (TinkerVertex) ElementHelper.getOrAddVertex(this.tinkerGraph, edge.outV().id().next(), edge.outV().label().next());
final TinkerEdge tinkerEdge = (TinkerEdge) otherVertex.addEdge(edge.label(), vertex, Element.ID, edge.id());
edge.properties().forEach((k, v) -> tinkerEdge.property(k, v.value()));
});
this.initialize(new LongWritable(Long.valueOf(this.tinkerVertex.id().toString())), this.deflateTinkerVertex(), EmptyOutEdges.instance());
// TODO? this.tinkerVertex = vertex;
}
public TinkerVertex getTinkerVertex() {
return this.tinkerVertex;
}
@Override
public void compute(final Iterable messages) {
if (null == this.tinkerVertex)
inflateTinkerVertex();
if (null == this.vertexProgram)
this.vertexProgram = VertexProgram.createVertexProgram(ConfUtil.makeApacheConfiguration(this.getConf()));
if (null == this.sideEffects)
this.sideEffects = new GiraphGraphComputerSideEffects(this, this.vertexProgram);
this.vertexProgram.execute(this.tinkerVertex, new GiraphMessenger(this, messages), this.sideEffects);
if (this.getConf().getBoolean(Constants.GREMLIN_DERIVE_COMPUTER_SIDE_EFFECTS, false)) {
this.sideEffects.keys().forEach(key ->
this.sideEffects.get(key).ifPresent(v ->
this.tinkerVertex. © 2015 - 2025 Weber Informatics LLC | Privacy Policy