Skip to content

Commit 0b169d5

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1518 - Add support for collations.
We now support collations for collections, indexes, queries, findAndModify, delete, geo, bulk and aggregation operations in both the imperative and reactive implementations on template level. Collation collation = Collation.of("fr") .strength(ComparisonLevel.secondary() .includeCase()) .numericOrderingEnabled() .alternate(Alternate.shifted().punct()) .forwardDiacriticSort() .normalizationEnabled(); template.createCollection(Person.class, CollectionOptions.just(collation)); Query query = new Query(Criteria.where("firstName").is("Amél")).collation(collation); Original pull request: #459.
1 parent b9d301e commit 0b169d5

25 files changed

+2512
-275
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/Collation.java

Lines changed: 754 additions & 0 deletions
Large diffs are not rendered by default.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,22 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18+
import java.util.Optional;
19+
20+
import org.springframework.util.Assert;
21+
1822
/**
1923
* Provides a simple wrapper to encapsulate the variety of settings you can use when creating a collection.
20-
*
24+
*
2125
* @author Thomas Risberg
26+
* @author Christoph Strobl
2227
*/
2328
public class CollectionOptions {
2429

2530
private Integer maxDocuments;
26-
2731
private Integer size;
28-
2932
private Boolean capped;
33+
private Collation collation;
3034

3135
/**
3236
* Constructs a new <code>CollectionOptions</code> instance.
@@ -37,12 +41,85 @@ public class CollectionOptions {
3741
* false otherwise.
3842
*/
3943
public CollectionOptions(Integer size, Integer maxDocuments, Boolean capped) {
40-
super();
44+
4145
this.maxDocuments = maxDocuments;
4246
this.size = size;
4347
this.capped = capped;
4448
}
4549

50+
private CollectionOptions() {}
51+
52+
/**
53+
* Create new {@link CollectionOptions} by just providing the {@link Collation} to use.
54+
*
55+
* @param collation must not be {@literal null}.
56+
* @return new {@link CollectionOptions}.
57+
* @since 2.0
58+
*/
59+
public static CollectionOptions just(Collation collation) {
60+
61+
Assert.notNull(collation, "Collation must not be null!");
62+
63+
CollectionOptions options = new CollectionOptions();
64+
options.setCollation(collation);
65+
return options;
66+
}
67+
68+
/**
69+
* Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}.
70+
*
71+
* @return new {@link CollectionOptions}.
72+
* @since 2.0
73+
*/
74+
public CollectionOptions capped() {
75+
76+
CollectionOptions options = new CollectionOptions(size, maxDocuments, true);
77+
options.setCollation(collation);
78+
return options;
79+
}
80+
81+
/**
82+
* Create new {@link CollectionOptions} with already given settings and {@code maxDocuments} set to given value.
83+
*
84+
* @param maxDocuments can be {@literal null}.
85+
* @return new {@link CollectionOptions}.
86+
* @since 2.0
87+
*/
88+
public CollectionOptions maxDocuments(Integer maxDocuments) {
89+
90+
CollectionOptions options = new CollectionOptions(size, maxDocuments, capped);
91+
options.setCollation(collation);
92+
return options;
93+
}
94+
95+
/**
96+
* Create new {@link CollectionOptions} with already given settings and {@code size} set to given value.
97+
*
98+
* @param size can be {@literal null}.
99+
* @return new {@link CollectionOptions}.
100+
* @since 2.0
101+
*/
102+
public CollectionOptions size(Integer size) {
103+
104+
CollectionOptions options = new CollectionOptions(size, maxDocuments, capped);
105+
options.setCollation(collation);
106+
return options;
107+
}
108+
109+
/**
110+
* Create new {@link CollectionOptions} with already given settings and {@code collation} set to given value.
111+
*
112+
* @param collation can be {@literal null}.
113+
* @return new {@link CollectionOptions}.
114+
* @since 2.0
115+
*/
116+
public CollectionOptions collation(Collation collation) {
117+
118+
CollectionOptions options = new CollectionOptions(size, maxDocuments, capped);
119+
options.setCollation(collation);
120+
return options;
121+
}
122+
46123
public Integer getMaxDocuments() {
47124
return maxDocuments;
48125
}
@@ -67,4 +144,23 @@ public void setCapped(Boolean capped) {
67144
this.capped = capped;
68145
}
69146

147+
/**
148+
* Set {@link Collation} options.
149+
*
150+
* @param collation
151+
* @since 2.0
152+
*/
153+
public void setCollation(Collation collation) {
154+
this.collation = collation;
155+
}
156+
157+
/**
158+
* Get the {@link Collation} settings.
159+
*
160+
* @return
161+
* @since 2.0
162+
*/
163+
public Optional<Collation> getCollation() {
164+
return Optional.ofNullable(collation);
165+
}
70166
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.List;
2121

22+
import com.mongodb.client.model.DeleteOptions;
2223
import org.bson.Document;
2324
import org.springframework.dao.DataAccessException;
2425
import org.springframework.dao.support.PersistenceExceptionTranslator;
@@ -58,12 +59,12 @@ class DefaultBulkOperations implements BulkOperations {
5859

5960
private BulkWriteOptions bulkOptions;
6061

61-
List<WriteModel<Document>> models = new ArrayList<WriteModel<Document>>();
62+
List<WriteModel<Document>> models = new ArrayList<>();
6263

6364
/**
6465
* Creates a new {@link DefaultBulkOperations} for the given {@link MongoOperations}, {@link BulkMode}, collection
6566
* name and {@link WriteConcern}.
66-
*
67+
*
6768
* @param mongoOperations The underlying {@link MongoOperations}, must not be {@literal null}.
6869
* @param bulkMode must not be {@literal null}.
6970
* @param collectionName Name of the collection to work on, must not be {@literal null} or empty.
@@ -88,7 +89,7 @@ class DefaultBulkOperations implements BulkOperations {
8889

8990
/**
9091
* Configures the {@link PersistenceExceptionTranslator} to be used. Defaults to {@link MongoExceptionTranslator}.
91-
*
92+
*
9293
* @param exceptionTranslator can be {@literal null}.
9394
*/
9495
public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) {
@@ -97,7 +98,7 @@ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTrans
9798

9899
/**
99100
* Configures the {@link WriteConcernResolver} to be used. Defaults to {@link DefaultWriteConcernResolver}.
100-
*
101+
*
101102
* @param writeConcernResolver can be {@literal null}.
102103
*/
103104
public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver) {
@@ -107,7 +108,7 @@ public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver) {
107108

108109
/**
109110
* Configures the default {@link WriteConcern} to be used. Defaults to {@literal null}.
110-
*
111+
*
111112
* @param defaultWriteConcern can be {@literal null}.
112113
*/
113114
public void setDefaultWriteConcern(WriteConcern defaultWriteConcern) {
@@ -244,7 +245,10 @@ public BulkOperations remove(Query query) {
244245

245246
Assert.notNull(query, "Query must not be null!");
246247

247-
models.add(new DeleteManyModel<Document>(query.getQueryObject()));
248+
DeleteOptions deleteOptions = new DeleteOptions();
249+
query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation);
250+
251+
models.add(new DeleteManyModel(query.getQueryObject(), deleteOptions));
248252
return this;
249253
}
250254

@@ -306,11 +310,12 @@ private BulkOperations update(Query query, Update update, boolean upsert, boolea
306310

307311
UpdateOptions options = new UpdateOptions();
308312
options.upsert(upsert);
313+
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
309314

310315
if (multi) {
311-
models.add(new UpdateManyModel<Document>(query.getQueryObject(), update.getUpdateObject(), options));
316+
models.add(new UpdateManyModel<>(query.getQueryObject(), update.getUpdateObject(), options));
312317
} else {
313-
models.add(new UpdateOneModel<Document>(query.getQueryObject(), update.getUpdateObject(), options));
318+
models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options));
314319
}
315320
return this;
316321
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindAndModifyOptions.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,21 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18+
import java.util.Optional;
19+
20+
/**
21+
* @author Mark Pollak
22+
* @author Oliver Gierke
23+
* @author Christoph Strobl
24+
*/
1825
public class FindAndModifyOptions {
1926

2027
boolean returnNew;
21-
2228
boolean upsert;
23-
2429
boolean remove;
2530

31+
private Collation collation;
32+
2633
/**
2734
* Static factory method to create a FindAndModifyOptions instance
2835
*
@@ -32,6 +39,27 @@ public static FindAndModifyOptions options() {
3239
return new FindAndModifyOptions();
3340
}
3441

42+
/**
43+
* @param options
44+
* @return
45+
* @since 2.0
46+
*/
47+
public static FindAndModifyOptions of(FindAndModifyOptions source) {
48+
49+
50+
FindAndModifyOptions options = new FindAndModifyOptions();
51+
if(source == null) {
52+
return options;
53+
}
54+
55+
options.returnNew = source.returnNew;
56+
options.upsert = source.upsert;
57+
options.remove = source.remove;
58+
options.collation = source.collation;
59+
60+
return options;
61+
}
62+
3563
public FindAndModifyOptions returnNew(boolean returnNew) {
3664
this.returnNew = returnNew;
3765
return this;
@@ -47,6 +75,19 @@ public FindAndModifyOptions remove(boolean remove) {
4775
return this;
4876
}
4977

78+
/**
79+
* Define the {@link Collation} specifying language-specific rules for string comparison.
80+
*
81+
* @param collation
82+
* @return
83+
* @since 2.0
84+
*/
85+
public FindAndModifyOptions collation(Collation collation) {
86+
87+
this.collation = collation;
88+
return this;
89+
}
90+
5091
public boolean isReturnNew() {
5192
return returnNew;
5293
}
@@ -59,4 +100,14 @@ public boolean isRemove() {
59100
return remove;
60101
}
61102

103+
/**
104+
* Get the {@link Collation} specifying language-specific rules for string comparison.
105+
*
106+
* @return
107+
* @since 2.0
108+
*/
109+
public Optional<Collation> getCollation() {
110+
return Optional.ofNullable(collation);
111+
}
112+
62113
}

0 commit comments

Comments
 (0)