Commit be7c4cb6 by Venkatesh Seetharam

ISSUE-38 Map type to graph with type prefixes to enable search. Contributed by Venkatesh Seetharam

parent 1d1531ef
...@@ -23,10 +23,14 @@ import com.tinkerpop.blueprints.Edge; ...@@ -23,10 +23,14 @@ import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.GraphQuery; import com.tinkerpop.blueprints.GraphQuery;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import org.apache.hadoop.metadata.ITypedInstance;
import org.apache.hadoop.metadata.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.storage.Id;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID;
/** /**
* Utility class for graph operations. * Utility class for graph operations.
...@@ -38,40 +42,26 @@ public final class GraphUtils { ...@@ -38,40 +42,26 @@ public final class GraphUtils {
private GraphUtils() { private GraphUtils() {
} }
public static Edge addEdge(Vertex fromVertex, Vertex toVertex, public static Vertex createVertex(Graph graph,
String vertexPropertyKey, String edgeLabel) { ITypedReferenceableInstance typedInstance) {
return addEdge(fromVertex, toVertex, vertexPropertyKey, edgeLabel, null); return createVertex(graph, typedInstance, typedInstance.getId());
} }
public static Edge addEdge(Vertex fromVertex, Vertex toVertex, public static Vertex createVertex(Graph graph,
String vertexPropertyKey, String edgeLabel, String timestamp) { ITypedInstance typedInstance,
Edge edge = findEdge(fromVertex, toVertex, vertexPropertyKey, edgeLabel); Id typedInstanceId) {
final Vertex instanceVertex = graph.addVertex(null);
// type
instanceVertex.setProperty(Constants.ENTITY_TYPE_PROPERTY_KEY, typedInstance.getTypeName());
Edge edgeToVertex = edge != null ? edge : fromVertex.addEdge(edgeLabel, toVertex); // id
if (timestamp != null) { final String guid = UUID.randomUUID().toString();
edgeToVertex.setProperty(Constants.TIMESTAMP_PROPERTY_KEY, timestamp); instanceVertex.setProperty(Constants.GUID_PROPERTY_KEY, guid);
}
return edgeToVertex;
}
public static Edge findEdge(Vertex fromVertex, Vertex toVertex, // version
String vertexPropertyKey, String edgeLabel) { instanceVertex.setProperty(Constants.VERSION_PROPERTY_KEY, typedInstanceId.version);
return findEdge(fromVertex, toVertex.getProperty(vertexPropertyKey),
vertexPropertyKey, edgeLabel);
}
public static Edge findEdge(Vertex fromVertex, Object toVertexName,
String vertexPropertyKey, String edgeLabel) {
Edge edgeToFind = null;
for (Edge edge : fromVertex.getEdges(Direction.OUT, edgeLabel)) {
if (edge.getVertex(Direction.IN).getProperty(vertexPropertyKey).equals(toVertexName)) {
edgeToFind = edge;
break;
}
}
return edgeToFind; return instanceVertex;
} }
public static Vertex findVertex(Graph blueprintsGraph, public static Vertex findVertex(Graph blueprintsGraph,
...@@ -97,23 +87,23 @@ public final class GraphUtils { ...@@ -97,23 +87,23 @@ public final class GraphUtils {
public static String edgeString(final Edge edge) { public static String edgeString(final Edge edge) {
return "e[" + edge.getLabel() + "], [" return "e[" + edge.getLabel() + "], ["
+ edge.getVertex(Direction.OUT).getProperty("name") + edge.getVertex(Direction.OUT)
+ " -> " + edge.getLabel() + " -> " + " -> " + edge.getLabel() + " -> "
+ edge.getVertex(Direction.IN).getProperty("name") + edge.getVertex(Direction.IN)
+ "]"; + "]";
} }
public static void dumpToLog(final Graph graph) { public static void dumpToLog(final Graph graph) {
LOG.debug("*******************Graph Dump****************************");
LOG.debug("Vertices of {}", graph); LOG.debug("Vertices of {}", graph);
for (Vertex vertex : graph.getVertices()) { for (Vertex vertex : graph.getVertices()) {
LOG.debug(vertexString(vertex)); LOG.debug(vertexString(vertex));
System.out.println(vertexString(vertex));
} }
LOG.debug("Edges of {}", graph); LOG.debug("Edges of {}", graph);
for (Edge edge : graph.getEdges()) { for (Edge edge : graph.getEdges()) {
LOG.debug(edgeString(edge)); LOG.debug(edgeString(edge));
System.out.println(edgeString(edge));
} }
LOG.debug("*******************Graph Dump****************************");
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment