Skip to content

Commit

Permalink
DATAMONGO-1518 - Add support for collations.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
christophstrobl authored and mp911de committed May 4, 2017
1 parent b9d301e commit 0b169d5
Show file tree
Hide file tree
Showing 25 changed files with 2,512 additions and 275 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2011 the original author or authors.
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,18 +15,22 @@
*/
package org.springframework.data.mongodb.core;

import java.util.Optional;

import org.springframework.util.Assert;

/**
* Provides a simple wrapper to encapsulate the variety of settings you can use when creating a collection.
*
*
* @author Thomas Risberg
* @author Christoph Strobl
*/
public class CollectionOptions {

private Integer maxDocuments;

private Integer size;

private Boolean capped;
private Collation collation;

/**
* Constructs a new <code>CollectionOptions</code> instance.
Expand All @@ -37,12 +41,85 @@ public class CollectionOptions {
* false otherwise.
*/
public CollectionOptions(Integer size, Integer maxDocuments, Boolean capped) {
super();

this.maxDocuments = maxDocuments;
this.size = size;
this.capped = capped;
}

private CollectionOptions() {}

/**
* Create new {@link CollectionOptions} by just providing the {@link Collation} to use.
*
* @param collation must not be {@literal null}.
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public static CollectionOptions just(Collation collation) {

Assert.notNull(collation, "Collation must not be null!");

CollectionOptions options = new CollectionOptions();
options.setCollation(collation);
return options;
}

/**
* Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}.
*
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public CollectionOptions capped() {

CollectionOptions options = new CollectionOptions(size, maxDocuments, true);
options.setCollation(collation);
return options;
}

/**
* Create new {@link CollectionOptions} with already given settings and {@code maxDocuments} set to given value.
*
* @param maxDocuments can be {@literal null}.
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public CollectionOptions maxDocuments(Integer maxDocuments) {

CollectionOptions options = new CollectionOptions(size, maxDocuments, capped);
options.setCollation(collation);
return options;
}

/**
* Create new {@link CollectionOptions} with already given settings and {@code size} set to given value.
*
* @param size can be {@literal null}.
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public CollectionOptions size(Integer size) {

CollectionOptions options = new CollectionOptions(size, maxDocuments, capped);
options.setCollation(collation);
return options;
}

/**
* Create new {@link CollectionOptions} with already given settings and {@code collation} set to given value.
*
* @param collation can be {@literal null}.
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public CollectionOptions collation(Collation collation) {

CollectionOptions options = new CollectionOptions(size, maxDocuments, capped);
options.setCollation(collation);
return options;
}

public Integer getMaxDocuments() {
return maxDocuments;
}
Expand All @@ -67,4 +144,23 @@ public void setCapped(Boolean capped) {
this.capped = capped;
}

/**
* Set {@link Collation} options.
*
* @param collation
* @since 2.0
*/
public void setCollation(Collation collation) {
this.collation = collation;
}

/**
* Get the {@link Collation} settings.
*
* @return
* @since 2.0
*/
public Optional<Collation> getCollation() {
return Optional.ofNullable(collation);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.List;

import com.mongodb.client.model.DeleteOptions;
import org.bson.Document;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
Expand Down Expand Up @@ -58,12 +59,12 @@ class DefaultBulkOperations implements BulkOperations {

private BulkWriteOptions bulkOptions;

List<WriteModel<Document>> models = new ArrayList<WriteModel<Document>>();
List<WriteModel<Document>> models = new ArrayList<>();

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

/**
* Configures the {@link PersistenceExceptionTranslator} to be used. Defaults to {@link MongoExceptionTranslator}.
*
*
* @param exceptionTranslator can be {@literal null}.
*/
public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) {
Expand All @@ -97,7 +98,7 @@ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTrans

/**
* Configures the {@link WriteConcernResolver} to be used. Defaults to {@link DefaultWriteConcernResolver}.
*
*
* @param writeConcernResolver can be {@literal null}.
*/
public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver) {
Expand All @@ -107,7 +108,7 @@ public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver) {

/**
* Configures the default {@link WriteConcern} to be used. Defaults to {@literal null}.
*
*
* @param defaultWriteConcern can be {@literal null}.
*/
public void setDefaultWriteConcern(WriteConcern defaultWriteConcern) {
Expand Down Expand Up @@ -244,7 +245,10 @@ public BulkOperations remove(Query query) {

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

models.add(new DeleteManyModel<Document>(query.getQueryObject()));
DeleteOptions deleteOptions = new DeleteOptions();
query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation);

models.add(new DeleteManyModel(query.getQueryObject(), deleteOptions));
return this;
}

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

UpdateOptions options = new UpdateOptions();
options.upsert(upsert);
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);

if (multi) {
models.add(new UpdateManyModel<Document>(query.getQueryObject(), update.getUpdateObject(), options));
models.add(new UpdateManyModel<>(query.getQueryObject(), update.getUpdateObject(), options));
} else {
models.add(new UpdateOneModel<Document>(query.getQueryObject(), update.getUpdateObject(), options));
models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2011 the original author or authors.
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,21 @@
*/
package org.springframework.data.mongodb.core;

import java.util.Optional;

/**
* @author Mark Pollak
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class FindAndModifyOptions {

boolean returnNew;

boolean upsert;

boolean remove;

private Collation collation;

/**
* Static factory method to create a FindAndModifyOptions instance
*
Expand All @@ -32,6 +39,27 @@ public static FindAndModifyOptions options() {
return new FindAndModifyOptions();
}

/**
* @param options
* @return
* @since 2.0
*/
public static FindAndModifyOptions of(FindAndModifyOptions source) {


FindAndModifyOptions options = new FindAndModifyOptions();
if(source == null) {
return options;
}

options.returnNew = source.returnNew;
options.upsert = source.upsert;
options.remove = source.remove;
options.collation = source.collation;

return options;
}

public FindAndModifyOptions returnNew(boolean returnNew) {
this.returnNew = returnNew;
return this;
Expand All @@ -47,6 +75,19 @@ public FindAndModifyOptions remove(boolean remove) {
return this;
}

/**
* Define the {@link Collation} specifying language-specific rules for string comparison.
*
* @param collation
* @return
* @since 2.0
*/
public FindAndModifyOptions collation(Collation collation) {

this.collation = collation;
return this;
}

public boolean isReturnNew() {
return returnNew;
}
Expand All @@ -59,4 +100,14 @@ public boolean isRemove() {
return remove;
}

/**
* Get the {@link Collation} specifying language-specific rules for string comparison.
*
* @return
* @since 2.0
*/
public Optional<Collation> getCollation() {
return Optional.ofNullable(collation);
}

}
Loading

0 comments on commit 0b169d5

Please sign in to comment.