Skip to content

Commit c64d347

Browse files
author
Mark
committed
replace graph_vertices function with aql
1 parent 55a8e54 commit c64d347

File tree

4 files changed

+80
-68
lines changed

4 files changed

+80
-68
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
import com.arangodb.util.GraphEdgesOptions;
8282
import com.arangodb.util.GraphQueryUtil;
8383
import com.arangodb.util.GraphVerticesOptions;
84-
import com.arangodb.util.JsonUtils;
8584
import com.arangodb.util.MapBuilder;
8685
import com.arangodb.util.ShortestPathOptions;
8786
import com.arangodb.util.TraversalQueryOptions;
@@ -104,8 +103,6 @@ public class ArangoDriver extends BaseArangoDriver {
104103

105104
private static final String DATABASE_SYSTEM = "_system";
106105
private static final String COLLECTION_USERS = "_users";
107-
private static final String GRAPH_NAME = "graphName";
108-
private static final String VERTEX_EXAMPLE = "vertexExample";
109106
private final ArangoConfigure configure;
110107
private final BatchHttpManager httpManager;
111108

@@ -4439,11 +4436,15 @@ public <T> VertexCursor<T> graphGetVertexCursor(
44394436

44404437
validateCollectionName(graphName);
44414438

4442-
final String query = "for i in graph_vertices(@graphName , @vertexExample, @options) return i";
4439+
GraphVerticesOptions tmpGraphVerticesOptions = graphVerticesOptions;
4440+
if (tmpGraphVerticesOptions == null) {
4441+
tmpGraphVerticesOptions = new GraphVerticesOptions();
4442+
}
44434443

4444-
final Map<String, Object> bindVars = new MapBuilder().put(GRAPH_NAME, graphName)
4445-
.put(VERTEX_EXAMPLE, JsonUtils.convertNullToMap(vertexExample))
4446-
.put("options", JsonUtils.convertNullToMap(graphVerticesOptions)).get();
4444+
final MapBuilder mapBuilder = new MapBuilder();
4445+
final String query = GraphQueryUtil.createVerticesQuery(this, graphName, clazz, vertexExample,
4446+
tmpGraphVerticesOptions, mapBuilder);
4447+
final Map<String, Object> bindVars = mapBuilder.get();
44474448

44484449
return executeVertexQuery(query, bindVars, aqlQueryOptions, clazz);
44494450
}

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

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public static String createEdgeQuery(
3535
if (vertexExample != null && String.class.isAssignableFrom(vertexExample.getClass())) {
3636
sb.append("FOR v,e IN ");
3737
appendDepth(graphEdgesOptions, sb);
38-
appendDirection(graphEdgesOptions, sb);
38+
appendDirection(graphEdgesOptions.getDirection(), sb);
3939
sb.append(" @");
4040
sb.append(VERTEX_EXAMPLE);
41-
bindVars.put(VERTEX_EXAMPLE, JsonUtils.convertNullToMap(vertexExample));
41+
bindVars.put(VERTEX_EXAMPLE, vertexExample);
4242
} else {
4343
final List<String> startVertexCollectionRestriction = graphEdgesOptions
4444
.getStartVertexCollectionRestriction();
@@ -65,7 +65,7 @@ public static String createEdgeQuery(
6565
}
6666
sb.append(" FOR v,e IN ");
6767
appendDepth(graphEdgesOptions, sb);
68-
appendDirection(graphEdgesOptions, sb);
68+
appendDirection(graphEdgesOptions.getDirection(), sb);
6969
sb.append(" start");
7070
}
7171
sb.append(" ");
@@ -78,9 +78,7 @@ public static String createEdgeQuery(
7878
// remove last ,
7979
sb.deleteCharAt(sb.length() - 1);
8080
} else {
81-
sb.append("GRAPH @");
82-
sb.append(GRAPH_NAME);
83-
bindVars.put(GRAPH_NAME, graphName);
81+
appendGraphName(graphName, bindVars, sb);
8482
}
8583
appendFilter("e", graphEdgesOptions.getEdgeExamples(), sb);
8684
appendFilter("v", graphEdgesOptions.getNeighborExamples(), sb);
@@ -98,6 +96,17 @@ public static String createEdgeQuery(
9896
return query;
9997
}
10098

99+
/**
100+
* @param graphName
101+
* @param bindVars
102+
* @param sb
103+
*/
104+
private static void appendGraphName(final String graphName, final MapBuilder bindVars, final StringBuilder sb) {
105+
sb.append("GRAPH @");
106+
sb.append(GRAPH_NAME);
107+
bindVars.put(GRAPH_NAME, graphName);
108+
}
109+
101110
private static void appendDepth(final GraphEdgesOptions graphEdgesOptions, final StringBuilder sb) {
102111
final Integer minDepth = graphEdgesOptions.getMinDepth();
103112
final Integer maxDepth = graphEdgesOptions.getMaxDepth();
@@ -109,10 +118,9 @@ private static void appendDepth(final GraphEdgesOptions graphEdgesOptions, final
109118
}
110119
}
111120

112-
private static void appendDirection(final GraphEdgesOptions graphEdgesOptions, final StringBuilder sb) {
113-
final String direction = graphEdgesOptions.getDirection() != null ? graphEdgesOptions.getDirection().name()
114-
: Direction.ANY.name();
115-
sb.append(direction);
121+
private static void appendDirection(final Direction direction, final StringBuilder sb) {
122+
final String directionName = direction != null ? direction.name() : Direction.ANY.name();
123+
sb.append(directionName);
116124
}
117125

118126
private static void appendFilter(final String var, final Object example, final StringBuilder sb)
@@ -167,7 +175,53 @@ public static String createVerticesQuery(
167175
final Object vertexExample,
168176
final GraphVerticesOptions graphVerticesOptions,
169177
final MapBuilder bindVars) throws ArangoException {
170-
return null;
178+
179+
// final String query = "for i in graph_vertices(@graphName ,
180+
// @vertexExample, @options) return i";
181+
// final Map<String, Object> bindVars = new MapBuilder().put(GRAPH_NAME,
182+
// graphName)
183+
// .put(VERTEX_EXAMPLE, JsonUtils.convertNullToMap(vertexExample))
184+
// .put("options",
185+
// JsonUtils.convertNullToMap(graphVerticesOptions)).get();
186+
187+
StringBuilder sb = new StringBuilder();
188+
final boolean stringVertexExample = vertexExample != null
189+
&& String.class.isAssignableFrom(vertexExample.getClass());
190+
if (stringVertexExample) {
191+
sb.append("RETURN ");
192+
sb.append("DOCUMENT(");
193+
sb.append("@");
194+
sb.append(VERTEX_EXAMPLE);
195+
bindVars.put(VERTEX_EXAMPLE, vertexExample);
196+
sb.append(")");
197+
} else {
198+
final List<String> startVertexCollectionRestriction = graphVerticesOptions.getVertexCollectionRestriction();
199+
final List<String> vertexCollections = startVertexCollectionRestriction != null
200+
&& startVertexCollectionRestriction.size() > 0 ? startVertexCollectionRestriction
201+
: driver.graphGetVertexCollections(graphName, true);
202+
if (vertexCollections.size() == 1) {
203+
sb.append("FOR start IN `");
204+
sb.append(vertexCollections.get(0));
205+
sb.append("`");
206+
appendFilter("start", vertexExample, sb);
207+
} else {
208+
sb.append("FOR start IN UNION (");
209+
for (String vertexCollection : vertexCollections) {
210+
sb.append("(FOR start IN `");
211+
sb.append(vertexCollection);
212+
sb.append("`");
213+
appendFilter("start", vertexExample, sb);
214+
sb.append(" RETURN start),");
215+
}
216+
// remove last ,
217+
sb.deleteCharAt(sb.length() - 1);
218+
sb.append(")");
219+
}
220+
sb.append(" RETURN start");
221+
}
222+
223+
String query = sb.toString();
224+
return query;
171225
}
172226

173227
public static String createShortestPathQuery(
Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
package com.arangodb.util;
22

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

6-
import com.arangodb.Direction;
5+
public class GraphVerticesOptions {
76

8-
public class GraphVerticesOptions implements OptionsInterface {
9-
10-
private Direction direction;
117
private List<String> vertexCollectionRestriction;
128

13-
/**
14-
* The direction of the edges as a string. Possible values are outbound,
15-
* inbound and any (default).
16-
*
17-
* @return the direction
18-
*/
19-
public Direction getDirection() {
20-
return direction;
21-
}
22-
23-
/**
24-
* The direction of the edges as a string. Possible values are outbound,
25-
* inbound and any (default).
26-
*
27-
* @param direction
28-
*/
29-
public void setDirection(Direction direction) {
30-
this.direction = direction;
31-
}
32-
339
/**
3410
* One or multiple vertex collections that should be considered.
3511
*
@@ -48,21 +24,4 @@ public void setVertexCollectionRestriction(List<String> vertexCollectionRestrict
4824
this.vertexCollectionRestriction = vertexCollectionRestriction;
4925
}
5026

51-
/**
52-
* Returns a map of the options
53-
*
54-
* @return a map
55-
*/
56-
@Override
57-
public Map<String, Object> toMap() {
58-
MapBuilder mp = new MapBuilder();
59-
if (direction != null) {
60-
mp.put("direction", direction.toString().toLowerCase());
61-
}
62-
if (CollectionUtils.isNotEmpty(vertexCollectionRestriction)) {
63-
mp.put("vertexCollectionRestriction", vertexCollectionRestriction);
64-
}
65-
return mp.get();
66-
}
67-
6827
}

src/test/java/com/arangodb/ArangoDriverGraphVertexTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,22 +303,20 @@ public void graphGetVertexCursorTest() throws ArangoException {
303303
assertEquals(201, vertexCursor.getCode());
304304

305305
final GraphVerticesOptions graphVerticesOptions = new GraphVerticesOptions();
306-
graphVerticesOptions.setDirection(Direction.INBOUND);
307-
308-
vertexCursor = driver.graphGetVertexCursor(GRAPH_NAME, TestComplexEntity01.class, null, graphVerticesOptions,
309-
aqlQueryOptions);
310-
assertEquals(2, vertexCursor.getCount());
311-
assertEquals(201, vertexCursor.getCode());
312-
313306
final List<String> vertexCollectionRestriction = new ArrayList<String>();
314307
vertexCollectionRestriction.add("from1-1");
315308
graphVerticesOptions.setVertexCollectionRestriction(vertexCollectionRestriction);
316309

317310
vertexCursor = driver.graphGetVertexCursor(GRAPH_NAME, TestComplexEntity01.class, null, graphVerticesOptions,
318311
aqlQueryOptions);
319-
assertEquals(0, vertexCursor.getCount());
312+
assertEquals(2, vertexCursor.getCount());
320313
assertEquals(201, vertexCursor.getCode());
321314

315+
vertexCursor = driver.graphGetVertexCursor(GRAPH_NAME, TestComplexEntity01.class, vertex1.getDocumentHandle(),
316+
null, aqlQueryOptions);
317+
assertEquals(201, vertexCursor.getCode());
318+
assertEquals(1, vertexCursor.getCount());
319+
assertEquals(vertex1.getDocumentHandle(), vertexCursor.getUniqueResult().getDocumentHandle());
322320
}
323321

324322
@Test

0 commit comments

Comments
 (0)