diff --git a/mars-core/src/main/java/com/whaleal/mars/core/messaging/TailableCursorTask.java b/mars-core/src/main/java/com/whaleal/mars/core/messaging/TailableCursorTask.java index d23c50be..645be0b6 100644 --- a/mars-core/src/main/java/com/whaleal/mars/core/messaging/TailableCursorTask.java +++ b/mars-core/src/main/java/com/whaleal/mars/core/messaging/TailableCursorTask.java @@ -32,8 +32,9 @@ import com.mongodb.CursorType; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCursor; -import com.mongodb.client.model.Collation; +//import com.mongodb.client.model.Collation; import com.whaleal.mars.core.Mars; +import com.whaleal.mars.core.query.Collation; import com.whaleal.mars.core.query.Query; import com.whaleal.mars.core.internal.ErrorHandler; import org.bson.Document; @@ -54,7 +55,7 @@ public TailableCursorTask(Mars mars, TailableCursorRequest request, Class protected MongoCursor initCursor(Mars mars, SubscriptionRequest.RequestOptions options, Class targetType) { Document filter = new Document(); - Collation collation = null; + com.mongodb.client.model.Collation collation = null; if (options instanceof TailableCursorRequest.TailableCursorRequestOptions) { diff --git a/mars-core/src/main/java/com/whaleal/mars/core/query/Collation.java b/mars-core/src/main/java/com/whaleal/mars/core/query/Collation.java index 500e38c0..68d35cab 100644 --- a/mars-core/src/main/java/com/whaleal/mars/core/query/Collation.java +++ b/mars-core/src/main/java/com/whaleal/mars/core/query/Collation.java @@ -50,7 +50,6 @@ * NOTE: Please keep in mind that queries will only make use of an index with collation settings if the * query itself specifies the same collation. */ - //@Deprecated public class Collation { diff --git a/mars-core/src/main/java/com/whaleal/mars/core/query/Query.java b/mars-core/src/main/java/com/whaleal/mars/core/query/Query.java index 38c40d5b..e1633e38 100644 --- a/mars-core/src/main/java/com/whaleal/mars/core/query/Query.java +++ b/mars-core/src/main/java/com/whaleal/mars/core/query/Query.java @@ -56,7 +56,7 @@ public class Query { // projection private Projection projectionSpec = null; // sorting - private List sorts; + private List sorts = new ArrayList<>(); private long skip; private int limit; @@ -443,7 +443,7 @@ public void setMeta(Meta meta) { * @param collation can be {@literal null}. * @return this. */ - public Query collation( Collation collation ) { + public Query collation(Collation collation ) { this.collation = Optional.ofNullable(collation); return this; diff --git a/mars-core/src/main/java/com/whaleal/mars/session/DatastoreImpl.java b/mars-core/src/main/java/com/whaleal/mars/session/DatastoreImpl.java index 8530ee9a..a73e0aa3 100644 --- a/mars-core/src/main/java/com/whaleal/mars/session/DatastoreImpl.java +++ b/mars-core/src/main/java/com/whaleal/mars/session/DatastoreImpl.java @@ -1200,6 +1200,10 @@ private T findAllExecute( ClientSession session, MongoCollection collection, findIterable = findIterable.sort(query.getSortObject()); } + if (query.getCollation() != null){ + findIterable = findIterable.collation(query.getCollation().get().toMongoCollation()); + } + if (query.getSkip() > 0) { findIterable = findIterable.skip((int) query.getSkip()); } diff --git a/mars-test/src/test/java/com/whaleal/mars/core/query/QueryCollationTest.java b/mars-test/src/test/java/com/whaleal/mars/core/query/QueryCollationTest.java new file mode 100644 index 00000000..042c7b90 --- /dev/null +++ b/mars-test/src/test/java/com/whaleal/mars/core/query/QueryCollationTest.java @@ -0,0 +1,91 @@ +package com.whaleal.mars.core.query; + +import com.mongodb.client.model.CollationStrength; +import com.whaleal.mars.Constant; +import com.whaleal.mars.base.CreateDataUtil; +import com.whaleal.mars.core.Mars; +import com.whaleal.mars.session.QueryCursor; +import org.bson.Document; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.Locale; + +/** + * @author lyz + * @description + * @date 2022-06-27 18:01 + **/ +public class QueryCollationTest { + + private Mars mars = new Mars(Constant.connectionStr); + + @Before + public void tesFor() { + List list = CreateDataUtil.parseString("{ \"_id\" : 1, \"x\" : \"a\" }\n" + + "{ \"_id\" : 2, \"x\" : \"A\" }\n" + + "{ \"_id\" : 3, \"x\" : \"á\" }"); + + mars.insert(list, "foo"); + } + + @After + public void dropCollection(){ + mars.dropCollection("foo"); + } + + @Test + public void testFor(){ + Query query = new Query(); + + + /** + * Document{{_id=1, x=a}} + * Document{{_id=2, x=A}} + * Document{{_id=3, x=á}} + */ + query.collation(Collation.of(Locale.CHINA)); + + QueryCursor foo = mars.findAll(query, Document.class, "foo"); + while (foo.hasNext()){ + System.out.println(foo.next()); + } + + query.collation(Collation.of(Locale.FRENCH)); + + QueryCursor foo1 = mars.findAll(query, Document.class, "foo"); + while (foo.hasNext()){ + System.out.println(foo1.next()); + } + + + } + + /** + * Document{{_id=62ba5de1b4786d754c25e395, n=-10}} + * Document{{_id=62ba5de1b4786d754c25e391, n=-2.1}} + * Document{{_id=62ba5de1b4786d754c25e38e, n=1}} + * Document{{_id=62ba5de1b4786d754c25e396, n=10}} + * Document{{_id=62ba5de1b4786d754c25e38f, n=2}} + * Document{{_id=62ba5de1b4786d754c25e390, n=2.1}} + * Document{{_id=62ba5de1b4786d754c25e393, n=2.10}} + * Document{{_id=62ba5de1b4786d754c25e392, n=2.2}} + * Document{{_id=62ba5de1b4786d754c25e394, n=2.20}} + * Document{{_id=62ba5de1b4786d754c25e397, n=20}} + * Document{{_id=62ba5de1b4786d754c25e398, n=20.1}} + */ + @Test + public void testForNumber(){ + Query query = new Query(); + + query.with(Sort.ascending("n")); + query.collation(Collation.of(Locale.ENGLISH).numericOrdering(true)); + + QueryCursor c = mars.findAll(query, Document.class, "c"); + while (c.hasNext()){ + System.out.println(c.next()); + } + } +}