Skip to content

Commit 3ed215f

Browse files
author
mpv1989
committed
added ArangoDatabase.cursor() (issue #116)
1 parent 8acc03e commit 3ed215f

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
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 ArangoDatabase.cursor() (issue #116)
4+
15
v4.1.11 (2017-03-24)
26
---------------------------
37
* fixed exception handling in Connection (issue #110)

src/main/java/com/arangodb/ArangoCursor.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,13 @@ public class ArangoCursor<T> implements Iterator<T>, Closeable {
4444
private final Class<T> type;
4545
protected final ArangoCursorIterator<T> iterator;
4646
private final String id;
47-
private final Integer count;
48-
private final Extras extra;
49-
private final boolean cached;
5047
private final ArangoCursorExecute execute;
5148

5249
protected ArangoCursor(final InternalArangoDatabase<?, ?, ?, ?> db, final ArangoCursorExecute execute,
5350
final Class<T> type, final CursorEntity result) {
5451
super();
5552
this.execute = execute;
5653
this.type = type;
57-
count = result.getCount();
58-
extra = result.getExtra();
59-
cached = result.getCached().booleanValue();
6054
iterator = new ArangoCursorIterator<T>(this, execute, db, result);
6155
id = result.getId();
6256
}
@@ -77,22 +71,25 @@ public Class<T> getType() {
7771
* attribute set)
7872
*/
7973
public Integer getCount() {
80-
return count;
74+
return iterator.getResult().getCount();
8175
}
8276

8377
public Stats getStats() {
78+
final Extras extra = iterator.getResult().getExtra();
8479
return extra != null ? extra.getStats() : null;
8580
}
8681

8782
public Collection<Warning> getWarnings() {
83+
final Extras extra = iterator.getResult().getExtra();
8884
return extra != null ? extra.getWarnings() : null;
8985
}
9086

9187
/**
9288
* @return indicating whether the query result was served from the query cache or not
9389
*/
9490
public boolean isCached() {
95-
return cached;
91+
final Boolean cached = iterator.getResult().getCached();
92+
return cached != null && cached.booleanValue();
9693
}
9794

9895
@Override

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,28 @@ public <T> ArangoCursor<T> query(
262262
final Class<T> type) throws ArangoDBException {
263263
final Request request = queryRequest(query, bindVars, options);
264264
final CursorEntity result = executor.execute(request, CursorEntity.class);
265+
return createCursor(result, type);
266+
}
267+
268+
/**
269+
* Return an cursor from the given cursor-ID if still existing
270+
*
271+
* @see <a href=
272+
* "https://docs.arangodb.com/current/HTTP/AqlQueryCursor/AccessingCursors.html#read-next-batch-from-cursor">API
273+
* Documentation</a>
274+
* @param cursorId
275+
* The ID of the cursor
276+
* @param type
277+
* The type of the result (POJO class, VPackSlice, String for Json, or Collection/List/Map)
278+
* @return cursor of the results
279+
* @throws ArangoDBException
280+
*/
281+
public <T> ArangoCursor<T> cursor(final String cursorId, final Class<T> type) throws ArangoDBException {
282+
final CursorEntity result = executor.execute(queryNextRequest(cursorId), CursorEntity.class);
283+
return createCursor(result, type);
284+
}
285+
286+
private <T> ArangoCursor<T> createCursor(final CursorEntity result, final Class<T> type) {
265287
return new ArangoCursor<T>(this, new ArangoCursorExecute() {
266288
@Override
267289
public CursorEntity next(final String id) {

src/main/java/com/arangodb/internal/ArangoCursorIterator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public ArangoCursorIterator(final ArangoCursor<T> cursor, final ArangoCursorExec
5050
pos = 0;
5151
}
5252

53+
public CursorEntity getResult() {
54+
return result;
55+
}
56+
5357
@Override
5458
public boolean hasNext() {
5559
return pos < result.getResult().size() || result.getHasMore();

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,34 @@ public void queryWithCache() throws InterruptedException {
477477
}
478478
}
479479

480+
@Test
481+
public void queryCursor() {
482+
try {
483+
db.createCollection(COLLECTION_NAME, null);
484+
final int numbDocs = 10;
485+
for (int i = 0; i < numbDocs; i++) {
486+
db.collection(COLLECTION_NAME).insertDocument(new BaseDocument(), null);
487+
}
488+
489+
final int batchSize = 5;
490+
final ArangoCursor<String> cursor = db.query("for i in db_test return i._id", null,
491+
new AqlQueryOptions().batchSize(batchSize).count(true), String.class);
492+
assertThat(cursor, is(notNullValue()));
493+
assertThat(cursor.getCount(), is(numbDocs));
494+
495+
final ArangoCursor<String> cursor2 = db.cursor(cursor.getId(), String.class);
496+
assertThat(cursor2, is(notNullValue()));
497+
assertThat(cursor2.getCount(), is(numbDocs));
498+
assertThat(cursor2.hasNext(), is(true));
499+
500+
for (int i = 0; i < batchSize; i++, cursor.next()) {
501+
assertThat(cursor.hasNext(), is(i != batchSize));
502+
}
503+
} finally {
504+
db.collection(COLLECTION_NAME).drop();
505+
}
506+
}
507+
480508
@Test
481509
public void changeQueryTrackingProperties() {
482510
try {

0 commit comments

Comments
 (0)