Skip to content

Commit a9e6ca6

Browse files
author
mpv1989
committed
added ArangoDatabaseAsync.cursor()
1 parent 48c4d47 commit a9e6ca6

File tree

3 files changed

+79
-20
lines changed

3 files changed

+79
-20
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v4.1.12 (2017-04-xx)
2+
---------------------------
3+
* added ArangoDatabaseAsync.cursor()
4+
15
v4.1.11 (2017-03-24)
26
---------------------------
37
* fixed exception handling in Connection

src/main/java/com/arangodb/ArangoDatabaseAsync.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ public CompletableFuture<Void> revokeAccess(final String user) {
250250
* @param type
251251
* The type of the result (POJO class, VPackSlice, String for Json, or Collection/List/Map)
252252
* @return cursor of the results
253+
* @throws ArangoDBException
253254
*/
254255
public <T> CompletableFuture<ArangoCursorAsync<T>> query(
255256
final String query,
@@ -259,30 +260,56 @@ public <T> CompletableFuture<ArangoCursorAsync<T>> query(
259260
final Request request = queryRequest(query, bindVars, options);
260261
final CompletableFuture<CursorEntity> execution = executor.execute(request, CursorEntity.class);
261262
return execution.thenApply(result -> {
262-
return new ArangoCursorAsync<>(this, new ArangoCursorExecute() {
263-
@Override
264-
public CursorEntity next(final String id) {
265-
final CompletableFuture<CursorEntity> result = executor.execute(queryNextRequest(id),
266-
CursorEntity.class);
267-
try {
268-
return result.get();
269-
} catch (InterruptedException | ExecutionException e) {
270-
throw new ArangoDBException(e);
271-
}
272-
}
263+
return createCursor(result, type);
264+
});
265+
}
273266

274-
@Override
275-
public void close(final String id) {
276-
try {
277-
executor.execute(queryCloseRequest(id), Void.class).get();
278-
} catch (InterruptedException | ExecutionException e) {
279-
throw new ArangoDBException(e);
280-
}
281-
}
282-
}, type, result);
267+
/**
268+
* Return an cursor from the given cursor-ID if still existing
269+
*
270+
* @see <a href=
271+
* "https://docs.arangodb.com/current/HTTP/AqlQueryCursor/AccessingCursors.html#read-next-batch-from-cursor">API
272+
* Documentation</a>
273+
* @param cursorId
274+
* The ID of the cursor
275+
* @param type
276+
* The type of the result (POJO class, VPackSlice, String for Json, or Collection/List/Map)
277+
* @return cursor of the results
278+
* @throws ArangoDBException
279+
*/
280+
public <T> CompletableFuture<ArangoCursorAsync<T>> cursor(final String cursorId, final Class<T> type)
281+
throws ArangoDBException {
282+
final CompletableFuture<CursorEntity> execution = executor.execute(queryNextRequest(cursorId),
283+
CursorEntity.class);
284+
return execution.thenApply(result -> {
285+
return createCursor(result, type);
283286
});
284287
}
285288

289+
private <T> ArangoCursorAsync<T> createCursor(final CursorEntity result, final Class<T> type) {
290+
return new ArangoCursorAsync<>(this, new ArangoCursorExecute() {
291+
@Override
292+
public CursorEntity next(final String id) {
293+
final CompletableFuture<CursorEntity> result = executor.execute(queryNextRequest(id),
294+
CursorEntity.class);
295+
try {
296+
return result.get();
297+
} catch (InterruptedException | ExecutionException e) {
298+
throw new ArangoDBException(e);
299+
}
300+
}
301+
302+
@Override
303+
public void close(final String id) {
304+
try {
305+
executor.execute(queryCloseRequest(id), Void.class).get();
306+
} catch (InterruptedException | ExecutionException e) {
307+
throw new ArangoDBException(e);
308+
}
309+
}
310+
}, type, result);
311+
}
312+
286313
/**
287314
* Explain an AQL query and return information about it
288315
*

src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,34 @@ public void queryWithCache() throws InterruptedException, ArangoDBException, Exe
522522
}
523523
}
524524

525+
@Test
526+
public void queryCursor() throws ArangoDBException, InterruptedException, ExecutionException {
527+
try {
528+
db.createCollection(COLLECTION_NAME, null).get();
529+
final int numbDocs = 10;
530+
for (int i = 0; i < numbDocs; i++) {
531+
db.collection(COLLECTION_NAME).insertDocument(new BaseDocument(), null).get();
532+
}
533+
534+
final int batchSize = 5;
535+
final ArangoCursorAsync<String> cursor = db.query("for i in db_test return i._id", null,
536+
new AqlQueryOptions().batchSize(batchSize).count(true), String.class).get();
537+
assertThat(cursor, is(notNullValue()));
538+
assertThat(cursor.getCount(), is(numbDocs));
539+
540+
final ArangoCursorAsync<String> cursor2 = db.cursor(cursor.getId(), String.class).get();
541+
assertThat(cursor2, is(notNullValue()));
542+
assertThat(cursor2.getCount(), is(numbDocs));
543+
assertThat(cursor2.hasNext(), is(true));
544+
545+
for (int i = 0; i < batchSize; i++, cursor.next()) {
546+
assertThat(cursor.hasNext(), is(i != batchSize));
547+
}
548+
} finally {
549+
db.collection(COLLECTION_NAME).drop().get();
550+
}
551+
}
552+
525553
@Test
526554
public void changeQueryTrackingProperties() throws InterruptedException, ExecutionException {
527555
try {

0 commit comments

Comments
 (0)