Polishing.
Add since tags. Make sure IndexInfo holds hidden information. Move and add tests. Original Pull Request: #4349
This commit is contained in:
@@ -105,6 +105,7 @@ public class Index implements IndexDefinition {
|
||||
* @return this.
|
||||
* @see <a href=
|
||||
* "https://www.mongodb.com/docs/manual/core/index-hidden/">https://www.mongodb.com/docs/manual/core/index-hidden/</a>
|
||||
* @since 4.1
|
||||
*/
|
||||
public Index hidden() {
|
||||
this.hidden = true;
|
||||
|
||||
@@ -129,14 +129,15 @@ public class IndexInfo {
|
||||
|
||||
String name = sourceDocument.get("name").toString();
|
||||
|
||||
boolean unique = sourceDocument.containsKey("unique") ? (Boolean) sourceDocument.get("unique") : false;
|
||||
boolean sparse = sourceDocument.containsKey("sparse") ? (Boolean) sourceDocument.get("sparse") : false;
|
||||
String language = sourceDocument.containsKey("default_language") ? (String) sourceDocument.get("default_language")
|
||||
boolean unique = sourceDocument.get("unique", false);
|
||||
boolean sparse = sourceDocument.get("sparse", false);
|
||||
boolean hidden = sourceDocument.getBoolean("hidden", false);
|
||||
String language = sourceDocument.containsKey("default_language") ? sourceDocument.getString("default_language")
|
||||
: "";
|
||||
|
||||
String partialFilter = extractPartialFilterString(sourceDocument);
|
||||
|
||||
IndexInfo info = new IndexInfo(indexFields, name, unique, sparse, language);
|
||||
IndexInfo info = new IndexInfo(indexFields, name, unique, sparse, language, hidden);
|
||||
info.partialFilterExpression = partialFilter;
|
||||
info.collation = sourceDocument.get("collation", Document.class);
|
||||
|
||||
@@ -150,8 +151,6 @@ public class IndexInfo {
|
||||
info.wildcardProjection = sourceDocument.get("wildcardProjection", Document.class);
|
||||
}
|
||||
|
||||
boolean hidden = sourceDocument.containsKey("hidden") ? (Boolean) sourceDocument.get("hidden") : false;
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
|
||||
@@ -173,6 +173,30 @@ public class DefaultIndexOperationsIntegrationTests {
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test // GH-4348
|
||||
void indexShouldNotBeHiddenByDefault() {
|
||||
|
||||
IndexDefinition index = new Index().named("my-index").on("a", Direction.ASC);
|
||||
|
||||
indexOps = new DefaultIndexOperations(template, COLLECTION_NAME, MappingToSameCollection.class);
|
||||
indexOps.ensureIndex(index);
|
||||
|
||||
IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "my-index");
|
||||
assertThat(info.isHidden()).isFalse();
|
||||
}
|
||||
|
||||
@Test // GH-4348
|
||||
void shouldCreateHiddenIndex() {
|
||||
|
||||
IndexDefinition index = new Index().named("my-hidden-index").on("a", Direction.ASC).hidden();
|
||||
|
||||
indexOps = new DefaultIndexOperations(template, COLLECTION_NAME, MappingToSameCollection.class);
|
||||
indexOps.ensureIndex(index);
|
||||
|
||||
IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "my-hidden-index");
|
||||
assertThat(info.isHidden()).isTrue();
|
||||
}
|
||||
|
||||
private IndexInfo findAndReturnIndexInfo(org.bson.Document keys) {
|
||||
return findAndReturnIndexInfo(indexOps.getIndexInfo(), keys);
|
||||
}
|
||||
|
||||
@@ -164,6 +164,34 @@ public class DefaultReactiveIndexOperationsTests {
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // GH-4348
|
||||
void indexShouldNotBeHiddenByDefault() {
|
||||
|
||||
IndexDefinition index = new Index().named("my-index").on("a", Direction.ASC);
|
||||
|
||||
indexOps.ensureIndex(index).then().as(StepVerifier::create).verifyComplete();
|
||||
|
||||
indexOps.getIndexInfo().filter(this.indexByName("my-index")).as(StepVerifier::create) //
|
||||
.consumeNextWith(indexInfo -> {
|
||||
assertThat(indexInfo.isHidden()).isFalse();
|
||||
}) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // GH-4348
|
||||
void shouldCreateHiddenIndex() {
|
||||
|
||||
IndexDefinition index = new Index().named("my-hidden-index").on("a", Direction.ASC).hidden();
|
||||
|
||||
indexOps.ensureIndex(index).then().as(StepVerifier::create).verifyComplete();
|
||||
|
||||
indexOps.getIndexInfo().filter(this.indexByName("my-hidden-index")).as(StepVerifier::create) //
|
||||
.consumeNextWith(indexInfo -> {
|
||||
assertThat(indexInfo.isHidden()).isTrue();
|
||||
}) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
Predicate<IndexInfo> indexByName(String name) {
|
||||
return indexInfo -> indexInfo.getName().equals(name);
|
||||
}
|
||||
|
||||
@@ -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().hidden());
|
||||
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
|
||||
|
||||
MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
|
||||
List<org.bson.Document> indexInfo = new ArrayList<>();
|
||||
@@ -355,7 +355,6 @@ 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);
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.data.domain.Sort.Direction;
|
||||
* @author Christoph Strobl
|
||||
* @author Stefan Tirea
|
||||
*/
|
||||
public class IndexInfoUnitTests {
|
||||
class IndexInfoUnitTests {
|
||||
|
||||
static final String ID_INDEX = "{ \"v\" : 2, \"key\" : { \"_id\" : 1 }, \"name\" : \"_id_\", \"ns\" : \"db.collection\" }";
|
||||
static final String INDEX_WITH_PARTIAL_FILTER = "{ \"v\" : 2, \"key\" : { \"k3y\" : 1 }, \"name\" : \"partial-filter-index\", \"ns\" : \"db.collection\", \"partialFilterExpression\" : { \"quantity\" : { \"$gte\" : 10 } } }";
|
||||
@@ -39,9 +39,19 @@ public class IndexInfoUnitTests {
|
||||
static final String HASHED_INDEX = "{ \"v\" : 2, \"key\" : { \"score\" : \"hashed\" }, \"name\" : \"score_hashed\", \"ns\" : \"db.collection\" }";
|
||||
static final String WILDCARD_INDEX = "{ \"v\" : 2, \"key\" : { \"$**\" : 1 }, \"name\" : \"$**_1\", \"wildcardProjection\" : { \"fieldA\" : 0, \"fieldB.fieldC\" : 0 } }";
|
||||
static final String INDEX_WITH_COLLATION = "{ \"v\" : 2, \"key\" : { \"_id\" : 1 }, \"name\" : \"projectName\", \"collation\": { \"locale\": \"en_US\", \"strength\": 2 } }";
|
||||
static final String HIDDEN_INDEX = """
|
||||
{
|
||||
"v" : 2,
|
||||
"key" : {
|
||||
"borough" : 1
|
||||
},
|
||||
"name" : "borough_1",
|
||||
"hidden" : true
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
public void isIndexForFieldsCorrectly() {
|
||||
void isIndexForFieldsCorrectly() {
|
||||
|
||||
IndexField fooField = IndexField.create("foo", Direction.ASC);
|
||||
IndexField barField = IndexField.create("bar", Direction.DESC);
|
||||
@@ -51,29 +61,29 @@ public class IndexInfoUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2170
|
||||
public void partialFilterExpressionShouldBeNullIfNotSetInSource() {
|
||||
void partialFilterExpressionShouldBeNullIfNotSetInSource() {
|
||||
assertThat(getIndexInfo(ID_INDEX).getPartialFilterExpression()).isNull();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2170
|
||||
public void partialFilterExpressionShouldMatchSource() {
|
||||
void partialFilterExpressionShouldMatchSource() {
|
||||
|
||||
assertThat(Document.parse(getIndexInfo(INDEX_WITH_PARTIAL_FILTER).getPartialFilterExpression()))
|
||||
.isEqualTo(Document.parse("{ \"quantity\" : { \"$gte\" : 10 } }"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2081
|
||||
public void expireAfterIsParsedCorrectly() {
|
||||
void expireAfterIsParsedCorrectly() {
|
||||
assertThat(getIndexInfo(INDEX_WITH_EXPIRATION_TIME).getExpireAfter()).contains(Duration.ofHours(1));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2081
|
||||
public void expireAfterIsEmptyIfNotSet() {
|
||||
void expireAfterIsEmptyIfNotSet() {
|
||||
assertThat(getIndexInfo(ID_INDEX).getExpireAfter()).isEmpty();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1183
|
||||
public void readsHashedIndexCorrectly() {
|
||||
void readsHashedIndexCorrectly() {
|
||||
assertThat(getIndexInfo(HASHED_INDEX).getIndexFields()).containsExactly(IndexField.hashed("score"));
|
||||
}
|
||||
|
||||
@@ -83,22 +93,29 @@ public class IndexInfoUnitTests {
|
||||
}
|
||||
|
||||
@Test // GH-3225
|
||||
public void identifiesWildcardIndexCorrectly() {
|
||||
void identifiesWildcardIndexCorrectly() {
|
||||
assertThat(getIndexInfo(WILDCARD_INDEX).isWildcard()).isTrue();
|
||||
}
|
||||
|
||||
@Test // GH-3225
|
||||
public void readsWildcardIndexProjectionCorrectly() {
|
||||
void readsWildcardIndexProjectionCorrectly() {
|
||||
assertThat(getIndexInfo(WILDCARD_INDEX).getWildcardProjection())
|
||||
.contains(new Document("fieldA", 0).append("fieldB.fieldC", 0));
|
||||
}
|
||||
|
||||
@Test // GH-3002
|
||||
public void collationParsedCorrectly() {
|
||||
void collationParsedCorrectly() {
|
||||
assertThat(getIndexInfo(INDEX_WITH_COLLATION).getCollation())
|
||||
.contains(Document.parse("{ \"locale\": \"en_US\", \"strength\": 2 }"));
|
||||
}
|
||||
|
||||
@Test // GH-4348
|
||||
void hiddenInfoSetCorrectly() {
|
||||
|
||||
assertThat(getIndexInfo(ID_INDEX).isHidden()).isFalse();
|
||||
assertThat(getIndexInfo(HIDDEN_INDEX).isHidden()).isTrue();
|
||||
}
|
||||
|
||||
private static IndexInfo getIndexInfo(String documentJson) {
|
||||
return IndexInfo.indexInfoOf(Document.parse(documentJson));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user