-
Notifications
You must be signed in to change notification settings - Fork 273
Id Implementation
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>??</version>
</dependency>
IdGraph
is a graph wrapper which affords complete control over element ids. Some Graph
implementations, such as TinkerGraph
, allow you to specify your own ids when you create elements, e.g.
Graph g = new TinkerGraph();
Vertex v = g.addVertex("42");
System.out.println(v.getId()); // prints "42"
Other graph implementations, however, choose element ids for you, e.g.
Graph g = new Neo4jGraph("/tmp/neo");
Vertex v = g.addVertex("42");
System.out.println(v.getId()); // prints "0"
This means that ids are generally not conserved when you move data between graph instances and implementations, to XML and back again, etc. So for applications which need it, there is IdGraph
. Using vertex and edge indices under the hood, IdGraph
enables custom ids regardless of whether the underlying Graph implementation allows them. The only requirement is that the underlying graph is an instance of IndexableGraph
. E.g.
IndexableGraph g0 = new Neo4jGraph("/tmp/neo");
Graph g = new IdGraph(g0);
Vertex v = g.addVertex("42");
System.out.println(v.getId()); // prints "42"
When a non-null id is passed to IdGraph.addVertex
or IdGraph.addEdge
, that id will be used to uniquely identify the element. When null
is passed in, IdGraph
will generate an id for you. By default, the generated id is a pseudo-random UUID string. However, you can supply your own id factory when instantiating IdGraph
:
final Random random = new Random();
IdIndexGraph.IdFactory f = new IdIndexGraph.IdFactory() {
public Object createId() {
return random.nextInt();
}};
IndexableGraph g0 = new Neo4jGraph("/tmp/neo");
Graph g = new IdGraph(g0, f);
Vertex v = g.addVertex(null);
System.out.println(v.getId()); // prints the random integer id of the vertex