Skip to content

Commit aaa1450

Browse files
张行帅christophstrobl
张行帅
authored andcommitted
Add hidden support for index.
Closes: #4348 Original Pull Request: #4349
1 parent 89c6099 commit aaa1450

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexO
119119
ops.wildcardProjection(indexOptions.get("wildcardProjection", Document.class));
120120
}
121121

122+
if (indexOptions.containsKey("hidden")) {
123+
ops = ops.hidden((Boolean) indexOptions.get("hidden"));
124+
}
125+
122126
return ops;
123127
};
124128
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class Index implements IndexDefinition {
4242
private boolean unique = false;
4343
private boolean sparse = false;
4444
private boolean background = false;
45+
private boolean hidden = false;
4546
private long expire = -1;
4647
private Optional<IndexFilter> filter = Optional.empty();
4748
private Optional<Collation> collation = Optional.empty();
@@ -98,6 +99,18 @@ public Index background() {
9899
return this;
99100
}
100101

102+
/**
103+
* Hidden indexes are not visible to the query planner and cannot be used to support a query.
104+
*
105+
* @return this.
106+
* @see <a href=
107+
* "https://www.mongodb.com/docs/manual/core/index-hidden/">https://www.mongodb.com/docs/manual/core/index-hidden/</a>
108+
*/
109+
public Index hidden() {
110+
this.hidden = true;
111+
return this;
112+
}
113+
101114
/**
102115
* Specifies TTL in seconds.
103116
*
@@ -195,6 +208,9 @@ public Document getIndexOptions() {
195208
if (background) {
196209
document.put("background", true);
197210
}
211+
if (hidden) {
212+
document.put("hidden", true);
213+
}
198214
if (expire >= 0) {
199215
document.put("expireAfterSeconds", expire);
200216
}

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class IndexInfo {
5252
private final boolean unique;
5353
private final boolean sparse;
5454
private final String language;
55+
private final boolean hidden;
5556
private @Nullable Duration expireAfter;
5657
private @Nullable String partialFilterExpression;
5758
private @Nullable Document collation;
@@ -64,6 +65,17 @@ public IndexInfo(List<IndexField> indexFields, String name, boolean unique, bool
6465
this.unique = unique;
6566
this.sparse = sparse;
6667
this.language = language;
68+
this.hidden = false;
69+
}
70+
71+
public IndexInfo(List<IndexField> indexFields, String name, boolean unique, boolean sparse, String language, boolean hidden) {
72+
73+
this.indexFields = Collections.unmodifiableList(indexFields);
74+
this.name = name;
75+
this.unique = unique;
76+
this.sparse = sparse;
77+
this.language = language;
78+
this.hidden = hidden;
6779
}
6880

6981
/**
@@ -138,6 +150,8 @@ public static IndexInfo indexInfoOf(Document sourceDocument) {
138150
info.wildcardProjection = sourceDocument.get("wildcardProjection", Document.class);
139151
}
140152

153+
boolean hidden = sourceDocument.containsKey("hidden") ? (Boolean) sourceDocument.get("hidden") : false;
154+
141155
return info;
142156
}
143157

@@ -259,12 +273,17 @@ public boolean isWildcard() {
259273
return getIndexFields().stream().anyMatch(IndexField::isWildcard);
260274
}
261275

276+
public boolean isHidden() {
277+
return hidden;
278+
}
279+
280+
262281
@Override
263282
public String toString() {
264283

265284
return "IndexInfo [indexFields=" + indexFields + ", name=" + name + ", unique=" + unique + ", sparse=" + sparse
266285
+ ", language=" + language + ", partialFilterExpression=" + partialFilterExpression + ", collation=" + collation
267-
+ ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + "]";
286+
+ ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + ", hidden=" + hidden + "]";
268287
}
269288

270289
@Override
@@ -279,6 +298,7 @@ public int hashCode() {
279298
result += 31 * ObjectUtils.nullSafeHashCode(partialFilterExpression);
280299
result += 31 * ObjectUtils.nullSafeHashCode(collation);
281300
result += 31 * ObjectUtils.nullSafeHashCode(expireAfter);
301+
result += 31 * ObjectUtils.nullSafeHashCode(hidden);
282302
return result;
283303
}
284304

@@ -326,6 +346,9 @@ public boolean equals(@Nullable Object obj) {
326346
if (!ObjectUtils.nullSafeEquals(expireAfter, other.expireAfter)) {
327347
return false;
328348
}
349+
if (hidden != other.hidden) {
350+
return false;
351+
}
329352
return true;
330353
}
331354

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

+2-1
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());
332+
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique().hidden());
333333

334334
MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
335335
List<org.bson.Document> indexInfo = new ArrayList<>();
@@ -355,6 +355,7 @@ 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();
358359

359360
List<IndexField> indexFields = ii.getIndexFields();
360361
IndexField field = indexFields.get(0);

0 commit comments

Comments
 (0)