Skip to content

Commit f0d7c8a

Browse files
committed
Polishing.
Simplify converters.
1 parent 6336278 commit f0d7c8a

3 files changed

Lines changed: 58 additions & 82 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public String createIndex(IndexDefinition indexDefinition) {
122122

123123
MongoPersistentEntity<?> entity = lookupPersistentEntity(type, collectionName);
124124

125-
IndexOptions indexOptions = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
125+
IndexOptions indexOptions = IndexConverters.toIndexOptions(indexDefinition);
126126

127127
indexOptions = addPartialFilterIfPresent(indexOptions, indexDefinition.getIndexOptions(), entity);
128128
indexOptions = addDefaultCollationIfRequired(indexOptions, entity);
@@ -201,7 +201,7 @@ private List<IndexInfo> getIndexData(MongoCursor<Document> cursor) {
201201
while (cursor.hasNext()) {
202202

203203
Document ix = cursor.next();
204-
IndexInfo indexInfo = IndexConverters.documentToIndexInfoConverter().convert(ix);
204+
IndexInfo indexInfo = IndexInfo.indexInfoOf(ix);
205205
indexInfoList.add(indexInfo);
206206
}
207207

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
@@ -90,7 +90,7 @@ public Mono<String> createIndex(IndexDefinition indexDefinition) {
9090

9191
MongoPersistentEntity<?> entity = getConfiguredEntity();
9292

93-
IndexOptions indexOptions = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
93+
IndexOptions indexOptions = IndexConverters.toIndexOptions(indexDefinition);
9494

9595
indexOptions = addPartialFilterIfPresent(indexOptions, indexDefinition.getIndexOptions(), entity);
9696
indexOptions = addDefaultCollationIfRequired(indexOptions, entity);
@@ -141,7 +141,7 @@ public Mono<Void> dropAllIndexes() {
141141
public Flux<IndexInfo> getIndexInfo() {
142142

143143
return mongoOperations.execute(collectionName, collection -> collection.listIndexes(Document.class)) //
144-
.map(IndexConverters.documentToIndexInfoConverter()::convert);
144+
.map(IndexInfo::indexInfoOf);
145145
}
146146

147147
private @Nullable MongoPersistentEntity<?> getConfiguredEntity() {

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

Lines changed: 54 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
import org.bson.Document;
2121
import org.jspecify.annotations.Nullable;
22+
2223
import org.springframework.core.convert.converter.Converter;
2324
import org.springframework.data.mongodb.core.index.IndexDefinition;
24-
import org.springframework.data.mongodb.core.index.IndexInfo;
2525
import org.springframework.util.ObjectUtils;
2626

2727
import com.mongodb.client.model.Collation;
@@ -36,92 +36,72 @@
3636
*/
3737
abstract class IndexConverters {
3838

39-
private static final Converter<IndexDefinition, IndexOptions> DEFINITION_TO_MONGO_INDEX_OPTIONS;
40-
private static final Converter<Document, IndexInfo> DOCUMENT_INDEX_INFO;
41-
42-
static {
43-
44-
DEFINITION_TO_MONGO_INDEX_OPTIONS = getIndexDefinitionIndexOptionsConverter();
45-
DOCUMENT_INDEX_INFO = getDocumentIndexInfoConverter();
46-
}
47-
4839
private IndexConverters() {
4940

5041
}
5142

52-
static Converter<IndexDefinition, IndexOptions> indexDefinitionToIndexOptionsConverter() {
53-
return DEFINITION_TO_MONGO_INDEX_OPTIONS;
54-
}
43+
static IndexOptions toIndexOptions(IndexDefinition indexDefinition) {
5544

56-
static Converter<Document, IndexInfo> documentToIndexInfoConverter() {
57-
return DOCUMENT_INDEX_INFO;
58-
}
59-
60-
private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexOptionsConverter() {
61-
62-
return indexDefinition -> {
45+
Document indexOptions = indexDefinition.getIndexOptions();
46+
IndexOptions ops = new IndexOptions();
6347

64-
Document indexOptions = indexDefinition.getIndexOptions();
65-
IndexOptions ops = new IndexOptions();
66-
67-
if (indexOptions.containsKey("name")) {
68-
ops = ops.name(indexOptions.get("name").toString());
69-
}
70-
if (indexOptions.containsKey("unique")) {
71-
ops = ops.unique((Boolean) indexOptions.get("unique"));
72-
}
73-
if (indexOptions.containsKey("sparse")) {
74-
ops = ops.sparse((Boolean) indexOptions.get("sparse"));
75-
}
76-
if (indexOptions.containsKey("background")) {
77-
ops = ops.background((Boolean) indexOptions.get("background"));
78-
}
79-
if (indexOptions.containsKey("expireAfterSeconds")) {
80-
ops = ops.expireAfter((Long) indexOptions.get("expireAfterSeconds"), TimeUnit.SECONDS);
81-
}
82-
if (indexOptions.containsKey("min")) {
83-
ops = ops.min(((Number) indexOptions.get("min")).doubleValue());
84-
}
85-
if (indexOptions.containsKey("max")) {
86-
ops = ops.max(((Number) indexOptions.get("max")).doubleValue());
87-
}
88-
if (indexOptions.containsKey("bits")) {
89-
ops = ops.bits((Integer) indexOptions.get("bits"));
90-
}
91-
if (indexOptions.containsKey("default_language")) {
92-
ops = ops.defaultLanguage(indexOptions.get("default_language").toString());
93-
}
94-
if (indexOptions.containsKey("language_override")) {
95-
ops = ops.languageOverride(indexOptions.get("language_override").toString());
96-
}
97-
if (indexOptions.containsKey("weights")) {
98-
ops = ops.weights((org.bson.Document) indexOptions.get("weights"));
99-
}
48+
if (indexOptions.containsKey("name")) {
49+
ops = ops.name(indexOptions.get("name").toString());
50+
}
51+
if (indexOptions.containsKey("unique")) {
52+
ops = ops.unique((Boolean) indexOptions.get("unique"));
53+
}
54+
if (indexOptions.containsKey("sparse")) {
55+
ops = ops.sparse((Boolean) indexOptions.get("sparse"));
56+
}
57+
if (indexOptions.containsKey("background")) {
58+
ops = ops.background((Boolean) indexOptions.get("background"));
59+
}
60+
if (indexOptions.containsKey("expireAfterSeconds")) {
61+
ops = ops.expireAfter((Long) indexOptions.get("expireAfterSeconds"), TimeUnit.SECONDS);
62+
}
63+
if (indexOptions.containsKey("min")) {
64+
ops = ops.min(((Number) indexOptions.get("min")).doubleValue());
65+
}
66+
if (indexOptions.containsKey("max")) {
67+
ops = ops.max(((Number) indexOptions.get("max")).doubleValue());
68+
}
69+
if (indexOptions.containsKey("bits")) {
70+
ops = ops.bits((Integer) indexOptions.get("bits"));
71+
}
72+
if (indexOptions.containsKey("default_language")) {
73+
ops = ops.defaultLanguage(indexOptions.get("default_language").toString());
74+
}
75+
if (indexOptions.containsKey("language_override")) {
76+
ops = ops.languageOverride(indexOptions.get("language_override").toString());
77+
}
78+
if (indexOptions.containsKey("weights")) {
79+
ops = ops.weights((org.bson.Document) indexOptions.get("weights"));
80+
}
10081

101-
for (String key : indexOptions.keySet()) {
102-
if (ObjectUtils.nullSafeEquals("2dsphere", indexOptions.get(key))) {
103-
ops = ops.sphereVersion(2);
104-
}
82+
for (String key : indexOptions.keySet()) {
83+
if (ObjectUtils.nullSafeEquals("2dsphere", indexOptions.get(key))) {
84+
ops = ops.sphereVersion(2);
10585
}
86+
}
10687

107-
if (indexOptions.containsKey("partialFilterExpression")) {
108-
ops = ops.partialFilterExpression((org.bson.Document) indexOptions.get("partialFilterExpression"));
109-
}
88+
if (indexOptions.containsKey("partialFilterExpression")) {
89+
ops = ops.partialFilterExpression((org.bson.Document) indexOptions.get("partialFilterExpression"));
90+
}
11091

111-
if (indexOptions.containsKey("collation")) {
112-
ops = ops.collation(fromDocument(indexOptions.get("collation", Document.class)));
113-
}
92+
if (indexOptions.containsKey("collation")) {
93+
ops = ops.collation(fromDocument(indexOptions.get("collation", Document.class)));
94+
}
11495

115-
if (indexOptions.containsKey("wildcardProjection")) {
116-
ops.wildcardProjection(indexOptions.get("wildcardProjection", Document.class));
117-
}
96+
if (indexOptions.containsKey("wildcardProjection")) {
97+
ops.wildcardProjection(indexOptions.get("wildcardProjection", Document.class));
98+
}
11899

119-
if (indexOptions.containsKey("hidden")) {
120-
ops = ops.hidden((Boolean) indexOptions.get("hidden"));
121-
}
100+
if (indexOptions.containsKey("hidden")) {
101+
ops = ops.hidden((Boolean) indexOptions.get("hidden"));
102+
}
122103

123-
return ops;
124-
};
104+
return ops;
125105
}
126106

127107
public static @Nullable Collation fromDocument(@Nullable Document source) {
@@ -133,8 +113,4 @@ private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexO
133113
return org.springframework.data.mongodb.core.query.Collation.from(source).toMongoCollation();
134114
}
135115

136-
private static Converter<Document, IndexInfo> getDocumentIndexInfoConverter() {
137-
return IndexInfo::indexInfoOf;
138-
}
139-
140116
}

0 commit comments

Comments
 (0)