DATACOUCH-608 - Propagate expiry option and annotation to insert and upsert calls.

This commit is contained in:
mikereiche
2020-09-14 14:47:44 -07:00
parent 4a52268f19
commit e9add0c90c
10 changed files with 259 additions and 26 deletions

View File

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

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection;
import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
public <T> ExecutableInsertById<T> insertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {
@@ -46,18 +47,21 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
private final ReactiveInsertByIdOperationSupport.ReactiveInsertByIdSupport<T> reactiveSupport;
ExecutableInsertByIdSupport(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.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
this.reactiveSupport = new ReactiveInsertByIdOperationSupport.ReactiveInsertByIdSupport<>(template.reactive(),
domainType, collection, persistTo, replicateTo, durabilityLevel);
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
@@ -74,14 +78,14 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
public TerminatingInsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
@@ -89,7 +93,14 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel, expiry);
}
}

View File

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

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection;
import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
public <T> ExecutableUpsertById<T> upsertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {
@@ -46,18 +47,21 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
private final ReactiveUpsertByIdOperationSupport.ReactiveUpsertByIdSupport<T> reactiveSupport;
ExecutableUpsertByIdSupport(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.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
this.reactiveSupport = new ReactiveUpsertByIdOperationSupport.ReactiveUpsertByIdSupport<>(template.reactive(),
domainType, collection, persistTo, replicateTo, durabilityLevel);
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
@@ -74,14 +78,14 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
public TerminatingUpsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public UpsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
@@ -89,7 +93,14 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public UpsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ExecutableUpsertByIdSupport<>(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.Mono;
import java.time.Duration;
import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ public interface ReactiveInsertByIdOperation {
}
interface ReactiveInsertById<T> extends InsertByIdWithDurability<T> {}
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
InsertByIdWithDurability<T> withExpiry(Duration expiry);
}
interface ReactiveInsertById<T> extends InsertByIdWithExpiry<T> {}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.couchbase.core;
import com.couchbase.client.java.kv.UpsertOptions;
import org.springframework.data.couchbase.core.mapping.Document;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
@@ -40,7 +43,7 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
public <T> ReactiveInsertById<T> insertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ReactiveInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
@@ -51,16 +54,18 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
ReactiveInsertByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
final String collection, final PersistTo persistTo, final ReplicateTo replicateTo,
final DurabilityLevel durabilityLevel) {
final DurabilityLevel durabilityLevel, Duration expiry) {
this.template = template;
this.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
}
@Override
@@ -93,28 +98,40 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null) {
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));
}
return options;
}
@Override
public TerminatingInsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
public InsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ReactiveInsertByIdSupport<>(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.Mono;
import java.time.Duration;
import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ public interface ReactiveUpsertByIdOperation {
}
interface ReactiveUpsertById<T> extends UpsertByIdWithDurability<T> {}
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T> {
UpsertByIdWithDurability<T> withExpiry(Duration expiry);
}
interface ReactiveUpsertById<T> extends UpsertByIdWithExpiry<T> {}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.couchbase.core;
import com.couchbase.client.java.kv.Expiry;
import org.springframework.data.couchbase.core.mapping.Document;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
@@ -40,7 +43,7 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
public <T> ReactiveUpsertById<T> upsertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ReactiveUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ReactiveUpsertByIdSupport<T> implements ReactiveUpsertById<T> {
@@ -51,16 +54,18 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
ReactiveUpsertByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
final String collection, final PersistTo persistTo, final ReplicateTo replicateTo,
final DurabilityLevel durabilityLevel) {
final DurabilityLevel durabilityLevel, final Duration expiry) {
this.template = template;
this.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
}
@Override
@@ -93,28 +98,44 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null) {
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));
}
return options;
}
@Override
public TerminatingUpsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
@Override
public UpsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
@Override
public UpsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
@Override
public UpsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
}
}