From 1cfbf0e1cf64e9f107e1dda625fed1287de4f412 Mon Sep 17 00:00:00 2001 From: Michael Reiche <48999328+mikereiche@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:14:38 -0800 Subject: [PATCH] Take expiry from entity class annotation for getAndTouch. (#1640) Closes #1634. --- .../ReactiveFindByIdOperationSupport.java | 34 ++++++++--------- ...hbaseTemplateKeyValueIntegrationTests.java | 37 +++++++++++++++++++ 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java b/src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java index afdf1019..beae803d 100644 --- a/src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java +++ b/src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java @@ -72,6 +72,8 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati private final ReactiveTemplateSupport support; private final Duration expiry; + private Duration expiryToUse; + ReactiveFindByIdSupport(ReactiveCouchbaseTemplate template, Class domainType, String scope, String collection, CommonOptions options, List fields, Duration expiry, ReactiveTemplateSupport support) { this.template = template; @@ -98,7 +100,7 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati Mono reactiveEntity = TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> { if (!ctxOpt.isPresent()) { if (pArgs.getOptions() instanceof GetAndTouchOptions) { - return rc.getAndTouch(id.toString(), expiryToUse(), (GetAndTouchOptions) pArgs.getOptions()) + return rc.getAndTouch(id.toString(), expiryToUse, (GetAndTouchOptions) pArgs.getOptions()) .flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(), null, null)); } else { @@ -110,8 +112,7 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati rejectInvalidTransactionalOptions(); return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id.toString()) .flatMap(result -> support.decodeEntity(id, new String(result.contentAsBytes(), StandardCharsets.UTF_8), - result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(), - null, null)); + result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(), null, null)); } }); @@ -179,7 +180,18 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati private CommonOptions initGetOptions() { CommonOptions getOptions; - if (expiry != null || options instanceof GetAndTouchOptions) { + final CouchbasePersistentEntity entity = template.getConverter().getMappingContext() + .getRequiredPersistentEntity(domainType); + Duration entityExpiryAnnotation = entity.getExpiryDuration(); + if (expiry != null || entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero() + || options instanceof GetAndTouchOptions) { + if (expiry != null) { + expiryToUse = expiry; + } else if (entityExpiryAnnotation == null || !entityExpiryAnnotation.isZero()) { + expiryToUse = entityExpiryAnnotation; + } else { + expiryToUse = Duration.ZERO; + } GetAndTouchOptions gOptions = options != null ? (GetAndTouchOptions) options : getAndTouchOptions(); if (gOptions.build().transcoder() == null) { gOptions.transcoder(RawJsonTranscoder.INSTANCE); @@ -197,19 +209,7 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati } return getOptions; } - - private Duration expiryToUse() { - Duration expiryToUse = expiry; - if (expiryToUse != null || options instanceof GetAndTouchOptions) { - if (expiryToUse == null) { // GetAndTouchOptions without specifying expiry -> get expiry from annoation - final CouchbasePersistentEntity entity = template.getConverter().getMappingContext() - .getRequiredPersistentEntity(domainType); - expiryToUse = entity.getExpiryDuration(); - } - } - return expiryToUse; - } - + } } diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java index b2d39fe6..0741dce6 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java @@ -118,6 +118,43 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests { } + @Test + void findByIdWithExpiryAnnotation() { + try { + UserAnnotated user1 = new UserAnnotated(UUID.randomUUID().toString(), "user1", "user1"); + UserAnnotated user2 = new UserAnnotated(UUID.randomUUID().toString(), "user2", "user2"); + + Collection upserts = (Collection) couchbaseTemplate.upsertById(UserAnnotated.class) + .all(Arrays.asList(user1, user2)); + + // explicitly set expiry to 10 seconds + UserAnnotated foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).withExpiry(Duration.ofSeconds(10)).one(user1.getId()); + user1.setVersion(foundUser1.getVersion());// version will have changed + assertEquals(user1, foundUser1); + UserAnnotated foundUser2 = couchbaseTemplate.findById(UserAnnotated.class).withExpiry(Duration.ofSeconds(10)).one(user2.getId()); + user2.setVersion(foundUser2.getVersion());// version will have changed + assertEquals(user2, foundUser2); + + // now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation + foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).one(user1.getId()); + user1.setVersion(foundUser1.getVersion());// version will have changed + assertEquals(user1, foundUser1); + + // user1 should be gone, user2 should still be there + int tries = 0; + Collection foundUsers; + do { + sleepSecs(1); + foundUsers = (Collection) couchbaseTemplate.findById(User.class) + .all(Arrays.asList(user1.getId(), user2.getId())); + } while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next())); + assertEquals(1, foundUsers.size(), "should have found exactly 1 user"); + assertEquals(user2, foundUsers.iterator().next()); + } finally { + couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all(); + } + + } @Test void upsertAndFindById() { User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");