Skip to content

Commit e4b09ae

Browse files
authored
Add #available method to legacy cursor API (#838)
The new methods are * com.mongodb.DBCursor#available * com.mongodb.Cursor#available with the same semantics as the recently added com.mongodb.client.MongoCursor#available. JAVA-4422
1 parent 741914b commit e4b09ae

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed

driver-legacy/src/main/com/mongodb/Cursor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
*/
2828
public interface Cursor extends Iterator<DBObject>, Closeable {
2929

30+
/**
31+
* Gets the number of results available locally without blocking, which may be 0.
32+
*
33+
* <p>
34+
* If the cursor is known to be exhausted, returns 0. If the cursor is closed before it's been exhausted, it may return a non-zero
35+
* value.
36+
* </p>
37+
*
38+
* @return the number of results available locally without blocking
39+
* @since 4.5
40+
*/
41+
int available();
42+
3043
/**
3144
* Gets the server's identifier for this Cursor.
3245
*

driver-legacy/src/main/com/mongodb/DBCursor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ public DBObject next() {
188188
return nextInternal();
189189
}
190190

191+
/**
192+
* Gets the number of results available locally without blocking, which may be 0.
193+
*
194+
* <p>
195+
* If the cursor is known to be exhausted, returns 0. If the cursor is closed before it's been exhausted, it may return a non-zero
196+
* value.
197+
* </p>
198+
*
199+
* @return the number of results available locally without blocking
200+
* @since 4.5
201+
*/
202+
public int available() {
203+
return cursor != null ? cursor.available() : 0;
204+
}
205+
191206
/**
192207
* Non blocking check for tailable cursors to see if another object is available.
193208
*

driver-legacy/src/main/com/mongodb/MongoCursorAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class MongoCursorAdapter implements Cursor {
2626
this.cursor = cursor;
2727
}
2828

29+
@Override
30+
public int available() {
31+
return cursor.available();
32+
}
33+
2934
@Override
3035
public long getCursorId() {
3136
ServerCursor serverCursor = cursor.getServerCursor();

driver-legacy/src/test/functional/com/mongodb/DBCollectionAggregationTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ public void testWriteConcern() {
146146
}
147147
}
148148

149+
@Test
150+
public void testAvailable() {
151+
prepareData();
152+
Cursor cursor = collection.aggregate(asList(new BasicDBObject("$match", new BasicDBObject())),
153+
AggregationOptions.builder().build());
154+
155+
assertEquals(3, cursor.available());
156+
157+
cursor.next();
158+
assertEquals(2, cursor.available());
159+
160+
cursor.next();
161+
assertEquals(1, cursor.available());
162+
}
163+
164+
149165
private void verify(final List<DBObject> pipeline, final AggregationOptions options) {
150166
verify(pipeline, options, ReadPreference.primary());
151167
}

driver-legacy/src/test/functional/com/mongodb/DBCursorTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ public void after() {
6161
}
6262
}
6363

64+
@Test
65+
public void testAvailable() {
66+
cursor.batchSize(3);
67+
assertEquals(0, cursor.available());
68+
69+
cursor.next();
70+
assertEquals(2, cursor.available());
71+
72+
cursor.next();
73+
assertEquals(1, cursor.available());
74+
75+
cursor.next();
76+
assertEquals(0, cursor.available());
77+
78+
cursor.next();
79+
assertEquals(2, cursor.available());
80+
81+
cursor.close();
82+
assertEquals(0, cursor.available());
83+
}
84+
6485
@Test
6586
public void testNextHasNext() {
6687
cursor.sort(new BasicDBObject("_id", 1));

driver-sync/src/main/com/mongodb/client/MongoCursor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ public interface MongoCursor<TResult> extends Iterator<TResult>, Closeable {
5858
TResult next();
5959

6060
/**
61-
* Gets the number of results available locally without blocking,which may be 0, or 0 when the cursor is exhausted or closed.
61+
* Gets the number of results available locally without blocking, which may be 0.
62+
*
63+
* <p>
64+
* If the cursor is known to be exhausted, returns 0. If the cursor is closed before it's been exhausted, it may return a non-zero
65+
* value.
66+
* </p>
6267
*
6368
* @return the number of results available locally without blocking
6469
* @since 4.4

0 commit comments

Comments
 (0)