Skip to content

Commit e4a982a

Browse files
committed
DATAMONGO-1682 - Polishing.
Require non-null arguments in DefaultReactiveIndexOperations constructor. Remove superfluous publisher creation indirections. Use StepVerifier.verifyComplete() to verify the step sequence. Use provided entity type in template API to construct index operations.
1 parent 6acc774 commit e4a982a

File tree

5 files changed

+62
-69
lines changed

5 files changed

+62
-69
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
import org.bson.Document;
2525
import org.springframework.dao.DataAccessException;
26-
import org.springframework.data.mongodb.core.convert.QueryMapper;
2726
import org.springframework.data.mongodb.MongoDbFactory;
27+
import org.springframework.data.mongodb.core.convert.QueryMapper;
2828
import org.springframework.data.mongodb.core.index.IndexDefinition;
2929
import org.springframework.data.mongodb.core.index.IndexInfo;
3030
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
@@ -37,7 +37,7 @@
3737

3838
/**
3939
* Default implementation of {@link IndexOperations}.
40-
*
40+
*
4141
* @author Mark Pollack
4242
* @author Oliver Gierke
4343
* @author Komi Innocent
@@ -55,7 +55,7 @@ public class DefaultIndexOperations implements IndexOperations {
5555

5656
/**
5757
* Creates a new {@link DefaultIndexOperations}.
58-
*
58+
*
5959
* @param mongoDbFactory must not be {@literal null}.
6060
* @param collectionName must not be {@literal null}.
6161
* @param queryMapper must not be {@literal null}.
@@ -97,24 +97,22 @@ public String ensureIndex(final IndexDefinition indexDefinition) {
9797

9898
Document indexOptions = indexDefinition.getIndexOptions();
9999

100-
if (indexOptions != null) {
101-
102-
IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
100+
if (indexOptions == null) {
101+
return collection.createIndex(indexDefinition.getIndexKeys());
102+
}
103103

104-
if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
104+
IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
105105

106-
Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
106+
if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
107107

108-
ops.partialFilterExpression( mapper.getMappedObject(
109-
(Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName)));
110-
}
108+
Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
111109

112-
return collection.createIndex(indexDefinition.getIndexKeys(), ops);
110+
ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY),
111+
lookupPersistentEntity(type, collectionName)));
113112
}
114-
return collection.createIndex(indexDefinition.getIndexKeys());
115-
}
116113

117-
);
114+
return collection.createIndex(indexDefinition.getIndexKeys(), ops);
115+
});
118116
}
119117

120118
private MongoPersistentEntity<?> lookupPersistentEntity(Class<?> entityType, String collection) {

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

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-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.
@@ -29,10 +29,9 @@
2929
import org.springframework.util.Assert;
3030

3131
import com.mongodb.client.model.IndexOptions;
32-
import com.mongodb.reactivestreams.client.ListIndexesPublisher;
3332

3433
/**
35-
* Default implementation of {@link IndexOperations}.
34+
* Default implementation of {@link ReactiveIndexOperations}.
3635
*
3736
* @author Mark Paluch
3837
* @author Christoph Strobl
@@ -52,21 +51,28 @@ public class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
5251
*
5352
* @param mongoOperations must not be {@literal null}.
5453
* @param collectionName must not be {@literal null}.
54+
* @param queryMapper must not be {@literal null}.
5555
*/
5656
public DefaultReactiveIndexOperations(ReactiveMongoOperations mongoOperations, String collectionName,
5757
QueryMapper queryMapper) {
58-
59-
this(mongoOperations, collectionName, queryMapper, null);
58+
this(mongoOperations, collectionName, queryMapper, Optional.empty());
6059
}
6160

6261
/**
6362
* Creates a new {@link DefaultReactiveIndexOperations}.
6463
*
6564
* @param mongoOperations must not be {@literal null}.
6665
* @param collectionName must not be {@literal null}.
66+
* @param queryMapper must not be {@literal null}.
67+
* @param type used for mapping potential partial index filter expression, must not be {@literal null}.
6768
*/
6869
public DefaultReactiveIndexOperations(ReactiveMongoOperations mongoOperations, String collectionName,
6970
QueryMapper queryMapper, Class<?> type) {
71+
this(mongoOperations, collectionName, queryMapper, Optional.of(type));
72+
}
73+
74+
private DefaultReactiveIndexOperations(ReactiveMongoOperations mongoOperations, String collectionName,
75+
QueryMapper queryMapper, Optional<Class<?>> type) {
7076

7177
Assert.notNull(mongoOperations, "ReactiveMongoOperations must not be null!");
7278
Assert.notNull(collectionName, "Collection must not be null!");
@@ -75,7 +81,7 @@ public DefaultReactiveIndexOperations(ReactiveMongoOperations mongoOperations, S
7581
this.mongoOperations = mongoOperations;
7682
this.collectionName = collectionName;
7783
this.queryMapper = queryMapper;
78-
this.type = Optional.ofNullable(type);
84+
this.type = type;
7985
}
8086

8187
/* (non-Javadoc)
@@ -87,51 +93,44 @@ public Mono<String> ensureIndex(final IndexDefinition indexDefinition) {
8793

8894
Document indexOptions = indexDefinition.getIndexOptions();
8995

90-
if (indexOptions != null) {
91-
92-
IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
96+
if (indexOptions == null) {
97+
return collection.createIndex(indexDefinition.getIndexKeys());
98+
}
9399

94-
if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
100+
IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
95101

96-
Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
102+
if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) {
97103

98-
MongoPersistentEntity<?> entity = type
99-
.map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val))
100-
.orElseGet(() -> lookupPersistentEntity(collectionName));
104+
Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
101105

102-
ops = ops.partialFilterExpression(
103-
queryMapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
104-
}
106+
MongoPersistentEntity<?> entity = type
107+
.map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val))
108+
.orElseGet(() -> lookupPersistentEntity(collectionName));
105109

106-
return collection.createIndex(indexDefinition.getIndexKeys(), ops);
110+
ops = ops.partialFilterExpression(
111+
queryMapper.getMappedObject(indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY, Document.class), entity));
107112
}
108113

109-
return collection.createIndex(indexDefinition.getIndexKeys());
114+
return collection.createIndex(indexDefinition.getIndexKeys(), ops);
115+
110116
}).next();
111117
}
112118

113119
private MongoPersistentEntity<?> lookupPersistentEntity(String collection) {
114120

115121
Collection<? extends MongoPersistentEntity<?>> entities = queryMapper.getMappingContext().getPersistentEntities();
116122

117-
for (MongoPersistentEntity<?> entity : entities) {
118-
if (entity.getCollection().equals(collection)) {
119-
return entity;
120-
}
121-
}
122-
123-
return null;
123+
return entities.stream() //
124+
.filter(entity -> entity.getCollection().equals(collection)) //
125+
.findFirst() //
126+
.orElse(null);
124127
}
125128

126129
/* (non-Javadoc)
127130
* @see org.springframework.data.mongodb.core.ReactiveIndexOperations#dropIndex(java.lang.String)
128131
*/
129132
public Mono<Void> dropIndex(final String name) {
130-
131-
return mongoOperations.execute(collectionName, collection -> {
132-
133-
return Mono.from(collection.dropIndex(name));
134-
}).flatMap(success -> Mono.<Void> empty()).next();
133+
return mongoOperations.execute(collectionName, collection -> collection.dropIndex(name)).then();
135134
}
136135

137136
/* (non-Javadoc)
@@ -146,11 +145,8 @@ public Mono<Void> dropAllIndexes() {
146145
*/
147146
public Flux<IndexInfo> getIndexInfo() {
148147

149-
return mongoOperations.execute(collectionName, collection -> {
150-
151-
ListIndexesPublisher<Document> indexesPublisher = collection.listIndexes(Document.class);
152-
153-
return Flux.from(indexesPublisher).map(IndexConverters.documentToIndexInfoConverter()::convert);
154-
});
148+
return mongoOperations.execute(collectionName, collection -> collection.listIndexes(Document.class)) //
149+
.map(IndexConverters.documentToIndexInfoConverter()::convert);
155150
}
151+
156152
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ public IndexOperations indexOps(String collectionName) {
529529
}
530530

531531
public IndexOperations indexOps(Class<?> entityClass) {
532-
return new DefaultIndexOperations(getMongoDbFactory(), determineCollectionName(entityClass), queryMapper);
532+
return new DefaultIndexOperations(getMongoDbFactory(), determineCollectionName(entityClass), queryMapper,
533+
entityClass);
533534
}
534535

535536
public BulkOperations bulkOps(BulkMode bulkMode, String collectionName) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ public ReactiveIndexOperations indexOps(String collectionName) {
308308
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#reactiveIndexOps(java.lang.Class)
309309
*/
310310
public ReactiveIndexOperations indexOps(Class<?> entityClass) {
311-
return new DefaultReactiveIndexOperations(this, determineCollectionName(entityClass), this.queryMapper);
311+
return new DefaultReactiveIndexOperations(this, determineCollectionName(entityClass), this.queryMapper,
312+
entityClass);
312313
}
313314

314315
public String getCollectionName(Class<?> entityClass) {
@@ -1914,8 +1915,7 @@ String determineCollectionName(Class<?> entityClass) {
19141915
"No class parameter provided, entity collection can't be determined!");
19151916
}
19161917

1917-
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(entityClass);
1918-
return entity.getCollection();
1918+
return mappingContext.getRequiredPersistentEntity(entityClass).getCollection();
19191919
}
19201920

19211921
private static MappingMongoConverter getDefaultMongoConverter() {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperationsTests.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
package org.springframework.data.mongodb.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.hamcrest.core.Is.*;
2019
import static org.junit.Assume.*;
2120
import static org.springframework.data.mongodb.core.index.PartialIndexFilter.*;
2221
import static org.springframework.data.mongodb.core.query.Criteria.*;
2322

24-
import reactor.core.publisher.Mono;
2523
import reactor.test.StepVerifier;
2624

2725
import java.util.function.Predicate;
@@ -86,10 +84,10 @@ public void setUp() {
8684
String collectionName = this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class);
8785

8886
this.collection = this.template.getMongoDatabase().getCollection(collectionName, Document.class);
89-
Mono.from(this.collection.dropIndexes()).subscribe();
90-
9187
this.indexOps = new DefaultReactiveIndexOperations(template, collectionName,
9288
new QueryMapper(template.getConverter()));
89+
90+
StepVerifier.create(this.collection.dropIndexes()).expectNextCount(1).verifyComplete();
9391
}
9492

9593
private void queryMongoVersionIfNecessary() {
@@ -103,7 +101,7 @@ private void queryMongoVersionIfNecessary() {
103101
@Test // DATAMONGO-1518
104102
public void shouldCreateIndexWithCollationCorrectly() {
105103

106-
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR), is(true));
104+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR));
107105

108106
IndexDefinition id = new Index().named("with-collation").on("xyz", Direction.ASC)
109107
.collation(Collation.of("de_AT").caseFirst(CaseFirst.off()));
@@ -131,13 +129,13 @@ public void shouldCreateIndexWithCollationCorrectly() {
131129

132130
assertThat(result).isEqualTo(expected);
133131
}) //
134-
.thenAwait();
132+
.verifyComplete();
135133
}
136134

137135
@Test // DATAMONGO-1682
138136
public void shouldApplyPartialFilterCorrectly() {
139137

140-
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO), is(true));
138+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
141139

142140
IndexDefinition id = new Index().named("partial-with-criteria").on("k3y", Direction.ASC)
143141
.partial(of(where("q-t-y").gte(10)));
@@ -148,13 +146,13 @@ public void shouldApplyPartialFilterCorrectly() {
148146
.consumeNextWith(indexInfo -> {
149147
assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"q-t-y\" : { \"$gte\" : 10 } }");
150148
}) //
151-
.thenAwait();
149+
.verifyComplete();
152150
}
153151

154152
@Test // DATAMONGO-1682
155153
public void shouldApplyPartialFilterWithMappedPropertyCorrectly() {
156154

157-
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO), is(true));
155+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
158156

159157
IndexDefinition id = new Index().named("partial-with-mapped-criteria").on("k3y", Direction.ASC)
160158
.partial(of(where("quantity").gte(10)));
@@ -164,13 +162,13 @@ public void shouldApplyPartialFilterWithMappedPropertyCorrectly() {
164162
StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("partial-with-mapped-criteria"))) //
165163
.consumeNextWith(indexInfo -> {
166164
assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"qty\" : { \"$gte\" : 10 } }");
167-
}).thenAwait();
165+
}).verifyComplete();
168166
}
169167

170168
@Test // DATAMONGO-1682
171169
public void shouldApplyPartialDBOFilterCorrectly() {
172170

173-
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO), is(true));
171+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
174172

175173
IndexDefinition id = new Index().named("partial-with-dbo").on("k3y", Direction.ASC)
176174
.partial(of(new org.bson.Document("qty", new org.bson.Document("$gte", 10))));
@@ -181,14 +179,14 @@ public void shouldApplyPartialDBOFilterCorrectly() {
181179
.consumeNextWith(indexInfo -> {
182180
assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"qty\" : { \"$gte\" : 10 } }");
183181
}) //
184-
.thenAwait();
182+
.verifyComplete();
185183

186184
}
187185

188186
@Test // DATAMONGO-1682
189187
public void shouldFavorExplicitMappingHintViaClass() {
190188

191-
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO), is(true));
189+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
192190

193191
IndexDefinition id = new Index().named("partial-with-inheritance").on("k3y", Direction.ASC)
194192
.partial(of(where("age").gte(10)));

0 commit comments

Comments
 (0)