Skip to content

Commit 89eb8f4

Browse files
authored
Merge pull request #243 from dearrudam/issue-439
Making the Couchbase DocumentManager implementation to supports the count method
2 parents 8c81607 + cb17b96 commit 89eb8f4

File tree

5 files changed

+75
-32
lines changed

5 files changed

+75
-32
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ test-output/
1414
.project
1515
/.idea
1616
.settings/
17-
**/.DS_Store
17+
**/.DS_Store
18+
19+
# ignoring Quarkus plugin config files
20+
.quarkus
21+
.quarkus/**

CHANGELOG.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
1111
=== Fixed
1212

1313
- Fixes Repositories specializations when use methods from CrudRepository
14+
- Fixes in the Couchbase DocumentManager implementation to supports the count method
1415

1516
=== Added
1617

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

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Contributors:
1212
*
1313
* Otavio Santana
14+
* Maximillian Arruda
1415
*/
1516
package org.eclipse.jnosql.databases.couchbase.communication;
1617

@@ -22,15 +23,15 @@
2223
import com.couchbase.client.java.manager.collection.CollectionManager;
2324
import com.couchbase.client.java.manager.collection.CollectionSpec;
2425
import com.couchbase.client.java.manager.collection.ScopeSpec;
25-
import com.couchbase.client.java.manager.query.QueryIndex;
2626
import com.couchbase.client.java.manager.query.QueryIndexManager;
2727

28-
import java.util.Collections;
29-
import java.util.List;
30-
import java.util.Objects;
31-
import java.util.Optional;
28+
import java.time.Duration;
29+
import java.time.temporal.ChronoUnit;
30+
import java.util.*;
3231
import java.util.logging.Level;
3332
import java.util.logging.Logger;
33+
import java.util.stream.Collectors;
34+
import java.util.stream.Stream;
3435

3536
import static com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions;
3637
import static com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions;
@@ -154,8 +155,19 @@ public Cluster getCluster() {
154155
* @throws NullPointerException when parameter is null
155156
*/
156157
public void setUp(String database) {
158+
157159
Objects.requireNonNull(database, "database is required");
158160

161+
var collections = getCollections().stream().map(String::trim)
162+
.filter(index -> !index.isBlank()).toList();
163+
164+
var collectionsToIndex = Arrays
165+
.stream(Optional.ofNullable(getIndex()).orElse("").split(","))
166+
.map(String::trim)
167+
.filter(index -> !index.isBlank()).collect(Collectors.toSet());
168+
169+
var scope = getScope();
170+
159171
long start = System.currentTimeMillis();
160172
LOGGER.log(Level.FINEST, "starting the setup with database: " + database);
161173

@@ -168,47 +180,54 @@ public void setUp(String database) {
168180
LOGGER.log(Level.FINEST, "The database/bucket does not exist, creating it: " + database);
169181
buckets.createBucket(BucketSettings.create(database));
170182
}
183+
171184
Bucket bucket = cluster.bucket(database);
172185

186+
waitUntilReady(bucket);
187+
173188
CollectionManager manager = bucket.collections();
189+
174190
List<ScopeSpec> scopes = manager.getAllScopes();
175-
String finalScope = getScope().orElseGet(() -> bucket.defaultScope().name());
191+
String finalScope = scope.orElseGet(() -> bucket.defaultScope().name());
176192
ScopeSpec spec = scopes.stream().filter(s -> finalScope.equals(s.name()))
177193
.findFirst().get();
178-
for (String collection : collections) {
179-
if (spec.collections().stream().noneMatch(c -> collection.equals(c.name()))) {
194+
195+
collections.forEach(collection -> {
196+
if (spec.collections().stream().noneMatch(c -> collectionsToIndex.contains(c.name()))) {
180197
manager.createCollection(CollectionSpec.create(collection, finalScope));
181198
}
182-
}
183-
if (index != null) {
199+
});
200+
201+
waitUntilReady(bucket);
202+
203+
if (!collectionsToIndex.isEmpty()) {
204+
184205
QueryIndexManager queryIndexManager = cluster.queryIndexes();
185-
List<QueryIndex> indexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions()
186-
.scopeName(finalScope).collectionName(index));
187-
if (indexes.isEmpty()) {
188-
LOGGER.log(Level.FINEST, "Index does not exist, creating primary key with scope "
189-
+ scope + " collection " + index + " at database " + database);
190-
queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions()
191-
.scopeName(finalScope).collectionName(index));
192-
}
193206

194-
for (String collection : collections) {
195-
queryIndexManager = cluster.queryIndexes();
196-
indexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions()
197-
.scopeName(finalScope).collectionName(collection));
198-
if (indexes.isEmpty()) {
199-
LOGGER.log(Level.FINEST, "Index for " + collection + " collection does not exist, creating primary key with scope "
200-
+ scope + " collection " + collection + " at database " + database);
201-
queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions()
202-
.scopeName(finalScope).collectionName(collection));
203-
}
204-
}
207+
Stream.concat(collectionsToIndex.stream(), collectionsToIndex.stream())
208+
.distinct()
209+
.forEach(collection -> {
210+
var allIndexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions()
211+
.scopeName(finalScope).collectionName(collection));
212+
if (allIndexes.isEmpty()) {
213+
LOGGER.log(Level.FINEST, "Index for " + collection + " collection does not exist, creating primary key with scope "
214+
+ finalScope + " collection " + collection + " at database " + database);
215+
queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions()
216+
.scopeName(finalScope).collectionName(collection));
217+
}
218+
});
205219

206220
}
207221

208222
long end = System.currentTimeMillis() - start;
209223
LOGGER.log(Level.FINEST, "Finished the setup with database: " + database + " end with millis "
210224
+ end);
211225
}
226+
227+
}
228+
229+
private void waitUntilReady(Bucket bucket) {
230+
bucket.waitUntilReady(Duration.of(4, ChronoUnit.SECONDS));
212231
}
213232

214233
@Override

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");

jnosql-couchbase/src/test/java/org/eclipse/jnosql/databases/couchbase/communication/CouchbaseDocumentManagerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ public void shouldRetrieveListDocumentList() {
201201
assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
202202
}
203203

204+
@Test
205+
public void shouldCount() {
206+
DocumentEntity entity = getEntity();
207+
entityManager.insert(entity);
208+
long counted = entityManager.count(COLLECTION_PERSON_NAME);
209+
assertTrue(counted > 0);
210+
}
211+
204212
private DocumentEntity createSubdocumentList() {
205213
DocumentEntity entity = DocumentEntity.of(COLLECTION_APP_NAME);
206214
entity.add(Document.of("_id", "ids"));

0 commit comments

Comments
 (0)