Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support count with collation in MongoDB with Panache #27062

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Support count with collation in MongoDB with Panache
  • Loading branch information
diogocarleto authored and gsmet committed Aug 1, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d0bb07268a20cc09a68a6005ac63cc3dc3b0876d
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@

import com.mongodb.ReadPreference;
import com.mongodb.client.model.Collation;
import com.mongodb.client.model.CountOptions;

import io.quarkus.mongodb.FindOptions;
import io.quarkus.mongodb.panache.common.runtime.MongoPropertyUtil;
@@ -154,7 +155,14 @@ public <T extends Entity> CommonReactivePanacheQueryImpl<T> withReadPreference(R
@SuppressWarnings("unchecked")
public Uni<Long> count() {
if (count == null) {
count = mongoQuery == null ? collection.countDocuments() : collection.countDocuments(mongoQuery);
CountOptions countOptions = new CountOptions();
if (collation != null) {
countOptions.collation(collation);
}

count = mongoQuery == null
? collection.countDocuments()
: collection.countDocuments(mongoQuery, countOptions);
}
return count;
}
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Collation;
import com.mongodb.client.model.CountOptions;

import io.quarkus.panache.common.Page;
import io.quarkus.panache.common.Range;
@@ -157,7 +158,14 @@ public <T extends Entity> CommonPanacheQueryImpl<T> withReadPreference(ReadPrefe
public long count() {
if (count == null) {
Bson query = getQuery();
count = clientSession == null ? collection.countDocuments(query) : collection.countDocuments(clientSession, query);
CountOptions countOptions = new CountOptions();
if (collation != null) {
countOptions.collation(collation);
}

count = clientSession == null
? collection.countDocuments(query, countOptions)
: collection.countDocuments(clientSession, query, countOptions);
}
return count;
}
Original file line number Diff line number Diff line change
@@ -113,6 +113,16 @@ public Response testImperativeEntity() {
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);

//count with collation
collation = Collation.builder()
.locale("en")
.collationStrength(CollationStrength.SECONDARY)
.build();
Assertions.assertEquals(2, TestImperativeEntity.find("{'title' : ?1}", "aaa").withCollation(collation).count());
Assertions.assertEquals(2, TestImperativeEntity.find("{'title' : ?1}", "AAA").withCollation(collation).count());
Assertions.assertEquals(1, TestImperativeEntity.find("{'title' : ?1}", "bbb").withCollation(collation).count());
Assertions.assertEquals(1, TestImperativeEntity.find("{'title' : ?1}", "BBB").withCollation(collation).count());
entityAUpper.delete();
entityALower.delete();
entityB.delete();
@@ -264,6 +274,17 @@ public Response testImperativeRepository() {
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);

//count with collation
collation = Collation.builder()
.locale("en")
.collationStrength(CollationStrength.SECONDARY)
.build();

Assertions.assertEquals(2, testImperativeRepository.find("{'title' : ?1}", "aaa").withCollation(collation).count());
Assertions.assertEquals(2, testImperativeRepository.find("{'title' : ?1}", "AAA").withCollation(collation).count());
Assertions.assertEquals(1, testImperativeRepository.find("{'title' : ?1}", "bbb").withCollation(collation).count());
Assertions.assertEquals(1, testImperativeRepository.find("{'title' : ?1}", "BBB").withCollation(collation).count());
testImperativeRepository.delete(entityALower);
testImperativeRepository.delete(entityAUpper);
testImperativeRepository.delete(entityB);
@@ -494,6 +515,20 @@ public Response testReactiveEntity() {
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);

//count with collation
collation = Collation.builder()
.locale("en")
.collationStrength(CollationStrength.SECONDARY)
.build();
Assertions.assertEquals(2, TestReactiveEntity.find("{'title' : ?1}", "aaa").withCollation(collation).count()
.await().indefinitely());
Assertions.assertEquals(2, TestReactiveEntity.find("{'title' : ?1}", "AAA").withCollation(collation).count()
.await().indefinitely());
Assertions.assertEquals(1, TestReactiveEntity.find("{'title' : ?1}", "bbb").withCollation(collation).count()
.await().indefinitely());
Assertions.assertEquals(1, TestReactiveEntity.find("{'title' : ?1}", "BBB").withCollation(collation).count()
.await().indefinitely());
entityAUpper.delete().await().indefinitely();
entityALower.delete().await().indefinitely();
entityB.delete().await().indefinitely();
@@ -654,6 +689,20 @@ public Response testReactiveRepository() {
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);

//count with collation
collation = Collation.builder()
.locale("en")
.collationStrength(CollationStrength.SECONDARY)
.build();
Assertions.assertEquals(2, testReactiveRepository.find("{'title' : ?1}", "aaa").withCollation(collation).count()
.await().indefinitely());
Assertions.assertEquals(2, testReactiveRepository.find("{'title' : ?1}", "AAA").withCollation(collation).count()
.await().indefinitely());
Assertions.assertEquals(1, testReactiveRepository.find("{'title' : ?1}", "bbb").withCollation(collation).count()
.await().indefinitely());
Assertions.assertEquals(1, testReactiveRepository.find("{'title' : ?1}", "BBB").withCollation(collation).count()
.await().indefinitely());
testReactiveRepository.delete(entityALower).await().indefinitely();
testReactiveRepository.delete(entityAUpper).await().indefinitely();
testReactiveRepository.delete(entityB).await().indefinitely();