Skip to content

DATAMONGO-1854 - Add support for Collation to repositories. #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-1854-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-1854-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-1854-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-1854-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,14 @@ public String ensureIndex(final IndexDefinition indexDefinition) {

return execute(collection -> {

Document indexOptions = indexDefinition.getIndexOptions();
MongoPersistentEntity<?> entity = lookupPersistentEntity(type, collectionName);

IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
IndexOptions indexOptions = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);

if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
indexOptions = addPartialFilterIfPresent(indexOptions, indexDefinition.getIndexOptions(), entity);
indexOptions = addDefaultCollationIfRequired(indexOptions, entity);

Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));

ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY),
lookupPersistentEntity(type, collectionName)));
}

return collection.createIndex(indexDefinition.getIndexKeys(), ops);
return collection.createIndex(indexDefinition.getIndexKeys(), indexOptions);
});
}

Expand Down Expand Up @@ -192,7 +187,7 @@ public List<IndexInfo> doInCollection(MongoCollection<Document> collection)

private List<IndexInfo> getIndexData(MongoCursor<Document> cursor) {

List<IndexInfo> indexInfoList = new ArrayList<IndexInfo>();
List<IndexInfo> indexInfoList = new ArrayList<>();

while (cursor.hasNext()) {

Expand All @@ -217,4 +212,25 @@ public <T> T execute(CollectionCallback<T> callback) {

return mongoOperations.execute(collectionName, callback);
}

private IndexOptions addPartialFilterIfPresent(IndexOptions ops, Document sourceOptions,
@Nullable MongoPersistentEntity<?> entity) {

if (!sourceOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
return ops;
}

Assert.isInstanceOf(Document.class, sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
return ops.partialFilterExpression(
mapper.getMappedObject((Document) sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
}

private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops, MongoPersistentEntity<?> entity) {

if (ops.getCollation() != null || entity == null || !entity.hasCollation()) {
return ops;
}

return ops.collation(entity.getCollation().toMongoCollation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,16 @@ public Mono<String> ensureIndex(final IndexDefinition indexDefinition) {

return mongoOperations.execute(collectionName, collection -> {

Document indexOptions = indexDefinition.getIndexOptions();
MongoPersistentEntity<?> entity = type
.map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val))
.orElseGet(() -> lookupPersistentEntity(collectionName));

IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
IndexOptions indexOptions = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);

if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
indexOptions = addPartialFilterIfPresent(indexOptions, indexDefinition.getIndexOptions(), entity);
indexOptions = addDefaultCollationIfRequired(indexOptions, entity);

Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));

MongoPersistentEntity<?> entity = type
.map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val))
.orElseGet(() -> lookupPersistentEntity(collectionName));

ops = ops.partialFilterExpression(
queryMapper.getMappedObject(indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY, Document.class), entity));
}

return collection.createIndex(indexDefinition.getIndexKeys(), ops);
return collection.createIndex(indexDefinition.getIndexKeys(), indexOptions);

}).next();
}
Expand All @@ -126,26 +119,50 @@ private MongoPersistentEntity<?> lookupPersistentEntity(String collection) {
.orElse(null);
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.index.ReactiveIndexOperations#dropIndex(java.lang.String)
*/
public Mono<Void> dropIndex(final String name) {
return mongoOperations.execute(collectionName, collection -> collection.dropIndex(name)).then();
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.index.ReactiveIndexOperations#dropAllIndexes()
*/
public Mono<Void> dropAllIndexes() {
return dropIndex("*");
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.index.ReactiveIndexOperations#getIndexInfo()
*/
public Flux<IndexInfo> getIndexInfo() {

return mongoOperations.execute(collectionName, collection -> collection.listIndexes(Document.class)) //
.map(IndexConverters.documentToIndexInfoConverter()::convert);
}

private IndexOptions addPartialFilterIfPresent(IndexOptions ops, Document sourceOptions,
@Nullable MongoPersistentEntity<?> entity) {

if (!sourceOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
return ops;
}

Assert.isInstanceOf(Document.class, sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
return ops.partialFilterExpression(
queryMapper.getMappedObject((Document) sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
}

private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops, MongoPersistentEntity<?> entity) {

if (ops.getCollation() != null || entity == null || !entity.hasCollation()) {
return ops;
}

return ops.collation(entity.getCollation().toMongoCollation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.bson.Document;

import org.springframework.core.convert.ConversionService;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mongodb.core.convert.MongoWriter;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -176,6 +180,20 @@ private static Document parse(String source) {
}
}

public <T> TypedOperations<T> forType(@Nullable Class<T> entityClass) {

if (entityClass != null) {

MongoPersistentEntity<?> entity = context.getPersistentEntity(entityClass);

if (entity != null) {
return new TypedEntityOperations(entity);
}

}
return UntypedOperations.instance();
}

/**
* A representation of information about an entity.
*
Expand Down Expand Up @@ -263,7 +281,7 @@ default boolean isVersionedEntity() {

/**
* Returns whether the entity is considered to be new.
*
*
* @return
* @since 2.1.2
*/
Expand Down Expand Up @@ -414,7 +432,7 @@ public T getBean() {
return map;
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.EntityOperations.Entity#isNew()
*/
Expand Down Expand Up @@ -585,7 +603,7 @@ public T getBean() {
return propertyAccessor.getBean();
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.EntityOperations.Entity#isNew()
*/
Expand Down Expand Up @@ -698,4 +716,102 @@ public T incrementVersion() {
return propertyAccessor.getBean();
}
}

/**
* Type-specific operations abstraction.
*
* @author Mark Paluch
* @param <T>
* @since 2.2
*/
interface TypedOperations<T> {

/**
* Return the optional {@link Collation} for the underlying entity.
*
* @return
*/
Optional<Collation> getCollation();

/**
* Return the optional {@link Collation} from the given {@link Query} and fall back to the collation configured for
* the underlying entity.
*
* @return
*/
Optional<Collation> getCollation(Query query);
}

/**
* {@link TypedOperations} for generic entities that are not represented with {@link PersistentEntity} (e.g. custom
* conversions).
*/
@RequiredArgsConstructor
enum UntypedOperations implements TypedOperations<Object> {

INSTANCE;

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> TypedOperations<T> instance() {
return (TypedOperations) INSTANCE;
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation()
*/
@Override
public Optional<Collation> getCollation() {
return Optional.empty();
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation(org.springframework.data.mongodb.core.query.Query)
*/
@Override
public Optional<Collation> getCollation(Query query) {

if (query == null) {
return Optional.empty();
}

return query.getCollation();
}
}

/**
* {@link TypedOperations} backed by {@link MongoPersistentEntity}.
*
* @param <T>
*/
@RequiredArgsConstructor
static class TypedEntityOperations<T> implements TypedOperations<T> {

private final @NonNull MongoPersistentEntity<T> entity;

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation()
*/
@Override
public Optional<Collation> getCollation() {
return Optional.ofNullable(entity.getCollation());
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation(org.springframework.data.mongodb.core.query.Query)
*/
@Override
public Optional<Collation> getCollation(Query query) {

if (query.getCollation().isPresent()) {
return query.getCollation();
}

return Optional.ofNullable(entity.getCollation());
}
}

}
Loading