Skip to content

Commit 41b62d6

Browse files
committed
resume cursor
1 parent 0629f9a commit 41b62d6

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ public interface ArangoDatabase extends ArangoSerdeAccessor {
310310
*/
311311
<T> ArangoCursor<T> cursor(String cursorId, Class<T> type);
312312

313+
/**
314+
* Return an cursor from the given cursor-ID if still existing
315+
*
316+
* @param cursorId The ID of the cursor
317+
* @param type The type of the result (POJO or {@link com.arangodb.util.RawData})
318+
* @param nextBatchId The ID of the next cursor batch (set only if cursor allows retries, see
319+
* {@link AqlQueryOptions#allowRetry(Boolean)}
320+
* @return cursor of the results
321+
* @see <a href= "https://www.arangodb.com/docs/stable/http/aql-query-cursor-accessing-cursors
322+
* .html#read-next-batch-from-cursor">API Documentation</a>
323+
* @since ArangoDB 3.11
324+
*/
325+
<T> ArangoCursor<T> cursor(String cursorId, Class<T> type, String nextBatchId);
326+
313327
/**
314328
* Explain an AQL query and return information about it
315329
*

core/src/main/java/com/arangodb/internal/ArangoDatabaseImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,20 @@ public <T> ArangoCursor<T> query(final String query, final Class<T> type) {
183183
@Override
184184
public <T> ArangoCursor<T> cursor(final String cursorId, final Class<T> type) {
185185
final HostHandle hostHandle = new HostHandle();
186-
final InternalCursorEntity result = executor
187-
.execute(queryNextRequest(cursorId, null), internalCursorEntityDeserializer(), hostHandle);
186+
final InternalCursorEntity result = executor.execute(
187+
queryNextRequest(cursorId, null),
188+
internalCursorEntityDeserializer(),
189+
hostHandle);
190+
return createCursor(result, type, null, hostHandle);
191+
}
192+
193+
@Override
194+
public <T> ArangoCursor<T> cursor(final String cursorId, final Class<T> type, final String nextBatchId) {
195+
final HostHandle hostHandle = new HostHandle();
196+
final InternalCursorEntity result = executor.execute(
197+
queryNextByBatchIdRequest(cursorId, nextBatchId, null),
198+
internalCursorEntityDeserializer(),
199+
hostHandle);
188200
return createCursor(result, type, null, hostHandle);
189201
}
190202

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -834,25 +834,33 @@ void queryWithMaxWarningCount(ArangoDatabase db) {
834834
@ParameterizedTest(name = "{index}")
835835
@MethodSource("dbs")
836836
void queryCursor(ArangoDatabase db) {
837-
final int numbDocs = 10;
838-
for (int i = 0; i < numbDocs; i++) {
839-
db.collection(CNAME1).insertDocument(new BaseDocument(), null);
840-
}
841-
842-
final int batchSize = 5;
843-
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", String.class,
844-
new AqlQueryOptions().batchSize(batchSize).count(true));
845-
assertThat((Object) cursor).isNotNull();
846-
assertThat(cursor.getCount()).isGreaterThanOrEqualTo(numbDocs);
847-
848-
final ArangoCursor<String> cursor2 = db.cursor(cursor.getId(), String.class);
849-
assertThat((Object) cursor2).isNotNull();
850-
assertThat(cursor2.getCount()).isGreaterThanOrEqualTo(numbDocs);
851-
assertThat((Iterator<?>) cursor2).hasNext();
837+
ArangoCursor<Integer> cursor = db.query("for i in 1..4 return i", Integer.class,
838+
new AqlQueryOptions().batchSize(1));
839+
List<Integer> result = new ArrayList<>();
840+
result.add(cursor.next());
841+
result.add(cursor.next());
842+
ArangoCursor<Integer> cursor2 = db.cursor(cursor.getId(), Integer.class);
843+
result.add(cursor2.next());
844+
result.add(cursor2.next());
845+
assertThat(cursor2.hasNext()).isFalse();
846+
assertThat(result).containsExactly(1, 2, 3, 4);
847+
}
852848

853-
for (int i = 0; i < batchSize; i++, cursor.next()) {
854-
assertThat((Iterator<?>) cursor).hasNext();
855-
}
849+
@ParameterizedTest(name = "{index}")
850+
@MethodSource("dbs")
851+
void queryCursorRetry(ArangoDatabase db) throws IOException {
852+
assumeTrue(isAtLeastVersion(3, 11));
853+
ArangoCursor<Integer> cursor = db.query("for i in 1..4 return i", Integer.class,
854+
new AqlQueryOptions().batchSize(1).allowRetry(true));
855+
List<Integer> result = new ArrayList<>();
856+
result.add(cursor.next());
857+
result.add(cursor.next());
858+
ArangoCursor<Integer> cursor2 = db.cursor(cursor.getId(), Integer.class, cursor.getNextBatchId());
859+
result.add(cursor2.next());
860+
result.add(cursor2.next());
861+
cursor2.close();
862+
assertThat(cursor2.hasNext()).isFalse();
863+
assertThat(result).containsExactly(1, 2, 3, 4);
856864
}
857865

858866
@ParameterizedTest(name = "{index}")

0 commit comments

Comments
 (0)