The examples from the tutorials or the online documentations often use the Gremlin/Groovy shell to demonstrate the TitanDB APIs. I'm working in plain (old, but not so old) Java-8, and the first thing I need to implement is an efficient method to add vertices and edges to a graph.
So, to getOrCreate a vertex with a String identifier, I did this:
private Vertex getOrCreate(TitanGraph g, String vertexId) {
Iterator<Vertex> vertices = g.vertices();
if (!vertices.hasNext()) { // empty graph?
Vertex v = g.addVertex("id", vertexId);
return v;
} else
while (vertices.hasNext()) {
Vertex nextVertex = vertices.next();
if (nextVertex.property("id").equals(vertexId)) {
return nextVertex;
} else {
Vertex v = g.addVertex("id", vertexId);
return v;
}
}
return null;
}
Is this the most efficient technique offered by the TitanDB APIs ?