Skip to content

Commit 5b04d59

Browse files
committed
Use getAndTouch only when annotation attribute touchOnRead=true. (#1641)
Closes #1634.
1 parent 7800cf7 commit 5b04d59

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,12 @@ private CommonOptions<?> initGetOptions() {
146146
CommonOptions<?> getOptions;
147147
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
148148
.getRequiredPersistentEntity(domainType);
149-
Duration entityExpiryAnnotation = entity.getExpiryDuration();
150-
if (expiry != null || entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()
151-
|| options instanceof GetAndTouchOptions) {
149+
Boolean isTouchOnRead = entity.isTouchOnRead();
150+
if (expiry != null || isTouchOnRead || options instanceof GetAndTouchOptions) {
152151
if (expiry != null) {
153152
expiryToUse = expiry;
154-
} else if (entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()) {
155-
expiryToUse = entityExpiryAnnotation;
153+
} else if (isTouchOnRead) {
154+
expiryToUse = entity.getExpiryDuration();
156155
} else {
157156
expiryToUse = Duration.ZERO;
158157
}
@@ -173,7 +172,7 @@ private CommonOptions<?> initGetOptions() {
173172
}
174173
return getOptions;
175174
}
176-
175+
177176
}
178177

179178
}

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.data.couchbase.domain.UserAnnotated;
5656
import org.springframework.data.couchbase.domain.UserAnnotated2;
5757
import org.springframework.data.couchbase.domain.UserAnnotated3;
58+
import org.springframework.data.couchbase.domain.UserAnnotatedTouchOnRead;
5859
import org.springframework.data.couchbase.domain.UserSubmission;
5960
import org.springframework.data.couchbase.util.ClusterType;
6061
import org.springframework.data.couchbase.util.IgnoreWhen;
@@ -130,24 +131,60 @@ void findByIdWithExpiryAnnotation() {
130131
assertEquals(user2, foundUser2);
131132

132133
// now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation
134+
// This will have no effect as UserAnnotated does not have touchOnGet
133135
foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).one(user1.getId());
134136
user1.setVersion(foundUser1.getVersion());// version will have changed
135137
assertEquals(user1, foundUser1);
136138

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

175+
// user1 should be gone, user2 should still be there
176+
int tries = 0;
177+
Collection<UserAnnotatedTouchOnRead> foundUsers;
178+
do {
179+
sleepSecs(3);
180+
foundUsers = (Collection<UserAnnotatedTouchOnRead>) couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class)
181+
.all(Arrays.asList(user1.getId(), user2.getId()));
182+
} while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next()));
183+
assertEquals(1, foundUsers.size(), "should have found exactly 1 user1");
184+
assertEquals(user2.getId(), foundUsers.iterator().next().getId());
185+
} finally {
186+
couchbaseTemplate.removeByQuery(UserAnnotatedTouchOnRead.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
187+
}
151188
}
152189
@Test
153190
void upsertAndFindById() {

0 commit comments

Comments
 (0)