Skip to content

Commit e06ab80

Browse files
DATAMONGO-1444 - Polishing.
- Update Javadoc comments and reference documentation. - Introduce Adapter for blocking IndexOperations. - Remove transaction synchronization. - Remove unused types. - Remove dropDupos options from indexes. - Prevent usage of Querydsl along with reactive repository. - Use ReactiveQueryMethod in ReactiveMongoQuery.
1 parent 8ec0dee commit e06ab80

File tree

58 files changed

+493
-771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+493
-771
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/ReactiveMongoDatabaseFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@
2424

2525
/**
2626
* Interface for factories creating reactive {@link MongoDatabase} instances.
27-
*
27+
*
2828
* @author Mark Paluch
2929
* @since 2.0
3030
*/
3131
public interface ReactiveMongoDatabaseFactory {
3232

3333
/**
3434
* Creates a default {@link MongoDatabase} instance.
35-
*
35+
*
3636
* @return
3737
* @throws DataAccessException
3838
*/
3939
MongoDatabase getMongoDatabase() throws DataAccessException;
4040

4141
/**
4242
* Creates a {@link MongoDatabase} instance to access the database with the given name.
43-
*
43+
*
4444
* @param dbName must not be {@literal null} or empty.
4545
* @return
4646
* @throws DataAccessException
@@ -49,7 +49,7 @@ public interface ReactiveMongoDatabaseFactory {
4949

5050
/**
5151
* Exposes a shared {@link MongoExceptionTranslator}.
52-
*
52+
*
5353
* @return will never be {@literal null}.
5454
*/
5555
PersistenceExceptionTranslator getExceptionTranslator();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractReactiveMongoConfiguration.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import org.springframework.context.annotation.Bean;
2020
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
2122
import org.springframework.data.mongodb.core.MongoTemplate;
23+
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
2224
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
2325
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
2426
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
@@ -28,7 +30,7 @@
2830

2931
/**
3032
* Base class for reactive Spring Data MongoDB configuration using JavaConfig.
31-
*
33+
*
3234
* @author Mark Paluch
3335
* @since 2.0
3436
* @see MongoConfigurationSupport
@@ -39,39 +41,39 @@ public abstract class AbstractReactiveMongoConfiguration extends MongoConfigurat
3941
/**
4042
* Return the {@link MongoClient} instance to connect to. Annotate with {@link Bean} in case you want to expose a
4143
* {@link MongoClient} instance to the {@link org.springframework.context.ApplicationContext}.
42-
*
44+
*
4345
* @return
4446
*/
4547
public abstract MongoClient mongoClient();
4648

4749
/**
4850
* Creates a {@link ReactiveMongoTemplate}.
49-
*
51+
*
5052
* @return
5153
*/
5254
@Bean
53-
public ReactiveMongoTemplate reactiveMongoTemplate() throws Exception {
55+
public ReactiveMongoOperations reactiveMongoTemplate() throws Exception {
5456
return new ReactiveMongoTemplate(mongoDbFactory(), mappingMongoConverter());
5557
}
5658

5759
/**
5860
* Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link Mongo} instance
5961
* configured in {@link #mongoClient()}.
60-
*
62+
*
6163
* @see #mongoClient()
6264
* @see #reactiveMongoTemplate()
6365
* @return
6466
* @throws Exception
6567
*/
6668
@Bean
67-
public SimpleReactiveMongoDatabaseFactory mongoDbFactory() {
69+
public ReactiveMongoDatabaseFactory mongoDbFactory() {
6870
return new SimpleReactiveMongoDatabaseFactory(mongoClient(), getDatabaseName());
6971
}
7072

7173
/**
7274
* Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and
7375
* {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied.
74-
*
76+
*
7577
* @see #customConversions()
7678
* @see #mongoMappingContext()
7779
* @see #mongoDbFactory()

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -73,6 +73,7 @@
7373
* @author Maciej Walkowiak
7474
* @author Thomas Darimont
7575
* @author Christoph Strobl
76+
* @author Mark Paluch
7677
*/
7778
public class MappingMongoConverterParser implements BeanDefinitionParser {
7879

@@ -121,6 +122,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
121122
}
122123

123124
if(!registry.containsBeanDefinition("indexOperationsProvider")){
125+
124126
BeanDefinitionBuilder indexOperationsProviderBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.data.mongodb.core.DefaultIndexOperationsProvider");
125127
indexOperationsProviderBuilder.addConstructorArgReference(dbFactoryRef);
126128
parserContext.registerBeanComponent(new BeanComponentDefinition(indexOperationsProviderBuilder.getBeanDefinition(), "indexOperationsProvider"));
@@ -129,9 +131,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
129131
try {
130132
registry.getBeanDefinition(INDEX_HELPER_BEAN_NAME);
131133
} catch (NoSuchBeanDefinitionException ignored) {
132-
if (!StringUtils.hasText(dbFactoryRef)) {
133-
dbFactoryRef = DB_FACTORY_BEAN_NAME;
134-
}
134+
135135
BeanDefinitionBuilder indexHelperBuilder = BeanDefinitionBuilder
136136
.genericBeanDefinition(MongoPersistentEntityIndexCreator.class);
137137
indexHelperBuilder.addConstructorArgReference(ctxRef);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.data.mongodb.config;
1818

19+
import java.util.Arrays;
1920
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.HashSet;
@@ -28,7 +29,9 @@
2829
import org.springframework.core.type.filter.AnnotationTypeFilter;
2930
import org.springframework.data.annotation.Persistent;
3031
import org.springframework.data.authentication.UserCredentials;
32+
import org.springframework.data.mapping.context.MappingContext;
3133
import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory;
34+
import org.springframework.data.mapping.context.PersistentEntities;
3235
import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy;
3336
import org.springframework.data.mapping.model.FieldNamingStrategy;
3437
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
@@ -51,7 +54,7 @@
5154

5255
/**
5356
* Base class for Spring Data MongoDB to be extended for JavaConfiguration usage.
54-
*
57+
*
5558
* @author Mark Paluch
5659
* @since 2.0
5760
*/
@@ -100,20 +103,22 @@ public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
100103

101104
/**
102105
* Returns a {@link MappingContextIsNewStrategyFactory} wrapped into a {@link CachingIsNewStrategyFactory}.
103-
*
106+
*
104107
* @return
105108
* @throws ClassNotFoundException
106109
*/
107110
@Bean
108111
public IsNewStrategyFactory isNewStrategyFactory() throws ClassNotFoundException {
109-
return new CachingIsNewStrategyFactory(new MappingContextIsNewStrategyFactory(mongoMappingContext()));
112+
113+
return new CachingIsNewStrategyFactory(new MappingContextIsNewStrategyFactory(
114+
new PersistentEntities(Arrays.<MappingContext<?, ?>> asList(new MappingContext[] { mongoMappingContext() }))));
110115
}
111116

112117
/**
113118
* Register custom {@link Converter}s in a {@link CustomConversions} object if required. These
114119
* {@link CustomConversions} will be registered with the {@link #mappingMongoConverter()} and
115120
* {@link #mongoMappingContext()}. Returns an empty {@link CustomConversions} instance by default.
116-
*
121+
*
117122
* @return must not be {@literal null}.
118123
*/
119124
@Bean
@@ -124,7 +129,7 @@ public CustomConversions customConversions() {
124129
/**
125130
* Scans the mapping base package for classes annotated with {@link Document}. By default, it scans for entities in
126131
* all packages returned by {@link #getMappingBasePackages()}.
127-
*
132+
*
128133
* @see #getMappingBasePackages()
129134
* @return
130135
* @throws ClassNotFoundException
@@ -143,7 +148,7 @@ protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
143148
/**
144149
* Scans the given base package for entities, i.e. MongoDB specific types annotated with {@link Document} and
145150
* {@link Persistent}.
146-
*
151+
*
147152
* @param basePackage must not be {@literal null}.
148153
* @return
149154
* @throws ClassNotFoundException
@@ -178,7 +183,7 @@ protected Set<Class<?>> scanForEntities(String basePackage) throws ClassNotFound
178183
* Configures whether to abbreviate field names for domain objects by configuring a
179184
* {@link CamelCaseAbbreviatingFieldNamingStrategy} on the {@link MongoMappingContext} instance created. For advanced
180185
* customization needs, consider overriding {@link #mappingMongoConverter()}.
181-
*
186+
*
182187
* @return
183188
*/
184189
protected boolean abbreviateFieldNames() {
@@ -187,7 +192,7 @@ protected boolean abbreviateFieldNames() {
187192

188193
/**
189194
* Configures a {@link FieldNamingStrategy} on the {@link MongoMappingContext} instance created.
190-
*
195+
*
191196
* @return
192197
* @since 1.5
193198
*/

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException;
18+
import static org.springframework.data.mongodb.core.MongoTemplate.*;
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
@@ -65,19 +65,18 @@ public DefaultIndexOperations(MongoDbFactory mongoDbFactory, String collectionNa
6565
* (non-Javadoc)
6666
* @see org.springframework.data.mongodb.core.IndexOperations#ensureIndex(org.springframework.data.mongodb.core.index.IndexDefinition)
6767
*/
68-
public void ensureIndex(final IndexDefinition indexDefinition) {
69-
execute(collection -> {
68+
public String ensureIndex(final IndexDefinition indexDefinition) {
69+
70+
return execute(collection -> {
7071

7172
Document indexOptions = indexDefinition.getIndexOptions();
7273

7374
if (indexOptions != null) {
7475

75-
IndexOptions ops = IndexConverters.DEFINITION_TO_MONGO_INDEX_OPTIONS.convert(indexDefinition);
76-
collection.createIndex(indexDefinition.getIndexKeys(), ops);
77-
} else {
78-
collection.createIndex(indexDefinition.getIndexKeys());
76+
IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
77+
return collection.createIndex(indexDefinition.getIndexKeys(), ops);
7978
}
80-
return null;
79+
return collection.createIndex(indexDefinition.getIndexKeys());
8180
});
8281
}
8382

@@ -86,11 +85,10 @@ public void ensureIndex(final IndexDefinition indexDefinition) {
8685
* @see org.springframework.data.mongodb.core.IndexOperations#dropIndex(java.lang.String)
8786
*/
8887
public void dropIndex(final String name) {
89-
execute(new CollectionCallback<Void>() {
90-
public Void doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
91-
collection.dropIndex(name);
92-
return null;
93-
}
88+
89+
execute(collection -> {
90+
collection.dropIndex(name);
91+
return null;
9492
});
9593

9694
}
@@ -110,6 +108,7 @@ public void dropAllIndexes() {
110108
public List<IndexInfo> getIndexInfo() {
111109

112110
return execute(new CollectionCallback<List<IndexInfo>>() {
111+
113112
public List<IndexInfo> doInCollection(MongoCollection<Document> collection)
114113
throws MongoException, DataAccessException {
115114

@@ -124,7 +123,7 @@ private List<IndexInfo> getIndexData(MongoCursor<Document> cursor) {
124123
while (cursor.hasNext()) {
125124

126125
Document ix = cursor.next();
127-
IndexInfo indexInfo = IndexConverters.DOCUMENT_INDEX_INFO.convert(ix);
126+
IndexInfo indexInfo = IndexConverters.documentToIndexInfoConverter().convert(ix);
128127
indexInfoList.add(indexInfo);
129128
}
130129

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Mono<String> ensureIndex(final IndexDefinition indexDefinition) {
6262

6363
if (indexOptions != null) {
6464
return collection.createIndex(indexDefinition.getIndexKeys(),
65-
IndexConverters.DEFINITION_TO_MONGO_INDEX_OPTIONS.convert(indexDefinition));
65+
IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition));
6666
}
6767

6868
return collection.createIndex(indexDefinition.getIndexKeys());
@@ -96,7 +96,7 @@ public Flux<IndexInfo> getIndexInfo() {
9696

9797
ListIndexesPublisher<Document> indexesPublisher = collection.listIndexes(Document.class);
9898

99-
return Flux.from(indexesPublisher).map(t -> IndexConverters.DOCUMENT_INDEX_INFO.convert(t));
99+
return Flux.from(indexesPublisher).map(IndexConverters.documentToIndexInfoConverter()::convert);
100100
});
101101
}
102102
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@
3535

3636
/**
3737
* {@link Converter Converters} for index-related MongoDB documents/types.
38-
*
38+
*
3939
* @author Mark Paluch
40+
* @author Christoph Strobl
4041
* @since 2.0
4142
*/
4243
abstract class IndexConverters {
4344

44-
public final static Converter<IndexDefinition, IndexOptions> DEFINITION_TO_MONGO_INDEX_OPTIONS;
45-
public final static Converter<Document, IndexInfo> DOCUMENT_INDEX_INFO;
45+
private static final Converter<IndexDefinition, IndexOptions> DEFINITION_TO_MONGO_INDEX_OPTIONS;
46+
private static final Converter<Document, IndexInfo> DOCUMENT_INDEX_INFO;
4647

4748
private static final Double ONE = Double.valueOf(1);
4849
private static final Double MINUS_ONE = Double.valueOf(-1);
@@ -58,6 +59,14 @@ private IndexConverters() {
5859

5960
}
6061

62+
static Converter<IndexDefinition, IndexOptions> indexDefinitionToIndexOptionsConverter() {
63+
return DEFINITION_TO_MONGO_INDEX_OPTIONS;
64+
}
65+
66+
static Converter<Document, IndexInfo> documentToIndexInfoConverter() {
67+
return DOCUMENT_INDEX_INFO;
68+
}
69+
6170
private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexOptionsConverter() {
6271

6372
return indexDefinition -> {
@@ -115,14 +124,14 @@ private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexO
115124
private static Converter<Document, IndexInfo> getDocumentIndexInfoConverter() {
116125

117126
return ix -> {
118-
Document keyDbObject = (Document) ix.get("key");
119-
int numberOfElements = keyDbObject.keySet().size();
127+
Document keyDocument = (Document) ix.get("key");
128+
int numberOfElements = keyDocument.keySet().size();
120129

121130
List<IndexField> indexFields = new ArrayList<IndexField>(numberOfElements);
122131

123-
for (String key : keyDbObject.keySet()) {
132+
for (String key : keyDocument.keySet()) {
124133

125-
Object value = keyDbObject.get(key);
134+
Object value = keyDocument.get(key);
126135

127136
if (TWO_D_IDENTIFIERS.contains(value)) {
128137
indexFields.add(IndexField.geo(key));
@@ -148,11 +157,10 @@ private static Converter<Document, IndexInfo> getDocumentIndexInfoConverter() {
148157
String name = ix.get("name").toString();
149158

150159
boolean unique = ix.containsKey("unique") ? (Boolean) ix.get("unique") : false;
151-
boolean dropDuplicates = ix.containsKey("dropDups") ? (Boolean) ix.get("dropDups") : false;
152160
boolean sparse = ix.containsKey("sparse") ? (Boolean) ix.get("sparse") : false;
153161

154162
String language = ix.containsKey("default_language") ? (String) ix.get("default_language") : "";
155-
return new IndexInfo(indexFields, name, unique, dropDuplicates, sparse, language);
163+
return new IndexInfo(indexFields, name, unique, sparse, language);
156164
};
157165
}
158166
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface IndexOperations {
3535
*
3636
* @param indexDefinition must not be {@literal null}.
3737
*/
38-
void ensureIndex(IndexDefinition indexDefinition);
38+
String ensureIndex(IndexDefinition indexDefinition);
3939

4040
/**
4141
* Drops an index from this collection.

0 commit comments

Comments
 (0)