DATACOUCH-630 - Add expiry to replace 4.2.x (#287)

Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
Michael Reiche
2020-12-09 07:56:08 -08:00
committed by GitHub
parent af51c27015
commit bce764c712
5 changed files with 106 additions and 22 deletions

View File

@@ -15,6 +15,7 @@
*/ */
package org.springframework.data.couchbase.core; package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel; import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -46,6 +47,11 @@ public interface ExecutableReplaceByIdOperation {
} }
interface ExecutableReplaceById<T> extends ReplaceByIdWithDurability<T> {} interface ReplaceByIdWithExpiry<T> extends ReplaceByIdWithDurability<T> {
ReplaceByIdWithDurability<T> withExpiry(final Duration expiry);
}
interface ExecutableReplaceById<T> extends ReplaceByIdWithExpiry<T> {}
} }

View File

@@ -15,6 +15,7 @@
*/ */
package org.springframework.data.couchbase.core; package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public class ExecutableReplaceByIdOperationSupport implements ExecutableReplaceB
public <T> ExecutableReplaceById<T> replaceById(final Class<T> domainType) { public <T> ExecutableReplaceById<T> replaceById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!"); Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE, return new ExecutableReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE); DurabilityLevel.NONE, Duration.ZERO);
} }
static class ExecutableReplaceByIdSupport<T> implements ExecutableReplaceById<T> { static class ExecutableReplaceByIdSupport<T> implements ExecutableReplaceById<T> {
@@ -46,18 +47,21 @@ public class ExecutableReplaceByIdOperationSupport implements ExecutableReplaceB
private final PersistTo persistTo; private final PersistTo persistTo;
private final ReplicateTo replicateTo; private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel; private final DurabilityLevel durabilityLevel;
private final Duration expiry;
private final ReactiveReplaceByIdOperationSupport.ReactiveReplaceByIdSupport<T> reactiveSupport; private final ReactiveReplaceByIdOperationSupport.ReactiveReplaceByIdSupport<T> reactiveSupport;
ExecutableReplaceByIdSupport(final CouchbaseTemplate template, final Class<T> domainType, final String collection, ExecutableReplaceByIdSupport(final CouchbaseTemplate template, final Class<T> domainType, final String collection,
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel) { final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel,
final Duration expiry) {
this.template = template; this.template = template;
this.domainType = domainType; this.domainType = domainType;
this.collection = collection; this.collection = collection;
this.persistTo = persistTo; this.persistTo = persistTo;
this.replicateTo = replicateTo; this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel; this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
this.reactiveSupport = new ReactiveReplaceByIdOperationSupport.ReactiveReplaceByIdSupport<>(template.reactive(), this.reactiveSupport = new ReactiveReplaceByIdOperationSupport.ReactiveReplaceByIdSupport<>(template.reactive(),
domainType, collection, persistTo, replicateTo, durabilityLevel); domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
} }
@Override @Override
@@ -74,14 +78,14 @@ public class ExecutableReplaceByIdOperationSupport implements ExecutableReplaceB
public TerminatingReplaceById<T> inCollection(final String collection) { public TerminatingReplaceById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty."); Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel); durabilityLevel, expiry);
} }
@Override @Override
public ReplaceByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) { public ReplaceByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null."); Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel); durabilityLevel, expiry);
} }
@Override @Override
@@ -89,7 +93,14 @@ public class ExecutableReplaceByIdOperationSupport implements ExecutableReplaceB
Assert.notNull(persistTo, "PersistTo must not be null."); Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null."); Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel); durabilityLevel, expiry);
}
@Override
public ReplaceByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ExecutableReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel, expiry);
} }
} }

View File

@@ -18,6 +18,7 @@ package org.springframework.data.couchbase.core;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel; import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ public interface ReactiveReplaceByIdOperation {
} }
interface ReactiveReplaceById<T> extends ReplaceByIdWithDurability<T> {} interface ReplaceByIdWithExpiry<T> extends ReplaceByIdWithDurability<T> {
ReplaceByIdWithDurability<T> withExpiry(final Duration expiry);
}
interface ReactiveReplaceById<T> extends ReplaceByIdWithExpiry<T> {}
} }

View File

@@ -18,9 +18,11 @@ package org.springframework.data.couchbase.core;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument; import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.couchbase.client.core.msg.kv.DurabilityLevel; import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -40,7 +42,7 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
public <T> ReactiveReplaceById<T> replaceById(final Class<T> domainType) { public <T> ReactiveReplaceById<T> replaceById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!"); Assert.notNull(domainType, "DomainType must not be null!");
return new ReactiveReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE, return new ReactiveReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE); DurabilityLevel.NONE, Duration.ZERO);
} }
static class ReactiveReplaceByIdSupport<T> implements ReactiveReplaceById<T> { static class ReactiveReplaceByIdSupport<T> implements ReactiveReplaceById<T> {
@@ -51,16 +53,18 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
private final PersistTo persistTo; private final PersistTo persistTo;
private final ReplicateTo replicateTo; private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel; private final DurabilityLevel durabilityLevel;
private final Duration expiry;
ReactiveReplaceByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType, ReactiveReplaceByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
final String collection, final PersistTo persistTo, final ReplicateTo replicateTo, final String collection, final PersistTo persistTo, final ReplicateTo replicateTo,
final DurabilityLevel durabilityLevel) { final DurabilityLevel durabilityLevel, final Duration expiry) {
this.template = template; this.template = template;
this.domainType = domainType; this.domainType = domainType;
this.collection = collection; this.collection = collection;
this.persistTo = persistTo; this.persistTo = persistTo;
this.replicateTo = replicateTo; this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel; this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
} }
@Override @Override
@@ -93,6 +97,13 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
} else if (durabilityLevel != DurabilityLevel.NONE) { } else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel); options.durability(durabilityLevel);
} }
if (expiry != null && !expiry.isZero()) {
options.expiry(expiry);
} else if (domainType.isAnnotationPresent(Document.class)) {
Document documentAnn = domainType.getAnnotation(Document.class);
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
options.expiry(Duration.ofSeconds(durationSeconds));
}
long cas = template.support().getCas(object); long cas = template.support().getCas(object);
options.cas(cas); options.cas(cas);
return options; return options;
@@ -101,23 +112,30 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
@Override @Override
public TerminatingReplaceById<T> inCollection(final String collection) { public TerminatingReplaceById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty."); Assert.hasText(collection, "Collection must not be null nor empty.");
return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
durabilityLevel); expiry);
} }
@Override @Override
public ReplaceByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) { public ReplaceByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null."); Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
durabilityLevel); expiry);
} }
@Override @Override
public ReplaceByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) { public ReplaceByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
Assert.notNull(persistTo, "PersistTo must not be null."); Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null."); Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
durabilityLevel); expiry);
}
@Override
public ReplaceByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ReactiveReplaceByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
} }
} }

View File

@@ -16,23 +16,25 @@
package org.springframework.data.couchbase.core; package org.springframework.data.couchbase.core;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.data.couchbase.config.BeanNames.*; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE;
import static org.springframework.data.couchbase.config.BeanNames.REACTIVE_COUCHBASE_TEMPLATE;
import java.io.IOException; import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.UUID; import java.util.UUID;
import com.couchbase.client.core.error.DocumentNotFoundException;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.dao.DataIntegrityViolationException;; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.couchbase.CouchbaseClientFactory; import org.springframework.data.couchbase.CouchbaseClientFactory;
@@ -44,6 +46,9 @@ import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
import org.springframework.data.couchbase.util.ClusterType; import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.IgnoreWhen;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
/** /**
* KV tests Theses tests rely on a cb server running. * KV tests Theses tests rely on a cb server running.
* *
@@ -141,6 +146,44 @@ class CouchbaseTemplateKeyValueIntegrationTests extends ClusterAwareIntegrationT
} }
} }
@Test
void replaceWithExpiry() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
try {
User modified = couchbaseTemplate.upsertById(User.class).withExpiry(Duration.ofSeconds(1)).one(user);
couchbaseTemplate.replaceById(User.class).withExpiry(Duration.ofSeconds(1)).one(user);
assertEquals(user, modified);
sleepSecs(2);
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertNull(found, "found should have been null as document should be expired");
} finally {
try {
couchbaseTemplate.removeById().one(user.getId());
} catch (DataRetrievalFailureException e) {
//
}
}
}
@Test
void replaceWithExpiryAnnotation() {
UserAnnotated user = new UserAnnotated(UUID.randomUUID().toString(), "firstname", "lastname");
try {
UserAnnotated modified = couchbaseTemplate.upsertById(UserAnnotated.class).one(user);
modified = couchbaseTemplate.replaceById(UserAnnotated.class).one(user);
assertEquals(user, modified);
sleepSecs(6);
User found = couchbaseTemplate.findById(UserAnnotated.class).one(user.getId());
assertNull(found, "found should have been null as document should be expired");
} finally {
try {
couchbaseTemplate.removeById().one(user.getId());
} catch (DataRetrievalFailureException e) {
//
}
}
}
@Test @Test
void findDocWhichDoesNotExist() { void findDocWhichDoesNotExist() {
assertNull(couchbaseTemplate.findById(User.class).one(UUID.randomUUID().toString())); assertNull(couchbaseTemplate.findById(User.class).one(UUID.randomUUID().toString()));