Skip to content

Commit c3a5454

Browse files
committed
DATAMONGO-2267 - Polishing.
Reuse collection name for index creation instead of resolving the collection for every index. Switch lambda to method reference. Original pull request: #746.
1 parent 207c82a commit c3a5454

File tree

5 files changed

+24
-25
lines changed

5 files changed

+24
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ObjectPath push(Object object, MongoPersistentEntity<?> entity, @Nullable Object
8787
Assert.notNull(object, "Object must not be null!");
8888
Assert.notNull(entity, "MongoPersistentEntity must not be null!");
8989

90-
return new ObjectPath(this, object, id, Lazy.of(() -> entity.getCollection()));
90+
return new ObjectPath(this, object, id, Lazy.of(entity::getCollection));
9191
}
9292

9393
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,18 @@ private void checkForIndexes(final MongoPersistentEntity<?> entity) {
134134
private void checkForAndCreateIndexes(MongoPersistentEntity<?> entity) {
135135

136136
if (entity.isAnnotationPresent(Document.class)) {
137+
138+
String collection = entity.getCollection();
139+
137140
for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {
138141

139142
JustOnceLogger.logWarnIndexCreationConfigurationChange(this.getClass().getName());
140143

141144
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder
142145
? (IndexDefinitionHolder) indexDefinition
143-
: new IndexDefinitionHolder("", indexDefinition, entity.getCollection());
146+
: new IndexDefinitionHolder("", indexDefinition, collection);
144147

145148
createIndex(indexToCreate);
146-
147149
}
148150
}
149151
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,15 @@ public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEn
118118
Document document = root.findAnnotation(Document.class);
119119
Assert.notNull(document, "Given entity is not collection root.");
120120

121-
final List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
122-
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root));
123-
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root));
121+
List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
122+
String collection = root.getCollection();
123+
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", collection, root));
124+
indexInformation.addAll(potentiallyCreateTextIndexDefinition(root, collection));
124125

125126
root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this
126127
.potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard()));
127128

128-
indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root));
129+
indexInformation.addAll(resolveIndexesForDbrefs("", collection, root));
129130

130131
return indexInformation;
131132
}
@@ -225,7 +226,7 @@ private List<IndexDefinitionHolder> potentiallyCreateCompoundIndexDefinitions(St
225226
}
226227

227228
private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDefinition(
228-
MongoPersistentEntity<?> root) {
229+
MongoPersistentEntity<?> root, String collection) {
229230

230231
String name = root.getType().getSimpleName() + "_TextIndex";
231232
if (name.getBytes().length > 127) {
@@ -261,7 +262,7 @@ private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDe
261262
return Collections.emptyList();
262263
}
263264

264-
IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, root.getCollection());
265+
IndexDefinitionHolder holder = new IndexDefinitionHolder("", indexDefinition, collection);
265266
return Collections.singletonList(holder);
266267

267268
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,13 @@ private Mono<Void> checkForAndCreateIndexes(MongoPersistentEntity<?> entity) {
125125
List<Mono<?>> publishers = new ArrayList<>();
126126

127127
if (entity.isAnnotationPresent(Document.class)) {
128+
129+
String collection = entity.getCollection();
128130
for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {
129131

130132
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder
131133
? (IndexDefinitionHolder) indexDefinition
132-
: new IndexDefinitionHolder("", indexDefinition, entity.getCollection());
134+
: new IndexDefinitionHolder("", indexDefinition, collection);
133135

134136
publishers.add(createIndex(indexToCreate));
135137
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.springframework.data.util.ClassTypeInformation;
2727

2828
/**
29+
* Unit tests for {@link ObjectPath}.
30+
*
2931
* @author Christoph Strobl
3032
*/
3133
public class ObjectPathUnitTests {
@@ -37,9 +39,9 @@ public class ObjectPathUnitTests {
3739
@Before
3840
public void setUp() {
3941

40-
one = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityOne.class));
41-
two = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityTwo.class));
42-
three = new BasicMongoPersistentEntity(ClassTypeInformation.from(EntityThree.class));
42+
one = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityOne.class));
43+
two = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityTwo.class));
44+
three = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(EntityThree.class));
4345
}
4446

4547
@Test // DATAMONGO-1703
@@ -96,20 +98,12 @@ public void getPathItemShouldReturnNullWhenIdAndCollectionMatchAndAssignableToIn
9698
}
9799

98100
@Document("one")
99-
static class EntityOne {
100-
101-
}
101+
static class EntityOne {}
102102

103-
static class EntityTwo extends EntityOne {
104-
105-
}
103+
static class EntityTwo extends EntityOne {}
106104

107-
interface ValueInterface {
108-
109-
}
105+
interface ValueInterface {}
110106

111107
@Document("three")
112-
static class EntityThree implements ValueInterface {
113-
114-
}
108+
static class EntityThree implements ValueInterface {}
115109
}

0 commit comments

Comments
 (0)