Skip to content

Commit 4452938

Browse files
committed
graph: getEdges
1 parent f970391 commit 4452938

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ public CursorEntity<PlainEdgeEntity> graphGetEdges(String graphName) throws Aran
24742474
}
24752475

24762476
/**
2477-
* Returns all Edges of a a given vertex.
2477+
* Returns all Edges of a given vertex.
24782478
*
24792479
* @param graphName
24802480
* @param clazz
@@ -2496,6 +2496,69 @@ public <T> CursorEntity<T> graphGetEdges(String graphName, Class<T> clazz, Strin
24962496

24972497
}
24982498

2499+
/**
2500+
* Returns all Edges of vertices matching the example object (non-primitive
2501+
* set to null will not be used for comparing).
2502+
*
2503+
* @param graphName
2504+
* @param clazzT
2505+
* @param vertexExample
2506+
* @return <T> CursorEntity<T>
2507+
* @throws ArangoException
2508+
*/
2509+
public <T, S> CursorEntity<T> graphGetEdgesByExampleObject(String graphName, Class<T> clazzT, S vertexExample)
2510+
throws ArangoException {
2511+
validateCollectionName(graphName);
2512+
String query = "for i in graph_edges(@graphName, @vertexExample) return i";
2513+
2514+
Map<String, Object> bindVars = new MapBuilder().put("graphName", graphName).put("vertexExample", vertexExample)
2515+
.get();
2516+
2517+
CursorEntity<T> result = this.executeQuery(query, bindVars, clazzT, true, 20);
2518+
2519+
return result;
2520+
}
2521+
2522+
/**
2523+
* Returns all Edges of vertices matching the map.
2524+
*
2525+
* @param graphName
2526+
* @param clazzT
2527+
* @param vertexExample
2528+
* @return <T> CursorEntity<T>
2529+
* @throws ArangoException
2530+
*/
2531+
public <T> CursorEntity<T> graphGetEdgesByExampleMap(
2532+
String graphName,
2533+
Class<T> clazzT,
2534+
Map<String, Object> vertexExample) throws ArangoException {
2535+
validateCollectionName(graphName);
2536+
String query = "for i in graph_edges(@graphName, @vertexExample) return i";
2537+
2538+
Map<String, Object> bindVars = new MapBuilder().put("graphName", graphName).put("vertexExample", vertexExample)
2539+
.get();
2540+
2541+
CursorEntity<T> result = this.executeQuery(query, bindVars, clazzT, true, 20);
2542+
2543+
return result;
2544+
}
2545+
2546+
public <T, S> CursorEntity<EdgeEntity<T>> graphGetEdgesByExampleObject1(
2547+
String graphName,
2548+
Class<T> clazzT,
2549+
S vertexExample) throws ArangoException {
2550+
validateCollectionName(graphName);
2551+
String query = "for i in graph_edges(@graphName, @vertexExample) return i";
2552+
2553+
Map<String, Object> bindVars = new MapBuilder().put("graphName", graphName).put("vertexExample", vertexExample)
2554+
.get();
2555+
2556+
// CursorEntity<EdgeEntity<T>> result = this.executeQuery(query, bindVars,
2557+
// clazzT, true, 20);
2558+
2559+
return null;
2560+
}
2561+
24992562
// public <T> CursorEntity<EdgeEntity<T>> graphGetEdgesWithData(
25002563
// String graphName,
25012564
// Class<T> clazz,

src/test/java/com/arangodb/ArangoDriverGraphEdgesGetTest.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import static org.hamcrest.Matchers.startsWith;
2121
import static org.junit.Assert.assertThat;
2222

23+
import java.util.HashMap;
24+
import java.util.Map;
25+
2326
import org.junit.Test;
2427

2528
import com.arangodb.entity.CursorEntity;
@@ -102,6 +105,135 @@ public void test_getEdges_Vertex() throws ArangoException {
102105

103106
cursor = driver.graphGetEdges(graphName, TestComplexEntity02.class, vertex2.getDocumentHandle());
104107
assertThat(cursor.getCount(), is(1));
108+
}
109+
110+
@Test
111+
public void test_GetEdgesByExampleObject() throws ArangoException {
112+
String graphName = "UnitTestGraph";
113+
114+
GraphEntity graph = driver.createGraph(
115+
graphName,
116+
this.createEdgeDefinitions(1, 0),
117+
this.createOrphanCollections(0),
118+
true);
119+
120+
TestComplexEntity01 v1 = new TestComplexEntity01("Homer", "A Simpson", 38);
121+
TestComplexEntity01 v2 = new TestComplexEntity01("Marge", "A Simpson", 36);
122+
TestComplexEntity01 v3 = new TestComplexEntity01("Bart", "A Simpson", 10);
123+
TestComplexEntity01 v4 = new TestComplexEntity01("Remoh", "Homer's twin", 38);
124+
125+
DocumentEntity<TestComplexEntity01> vertex1 = driver.graphCreateVertex(graphName, "from1-1", v1, true);
126+
127+
DocumentEntity<TestComplexEntity01> vertex2 = driver.graphCreateVertex(graphName, "to1-1", v2, true);
128+
129+
DocumentEntity<TestComplexEntity01> vertex3 = driver.graphCreateVertex(graphName, "to1-1", v3, true);
130+
131+
DocumentEntity<TestComplexEntity01> vertex4 = driver.graphCreateVertex(graphName, "from1-1", v4, true);
132+
133+
EdgeEntity<?> edge1 = driver.graphCreateEdge(
134+
graphName,
135+
"edge-1",
136+
null,
137+
vertex1.getDocumentHandle(),
138+
vertex2.getDocumentHandle(),
139+
new TestComplexEntity02(1, 2, 3),
140+
null);
141+
142+
EdgeEntity<?> edge2 = driver.graphCreateEdge(
143+
graphName,
144+
"edge-1",
145+
null,
146+
vertex1.getDocumentHandle(),
147+
vertex3.getDocumentHandle(),
148+
new TestComplexEntity02(4, 5, 6),
149+
null);
150+
151+
EdgeEntity<?> edge3 = driver.graphCreateEdge(
152+
graphName,
153+
"edge-1",
154+
null,
155+
vertex4.getDocumentHandle(),
156+
vertex2.getDocumentHandle(),
157+
new TestComplexEntity02(7, 8, 9),
158+
null);
159+
160+
CursorEntity<TestComplexEntity02> cursor = driver.graphGetEdgesByExampleObject(
161+
graphName,
162+
TestComplexEntity02.class,
163+
new TestComplexEntity01(null, null, 38));
164+
assertThat(cursor.getCount(), is(3));
165+
166+
cursor = driver.graphGetEdgesByExampleObject(graphName, TestComplexEntity02.class, v3);
167+
assertThat(cursor.getCount(), is(1));
168+
assertThat(cursor.get(0).getClass().getName(), is(TestComplexEntity02.class.getName()));
169+
assertThat(cursor.get(0).getX(), is(4));
170+
171+
}
172+
173+
@Test
174+
public void test_GetEdgesByExampleMap() throws ArangoException {
175+
String graphName = "UnitTestGraph";
176+
177+
GraphEntity graph = driver.createGraph(
178+
graphName,
179+
this.createEdgeDefinitions(1, 0),
180+
this.createOrphanCollections(0),
181+
true);
182+
183+
TestComplexEntity01 v1 = new TestComplexEntity01("Homer", "A Simpson", 38);
184+
TestComplexEntity01 v2 = new TestComplexEntity01("Marge", "A Simpson", 36);
185+
TestComplexEntity01 v3 = new TestComplexEntity01("Bart", "A Simpson", 10);
186+
TestComplexEntity01 v4 = new TestComplexEntity01("Remoh", "Homer's twin", 38);
187+
188+
DocumentEntity<TestComplexEntity01> vertex1 = driver.graphCreateVertex(graphName, "from1-1", v1, true);
189+
190+
DocumentEntity<TestComplexEntity01> vertex2 = driver.graphCreateVertex(graphName, "to1-1", v2, true);
191+
192+
DocumentEntity<TestComplexEntity01> vertex3 = driver.graphCreateVertex(graphName, "to1-1", v3, true);
193+
194+
DocumentEntity<TestComplexEntity01> vertex4 = driver.graphCreateVertex(graphName, "from1-1", v4, true);
195+
196+
EdgeEntity<?> edge1 = driver.graphCreateEdge(
197+
graphName,
198+
"edge-1",
199+
null,
200+
vertex1.getDocumentHandle(),
201+
vertex2.getDocumentHandle(),
202+
new TestComplexEntity02(1, 2, 3),
203+
null);
204+
205+
EdgeEntity<?> edge2 = driver.graphCreateEdge(
206+
graphName,
207+
"edge-1",
208+
null,
209+
vertex1.getDocumentHandle(),
210+
vertex3.getDocumentHandle(),
211+
new TestComplexEntity02(4, 5, 6),
212+
null);
213+
214+
EdgeEntity<?> edge3 = driver.graphCreateEdge(
215+
graphName,
216+
"edge-1",
217+
null,
218+
vertex4.getDocumentHandle(),
219+
vertex2.getDocumentHandle(),
220+
new TestComplexEntity02(7, 8, 9),
221+
null);
222+
223+
Map<String, Object> exampleVertex = new HashMap<String, Object>();
224+
exampleVertex.put("user", "Homer");
225+
226+
CursorEntity<TestComplexEntity02> cursor = driver.graphGetEdgesByExampleMap(
227+
graphName,
228+
TestComplexEntity02.class,
229+
exampleVertex);
230+
assertThat(cursor.getCount(), is(2));
231+
232+
exampleVertex.put("user", "Bart");
233+
cursor = driver.graphGetEdgesByExampleMap(graphName, TestComplexEntity02.class, exampleVertex);
234+
assertThat(cursor.getCount(), is(1));
235+
assertThat(cursor.get(0).getClass().getName(), is(TestComplexEntity02.class.getName()));
236+
assertThat(cursor.get(0).getX(), is(4));
105237

106238
}
107239
//

0 commit comments

Comments
 (0)