Skip to content

Commit 72a4f15

Browse files
committed
chore: make the Couchbase DocumentManager implementation to supports count method
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 3ad7194 commit 72a4f15

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

jnosql-couchbase/src/main/java/org/eclipse/jnosql/databases/couchbase/communication/DefaultCouchbaseDocumentManager.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,21 @@ public Stream<DocumentEntity> select(final DocumentQuery query) throws NullPoint
188188

189189
@Override
190190
public long count(String documentCollection) {
191-
throw new UnsupportedOperationException("Couchbase does not support count method by document collection");
191+
Objects.requireNonNull(documentCollection, "documentCollection is required");
192+
return waitBucketBeReadyAndGet(() -> {
193+
DocumentQuery countQuery = DocumentQuery
194+
.select("COUNT(*)").from(documentCollection).build();
195+
N1QLQuery n1QLQuery = N1QLBuilder
196+
.of(countQuery, database, bucket.defaultScope().name()).get();
197+
QueryResult query = cluster.query(n1QLQuery.getQuery());
198+
List<JsonObject> result = query.rowsAsObject();
199+
var count = result.stream().findFirst()
200+
.map(data -> data.getNumber("$1"))
201+
.orElse(0L);
202+
return count.longValue();
203+
});
192204
}
193205

194-
195206
@Override
196207
public Stream<DocumentEntity> n1qlQuery(final String n1ql, final JsonObject params) throws NullPointerException {
197208
requireNonNull(n1ql, "n1qlQuery is required");

0 commit comments

Comments
 (0)