Skip to content

Commit 0798510

Browse files
committed
Merge branch 'develop' into TASK-7353
2 parents 7ffbf11 + a1f8a01 commit 0798510

9 files changed

Lines changed: 27 additions & 7 deletions

File tree

commons-datastore/commons-datastore-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.opencb.commons</groupId>
88
<artifactId>commons-datastore</artifactId>
9-
<version>7.0.0-SNAPSHOT</version>
9+
<version>8.0.0-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

commons-datastore/commons-datastore-mongodb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.opencb.commons</groupId>
88
<artifactId>commons-datastore</artifactId>
9-
<version>7.0.0-SNAPSHOT</version>
9+
<version>8.0.0-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

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
@@ -133,6 +133,12 @@ public DataResult<Long> count(ClientSession clientSession, Bson query) {
133133
return endQuery(Collections.emptyList(), l, start);
134134
}
135135

136+
public DataResult<Long> estimatedCount() {
137+
long start = startQuery();
138+
long l = mongoDBNativeQuery.estimatedCount();
139+
return endQuery(Collections.emptyList(), l, start);
140+
}
141+
136142
public DataResult<?> distinct(String key, Bson query) {
137143
long start = startQuery();
138144
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
@@ -333,6 +333,12 @@ public void testCount1() throws Exception {
333333
assertEquals("The number must be equals", N, queryResult.getNumMatches());
334334
}
335335

336+
@Test
337+
public void testEstimatedCount() {
338+
DataResult<Long> queryResult = mongoDBCollection.estimatedCount();
339+
assertEquals("The number of documents must be equals", N, queryResult.getNumMatches());
340+
}
341+
336342
@Test
337343
public void testDistinct1() throws Exception {
338344
DataResult<Integer> queryResult = mongoDBCollection.distinct("age", null, Integer.class);

commons-datastore/commons-datastore-solr/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.opencb.commons</groupId>
2424
<artifactId>commons-datastore</artifactId>
25-
<version>7.0.0-SNAPSHOT</version>
25+
<version>8.0.0-SNAPSHOT</version>
2626
<relativePath>../pom.xml</relativePath>
2727
</parent>
2828

commons-datastore/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.opencb.commons</groupId>
88
<artifactId>commons</artifactId>
9-
<version>7.0.0-SNAPSHOT</version>
9+
<version>8.0.0-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

commons-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.opencb.commons</groupId>
88
<artifactId>commons</artifactId>
9-
<version>7.0.0-SNAPSHOT</version>
9+
<version>8.0.0-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.opencb.commons</groupId>
88
<artifactId>commons</artifactId>
9-
<version>7.0.0-SNAPSHOT</version>
9+
<version>8.0.0-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111

1212
<name>OpenCB commons project</name>

0 commit comments

Comments
 (0)