From 24f23c5365a405bd805ddb7289011dd1ada1a763 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 15 May 2019 13:44:05 +0200 Subject: [PATCH] DATAMONGO-2081 - Expose expireAfterSeconds via IndexInfo. Original pull request: #749. --- .../data/mongodb/core/index/IndexInfo.java | 29 +++++++++++++++++-- .../core/index/IndexInfoUnitTests.java | 12 ++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java index f58435548..8f05f6e5d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexInfo.java @@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.index; import static org.springframework.data.domain.Sort.Direction.*; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -27,6 +28,7 @@ import java.util.Optional; import org.bson.Document; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.NumberUtils; import org.springframework.util.ObjectUtils; /** @@ -47,6 +49,7 @@ public class IndexInfo { private final boolean unique; private final boolean sparse; private final String language; + private @Nullable Duration expireAfter; private @Nullable String partialFilterExpression; private @Nullable Document collation; @@ -108,11 +111,15 @@ public class IndexInfo { String language = sourceDocument.containsKey("default_language") ? (String) sourceDocument.get("default_language") : ""; String partialFilter = sourceDocument.containsKey("partialFilterExpression") - ? ((Document) sourceDocument.get("partialFilterExpression")).toJson() : null; + ? ((Document) sourceDocument.get("partialFilterExpression")).toJson() + : null; IndexInfo info = new IndexInfo(indexFields, name, unique, sparse, language); info.partialFilterExpression = partialFilter; info.collation = sourceDocument.get("collation", Document.class); + info.expireAfter = !sourceDocument.containsKey("expireAfterSeconds") ? null + : Duration.ofSeconds( + NumberUtils.convertNumberToTargetClass(sourceDocument.get("expireAfterSeconds", Number.class), Long.class)); return info; } @@ -183,11 +190,22 @@ public class IndexInfo { return Optional.ofNullable(collation); } + /** + * Get the duration after which documents within the index expire. + * + * @return the expiration time if set, {@link Optional#empty()} otherwise. + * @since 2.2 + */ + public Optional getExpireAfter() { + return Optional.ofNullable(expireAfter); + } + @Override public String toString() { + return "IndexInfo [indexFields=" + indexFields + ", name=" + name + ", unique=" + unique + ", sparse=" + sparse + ", language=" + language + ", partialFilterExpression=" + partialFilterExpression + ", collation=" + collation - + "]"; + + ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + "]"; } @Override @@ -201,6 +219,7 @@ public class IndexInfo { result += 31 * ObjectUtils.nullSafeHashCode(language); result += 31 * ObjectUtils.nullSafeHashCode(partialFilterExpression); result += 31 * ObjectUtils.nullSafeHashCode(collation); + result += 31 * ObjectUtils.nullSafeHashCode(expireAfter); return result; } @@ -243,7 +262,11 @@ public class IndexInfo { return false; } - if (!ObjectUtils.nullSafeEquals(collation, collation)) { + if (!ObjectUtils.nullSafeEquals(collation, other.collation)) { + return false; + } + + if (!ObjectUtils.nullSafeEquals(expireAfter, other.expireAfter)) { return false; } return true; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java index 65b6f5427..01c56fca6 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java @@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.index; import static org.assertj.core.api.Assertions.*; +import java.time.Duration; import java.util.Arrays; import org.bson.Document; @@ -33,6 +34,7 @@ public 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 } } }"; + static final String INDEX_WITH_EXPIRATION_TIME = "{ \"v\" : 2, \"key\" : { \"lastModifiedDate\" : 1 },\"name\" : \"expire-after-last-modified\", \"ns\" : \"db.collectio\", \"expireAfterSeconds\" : 3600 }"; @Test public void isIndexForFieldsCorrectly() { @@ -56,6 +58,16 @@ public class IndexInfoUnitTests { .isEqualTo(Document.parse("{ \"quantity\" : { \"$gte\" : 10 } }")); } + @Test // DATAMONGO-2081 + public void expireAfterIsParsedCorrectly() { + assertThat(getIndexInfo(INDEX_WITH_EXPIRATION_TIME).getExpireAfter()).contains(Duration.ofSeconds(3600)); + } + + @Test // DATAMONGO-2081 + public void expireAfterIsEmptyIfNotSet() { + assertThat(getIndexInfo(ID_INDEX).getExpireAfter()).isEmpty(); + } + private static IndexInfo getIndexInfo(String documentJson) { return IndexInfo.indexInfoOf(Document.parse(documentJson)); }