From d0bb07268a20cc09a68a6005ac63cc3dc3b0876d Mon Sep 17 00:00:00 2001 From: DiogoCarleto Date: Sun, 31 Jul 2022 09:20:48 -0300 Subject: [PATCH] Support count with collation in MongoDB with Panache --- .../CommonReactivePanacheQueryImpl.java | 10 +++- .../runtime/CommonPanacheQueryImpl.java | 10 +++- .../it/mongodb/panache/test/TestResource.java | 49 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/CommonReactivePanacheQueryImpl.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/CommonReactivePanacheQueryImpl.java index 11e71dea32603..3e80f4f63f3e7 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/CommonReactivePanacheQueryImpl.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/CommonReactivePanacheQueryImpl.java @@ -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 CommonReactivePanacheQueryImpl withReadPreference(R @SuppressWarnings("unchecked") public Uni 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; } diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java index 73b112ddc07ee..8ff48b6abd526 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java @@ -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 CommonPanacheQueryImpl 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; } diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java index 9d8d4cea8071f..7af7c72c5108e 100644 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/test/TestResource.java @@ -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();