Add hidden support for index.

Closes: #4348
Original Pull Request: #4349
This commit is contained in:
张行帅
2023-03-28 15:53:42 +08:00
committed by Christoph Strobl
parent 89c6099a3c
commit aaa1450b2a
4 changed files with 46 additions and 2 deletions

View File

@@ -119,6 +119,10 @@ abstract class IndexConverters {
ops.wildcardProjection(indexOptions.get("wildcardProjection", Document.class));
}
if (indexOptions.containsKey("hidden")) {
ops = ops.hidden((Boolean) indexOptions.get("hidden"));
}
return ops;
};
}

View File

@@ -42,6 +42,7 @@ public class Index implements IndexDefinition {
private boolean unique = false;
private boolean sparse = false;
private boolean background = false;
private boolean hidden = false;
private long expire = -1;
private Optional<IndexFilter> filter = Optional.empty();
private Optional<Collation> collation = Optional.empty();
@@ -98,6 +99,18 @@ public class Index implements IndexDefinition {
return this;
}
/**
* Hidden indexes are not visible to the query planner and cannot be used to support a query.
*
* @return this.
* @see <a href=
* "https://www.mongodb.com/docs/manual/core/index-hidden/">https://www.mongodb.com/docs/manual/core/index-hidden/</a>
*/
public Index hidden() {
this.hidden = true;
return this;
}
/**
* Specifies TTL in seconds.
*
@@ -195,6 +208,9 @@ public class Index implements IndexDefinition {
if (background) {
document.put("background", true);
}
if (hidden) {
document.put("hidden", true);
}
if (expire >= 0) {
document.put("expireAfterSeconds", expire);
}

View File

@@ -52,6 +52,7 @@ public class IndexInfo {
private final boolean unique;
private final boolean sparse;
private final String language;
private final boolean hidden;
private @Nullable Duration expireAfter;
private @Nullable String partialFilterExpression;
private @Nullable Document collation;
@@ -64,6 +65,17 @@ public class IndexInfo {
this.unique = unique;
this.sparse = sparse;
this.language = language;
this.hidden = false;
}
public IndexInfo(List<IndexField> indexFields, String name, boolean unique, boolean sparse, String language, boolean hidden) {
this.indexFields = Collections.unmodifiableList(indexFields);
this.name = name;
this.unique = unique;
this.sparse = sparse;
this.language = language;
this.hidden = hidden;
}
/**
@@ -138,6 +150,8 @@ public class IndexInfo {
info.wildcardProjection = sourceDocument.get("wildcardProjection", Document.class);
}
boolean hidden = sourceDocument.containsKey("hidden") ? (Boolean) sourceDocument.get("hidden") : false;
return info;
}
@@ -259,12 +273,17 @@ public class IndexInfo {
return getIndexFields().stream().anyMatch(IndexField::isWildcard);
}
public boolean isHidden() {
return hidden;
}
@Override
public String toString() {
return "IndexInfo [indexFields=" + indexFields + ", name=" + name + ", unique=" + unique + ", sparse=" + sparse
+ ", language=" + language + ", partialFilterExpression=" + partialFilterExpression + ", collation=" + collation
+ ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + "]";
+ ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + ", hidden=" + hidden + "]";
}
@Override
@@ -279,6 +298,7 @@ public class IndexInfo {
result += 31 * ObjectUtils.nullSafeHashCode(partialFilterExpression);
result += 31 * ObjectUtils.nullSafeHashCode(collation);
result += 31 * ObjectUtils.nullSafeHashCode(expireAfter);
result += 31 * ObjectUtils.nullSafeHashCode(hidden);
return result;
}
@@ -326,6 +346,9 @@ public class IndexInfo {
if (!ObjectUtils.nullSafeEquals(expireAfter, other.expireAfter)) {
return false;
}
if (hidden != other.hidden) {
return false;
}
return true;
}

View File

@@ -329,7 +329,7 @@ public class MongoTemplateTests {
p2.setAge(40);
template.insert(p2);
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique().hidden());
MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
List<org.bson.Document> indexInfo = new ArrayList<>();
@@ -355,6 +355,7 @@ public class MongoTemplateTests {
IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique()).isTrue();
assertThat(ii.isSparse()).isFalse();
assertThat(ii.isHidden()).isTrue();
List<IndexField> indexFields = ii.getIndexFields();
IndexField field = indexFields.get(0);