Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 29818fb

Browse files
committed
collection prefix refactoring
1 parent 721cfaf commit 29818fb

File tree

16 files changed

+340
-401
lines changed

16 files changed

+340
-401
lines changed

src/main/java/com/arangodb/tinkerpop/gremlin/client/ArangoDBBaseDocument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public ArangoDBBaseDocument(String key, String label, ArangoDBGraph graph) {
7979
this._key = key;
8080
this.label = label;
8181
this.graph = graph;
82-
this.collection = graph.getPrefixedCollectioName(label);
82+
this.collection = graph.getPrefixedCollectionName(label);
8383
}
8484

8585
/**

src/main/java/com/arangodb/tinkerpop/gremlin/client/ArangoDBGraphClient.java

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717
import com.arangodb.config.ArangoConfigProperties;
1818
import com.arangodb.entity.*;
1919
import com.arangodb.model.*;
20+
import com.arangodb.tinkerpop.gremlin.persistence.*;
2021
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBEdge;
2122
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBVertex;
22-
import com.arangodb.tinkerpop.gremlin.persistence.EdgeData;
23-
import com.arangodb.tinkerpop.gremlin.persistence.VertexData;
2423
import com.arangodb.tinkerpop.gremlin.structure.*;
2524
import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalInterruptedException;
2625
import org.apache.tinkerpop.gremlin.structure.Direction;
2726
import org.apache.tinkerpop.gremlin.structure.Graph;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029

31-
import com.arangodb.tinkerpop.gremlin.utils.ArangoDBUtil;
32-
3330
import static com.arangodb.tinkerpop.gremlin.utils.ArangoDBUtil.getArangoDirectionFromGremlinDirection;
3431

3532
/**
@@ -105,25 +102,6 @@ public static ArangoDBGraphException getArangoDBException(ArangoDBException ex)
105102
return new ArangoDBGraphException("General ArangoDB error (unkown error code)", ex);
106103
}
107104

108-
/**
109-
* "name to long" Message.
110-
*/
111-
112-
public static String NAME_TO_LONG = "Name is too long: {} bytes (max 64 bytes for labels, 256 for keys)";
113-
114-
/**
115-
* Gets the naming convention error.
116-
*
117-
* @param cause the cause
118-
* @param details the details
119-
* @return the naming convention error
120-
*/
121-
122-
public static ArangoDBGraphException getNamingConventionError(String cause, String details) {
123-
return new ArangoDBGraphException("The provided label or name name does not satisfy the naming conventions." +
124-
String.format(cause, details));
125-
}
126-
127105
}
128106

129107
private static final Logger logger = LoggerFactory.getLogger(ArangoDBGraphClient.class);
@@ -162,21 +140,6 @@ public void shutdown() {
162140
db.arango().shutdown();
163141
}
164142

165-
/**
166-
* Request the version of ArangoDB.
167-
*
168-
* @return the Version number
169-
* @throws ArangoDBGraphException if user has no access to the db
170-
*/
171-
172-
public String getVersion() throws ArangoDBGraphException {
173-
try {
174-
return db.getVersion().getVersion();
175-
} catch (ArangoDBException ex) {
176-
throw ArangoDBExceptions.getArangoDBException(ex);
177-
}
178-
}
179-
180143
public ArangoDBGraphVariables getGraphVariables() {
181144
logger.debug("Get graph variables");
182145
ArangoDBGraphVariables result;
@@ -205,9 +168,9 @@ public void insertGraphVariables(ArangoDBGraphVariables document) {
205168
if (document.isPaired()) {
206169
throw new ArangoDBGraphException("Paired docuements can not be inserted, only updated");
207170
}
208-
ArangoCollection gVars = db.collection(document.collection());
171+
ArangoCollection gVars = db.collection(ArangoDBGraph.GRAPH_VARIABLES_COLLECTION);
209172
if (!gVars.exists()) {
210-
CollectionEntity ce = db.createCollection(document.collection());
173+
CollectionEntity ce = gVars.create();
211174
System.out.println(ce.getStatus());
212175
}
213176
DocumentCreateEntity<?> vertexEntity;
@@ -277,9 +240,9 @@ public void updateGraphVariables(ArangoDBGraphVariables document) {
277240
*/
278241

279242
// FIXME: use multi-docs API
280-
public ArangoIterable<VertexData> getGraphVertices(final List<String> ids) {
243+
public ArangoIterable<VertexData> getGraphVertices(final List<ArangoDBId> ids) {
281244
logger.debug("Get all {} graph vertices, filtered by ids: {}", graph.name(), ids);
282-
List<String> prefixedColNames = graph.vertexCollections().stream().map(graph::getPrefixedCollectioName).collect(Collectors.toList());
245+
List<String> prefixedColNames = graph.vertexCollections().stream().map(graph::getPrefixedCollectionName).collect(Collectors.toList());
283246
return getGraphDocuments(ids, prefixedColNames, VertexData.class);
284247
}
285248

@@ -290,13 +253,13 @@ public ArangoIterable<VertexData> getGraphVertices(final List<String> ids) {
290253
* @return ArangoDBBaseQuery the query object
291254
*/
292255
// FIXME: use multi-docs API
293-
public ArangoIterable<EdgeData> getGraphEdges(List<String> ids) {
256+
public ArangoIterable<EdgeData> getGraphEdges(List<ArangoDBId> ids) {
294257
logger.debug("Get all {} graph edges, filtered by ids: {}", graph.name(), ids);
295-
List<String> prefixedColNames = graph.edgeCollections().stream().map(graph::getPrefixedCollectioName).collect(Collectors.toList());
258+
List<String> prefixedColNames = graph.edgeCollections().stream().map(graph::getPrefixedCollectionName).collect(Collectors.toList());
296259
return getGraphDocuments(ids, prefixedColNames, EdgeData.class);
297260
}
298261

299-
private <V> ArangoIterable<V> getGraphDocuments(List<String> ids, List<String> prefixedColNames, Class<V> clazz) {
262+
private <V> ArangoIterable<V> getGraphDocuments(List<ArangoDBId> ids, List<String> prefixedColNames, Class<V> clazz) {
300263
Map<String, Object> bindVars = new HashMap<>();
301264
ArangoDBQueryBuilder queryBuilder = new ArangoDBQueryBuilder();
302265
if (ids.isEmpty()) {
@@ -306,9 +269,9 @@ private <V> ArangoIterable<V> getGraphDocuments(List<String> ids, List<String> p
306269
queryBuilder.iterateCollection("d", prefixedColNames.get(0), bindVars);
307270
}
308271
} else {
309-
List<String> prunedIds = ids.stream()
310-
.map(graph::getPrefixedCollectioName)
311-
.filter(it -> prefixedColNames.contains(ArangoDBUtil.extractCollection(it)))
272+
List<ArangoDBId> prunedIds = ids.stream()
273+
.map(graph::getArangoDBId)
274+
.filter(it -> prefixedColNames.contains(it.getCollection()))
312275
.collect(Collectors.toList());
313276
queryBuilder.with(prefixedColNames, bindVars).documentsById(prunedIds, "d", bindVars);
314277
}
@@ -396,7 +359,7 @@ public void insertEdge(ArangoDBEdge edge) {
396359
}
397360
throw arangoDBException;
398361
}
399-
edge.key(insertEntity.getKey());
362+
edge.update(insertEntity);
400363
}
401364

402365
public void deleteEdge(ArangoDBEdge edge) {
@@ -426,17 +389,15 @@ public void updateEdge(ArangoDBEdge edge) {
426389
throw ArangoDBExceptions.getArangoDBException(e);
427390
}
428391
logger.debug("Edge updated, new rev {}", updateEntity.getRev());
429-
edge.key(updateEntity.getKey());
392+
edge.update(updateEntity);
430393
}
431394

432-
public VertexData readVertex(String id) {
395+
public VertexData readVertex(ArangoDBId id) {
433396
logger.debug("Read vertex {} in {}", id, graph.name());
434-
String col = ArangoDBUtil.extractCollection(id);
435-
String key = ArangoDBUtil.extractKey(id);
436397
try {
437398
return db.graph(graph.name())
438-
.vertexCollection(col)
439-
.getVertex(key, VertexData.class);
399+
.vertexCollection(id.getCollection())
400+
.getVertex(id.getKey(), VertexData.class);
440401
} catch (ArangoDBException e) {
441402
logger.error("Failed to read vertex: {}", e.getErrorMessage());
442403
throw ArangoDBExceptions.getArangoDBException(e);
@@ -458,7 +419,7 @@ public void insertVertex(ArangoDBVertex vertex) {
458419
}
459420
throw arangoDBException;
460421
}
461-
vertex.key(vertexEntity.getKey());
422+
vertex.update(vertexEntity);
462423
}
463424

464425
public void deleteVertex(ArangoDBVertex vertex) {
@@ -490,7 +451,7 @@ public void updateVertex(ArangoDBVertex vertex) {
490451
logger.debug("Document updated, new rev {}", vertexEntity.getRev());
491452
}
492453

493-
public Iterator<VertexData> getVertexNeighbors(String vertexId, List<String> edgeCollections, Direction direction) {
454+
public Iterator<VertexData> getVertexNeighbors(ArangoDBId vertexId, List<String> edgeCollections, Direction direction) {
494455
logger.debug("Get vertex {}:{} Neighbors, in {}, from collections {}", vertexId, direction, graph.name(), edgeCollections);
495456
Map<String, Object> bindVars = new HashMap<>();
496457
bindVars.put("start", vertexId);
@@ -501,7 +462,7 @@ public Iterator<VertexData> getVertexNeighbors(String vertexId, List<String> edg
501462
return executeAqlQuery(query, bindVars, null, VertexData.class);
502463
}
503464

504-
public Iterator<EdgeData> getVertexEdges(String vertexId, List<String> edgeCollections, Direction direction) {
465+
public Iterator<EdgeData> getVertexEdges(ArangoDBId vertexId, List<String> edgeCollections, Direction direction) {
505466
logger.debug("Get vertex {}:{} Edges, in {}, from collections {}", vertexId, direction, graph.name(), edgeCollections);
506467
Map<String, Object> bindVars = new HashMap<>();
507468
bindVars.put("start", vertexId);

src/main/java/com/arangodb/tinkerpop/gremlin/client/ArangoDBQueryBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Map;
1313

14+
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBId;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617

@@ -114,14 +115,14 @@ public ArangoDBQueryBuilder with(List<String> collections, Map<String, Object> b
114115
*/
115116

116117
public ArangoDBQueryBuilder documentsById(
117-
List<String> ids,
118+
List<ArangoDBId> ids,
118119
String loopVariable,
119120
Map<String, Object> bindVars) {
120121
queryBuilder.append("LET docs = FLATTEN(RETURN Document(@ids))\n");
121122
queryBuilder.append(String.format("FOR %s IN docs\n", loopVariable));
122123
queryBuilder.append(String.format(" FILTER NOT IS_NULL(%s)\n", loopVariable)); // Not needed?
123124
bindVars.put("ids", ids);
124-
logger.debug("documentsById", queryBuilder.toString());
125+
logger.debug("documentsById: {}", queryBuilder.toString());
125126
return this;
126127
}
127128

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,36 @@
2121

2222
import com.arangodb.serde.*;
2323
import com.arangodb.shaded.fasterxml.jackson.annotation.JsonProperty;
24-
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
24+
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBId;
2525

2626
import java.util.*;
2727

2828
public class EdgeData extends SimplePropertyData implements PersistentData {
2929

30+
@InternalId
31+
private ArangoDBId id;
32+
3033
@JsonProperty
3134
private String label;
3235

3336
@InternalKey
3437
private String key;
3538

3639
@InternalFrom
37-
private String from;
40+
private ArangoDBId from;
3841

3942
@InternalTo
40-
private String to;
43+
private ArangoDBId to;
4144

4245
public static EdgeData of(
43-
String label,
44-
String key,
45-
String from,
46-
String to
46+
ArangoDBId id,
47+
ArangoDBId from,
48+
ArangoDBId to
4749
) {
48-
ElementHelper.validateLabel(label);
49-
if (key != null && key.isEmpty()) throw new IllegalArgumentException("empty key");
50-
Objects.requireNonNull(from, "from");
51-
Objects.requireNonNull(to, "to");
52-
5350
EdgeData data = new EdgeData();
54-
data.label = label;
55-
data.key = key;
51+
data.id = id;
52+
data.label = id.getLabel();
53+
data.key = id.getKey();
5654
data.from = from;
5755
data.to = to;
5856
return data;
@@ -62,56 +60,57 @@ public EdgeData() {
6260
}
6361

6462
@Override
65-
public String getLabel() {
66-
return label;
63+
public ArangoDBId getId() {
64+
return id;
6765
}
6866

6967
@Override
70-
public String getKey() {
71-
return key;
68+
public void setId(ArangoDBId id) {
69+
this.id = id;
7270
}
7371

7472
@Override
7573
public void setKey(String key) {
7674
this.key = key;
7775
}
7876

79-
public String getFrom() {
77+
public ArangoDBId getFrom() {
8078
return from;
8179
}
8280

83-
public void setFrom(String from) {
81+
public void setFrom(ArangoDBId from) {
8482
this.from = from;
8583
}
8684

87-
public String getTo() {
85+
public ArangoDBId getTo() {
8886
return to;
8987
}
9088

91-
public void setTo(String to) {
89+
public void setTo(ArangoDBId to) {
9290
this.to = to;
9391
}
9492

9593
@Override
9694
public String toString() {
9795
return "EdgeData{" +
98-
"from='" + from + '\'' +
96+
"from=" + from +
97+
", id=" + id +
9998
", label='" + label + '\'' +
10099
", key='" + key + '\'' +
101-
", to='" + to + '\'' +
102-
", super=" + super.toString() +
100+
", to=" + to +
103101
'}';
104102
}
105103

106104
@Override
107105
public boolean equals(Object o) {
108106
if (!(o instanceof EdgeData)) return false;
107+
if (!super.equals(o)) return false;
109108
EdgeData edgeData = (EdgeData) o;
110-
return Objects.equals(label, edgeData.label) && Objects.equals(key, edgeData.key) && Objects.equals(from, edgeData.from) && Objects.equals(to, edgeData.to);
109+
return Objects.equals(id, edgeData.id) && Objects.equals(label, edgeData.label) && Objects.equals(key, edgeData.key) && Objects.equals(from, edgeData.from) && Objects.equals(to, edgeData.to);
111110
}
112111

113112
@Override
114113
public int hashCode() {
115-
return Objects.hash(label, key, from, to);
114+
return Objects.hash(super.hashCode(), id, label, key, from, to);
116115
}
117116
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,33 @@
1919

2020
package com.arangodb.tinkerpop.gremlin.persistence;
2121

22+
import com.arangodb.entity.DocumentEntity;
23+
import com.arangodb.tinkerpop.gremlin.structure.ArangoDBId;
24+
2225
public interface PersistentData {
23-
String getLabel();
2426

25-
String getKey();
27+
ArangoDBId getId();
28+
29+
void setId(ArangoDBId id);
2630

2731
void setKey(String key);
32+
33+
default String getLabel() {
34+
return getId().getLabel();
35+
}
36+
37+
default String getKey() {
38+
return getId().getKey();
39+
}
40+
41+
default String getCollection() {
42+
return getId().getCollection();
43+
}
44+
45+
default void update(DocumentEntity entity) {
46+
String k = entity.getKey();
47+
setKey(k);
48+
setId(getId().withKey(k));
49+
}
50+
2851
}

0 commit comments

Comments
 (0)