Skip to content

Commit 83217f3

Browse files
Polishing.
Add since tags. Make sure IndexInfo holds hidden information. Move and add tests. Original Pull Request: #4349
1 parent aaa1450 commit 83217f3

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public Index background() {
105105
* @return this.
106106
* @see <a href=
107107
* "https://www.mongodb.com/docs/manual/core/index-hidden/">https://www.mongodb.com/docs/manual/core/index-hidden/</a>
108+
* @since 4.1
108109
*/
109110
public Index hidden() {
110111
this.hidden = true;

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,15 @@ public static IndexInfo indexInfoOf(Document sourceDocument) {
129129

130130
String name = sourceDocument.get("name").toString();
131131

132-
boolean unique = sourceDocument.containsKey("unique") ? (Boolean) sourceDocument.get("unique") : false;
133-
boolean sparse = sourceDocument.containsKey("sparse") ? (Boolean) sourceDocument.get("sparse") : false;
134-
String language = sourceDocument.containsKey("default_language") ? (String) sourceDocument.get("default_language")
132+
boolean unique = sourceDocument.get("unique", false);
133+
boolean sparse = sourceDocument.get("sparse", false);
134+
boolean hidden = sourceDocument.getBoolean("hidden", false);
135+
String language = sourceDocument.containsKey("default_language") ? sourceDocument.getString("default_language")
135136
: "";
136137

137138
String partialFilter = extractPartialFilterString(sourceDocument);
138139

139-
IndexInfo info = new IndexInfo(indexFields, name, unique, sparse, language);
140+
IndexInfo info = new IndexInfo(indexFields, name, unique, sparse, language, hidden);
140141
info.partialFilterExpression = partialFilter;
141142
info.collation = sourceDocument.get("collation", Document.class);
142143

@@ -150,8 +151,6 @@ public static IndexInfo indexInfoOf(Document sourceDocument) {
150151
info.wildcardProjection = sourceDocument.get("wildcardProjection", Document.class);
151152
}
152153

153-
boolean hidden = sourceDocument.containsKey("hidden") ? (Boolean) sourceDocument.get("hidden") : false;
154-
155154
return info;
156155
}
157156

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ public void shouldCreateIndexWithCollationCorrectly() {
173173
assertThat(result).isEqualTo(expected);
174174
}
175175

176+
@Test // GH-4348
177+
void indexShouldNotBeHiddenByDefault() {
178+
179+
IndexDefinition index = new Index().named("my-index").on("a", Direction.ASC);
180+
181+
indexOps = new DefaultIndexOperations(template, COLLECTION_NAME, MappingToSameCollection.class);
182+
indexOps.ensureIndex(index);
183+
184+
IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "my-index");
185+
assertThat(info.isHidden()).isFalse();
186+
}
187+
188+
@Test // GH-4348
189+
void shouldCreateHiddenIndex() {
190+
191+
IndexDefinition index = new Index().named("my-hidden-index").on("a", Direction.ASC).hidden();
192+
193+
indexOps = new DefaultIndexOperations(template, COLLECTION_NAME, MappingToSameCollection.class);
194+
indexOps.ensureIndex(index);
195+
196+
IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "my-hidden-index");
197+
assertThat(info.isHidden()).isTrue();
198+
}
199+
176200
private IndexInfo findAndReturnIndexInfo(org.bson.Document keys) {
177201
return findAndReturnIndexInfo(indexOps.getIndexInfo(), keys);
178202
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,34 @@ public void shouldFavorExplicitMappingHintViaClass() {
164164
.verifyComplete();
165165
}
166166

167+
@Test // GH-4348
168+
void indexShouldNotBeHiddenByDefault() {
169+
170+
IndexDefinition index = new Index().named("my-index").on("a", Direction.ASC);
171+
172+
indexOps.ensureIndex(index).then().as(StepVerifier::create).verifyComplete();
173+
174+
indexOps.getIndexInfo().filter(this.indexByName("my-index")).as(StepVerifier::create) //
175+
.consumeNextWith(indexInfo -> {
176+
assertThat(indexInfo.isHidden()).isFalse();
177+
}) //
178+
.verifyComplete();
179+
}
180+
181+
@Test // GH-4348
182+
void shouldCreateHiddenIndex() {
183+
184+
IndexDefinition index = new Index().named("my-hidden-index").on("a", Direction.ASC).hidden();
185+
186+
indexOps.ensureIndex(index).then().as(StepVerifier::create).verifyComplete();
187+
188+
indexOps.getIndexInfo().filter(this.indexByName("my-hidden-index")).as(StepVerifier::create) //
189+
.consumeNextWith(indexInfo -> {
190+
assertThat(indexInfo.isHidden()).isTrue();
191+
}) //
192+
.verifyComplete();
193+
}
194+
167195
Predicate<IndexInfo> indexByName(String name) {
168196
return indexInfo -> indexInfo.getName().equals(name);
169197
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public void testEnsureIndex() throws Exception {
329329
p2.setAge(40);
330330
template.insert(p2);
331331

332-
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique().hidden());
332+
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
333333

334334
MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
335335
List<org.bson.Document> indexInfo = new ArrayList<>();
@@ -355,7 +355,6 @@ public void testEnsureIndex() throws Exception {
355355
IndexInfo ii = indexInfoList.get(1);
356356
assertThat(ii.isUnique()).isTrue();
357357
assertThat(ii.isSparse()).isFalse();
358-
assertThat(ii.isHidden()).isTrue();
359358

360359
List<IndexField> indexFields = ii.getIndexFields();
361360
IndexField field = indexFields.get(0);

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,27 @@
3131
* @author Christoph Strobl
3232
* @author Stefan Tirea
3333
*/
34-
public class IndexInfoUnitTests {
34+
class IndexInfoUnitTests {
3535

3636
static final String ID_INDEX = "{ \"v\" : 2, \"key\" : { \"_id\" : 1 }, \"name\" : \"_id_\", \"ns\" : \"db.collection\" }";
3737
static final String INDEX_WITH_PARTIAL_FILTER = "{ \"v\" : 2, \"key\" : { \"k3y\" : 1 }, \"name\" : \"partial-filter-index\", \"ns\" : \"db.collection\", \"partialFilterExpression\" : { \"quantity\" : { \"$gte\" : 10 } } }";
3838
static final String INDEX_WITH_EXPIRATION_TIME = "{ \"v\" : 2, \"key\" : { \"lastModifiedDate\" : 1 },\"name\" : \"expire-after-last-modified\", \"ns\" : \"db.collectio\", \"expireAfterSeconds\" : 3600 }";
3939
static final String HASHED_INDEX = "{ \"v\" : 2, \"key\" : { \"score\" : \"hashed\" }, \"name\" : \"score_hashed\", \"ns\" : \"db.collection\" }";
4040
static final String WILDCARD_INDEX = "{ \"v\" : 2, \"key\" : { \"$**\" : 1 }, \"name\" : \"$**_1\", \"wildcardProjection\" : { \"fieldA\" : 0, \"fieldB.fieldC\" : 0 } }";
4141
static final String INDEX_WITH_COLLATION = "{ \"v\" : 2, \"key\" : { \"_id\" : 1 }, \"name\" : \"projectName\", \"collation\": { \"locale\": \"en_US\", \"strength\": 2 } }";
42+
static final String HIDDEN_INDEX = """
43+
{
44+
"v" : 2,
45+
"key" : {
46+
"borough" : 1
47+
},
48+
"name" : "borough_1",
49+
"hidden" : true
50+
}
51+
""";
4252

4353
@Test
44-
public void isIndexForFieldsCorrectly() {
54+
void isIndexForFieldsCorrectly() {
4555

4656
IndexField fooField = IndexField.create("foo", Direction.ASC);
4757
IndexField barField = IndexField.create("bar", Direction.DESC);
@@ -51,29 +61,29 @@ public void isIndexForFieldsCorrectly() {
5161
}
5262

5363
@Test // DATAMONGO-2170
54-
public void partialFilterExpressionShouldBeNullIfNotSetInSource() {
64+
void partialFilterExpressionShouldBeNullIfNotSetInSource() {
5565
assertThat(getIndexInfo(ID_INDEX).getPartialFilterExpression()).isNull();
5666
}
5767

5868
@Test // DATAMONGO-2170
59-
public void partialFilterExpressionShouldMatchSource() {
69+
void partialFilterExpressionShouldMatchSource() {
6070

6171
assertThat(Document.parse(getIndexInfo(INDEX_WITH_PARTIAL_FILTER).getPartialFilterExpression()))
6272
.isEqualTo(Document.parse("{ \"quantity\" : { \"$gte\" : 10 } }"));
6373
}
6474

6575
@Test // DATAMONGO-2081
66-
public void expireAfterIsParsedCorrectly() {
76+
void expireAfterIsParsedCorrectly() {
6777
assertThat(getIndexInfo(INDEX_WITH_EXPIRATION_TIME).getExpireAfter()).contains(Duration.ofHours(1));
6878
}
6979

7080
@Test // DATAMONGO-2081
71-
public void expireAfterIsEmptyIfNotSet() {
81+
void expireAfterIsEmptyIfNotSet() {
7282
assertThat(getIndexInfo(ID_INDEX).getExpireAfter()).isEmpty();
7383
}
7484

7585
@Test // DATAMONGO-1183
76-
public void readsHashedIndexCorrectly() {
86+
void readsHashedIndexCorrectly() {
7787
assertThat(getIndexInfo(HASHED_INDEX).getIndexFields()).containsExactly(IndexField.hashed("score"));
7888
}
7989

@@ -83,22 +93,29 @@ public void hashedIndexIsMarkedAsSuch() {
8393
}
8494

8595
@Test // GH-3225
86-
public void identifiesWildcardIndexCorrectly() {
96+
void identifiesWildcardIndexCorrectly() {
8797
assertThat(getIndexInfo(WILDCARD_INDEX).isWildcard()).isTrue();
8898
}
8999

90100
@Test // GH-3225
91-
public void readsWildcardIndexProjectionCorrectly() {
101+
void readsWildcardIndexProjectionCorrectly() {
92102
assertThat(getIndexInfo(WILDCARD_INDEX).getWildcardProjection())
93103
.contains(new Document("fieldA", 0).append("fieldB.fieldC", 0));
94104
}
95105

96106
@Test // GH-3002
97-
public void collationParsedCorrectly() {
107+
void collationParsedCorrectly() {
98108
assertThat(getIndexInfo(INDEX_WITH_COLLATION).getCollation())
99109
.contains(Document.parse("{ \"locale\": \"en_US\", \"strength\": 2 }"));
100110
}
101111

112+
@Test // GH-4348
113+
void hiddenInfoSetCorrectly() {
114+
115+
assertThat(getIndexInfo(ID_INDEX).isHidden()).isFalse();
116+
assertThat(getIndexInfo(HIDDEN_INDEX).isHidden()).isTrue();
117+
}
118+
102119
private static IndexInfo getIndexInfo(String documentJson) {
103120
return IndexInfo.indexInfoOf(Document.parse(documentJson));
104121
}

0 commit comments

Comments
 (0)