Skip to content

Commit d4e66c1

Browse files
authored
miscellaneous fixes (#13)
* use GraphFactory in demo * improved ArangoDBEdge.stringify() * throw if incident vertex is not found * doc review * fixed SimpleElementIdFactory for non-default collection names * fixed ArangoDBEdge.stringify() * java driver v7.22.0
1 parent b59fd1d commit d4e66c1

File tree

9 files changed

+95
-48
lines changed

9 files changed

+95
-48
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ gremlin:
329329

330330
If `edgeDefinitions` are not configured, the default names will be used:
331331

332-
- `<graphName>_vertex` will be used for the vertex collection
333-
- `<graphName>_edge` will be used for the edge collection
332+
- `vertex` will be used for the vertex collection
333+
- `edge` will be used for the edge collection
334334

335335
Using a `SIMPLE` graph configured as in the example above and creating a new element like:
336336

@@ -340,7 +340,8 @@ graph.addVertex("person", T.id, "foo");
340340
```
341341
[//]: <> (@formatter:on)
342342

343-
would result in creating a document in the vertex collection `myGraph_v` with `_id` equals to `myGraph_v/foo`.
343+
would result in creating a document in the vertex collection `myGraph_v` with `_key` equals to `foo` (and `_id` equals
344+
to `myGraph_v/foo`).
344345

345346
### COMPLEX Graph Type
346347

@@ -355,8 +356,7 @@ edge definitions. It has the following advantages:
355356

356357
But on the other side has the following constraints:
357358

358-
- Element IDs must have the format: `<graph>_<label>/<key>`, where:
359-
- `<graph>` is the graph name
359+
- Element IDs must have the format: `<label>/<key>`, where:
360360
- `<label>` is the element label
361361
- `<key>` is the database document key
362362
- Only labels corresponding to graph collections can be used
@@ -385,7 +385,8 @@ graph.addVertex("person", T.id, "foo");
385385
```
386386
[//]: <> (@formatter:on)
387387

388-
would result in creating a document in the vertex collection `myGraph_person` with `_id` equals to `myGraph_person/foo`.
388+
would result in creating a document in the vertex collection `myGraph_person` with `_key` equals to `foo` (and `_id`
389+
equals to `myGraph_person/foo`).
389390

390391
## Naming Constraints
391392

@@ -396,7 +397,6 @@ When using the ArangoDB TinkerPop Provider, be aware of these naming constraints
396397
cannot be used in:
397398
- Graph name (`gremlin.arangodb.conf.graph.name`)
398399
- Labels
399-
- Element IDs
400400

401401
## Persistent Structure
402402

@@ -405,7 +405,7 @@ The ArangoDB TinkerPop Provider maps TinkerPop data structures to ArangoDB data
405405
### Vertices
406406

407407
Vertices are stored as documents in vertex collections. In a `SIMPLE` graph, all vertices are stored in a single
408-
collection named `<graphName>_vertex`. In a `COMPLEX` graph, vertices are stored in collections named
408+
collection, by default named `<graphName>_vertex`. In a `COMPLEX` graph, vertices are stored in collections named
409409
`<graphName>_<label>`.
410410

411411
Each vertex document contains:
@@ -445,8 +445,8 @@ creates a document like this:
445445

446446
### Edges
447447

448-
Edges are stored as documents in edge collections. In a `SIMPLE` graph, all edges are stored in a single collection
449-
named `<graphName>_edge`. In a `COMPLEX` graph, edges are stored in collections named `<graphName>_<label>`.
448+
Edges are stored as documents in edge collections. In a `SIMPLE` graph, all edges are stored in a single collection, by
449+
default named `<graphName>_edge`. In a `COMPLEX` graph, edges are stored in collections named `<graphName>_<label>`.
450450

451451
Each edge document contains:
452452

demo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Configuration conf = new ArangoDBConfigurationBuilder() (1)
5656
.database(DB_NAME)
5757
.enableDataDefinition(true) (3)
5858
.build();
59-
ArangoDBGraph graph = ArangoDBGraph.open(conf); (4)
59+
ArangoDBGraph graph = (ArangoDBGraph) GraphFactory.open(conf); (4)
6060
GraphTraversalSource g = graph.traversal(); (5)
6161
```
6262
[//]: <> (@formatter:on)

demo/src/main/java/org/example/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
2424
import org.apache.tinkerpop.gremlin.structure.Edge;
2525
import org.apache.tinkerpop.gremlin.structure.Vertex;
26+
import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;
2627

2728
import java.util.List;
2829
import java.util.Map;
@@ -52,7 +53,7 @@ public static void main(String[] args) {
5253
.db(DB_NAME)
5354
.enableDataDefinition(true)
5455
.build();
55-
ArangoDBGraph graph = ArangoDBGraph.open(conf);
56+
ArangoDBGraph graph = (ArangoDBGraph) GraphFactory.open(conf);
5657
GraphTraversalSource g = graph.traversal();
5758

5859
// print supported features

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<properties>
3131
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32-
<arangodb-java-driver.version>7.22.0-SNAPSHOT</arangodb-java-driver.version>
32+
<arangodb-java-driver.version>7.22.0</arangodb-java-driver.version>
3333
<test.graph.type>simple</test.graph.type>
3434
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
3535
<sonar.organization>arangodb-1</sonar.organization>

src/main/java/com/arangodb/tinkerpop/gremlin/persistence/ElementIdFactory.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,28 @@
3030

3131

3232
public abstract class ElementIdFactory {
33-
protected final String prefix;
33+
protected final ArangoDBGraphConfig config;
3434

3535
public static ElementIdFactory create(ArangoDBGraphConfig config) {
3636
switch (config.graphType) {
3737
case SIMPLE:
38-
return new SimpleElementIdFactory(config.graphName);
38+
return new SimpleElementIdFactory(config);
3939
case COMPLEX:
40-
return new ComplexElementIdFactory(config.graphName);
40+
return new ComplexElementIdFactory(config);
4141
default:
4242
throw new IllegalArgumentException("Unsupported graph type: " + config.graphType);
4343
}
4444
}
4545

46-
protected ElementIdFactory(String prefix) {
47-
this.prefix = prefix;
46+
protected ElementIdFactory(ArangoDBGraphConfig config) {
47+
this.config = config;
4848
}
4949

50-
protected abstract String inferCollection(final String collection, final String label, final String defaultLabel);
50+
protected abstract String defaultVertexCollection();
51+
52+
protected abstract String defaultEdgeCollection();
53+
54+
protected abstract String inferCollection(final String collection, final String label, final String defaultCollection);
5155

5256
protected abstract void validateId(String id);
5357

@@ -59,24 +63,24 @@ private String extractKey(final String id) {
5963
}
6064

6165
private String extractCollection(final String id) {
62-
String[] parts = id.replaceFirst("^" + prefix + "_", "").split("/");
66+
String[] parts = id.replaceFirst("^" + config.prefix, "").split("/");
6367
if (parts.length > 2) {
6468
throw new IllegalArgumentException(String.format("key (%s) contains invalid character '/'", id));
6569
}
6670
return parts.length == 2 ? parts[0] : null;
6771
}
6872

6973
public ElementId createVertexId(String label, Object id) {
70-
return createId(label, Vertex.DEFAULT_LABEL, id);
74+
return createId(label, id, defaultVertexCollection());
7175
}
7276

7377
public ElementId createEdgeId(String label, Object id) {
74-
return createId(label, Edge.DEFAULT_LABEL, id);
78+
return createId(label, id, defaultEdgeCollection());
7579
}
7680

7781
public ElementId parseVertexId(Object id) {
7882
if (id instanceof String) {
79-
return parseWithDefaultLabel((String) id, Vertex.DEFAULT_LABEL);
83+
return parseWithDefaultCollection((String) id, defaultVertexCollection());
8084
} else if (id instanceof Element) {
8185
return parseVertexId(((Element) id).id());
8286
} else {
@@ -92,7 +96,7 @@ public List<ElementId> parseVertexIds(Object[] ids) {
9296

9397
public ElementId parseEdgeId(Object id) {
9498
if (id instanceof String) {
95-
return parseWithDefaultLabel((String) id, Edge.DEFAULT_LABEL);
99+
return parseWithDefaultCollection((String) id, defaultEdgeCollection());
96100
} else if (id instanceof Element) {
97101
return parseEdgeId(((Element) id).id());
98102
} else {
@@ -109,18 +113,18 @@ public List<ElementId> parseEdgeIds(Object[] ids) {
109113
public ElementId parseId(String id) {
110114
String collection = extractCollection(id);
111115
String key = extractKey(id);
112-
return of(prefix, collection, key);
116+
return of(config.graphName, collection, key);
113117
}
114118

115-
private ElementId parseWithDefaultLabel(String id, String defaultLabel) {
116-
String collection = inferCollection(extractCollection(id), null, defaultLabel);
119+
private ElementId parseWithDefaultCollection(String id, String defaultCollection) {
120+
String collection = inferCollection(extractCollection(id), null, defaultCollection);
117121
String key = extractKey(id);
118-
return of(prefix, collection, key);
122+
return of(config.graphName, collection, key);
119123
}
120124

121-
private ElementId createId(String label, String defaultLabel, Object nullableId) {
125+
private ElementId createId(String label, Object nullableId, String defaultCollection) {
122126
if (nullableId == null) {
123-
return of(prefix, inferCollection(null, label, defaultLabel), null);
127+
return of(config.graphName, inferCollection(null, label, defaultCollection), null);
124128
}
125129

126130
if (!(nullableId instanceof String)) {
@@ -129,13 +133,13 @@ private ElementId createId(String label, String defaultLabel, Object nullableId)
129133

130134
String id = (String) nullableId;
131135
validateId(id);
132-
return of(prefix, inferCollection(extractCollection(id), label, defaultLabel), extractKey(id));
136+
return of(config.graphName, inferCollection(extractCollection(id), label, defaultCollection), extractKey(id));
133137
}
134138

135-
private ElementId of(String prefix, String collection, String key) {
136-
Objects.requireNonNull(prefix);
139+
private ElementId of(String graphName, String collection, String key) {
140+
Objects.requireNonNull(graphName);
137141
Objects.requireNonNull(collection);
138-
ElementId.validateIdParts(prefix, collection, key);
139-
return doCreate(prefix, collection, key);
142+
ElementId.validateIdParts(graphName, collection, key);
143+
return doCreate(graphName, collection, key);
140144
}
141145
}

src/main/java/com/arangodb/tinkerpop/gremlin/persistence/complex/ComplexElementIdFactory.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,29 @@
1818

1919
import com.arangodb.tinkerpop.gremlin.persistence.ElementId;
2020
import com.arangodb.tinkerpop.gremlin.persistence.ElementIdFactory;
21+
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBGraphConfig;
22+
import org.apache.tinkerpop.gremlin.structure.Edge;
23+
import org.apache.tinkerpop.gremlin.structure.Vertex;
2124

2225

2326
public class ComplexElementIdFactory extends ElementIdFactory {
2427

25-
public ComplexElementIdFactory(String prefix) {
26-
super(prefix);
28+
public ComplexElementIdFactory(ArangoDBGraphConfig config) {
29+
super(config);
2730
}
2831

2932
@Override
30-
protected String inferCollection(final String collection, final String label, final String defaultLabel) {
33+
protected String defaultVertexCollection() {
34+
return Vertex.DEFAULT_LABEL;
35+
}
36+
37+
@Override
38+
protected String defaultEdgeCollection() {
39+
return Edge.DEFAULT_LABEL;
40+
}
41+
42+
@Override
43+
protected String inferCollection(final String collection, final String label, final String defaultCollection) {
3144
if (collection != null) {
3245
if (label != null && !label.equals(collection)) {
3346
throw new IllegalArgumentException("Mismatching label: [" + label + "] and collection: [" + collection + "]");
@@ -37,12 +50,12 @@ protected String inferCollection(final String collection, final String label, fi
3750
if (label != null) {
3851
return label;
3952
}
40-
return defaultLabel;
53+
return defaultCollection;
4154
}
4255

4356
@Override
4457
protected void validateId(String id) {
45-
if (id.replaceFirst("^" + prefix + "_", "").contains("_")) {
58+
if (id.replaceFirst("^" + config.prefix, "").contains("_")) {
4659
throw new IllegalArgumentException(String.format("id (%s) contains invalid character '_'", id));
4760
}
4861
}

src/main/java/com/arangodb/tinkerpop/gremlin/persistence/simple/SimpleElementIdFactory.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,35 @@
1818

1919
import com.arangodb.tinkerpop.gremlin.persistence.ElementId;
2020
import com.arangodb.tinkerpop.gremlin.persistence.ElementIdFactory;
21+
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBGraphConfig;
2122

2223

2324
public class SimpleElementIdFactory extends ElementIdFactory {
2425

25-
public SimpleElementIdFactory(String prefix) {
26-
super(prefix);
26+
private final String defaultVertexCollection;
27+
private final String defaultEdgeCollection;
28+
29+
public SimpleElementIdFactory(ArangoDBGraphConfig config) {
30+
super(config);
31+
defaultVertexCollection = config.vertices.iterator().next()
32+
.replaceFirst(config.prefix, "");
33+
defaultEdgeCollection = config.edges.iterator().next()
34+
.replaceFirst(config.prefix, "");
35+
}
36+
37+
@Override
38+
protected String defaultVertexCollection() {
39+
return defaultVertexCollection;
40+
}
41+
42+
@Override
43+
protected String defaultEdgeCollection() {
44+
return defaultEdgeCollection;
2745
}
2846

2947
@Override
30-
protected String inferCollection(final String collection, final String label, final String defaultLabel) {
31-
return defaultLabel;
48+
protected String inferCollection(final String collection, final String label, final String defaultCollection) {
49+
return defaultCollection;
3250
}
3351

3452
@Override

src/main/java/com/arangodb/tinkerpop/gremlin/structure/ArangoDBEdge.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import com.arangodb.tinkerpop.gremlin.persistence.EdgeData;
2020
import com.arangodb.tinkerpop.gremlin.persistence.ElementId;
21+
import com.arangodb.tinkerpop.gremlin.persistence.VertexData;
2122
import org.apache.tinkerpop.gremlin.structure.*;
22-
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
2323
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
2424

2525
import java.util.*;
@@ -53,17 +53,26 @@ public void doInsert() {
5353

5454
@Override
5555
protected String stringify() {
56-
return StringFactory.edgeString(this);
56+
return "e[" + id() + "][" + data.getFrom().getId() + "-" + label() + "->" + data.getTo().getId() + "]";
5757
}
5858

5959
@Override
6060
public Vertex outVertex() {
61-
return new ArangoDBVertex(graph, graph.getClient().readVertex(data.getFrom()));
61+
return vertex(data.getFrom());
6262
}
6363

6464
@Override
6565
public Vertex inVertex() {
66-
return new ArangoDBVertex(graph, graph.getClient().readVertex(data.getTo()));
66+
return vertex(data.getTo());
67+
}
68+
69+
private Vertex vertex(ElementId eId) {
70+
if (removed()) throw ArangoDBElement.Exceptions.elementAlreadyRemoved(id());
71+
VertexData v = graph.getClient().readVertex(eId);
72+
if (v == null) {
73+
throw ArangoDBElement.Exceptions.elementAlreadyRemoved(eId.getId());
74+
}
75+
return new ArangoDBVertex(graph, v);
6776
}
6877

6978
@Override

src/main/java/com/arangodb/tinkerpop/gremlin/structure/ArangoDBElement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public abstract class ArangoDBElement<P, D extends PropertiesContainer<P>> imple
3333
private boolean removed = false;
3434

3535
ArangoDBElement(ArangoDBGraph graph, D data) {
36+
Objects.requireNonNull(graph);
37+
Objects.requireNonNull(data);
3638
this.graph = graph;
3739
this.data = data;
3840
}

0 commit comments

Comments
 (0)