Skip to content

Commit 7800cf7

Browse files
committed
Take expiry from entity class annotation for getAndTouch. (#1640)
Closes #1634.
1 parent 3cba5d9 commit 7800cf7

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ static class ReactiveFindByIdSupport<T> implements ReactiveFindById<T> {
6363
private final ReactiveTemplateSupport support;
6464
private final Duration expiry;
6565

66+
private Duration expiryToUse;
67+
6668
ReactiveFindByIdSupport(ReactiveCouchbaseTemplate template, Class<T> domainType, String scope, String collection,
6769
CommonOptions<?> options, List<String> fields, Duration expiry, ReactiveTemplateSupport support) {
6870
this.template = template;
@@ -86,7 +88,7 @@ public Mono<T> one(final String id) {
8688
ReactiveCollection reactive = template.getCouchbaseClientFactory().withScope(pArgs.getScope())
8789
.getCollection(pArgs.getCollection()).reactive();
8890
if (pArgs.getOptions() instanceof GetAndTouchOptions) {
89-
return reactive.getAndTouch(docId, expiryToUse(), (GetAndTouchOptions) pArgs.getOptions());
91+
return reactive.getAndTouch(docId, expiryToUse, (GetAndTouchOptions) pArgs.getOptions());
9092
} else {
9193
return reactive.get(docId, (GetOptions) pArgs.getOptions());
9294
}
@@ -142,7 +144,18 @@ public FindByIdWithProjection<T> withExpiry(final Duration expiry) {
142144

143145
private CommonOptions<?> initGetOptions() {
144146
CommonOptions<?> getOptions;
145-
if (expiry != null || options instanceof GetAndTouchOptions) {
147+
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
148+
.getRequiredPersistentEntity(domainType);
149+
Duration entityExpiryAnnotation = entity.getExpiryDuration();
150+
if (expiry != null || entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()
151+
|| options instanceof GetAndTouchOptions) {
152+
if (expiry != null) {
153+
expiryToUse = expiry;
154+
} else if (entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()) {
155+
expiryToUse = entityExpiryAnnotation;
156+
} else {
157+
expiryToUse = Duration.ZERO;
158+
}
146159
GetAndTouchOptions gOptions = options != null ? (GetAndTouchOptions) options : getAndTouchOptions();
147160
if (gOptions.build().transcoder() == null) {
148161
gOptions.transcoder(RawJsonTranscoder.INSTANCE);
@@ -160,18 +173,7 @@ private CommonOptions<?> initGetOptions() {
160173
}
161174
return getOptions;
162175
}
163-
164-
private Duration expiryToUse() {
165-
Duration expiryToUse = expiry;
166-
if (expiryToUse != null || options instanceof GetAndTouchOptions) {
167-
if (expiryToUse == null) { // GetAndTouchOptions without specifying expiry -> get expiry from annoation
168-
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
169-
.getRequiredPersistentEntity(domainType);
170-
expiryToUse = entity.getExpiryDuration();
171-
}
172-
}
173-
return expiryToUse;
174-
}
176+
175177
}
176178

177179
}

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,43 @@ void findByIdWithExpiry() {
112112

113113
}
114114

115+
@Test
116+
void findByIdWithExpiryAnnotation() {
117+
try {
118+
UserAnnotated user1 = new UserAnnotated(UUID.randomUUID().toString(), "user1", "user1");
119+
UserAnnotated user2 = new UserAnnotated(UUID.randomUUID().toString(), "user2", "user2");
120+
121+
Collection<UserAnnotated> upserts = (Collection<UserAnnotated>) couchbaseTemplate.upsertById(UserAnnotated.class)
122+
.all(Arrays.asList(user1, user2));
123+
124+
// explicitly set expiry to 10 seconds
125+
UserAnnotated foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).withExpiry(Duration.ofSeconds(10)).one(user1.getId());
126+
user1.setVersion(foundUser1.getVersion());// version will have changed
127+
assertEquals(user1, foundUser1);
128+
UserAnnotated foundUser2 = couchbaseTemplate.findById(UserAnnotated.class).withExpiry(Duration.ofSeconds(10)).one(user2.getId());
129+
user2.setVersion(foundUser2.getVersion());// version will have changed
130+
assertEquals(user2, foundUser2);
131+
132+
// now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation
133+
foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).one(user1.getId());
134+
user1.setVersion(foundUser1.getVersion());// version will have changed
135+
assertEquals(user1, foundUser1);
136+
137+
// user1 should be gone, user2 should still be there
138+
int tries = 0;
139+
Collection<User> foundUsers;
140+
do {
141+
sleepSecs(1);
142+
foundUsers = (Collection<User>) couchbaseTemplate.findById(User.class)
143+
.all(Arrays.asList(user1.getId(), user2.getId()));
144+
} while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next()));
145+
assertEquals(1, foundUsers.size(), "should have found exactly 1 user");
146+
assertEquals(user2, foundUsers.iterator().next());
147+
} finally {
148+
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
149+
}
150+
151+
}
115152
@Test
116153
void upsertAndFindById() {
117154
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");

0 commit comments

Comments
 (0)