Skip to content

Commit a7cef4e

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2112 - Polishing.
Align Indexed(expireAfter) default to empty string according to the documentation. Slightly reword Javadoc.
1 parent 7489c0f commit a7cef4e

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,29 @@
136136
int expireAfterSeconds() default -1;
137137

138138
/**
139-
* Alternative for {@link #expireAfterSeconds()} to configure the timeout after which the collection should expire.
140-
* Defaults to an empty String for no expiry. Accepts numeric values followed by their unit of measure (d(ays),
141-
* h(ours), m(inutes), s(seconds)) or a Spring {@literal template expression}. The expression can result in a a valid
142-
* expiration {@link String} following the conventions already mentioned or a {@link java.time.Duration}.
139+
* Alternative for {@link #expireAfterSeconds()} to configure the timeout after which the document should expire.
140+
* Defaults to an empty {@link String} for no expiry. Accepts numeric values followed by their unit of measure:
141+
* <ul>
142+
* <li><b>d</b>: Days</li>
143+
* <li><b>h</b>: Hours</li>
144+
* <li><b>m</b>: Minutes</li>
145+
* <li><b>s</b>: Seconds</li>
146+
* <li>Alternatively: A Spring {@literal template expression}. The expression can result in a
147+
* {@link java.time.Duration} or a valid expiration {@link String} according to the already mentioned
148+
* conventions.</li>
149+
* </ul>
143150
*
144-
* <pre>
145-
* <code>
151+
* <pre class="code">
146152
*
147-
* &#0064;Indexed(expireAfter = "10s")
148-
* String expireAfterTenSeconds;
153+
* &#0064;Indexed(expireAfter = "10s") String expireAfterTenSeconds;
149154
*
150-
* &#0064;Indexed(expireAfter = "1d")
151-
* String expireAfterOneDay;
155+
* &#0064;Indexed(expireAfter = "1d") String expireAfterOneDay;
152156
*
153-
* &#0064;Indexed(expireAfter = "#{&#0064;mySpringBean.timeout}")
154-
* String expireAfterTimeoutObtainedFromSpringBean;
155-
* </code>
157+
* &#0064;Indexed(expireAfter = "#{&#0064;mySpringBean.timeout}") String expireAfterTimeoutObtainedFromSpringBean;
156158
* </pre>
157159
*
158-
* @return {@literal 0s} by default.
160+
* @return empty by default.
159161
* @since 2.2
160162
*/
161-
String expireAfter() default "0s";
163+
String expireAfter() default "";
162164
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
38+
3839
import org.springframework.dao.InvalidDataAccessApiUsageException;
3940
import org.springframework.data.domain.Sort;
4041
import org.springframework.data.mapping.Association;
@@ -450,11 +451,11 @@ protected IndexDefinitionHolder createIndexDefinition(String dotPath, String col
450451
indexDefinition.expire(index.expireAfterSeconds(), TimeUnit.SECONDS);
451452
}
452453

453-
if (!index.expireAfter().isEmpty() && !index.expireAfter().equals("0s")) {
454+
if (StringUtils.hasText(index.expireAfter())) {
454455

455456
if (index.expireAfterSeconds() >= 0) {
456457
throw new IllegalStateException(String.format(
457-
"@Indexed already defines an expiration timeout of %s sec. via Indexed#expireAfterSeconds. Please make to use either expireAfterSeconds or expireAfter.",
458+
"@Indexed already defines an expiration timeout of %s seconds via Indexed#expireAfterSeconds. Please make to use either expireAfterSeconds or expireAfter.",
458459
index.expireAfterSeconds()));
459460
}
460461

@@ -480,7 +481,7 @@ protected EvaluationContext getEvaluationContext() {
480481

481482
/**
482483
* Get the {@link EvaluationContext} for a given {@link PersistentEntity entity} the default one.
483-
*
484+
*
484485
* @param persistentEntity can be {@literal null}
485486
* @return
486487
*/
@@ -626,7 +627,7 @@ private static Duration computeIndexTimeout(String timeoutValue, EvaluationConte
626627
Matcher matcher = TIMEOUT_PATTERN.matcher(val);
627628
if (matcher.find()) {
628629

629-
Long timeout = NumberUtils.parseNumber(matcher.group(1), Long.class);
630+
long timeout = NumberUtils.parseNumber(matcher.group(1), Long.class);
630631
String unit = matcher.group(3);
631632

632633
switch (unit) {
@@ -642,7 +643,7 @@ private static Duration computeIndexTimeout(String timeoutValue, EvaluationConte
642643
}
643644

644645
throw new IllegalArgumentException(
645-
String.format("Index timeout %s cannot be parsed. Please use the following pattern '\\d+\\W?[dhms]'.", val));
646+
String.format("Index timeout %s cannot be parsed. Please use the pattern '\\d+\\W?[dhms]'.", val));
646647
}
647648

648649
@Nullable

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.runner.RunWith;
3333
import org.junit.runners.Suite;
3434
import org.junit.runners.Suite.SuiteClasses;
35+
3536
import org.springframework.core.annotation.AliasFor;
3637
import org.springframework.data.annotation.Id;
3738
import org.springframework.data.geo.Point;
@@ -237,7 +238,6 @@ public void shouldErrorOnInvalidTimeoutExpression() {
237238

238239
Assertions.assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> indexResolver
239240
.resolveIndexForEntity(mappingContext.getRequiredPersistentEntity(WithInvalidExpireAfter.class)));
240-
241241
}
242242

243243
@Test // DATAMONGO-2112
@@ -384,7 +384,6 @@ static class WithIndexNameAsExpression {
384384
@Retention(RetentionPolicy.RUNTIME)
385385
@Indexed
386386
@interface IndexedFieldAnnotation {
387-
388387
}
389388

390389
@Document
@@ -660,22 +659,16 @@ static class CompoundIndexOnLevelZeroWithEmptyIndexDef {}
660659
unique = true)
661660
static class SingleCompoundIndex {}
662661

663-
static class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {
664-
665-
}
662+
static class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {}
666663

667664
@Document("ComountIndexWithAutogeneratedName")
668665
@CompoundIndexes({ @CompoundIndex(useGeneratedName = true, def = "{'foo': 1, 'bar': -1}", background = true,
669666
sparse = true, unique = true) })
670-
static class ComountIndexWithAutogeneratedName {
671-
672-
}
667+
static class ComountIndexWithAutogeneratedName {}
673668

674669
@Document("WithComposedAnnotation")
675670
@ComposedCompoundIndex
676-
static class CompoundIndexDocumentWithComposedAnnotation {
677-
678-
}
671+
static class CompoundIndexDocumentWithComposedAnnotation {}
679672

680673
@Retention(RetentionPolicy.RUNTIME)
681674
@Target({ ElementType.TYPE })

0 commit comments

Comments
 (0)