DATAMONGO-2081 - Expose expireAfterSeconds via IndexInfo.

Original pull request: #749.
This commit is contained in:
Christoph Strobl
2019-05-15 13:44:05 +02:00
committed by Mark Paluch
parent 8d2a68118b
commit 24f23c5365
2 changed files with 38 additions and 3 deletions

View File

@@ -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<Duration> 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;

View File

@@ -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));
}