Skip to content

Commit 897d3fc

Browse files
authored
Take expiry from entity class annotation for getAndTouch. (#1640)
Closes #1634.
1 parent 1d6727b commit 897d3fc

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static class ReactiveFindByIdSupport<T> implements ReactiveFindById<T> {
7272
private final ReactiveTemplateSupport support;
7373
private final Duration expiry;
7474

75+
private Duration expiryToUse;
76+
7577
ReactiveFindByIdSupport(ReactiveCouchbaseTemplate template, Class<T> domainType, String scope, String collection,
7678
CommonOptions<?> options, List<String> fields, Duration expiry, ReactiveTemplateSupport support) {
7779
this.template = template;
@@ -98,7 +100,7 @@ public Mono<T> one(final Object id) {
98100
Mono<T> reactiveEntity = TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
99101
if (!ctxOpt.isPresent()) {
100102
if (pArgs.getOptions() instanceof GetAndTouchOptions) {
101-
return rc.getAndTouch(id.toString(), expiryToUse(), (GetAndTouchOptions) pArgs.getOptions())
103+
return rc.getAndTouch(id.toString(), expiryToUse, (GetAndTouchOptions) pArgs.getOptions())
102104
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
103105
pArgs.getScope(), pArgs.getCollection(), null, null));
104106
} else {
@@ -110,8 +112,7 @@ public Mono<T> one(final Object id) {
110112
rejectInvalidTransactionalOptions();
111113
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id.toString())
112114
.flatMap(result -> support.decodeEntity(id, new String(result.contentAsBytes(), StandardCharsets.UTF_8),
113-
result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(),
114-
null, null));
115+
result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(), null, null));
115116
}
116117
});
117118

@@ -179,7 +180,18 @@ public FindByIdWithProjection<T> withExpiry(final Duration expiry) {
179180

180181
private CommonOptions<?> initGetOptions() {
181182
CommonOptions<?> getOptions;
182-
if (expiry != null || options instanceof GetAndTouchOptions) {
183+
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
184+
.getRequiredPersistentEntity(domainType);
185+
Duration entityExpiryAnnotation = entity.getExpiryDuration();
186+
if (expiry != null || entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()
187+
|| options instanceof GetAndTouchOptions) {
188+
if (expiry != null) {
189+
expiryToUse = expiry;
190+
} else if (entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()) {
191+
expiryToUse = entityExpiryAnnotation;
192+
} else {
193+
expiryToUse = Duration.ZERO;
194+
}
183195
GetAndTouchOptions gOptions = options != null ? (GetAndTouchOptions) options : getAndTouchOptions();
184196
if (gOptions.build().transcoder() == null) {
185197
gOptions.transcoder(RawJsonTranscoder.INSTANCE);
@@ -197,19 +209,7 @@ private CommonOptions<?> initGetOptions() {
197209
}
198210
return getOptions;
199211
}
200-
201-
private Duration expiryToUse() {
202-
Duration expiryToUse = expiry;
203-
if (expiryToUse != null || options instanceof GetAndTouchOptions) {
204-
if (expiryToUse == null) { // GetAndTouchOptions without specifying expiry -> get expiry from annoation
205-
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
206-
.getRequiredPersistentEntity(domainType);
207-
expiryToUse = entity.getExpiryDuration();
208-
}
209-
}
210-
return expiryToUse;
211-
}
212-
212+
213213
}
214214

215215
}

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

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

119119
}
120120

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

0 commit comments

Comments
 (0)