问题描述
教程或在线文档中的示例通常使用 Gremlin/Groovy shell 来演示 TitanDB API.我正在使用普通的(旧的,但不是那么旧的)Java-8,我需要实现的第一件事是向图形添加顶点和边的有效方法.
所以,为了获取或创建一个带有字符串标识符的顶点,我这样做了:
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; }
这是 TitanDB API 提供的最有效的技术吗?
推荐答案
首先,Gremlin Java 和 Groovy 之间没有真正的分离.您可以在两者中同样出色地编写 Gremlin.因此,我想说,只需将 Gremlin 用于您的 getOrCreate 即可归结为一个基本的单行:
gremlin> graph = TinkerFactory.createModern() ==>tinkergraph[vertices:6 edges:6] gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard] gremlin> g.V(1).tryNext().orElseGet{graph.addVertex(id, 1)} ==>v[1]
上面的代码是 Groovy 语法,但 Java 几乎是一样的:
g.V(1).tryNext().orElseGet(() -> graph.addVertex(id, 1));
唯一的区别是 lambda/closure 语法.请注意,在我的情况下, id 是 Element 的保留属性 - 它是唯一标识符.您可能会考虑为您的"标识符"使用与"id"不同的名称 - 可能是"uniqueId",在这种情况下,您的 getOrCreate 将如下所示:
private Vertex getOrCreate(TitanGraph graph, String vertexId) { GraphTraversalSource g = graph.traversal(); return g.V().has("uniqueId", vertexId).tryNext().orElseGet(() -> graph.addVertex("uniqueId", vertexId); }
如果可以的话,我还建议绕过 GraphTraversalSource - 无需使用 graph.traversal() 方法一遍又一遍地创建它.
问题描述
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 ?
推荐答案
First of all, there is no real separation any more between Gremlin Java and Groovy. You can write Gremlin equally well in both. So with that, I'd say, just use Gremlin for your getOrCreate which comes down to a basic one liner:
gremlin> graph = TinkerFactory.createModern() ==>tinkergraph[vertices:6 edges:6] gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard] gremlin> g.V(1).tryNext().orElseGet{graph.addVertex(id, 1)} ==>v[1]
The above code is Groovy syntax, but the Java is pretty much the same:
g.V(1).tryNext().orElseGet(() -> graph.addVertex(id, 1));
The only difference is the lambda/closure syntax. Note that in my case id is the reserved property of Element - it's unique identifier. You might consider a different name for your "identifier" than "id" - perhaps "uniqueId" in which case your getOrCreate will look like this:
private Vertex getOrCreate(TitanGraph graph, String vertexId) { GraphTraversalSource g = graph.traversal(); return g.V().has("uniqueId", vertexId).tryNext().orElseGet(() -> graph.addVertex("uniqueId", vertexId); }
I'd also recommend passing around the GraphTraversalSource if you can - no need to keep creating that over and over with the graph.traversal() method.