Skip to content

Commit 98bffc0

Browse files
authored
Use getAndTouch only when annotation attribute touchOnRead=true. (#1641)
Closes #1634.
1 parent 897d3fc commit 98bffc0

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

Diff for: src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,12 @@ private CommonOptions<?> initGetOptions() {
182182
CommonOptions<?> getOptions;
183183
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
184184
.getRequiredPersistentEntity(domainType);
185-
Duration entityExpiryAnnotation = entity.getExpiryDuration();
186-
if (expiry != null || entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()
187-
|| options instanceof GetAndTouchOptions) {
185+
Boolean isTouchOnRead = entity.isTouchOnRead();
186+
if (expiry != null || isTouchOnRead || options instanceof GetAndTouchOptions) {
188187
if (expiry != null) {
189188
expiryToUse = expiry;
190-
} else if (entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()) {
191-
expiryToUse = entityExpiryAnnotation;
189+
} else if (isTouchOnRead) {
190+
expiryToUse = entity.getExpiryDuration();
192191
} else {
193192
expiryToUse = Duration.ZERO;
194193
}
@@ -209,7 +208,7 @@ private CommonOptions<?> initGetOptions() {
209208
}
210209
return getOptions;
211210
}
212-
211+
213212
}
214213

215214
}

Diff for: src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

+44-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.data.couchbase.domain.UserAnnotated;
5757
import org.springframework.data.couchbase.domain.UserAnnotated2;
5858
import org.springframework.data.couchbase.domain.UserAnnotated3;
59+
import org.springframework.data.couchbase.domain.UserAnnotatedTouchOnRead;
5960
import org.springframework.data.couchbase.domain.UserSubmission;
6061
import org.springframework.data.couchbase.util.ClusterType;
6162
import org.springframework.data.couchbase.util.IgnoreWhen;
@@ -136,24 +137,60 @@ void findByIdWithExpiryAnnotation() {
136137
assertEquals(user2, foundUser2);
137138

138139
// now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation
140+
// This will have no effect as UserAnnotated does not have touchOnGet
139141
foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).one(user1.getId());
140142
user1.setVersion(foundUser1.getVersion());// version will have changed
141143
assertEquals(user1, foundUser1);
142144

143145
// user1 should be gone, user2 should still be there
144146
int tries = 0;
145-
Collection<User> foundUsers;
147+
Collection<UserAnnotated> foundUsers;
146148
do {
147-
sleepSecs(1);
148-
foundUsers = (Collection<User>) couchbaseTemplate.findById(User.class)
149+
sleepSecs(3);
150+
foundUsers = (Collection<UserAnnotated>) couchbaseTemplate.findById(UserAnnotated.class)
149151
.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());
152+
} while (tries++ < 7 && foundUsers.size() != 2 && !user2.equals(foundUsers.iterator().next()));
153+
assertEquals(2, foundUsers.size(), "should have found exactly 2 users");
153154
} finally {
154-
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
155+
couchbaseTemplate.removeByQuery(UserAnnotated.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
155156
}
157+
}
158+
159+
@Test
160+
void findByIdWithExpiryAnnotationTouchOnRead() {
161+
try {
162+
UserAnnotatedTouchOnRead user1 = new UserAnnotatedTouchOnRead(UUID.randomUUID().toString(), "user1", "user1");
163+
UserAnnotatedTouchOnRead user2 = new UserAnnotatedTouchOnRead(UUID.randomUUID().toString(), "user2", "user2");
164+
165+
Collection<UserAnnotatedTouchOnRead> upserts = (Collection<UserAnnotatedTouchOnRead>) couchbaseTemplate.upsertById(UserAnnotatedTouchOnRead.class)
166+
.all(Arrays.asList(user1, user2));
167+
168+
// explicitly set expiry to 10 seconds
169+
UserAnnotatedTouchOnRead foundUser1 = couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class).withExpiry(Duration.ofSeconds(10)).one(user1.getId());
170+
user1.setVersion(foundUser1.getVersion());// version will have changed
171+
assertEquals(user1, foundUser1);
172+
UserAnnotatedTouchOnRead foundUser2 = couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class).withExpiry(Duration.ofSeconds(10)).one(user2.getId());
173+
user2.setVersion(foundUser2.getVersion());// version will have changed
174+
assertEquals(user2, foundUser2);
175+
176+
// now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation
177+
foundUser1 = couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class).one(user1.getId());
178+
user1.setVersion(foundUser1.getVersion());// version will have changed
179+
assertEquals(user1, foundUser1);
156180

181+
// user1 should be gone, user2 should still be there
182+
int tries = 0;
183+
Collection<UserAnnotatedTouchOnRead> foundUsers;
184+
do {
185+
sleepSecs(3);
186+
foundUsers = (Collection<UserAnnotatedTouchOnRead>) couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class)
187+
.all(Arrays.asList(user1.getId(), user2.getId()));
188+
} while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next()));
189+
assertEquals(1, foundUsers.size(), "should have found exactly 1 user1");
190+
assertEquals(user2.getId(), foundUsers.iterator().next().getId());
191+
} finally {
192+
couchbaseTemplate.removeByQuery(UserAnnotatedTouchOnRead.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
193+
}
157194
}
158195
@Test
159196
void upsertAndFindById() {

0 commit comments

Comments
 (0)