Skip to content

Commit

Permalink
Merge pull request #1 from whaleal/v1-dev
Browse files Browse the repository at this point in the history
表相关注解调整优化
  • Loading branch information
a65858835 authored Jun 28, 2022
2 parents e7c2547 + cc95ea5 commit 33b9bf8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,7 +55,7 @@ public TailableCursorTask(Mars mars, TailableCursorRequest<?> request, Class<?>
protected MongoCursor<Document> 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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
* <strong>NOTE:</strong> 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Query {
// projection
private Projection projectionSpec = null;
// sorting
private List<Sort> sorts;
private List<Sort> sorts = new ArrayList<>();
private long skip;
private int limit;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,10 @@ private <T> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Document> 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<Document> foo = mars.findAll(query, Document.class, "foo");
while (foo.hasNext()){
System.out.println(foo.next());
}

query.collation(Collation.of(Locale.FRENCH));

QueryCursor<Document> 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<Document> c = mars.findAll(query, Document.class, "c");
while (c.hasNext()){
System.out.println(c.next());
}
}
}

0 comments on commit 33b9bf8

Please # to comment.