Skip to content

Commit 55a8e54

Browse files
author
Mark
committed
Replace graph_edge function with aql
1 parent 2633a00 commit 55a8e54

File tree

3 files changed

+172
-40
lines changed

3 files changed

+172
-40
lines changed

src/main/java/com/arangodb/util/GraphEdgesOptions.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.arangodb.util;
22

33
import java.util.List;
4-
import java.util.Map;
54

65
import com.arangodb.Direction;
76

8-
public class GraphEdgesOptions extends AbstractOptions implements OptionsInterface {
7+
public class GraphEdgesOptions {
98

109
private Direction direction;
1110
private List<String> edgeCollectionRestriction;
@@ -16,7 +15,7 @@ public class GraphEdgesOptions extends AbstractOptions implements OptionsInterfa
1615
private Integer minDepth;
1716
private Integer maxDepth;
1817
private Integer limit;
19-
private Boolean includeData = Boolean.TRUE;// false = i._id, true = i
18+
private Boolean includeData = Boolean.TRUE;
2019

2120
/**
2221
* The direction of the edges as a string. Possible values are outbound,
@@ -223,21 +222,4 @@ public void setIncludeData(Boolean includeData) {
223222
this.includeData = includeData;
224223
}
225224

226-
@Override
227-
public Map<String, Object> toMap() {
228-
MapBuilder mp = new MapBuilder();
229-
230-
putAttributeToLower(mp, "direction", direction);
231-
putAttributeCollection(mp, "edgeCollectionRestriction", edgeCollectionRestriction);
232-
putAttributeCollection(mp, "startVertexCollectionRestriction", startVertexCollectionRestriction);
233-
putAttributeCollection(mp, "endVertexCollectionRestriction", endVertexCollectionRestriction);
234-
putAttribute(mp, "edgeExamples", edgeExamples);
235-
putAttribute(mp, "neighborExamples", neighborExamples);
236-
putAttribute(mp, "minDepth", minDepth);
237-
putAttribute(mp, "maxDepth", maxDepth);
238-
putAttribute(mp, "includeData", includeData);
239-
240-
return mp.get();
241-
}
242-
243225
}

src/main/java/com/arangodb/util/GraphQueryUtil.java

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.arangodb.ArangoException;
99
import com.arangodb.Direction;
1010
import com.google.gson.Gson;
11+
import com.google.gson.JsonArray;
1112
import com.google.gson.JsonElement;
1213
import com.google.gson.JsonObject;
1314

@@ -18,6 +19,7 @@
1819
public class GraphQueryUtil {
1920

2021
private static final String AND = " && ";
22+
private static final String OR = " || ";
2123
private static final String GRAPH_NAME = "graphName";
2224
private static final String VERTEX_EXAMPLE = "vertexExample";
2325

@@ -31,33 +33,37 @@ public static String createEdgeQuery(
3133

3234
final StringBuilder sb = new StringBuilder();
3335
if (vertexExample != null && String.class.isAssignableFrom(vertexExample.getClass())) {
34-
sb.append("FOR v,i IN ");
36+
sb.append("FOR v,e IN ");
3537
appendDepth(graphEdgesOptions, sb);
3638
appendDirection(graphEdgesOptions, sb);
3739
sb.append(" @");
3840
sb.append(VERTEX_EXAMPLE);
3941
bindVars.put(VERTEX_EXAMPLE, JsonUtils.convertNullToMap(vertexExample));
4042
} else {
41-
final List<String> vertexCollections = driver.graphGetVertexCollections(graphName, true);
43+
final List<String> startVertexCollectionRestriction = graphEdgesOptions
44+
.getStartVertexCollectionRestriction();
45+
final List<String> vertexCollections = startVertexCollectionRestriction != null
46+
&& startVertexCollectionRestriction.size() > 0 ? startVertexCollectionRestriction
47+
: driver.graphGetVertexCollections(graphName, true);
4248
if (vertexCollections.size() == 1) {
4349
sb.append("FOR start IN `");
4450
sb.append(vertexCollections.get(0));
4551
sb.append("`");
46-
appendFilter(vertexExample, sb);
52+
appendFilter("start", vertexExample, sb);
4753
} else {
4854
sb.append("FOR start IN UNION (");
4955
for (String vertexCollection : vertexCollections) {
5056
sb.append("(FOR start IN `");
5157
sb.append(vertexCollection);
5258
sb.append("`");
53-
appendFilter(vertexExample, sb);
59+
appendFilter("start", vertexExample, sb);
5460
sb.append(" RETURN start),");
5561
}
5662
// remove last ,
5763
sb.deleteCharAt(sb.length() - 1);
5864
sb.append(")");
5965
}
60-
sb.append(" FOR v,i IN ");
66+
sb.append(" FOR v,e IN ");
6167
appendDepth(graphEdgesOptions, sb);
6268
appendDirection(graphEdgesOptions, sb);
6369
sb.append(" start");
@@ -76,12 +82,14 @@ public static String createEdgeQuery(
7682
sb.append(GRAPH_NAME);
7783
bindVars.put(GRAPH_NAME, graphName);
7884
}
85+
appendFilter("e", graphEdgesOptions.getEdgeExamples(), sb);
86+
appendFilter("v", graphEdgesOptions.getNeighborExamples(), sb);
7987
final Integer limit = graphEdgesOptions.getLimit();
8088
if (limit != null) {
8189
sb.append(" LIMIT ");
8290
sb.append(limit.intValue());
8391
}
84-
sb.append(" RETURN distinct i");
92+
sb.append(" RETURN distinct e");
8593
if (graphEdgesOptions.getIncludeData() != null && !graphEdgesOptions.getIncludeData().booleanValue()) {
8694
sb.append(".id");
8795
}
@@ -107,24 +115,51 @@ private static void appendDirection(final GraphEdgesOptions graphEdgesOptions, f
107115
sb.append(direction);
108116
}
109117

110-
private static void appendFilter(final Object vertexExample, final StringBuilder sb) {
111-
Gson gson = new Gson();
112-
final JsonElement json = gson.toJsonTree(vertexExample);
113-
if (json.isJsonObject()) {
114-
sb.append(" FILTER ");
115-
final JsonObject jsonObject = json.getAsJsonObject();
116-
final Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
117-
for (Entry<String, JsonElement> entry : entrySet) {
118-
sb.append("start.`");
119-
sb.append(entry.getKey());
120-
sb.append("` == ");
121-
sb.append(entry.getValue().toString());
122-
sb.append(AND);
118+
private static void appendFilter(final String var, final Object example, final StringBuilder sb)
119+
throws ArangoException {
120+
if (example != null) {
121+
final Gson gson = new Gson();
122+
final JsonElement json = gson.toJsonTree(example);
123+
if (json.isJsonObject()) {
124+
sb.append(" FILTER ");
125+
appendObjectinFilter(var, json.getAsJsonObject(), sb);
126+
} else if (json.isJsonArray()) {
127+
sb.append(" FILTER ");
128+
final JsonArray jsonArray = json.getAsJsonArray();
129+
if (jsonArray.size() > 0) {
130+
for (JsonElement jsonElement : jsonArray) {
131+
if (jsonElement.isJsonObject()) {
132+
sb.append("(");
133+
appendObjectinFilter(var, jsonElement.getAsJsonObject(), sb);
134+
sb.append(")");
135+
sb.append(OR);
136+
} else if (!jsonElement.isJsonNull()) {
137+
throw new ArangoException("invalide format of entry in array example: "
138+
+ example.getClass().getSimpleName() + ". only objects in array allowed.");
139+
}
140+
}
141+
sb.delete(sb.length() - OR.length(), sb.length() - 1);
142+
}
143+
} else {
144+
throw new ArangoException("invalide format of example: " + example.getClass().getSimpleName()
145+
+ ". only object or array allowed.");
123146
}
124-
sb.delete(sb.length() - AND.length(), sb.length() - 1);
125147
}
126148
}
127149

150+
private static void appendObjectinFilter(final String var, final JsonObject jsonObject, final StringBuilder sb) {
151+
final Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
152+
for (Entry<String, JsonElement> entry : entrySet) {
153+
sb.append(var);
154+
sb.append(".`");
155+
sb.append(entry.getKey());
156+
sb.append("` == ");
157+
sb.append(entry.getValue().toString());
158+
sb.append(AND);
159+
}
160+
sb.delete(sb.length() - AND.length(), sb.length() - 1);
161+
}
162+
128163
public static String createVerticesQuery(
129164
final ArangoDriver driver,
130165
final String graphName,

src/test/java/com/arangodb/ArangoDriverGraphEdgesGetCursorTest.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.assertThat;
2424
import static org.junit.Assert.assertTrue;
2525

26+
import java.util.ArrayList;
2627
import java.util.HashMap;
2728
import java.util.Iterator;
2829
import java.util.List;
@@ -295,6 +296,120 @@ public void edgesAqlTest() throws ArangoException {
295296

296297
}
297298

299+
@Test
300+
public void graphGetEdgeCursorWithEdgeExample() throws ArangoException {
301+
302+
final TestComplexEntity01 v1 = new TestComplexEntity01("Homer", "A Simpson", 38);
303+
final TestComplexEntity01 v2 = new TestComplexEntity01("Marge", "A Simpson", 36);
304+
final TestComplexEntity01 v3 = new TestComplexEntity01("Bart", "A Simpson", 10);
305+
final TestComplexEntity01 v4 = new TestComplexEntity01("Remoh", "Homer's twin", 38);
306+
307+
final VertexEntity<TestComplexEntity01> vertex1 = driver.graphCreateVertex(GRAPH_NAME, "from1-1", v1, true);
308+
final VertexEntity<TestComplexEntity01> vertex2 = driver.graphCreateVertex(GRAPH_NAME, "to1-1", v2, true);
309+
final VertexEntity<TestComplexEntity01> vertex3 = driver.graphCreateVertex(GRAPH_NAME, "to1-1", v3, true);
310+
final VertexEntity<TestComplexEntity01> vertex4 = driver.graphCreateVertex(GRAPH_NAME, "from1-1", v4, true);
311+
312+
final TestComplexEntity02 e1 = new TestComplexEntity02(1, 2, 3);
313+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex1.getDocumentHandle(), vertex2.getDocumentHandle(), e1,
314+
null);
315+
316+
final TestComplexEntity02 e2 = new TestComplexEntity02(4, 5, 6);
317+
EdgeEntity<TestComplexEntity02> edge2 = driver.graphCreateEdge(GRAPH_NAME, "edge-1", null,
318+
vertex1.getDocumentHandle(), vertex3.getDocumentHandle(), e2, null);
319+
320+
final TestComplexEntity02 e3 = new TestComplexEntity02(7, 8, 9);
321+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex4.getDocumentHandle(), vertex2.getDocumentHandle(), e3,
322+
null);
323+
324+
final TestComplexEntity02 e4 = new TestComplexEntity02(10, 11, 12);
325+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex4.getDocumentHandle(), vertex3.getDocumentHandle(), e4,
326+
null);
327+
328+
GraphEdgesOptions graphEdgesOptions = new GraphEdgesOptions();
329+
graphEdgesOptions.setEdgeExamples(e2);
330+
331+
EdgeCursor<TestComplexEntity02> cursor = driver.graphGetEdgeCursor(GRAPH_NAME, TestComplexEntity02.class,
332+
vertex1.getDocumentHandle(), graphEdgesOptions, getAqlQueryOptions(true, 10, true));
333+
334+
assertEquals(201, cursor.getCode());
335+
assertEquals(1, cursor.getCount());
336+
assertEquals(edge2.getDocumentHandle(), cursor.getUniqueResult().getDocumentHandle());
337+
}
338+
339+
@Test
340+
public void graphGetEdgeCursorWithNeighborExample() throws ArangoException {
341+
342+
final TestComplexEntity01 v1 = new TestComplexEntity01("Homer", "A Simpson", 38);
343+
final TestComplexEntity01 v2 = new TestComplexEntity01("Marge", "A Simpson", 36);
344+
final TestComplexEntity01 v3 = new TestComplexEntity01("Bart", "A Simpson", 10);
345+
final TestComplexEntity01 v4 = new TestComplexEntity01("Remoh", "Homer's twin", 38);
346+
347+
final VertexEntity<TestComplexEntity01> vertex1 = driver.graphCreateVertex(GRAPH_NAME, "from1-1", v1, true);
348+
final VertexEntity<TestComplexEntity01> vertex2 = driver.graphCreateVertex(GRAPH_NAME, "to1-1", v2, true);
349+
final VertexEntity<TestComplexEntity01> vertex3 = driver.graphCreateVertex(GRAPH_NAME, "to1-1", v3, true);
350+
final VertexEntity<TestComplexEntity01> vertex4 = driver.graphCreateVertex(GRAPH_NAME, "from1-1", v4, true);
351+
352+
final TestComplexEntity02 e1 = new TestComplexEntity02(1, 2, 3);
353+
EdgeEntity<TestComplexEntity02> edge1 = driver.graphCreateEdge(GRAPH_NAME, "edge-1", null,
354+
vertex1.getDocumentHandle(), vertex2.getDocumentHandle(), e1, null);
355+
356+
final TestComplexEntity02 e2 = new TestComplexEntity02(4, 5, 6);
357+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex1.getDocumentHandle(), vertex3.getDocumentHandle(), e2,
358+
null);
359+
360+
final TestComplexEntity02 e3 = new TestComplexEntity02(7, 8, 9);
361+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex4.getDocumentHandle(), vertex2.getDocumentHandle(), e3,
362+
null);
363+
364+
final TestComplexEntity02 e4 = new TestComplexEntity02(10, 11, 12);
365+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex4.getDocumentHandle(), vertex3.getDocumentHandle(), e4,
366+
null);
367+
368+
GraphEdgesOptions graphEdgesOptions = new GraphEdgesOptions();
369+
graphEdgesOptions.setNeighborExamples(new TestComplexEntity01(null, null, 36));
370+
371+
EdgeCursor<TestComplexEntity02> cursor = driver.graphGetEdgeCursor(GRAPH_NAME, TestComplexEntity02.class,
372+
vertex1.getDocumentHandle(), graphEdgesOptions, getAqlQueryOptions(true, 10, true));
373+
374+
assertEquals(201, cursor.getCode());
375+
assertEquals(1, cursor.getCount());
376+
assertEquals(edge1.getDocumentHandle(), cursor.getUniqueResult().getDocumentHandle());
377+
}
378+
379+
@Test
380+
public void graphGetEdgeCursorByExampleStartVertexRestriction() throws ArangoException {
381+
final TestComplexEntity01 v1 = new TestComplexEntity01("Homer", "A Simpson", 38);
382+
final TestComplexEntity01 v2 = new TestComplexEntity01("Marge", "A Simpson", 36);
383+
final TestComplexEntity01 v3 = new TestComplexEntity01("Bart", "A Simpson", 10);
384+
final TestComplexEntity01 v4 = new TestComplexEntity01("Remoh", "Homer's twin", 38);
385+
386+
final VertexEntity<TestComplexEntity01> vertex1 = driver.graphCreateVertex(GRAPH_NAME, "from1-1", v1, true);
387+
388+
final VertexEntity<TestComplexEntity01> vertex2 = driver.graphCreateVertex(GRAPH_NAME, "to1-1", v2, true);
389+
390+
final VertexEntity<TestComplexEntity01> vertex3 = driver.graphCreateVertex(GRAPH_NAME, "to1-1", v3, true);
391+
392+
final VertexEntity<TestComplexEntity01> vertex4 = driver.graphCreateVertex(GRAPH_NAME, "from1-1", v4, true);
393+
394+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex1.getDocumentHandle(), vertex2.getDocumentHandle(),
395+
new TestComplexEntity02(1, 2, 3), null);
396+
397+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex1.getDocumentHandle(), vertex3.getDocumentHandle(),
398+
new TestComplexEntity02(4, 5, 6), null);
399+
400+
driver.graphCreateEdge(GRAPH_NAME, "edge-1", null, vertex4.getDocumentHandle(), vertex2.getDocumentHandle(),
401+
new TestComplexEntity02(7, 8, 9), null);
402+
403+
GraphEdgesOptions graphEdgesOptions = new GraphEdgesOptions();
404+
List<String> startVertexCollectionRestriction = new ArrayList<String>();
405+
startVertexCollectionRestriction.add("from1-1");
406+
graphEdgesOptions.setStartVertexCollectionRestriction(startVertexCollectionRestriction);
407+
408+
EdgeCursor<TestComplexEntity02> cursor = driver.graphGetEdgeCursor(GRAPH_NAME, TestComplexEntity02.class,
409+
new TestComplexEntity01(null, "A Simpson", null), graphEdgesOptions, getAqlQueryOptions(true, 10, true));
410+
assertThat(cursor.getCount(), is(2));
411+
}
412+
298413
@Test
299414
public void shortestPathTest() throws ArangoException {
300415

0 commit comments

Comments
 (0)