Skip to content

Commit da99e2c

Browse files
committed
feat: add count method to DefaultCouchbaseDocumentManager
Implemented a count method that executes a N1QL count query on the provided SelectQuery, returning the total number of matching documents in the Couchbase bucket. Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 7224e59 commit da99e2c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.couchbase.client.java.query.QueryOptions;
2626
import com.couchbase.client.java.query.QueryResult;
2727
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
28+
import org.eclipse.jnosql.communication.semistructured.DefaultSelectQuery;
2829
import org.eclipse.jnosql.communication.semistructured.DeleteQuery;
2930
import org.eclipse.jnosql.communication.semistructured.Element;
3031
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
@@ -205,6 +206,26 @@ public long count(String documentCollection) {
205206
});
206207
}
207208

209+
@Override
210+
public long count(SelectQuery query) {
211+
Objects.requireNonNull(query, "query is required");
212+
return waitBucketBeReadyAndGet(() -> {
213+
N1QLQuery n1QLQuery = N1QLBuilder.countOf(query, database, bucket.defaultScope().name()).get();
214+
QueryResult result;
215+
if (n1QLQuery.isParameterEmpty()) {
216+
result = cluster.query(n1QLQuery.query());
217+
} else {
218+
result = cluster.query(n1QLQuery.query(), QueryOptions
219+
.queryOptions().parameters(n1QLQuery.params()));
220+
}
221+
return result.rowsAsObject()
222+
.stream()
223+
.findFirst()
224+
.map(json -> json.getLong("$1"))
225+
.orElse(0L);
226+
});
227+
}
228+
208229
@Override
209230
public Stream<CommunicationEntity> n1qlQuery(final String n1ql, final JsonObject params) throws NullPointerException {
210231
requireNonNull(n1ql, "n1qlQuery is required");

0 commit comments

Comments
 (0)