Skip to content

Commit 0066252

Browse files
committed
datastore: add the function estimatedCount() to call the MongoDB function estimatedDocumentCount(), #TASK-5564
1 parent 506086f commit 0066252

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBCollection.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public DataResult<Long> count(ClientSession clientSession, Bson query) {
131131
return endQuery(Collections.emptyList(), l, start);
132132
}
133133

134+
public DataResult<Long> estimatedCount() {
135+
long start = startQuery();
136+
long l = mongoDBNativeQuery.estimatedCount();
137+
return endQuery(Collections.emptyList(), l, start);
138+
}
139+
134140
public DataResult<?> distinct(String key, Bson query) {
135141
long start = startQuery();
136142
List l = new ArrayList<>();

commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBNativeQuery.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public long count(ClientSession clientSession, Bson query) {
6464
}
6565
}
6666

67+
public long estimatedCount() {
68+
return dbCollection.estimatedDocumentCount();
69+
}
70+
6771
public DistinctIterable<Document> distinct(String key) {
6872
return distinct(key, null, Document.class);
6973
}
@@ -235,7 +239,11 @@ public <T> MongoDBIterator<T> find(ClientSession clientSession, Bson query, Bson
235239
Future<Long> countFuture = null;
236240
if (options != null && options.getBoolean(QueryOptions.COUNT)) {
237241
ExecutorService executor = Executors.newSingleThreadExecutor();
238-
countFuture = executor.submit(() -> count(clientSession, query));
242+
if (clientSession == null && (query == null || query.equals(Filters.empty()) || query.toBsonDocument().isEmpty())) {
243+
countFuture = executor.submit(this::estimatedCount);
244+
} else {
245+
countFuture = executor.submit(() -> count(clientSession, query));
246+
}
239247
}
240248

241249
FindIterable<Document> findIterable = null;

commons-datastore/commons-datastore-mongodb/src/test/java/org/opencb/commons/datastore/mongodb/MongoDBCollectionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ public void testCount1() throws Exception {
296296
assertEquals("The number must be equals", N, queryResult.getNumMatches());
297297
}
298298

299+
@Test
300+
public void testEstimatedCount() {
301+
DataResult<Long> queryResult = mongoDBCollection.estimatedCount();
302+
assertEquals("The number of documents must be equals", N, queryResult.getNumMatches());
303+
}
304+
299305
@Test
300306
public void testDistinct1() throws Exception {
301307
DataResult<Integer> queryResult = mongoDBCollection.distinct("age", null, Integer.class);

0 commit comments

Comments
 (0)