Skip to content

Commit 9c9dde7

Browse files
authored
DATACOUCH-645 - Support document expiryExpression. (#296)
Support document expiryExpression and make the initial expiry to be null instead of Zero in the fluent API. This allows a client to 'unset' the expiry from an annotation by explcitly setting withExpiry(Zero). Co-authored-by: mikereiche <michael.reiche@couchbase.com>
1 parent 03ad369 commit 9c9dde7

8 files changed

+165
-182
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ExecutableInsertByIdOperationSupport(final CouchbaseTemplate template) {
3636
public <T> ExecutableInsertById<T> insertById(final Class<T> domainType) {
3737
Assert.notNull(domainType, "DomainType must not be null!");
3838
return new ExecutableInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
39-
DurabilityLevel.NONE, Duration.ofSeconds(0));
39+
DurabilityLevel.NONE, null);
4040
}
4141

4242
static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ExecutableReplaceByIdOperationSupport(final CouchbaseTemplate template) {
3636
public <T> ExecutableReplaceById<T> replaceById(final Class<T> domainType) {
3737
Assert.notNull(domainType, "DomainType must not be null!");
3838
return new ExecutableReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
39-
DurabilityLevel.NONE, Duration.ZERO);
39+
DurabilityLevel.NONE, null);
4040
}
4141

4242
static class ExecutableReplaceByIdSupport<T> implements ExecutableReplaceById<T> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ExecutableUpsertByIdOperationSupport(final CouchbaseTemplate template) {
3636
public <T> ExecutableUpsertById<T> upsertById(final Class<T> domainType) {
3737
Assert.notNull(domainType, "DomainType must not be null!");
3838
return new ExecutableUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
39-
DurabilityLevel.NONE, Duration.ofSeconds(0));
39+
DurabilityLevel.NONE, null);
4040
}
4141

4242
static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.mapping.Document;
1918
import reactor.core.publisher.Flux;
2019
import reactor.core.publisher.Mono;
2120

@@ -42,7 +41,7 @@ public ReactiveInsertByIdOperationSupport(final ReactiveCouchbaseTemplate templa
4241
public <T> ReactiveInsertById<T> insertById(final Class<T> domainType) {
4342
Assert.notNull(domainType, "DomainType must not be null!");
4443
return new ReactiveInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
45-
DurabilityLevel.NONE, Duration.ofSeconds(0));
44+
DurabilityLevel.NONE, null);
4645
}
4746

4847
static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
@@ -72,7 +71,7 @@ public Mono<T> one(T object) {
7271
return Mono.just(object).flatMap(o -> {
7372
CouchbaseDocument converted = template.support().encodeEntity(o);
7473
return template.getCollection(collection).reactive()
75-
.insert(converted.getId(), converted.export(), buildInsertOptions()).map(result -> {
74+
.insert(converted.getId(), converted.export(), buildInsertOptions(converted)).map(result -> {
7675
Object updatedObject = template.support().applyUpdatedId(o, converted.getId());
7776
return (T) template.support().applyUpdatedCas(updatedObject, result.cas());
7877
});
@@ -90,19 +89,17 @@ public Flux<? extends T> all(Collection<? extends T> objects) {
9089
return Flux.fromIterable(objects).flatMap(this::one);
9190
}
9291

93-
private InsertOptions buildInsertOptions() {
92+
private InsertOptions buildInsertOptions(CouchbaseDocument doc) { // CouchbaseDocument converted
9493
final InsertOptions options = InsertOptions.insertOptions();
9594
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
9695
options.durability(persistTo, replicateTo);
9796
} else if (durabilityLevel != DurabilityLevel.NONE) {
9897
options.durability(durabilityLevel);
9998
}
100-
if (expiry != null && !expiry.isZero()) {
99+
if (expiry != null) {
101100
options.expiry(expiry);
102-
} else if (domainType.isAnnotationPresent(Document.class)) {
103-
Document documentAnn = domainType.getAnnotation(Document.class);
104-
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
105-
options.expiry(Duration.ofSeconds(durationSeconds));
101+
} else if (doc.getExpiration() != 0) {
102+
options.expiry(Duration.ofSeconds(doc.getExpiration()));
106103
}
107104
return options;
108105
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collection;
2323

2424
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
25-
import org.springframework.data.couchbase.core.mapping.Document;
2625
import org.springframework.util.Assert;
2726

2827
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -42,7 +41,7 @@ public ReactiveReplaceByIdOperationSupport(final ReactiveCouchbaseTemplate templ
4241
public <T> ReactiveReplaceById<T> replaceById(final Class<T> domainType) {
4342
Assert.notNull(domainType, "DomainType must not be null!");
4443
return new ReactiveReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
45-
DurabilityLevel.NONE, Duration.ZERO);
44+
DurabilityLevel.NONE, null);
4645
}
4746

4847
static class ReactiveReplaceByIdSupport<T> implements ReactiveReplaceById<T> {
@@ -72,8 +71,8 @@ public Mono<T> one(T object) {
7271
return Mono.just(object).flatMap(o -> {
7372
CouchbaseDocument converted = template.support().encodeEntity(o);
7473
return template.getCollection(collection).reactive()
75-
.replace(converted.getId(), converted.export(), buildReplaceOptions(o)).map(result ->
76-
(T)template.support().applyUpdatedCas(o, result.cas()));
74+
.replace(converted.getId(), converted.export(), buildReplaceOptions(o, converted))
75+
.map(result -> (T) template.support().applyUpdatedCas(o, result.cas()));
7776
}).onErrorMap(throwable -> {
7877
if (throwable instanceof RuntimeException) {
7978
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
@@ -88,19 +87,17 @@ public Flux<? extends T> all(Collection<? extends T> objects) {
8887
return Flux.fromIterable(objects).flatMap(this::one);
8988
}
9089

91-
private ReplaceOptions buildReplaceOptions(T object) {
90+
private ReplaceOptions buildReplaceOptions(T object, CouchbaseDocument doc) {
9291
final ReplaceOptions options = ReplaceOptions.replaceOptions();
9392
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
9493
options.durability(persistTo, replicateTo);
9594
} else if (durabilityLevel != DurabilityLevel.NONE) {
9695
options.durability(durabilityLevel);
9796
}
98-
if (expiry != null && !expiry.isZero()) {
97+
if (expiry != null) {
9998
options.expiry(expiry);
100-
} else if (domainType.isAnnotationPresent(Document.class)) {
101-
Document documentAnn = domainType.getAnnotation(Document.class);
102-
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
103-
options.expiry(Duration.ofSeconds(durationSeconds));
99+
} else if (doc.getExpiration() != 0) {
100+
options.expiry(Duration.ofSeconds(doc.getExpiration()));
104101
}
105102
long cas = template.support().getCas(object);
106103
options.cas(cas);

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
19-
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
20-
import org.springframework.data.couchbase.core.mapping.Document;
2118
import reactor.core.publisher.Flux;
2219
import reactor.core.publisher.Mono;
2320

@@ -44,7 +41,7 @@ public ReactiveUpsertByIdOperationSupport(final ReactiveCouchbaseTemplate templa
4441
public <T> ReactiveUpsertById<T> upsertById(final Class<T> domainType) {
4542
Assert.notNull(domainType, "DomainType must not be null!");
4643
return new ReactiveUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
47-
DurabilityLevel.NONE, Duration.ofSeconds(0));
44+
DurabilityLevel.NONE, null);
4845
}
4946

5047
static class ReactiveUpsertByIdSupport<T> implements ReactiveUpsertById<T> {
@@ -74,7 +71,7 @@ public Mono<T> one(T object) {
7471
return Mono.just(object).flatMap(o -> {
7572
CouchbaseDocument converted = template.support().encodeEntity(o);
7673
return template.getCollection(collection).reactive()
77-
.upsert(converted.getId(), converted.export(), buildUpsertOptions()).map(result -> {
74+
.upsert(converted.getId(), converted.export(), buildUpsertOptions(converted)).map(result -> {
7875
Object updatedObject = template.support().applyUpdatedId(o, converted.getId());
7976
return (T) template.support().applyUpdatedCas(updatedObject, result.cas());
8077
});
@@ -92,19 +89,17 @@ public Flux<? extends T> all(Collection<? extends T> objects) {
9289
return Flux.fromIterable(objects).flatMap(this::one);
9390
}
9491

95-
private UpsertOptions buildUpsertOptions() {
92+
private UpsertOptions buildUpsertOptions(CouchbaseDocument doc) {
9693
final UpsertOptions options = UpsertOptions.upsertOptions();
9794
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
9895
options.durability(persistTo, replicateTo);
9996
} else if (durabilityLevel != DurabilityLevel.NONE) {
10097
options.durability(durabilityLevel);
10198
}
102-
if (expiry != null && !expiry.isZero()) {
99+
if (expiry != null) {
103100
options.expiry(expiry);
104-
} else if (domainType.isAnnotationPresent(Document.class)) {
105-
Document documentAnn = domainType.getAnnotation(Document.class);
106-
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
107-
options.expiry(Duration.ofSeconds(durationSeconds));
101+
} else if (doc.getExpiration() != 0) {
102+
options.expiry(Duration.ofSeconds(doc.getExpiration()));
108103
}
109104
return options;
110105
}

0 commit comments

Comments
 (0)