Skip to content

DATACOUCH-645 - Support document expiryExpression. #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ExecutableInsertByIdOperationSupport(final CouchbaseTemplate template) {
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, Duration.ofSeconds(0));
DurabilityLevel.NONE, null);
}

static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ExecutableReplaceByIdOperationSupport(final CouchbaseTemplate template) {
public <T> ExecutableReplaceById<T> replaceById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableReplaceByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE, Duration.ZERO);
DurabilityLevel.NONE, null);
}

static class ExecutableReplaceByIdSupport<T> implements ExecutableReplaceById<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ExecutableUpsertByIdOperationSupport(final CouchbaseTemplate template) {
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, Duration.ofSeconds(0));
DurabilityLevel.NONE, null);
}

static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.springframework.data.couchbase.core;

import org.springframework.data.couchbase.core.mapping.Document;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -42,7 +41,7 @@ public ReactiveInsertByIdOperationSupport(final ReactiveCouchbaseTemplate templa
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, Duration.ofSeconds(0));
DurabilityLevel.NONE, null);
}

static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
Expand Down Expand Up @@ -72,7 +71,7 @@ public Mono<T> one(T object) {
return Mono.just(object).flatMap(o -> {
CouchbaseDocument converted = template.support().encodeEntity(o);
return template.getCollection(collection).reactive()
.insert(converted.getId(), converted.export(), buildInsertOptions()).map(result -> {
.insert(converted.getId(), converted.export(), buildInsertOptions(converted)).map(result -> {
Object updatedObject = template.support().applyUpdatedId(o, converted.getId());
return (T) template.support().applyUpdatedCas(updatedObject, result.cas());
});
Expand All @@ -90,19 +89,17 @@ public Flux<? extends T> all(Collection<? extends T> objects) {
return Flux.fromIterable(objects).flatMap(this::one);
}

private InsertOptions buildInsertOptions() {
private InsertOptions buildInsertOptions(CouchbaseDocument doc) { // CouchbaseDocument converted
final InsertOptions options = InsertOptions.insertOptions();
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
options.durability(persistTo, replicateTo);
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null && !expiry.isZero()) {
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));
} else if (doc.getExpiration() != 0) {
options.expiry(Duration.ofSeconds(doc.getExpiration()));
}
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Collection;

import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.util.Assert;

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

static class ReactiveReplaceByIdSupport<T> implements ReactiveReplaceById<T> {
Expand Down Expand Up @@ -72,8 +71,8 @@ public Mono<T> one(T object) {
return Mono.just(object).flatMap(o -> {
CouchbaseDocument converted = template.support().encodeEntity(o);
return template.getCollection(collection).reactive()
.replace(converted.getId(), converted.export(), buildReplaceOptions(o)).map(result ->
(T)template.support().applyUpdatedCas(o, result.cas()));
.replace(converted.getId(), converted.export(), buildReplaceOptions(o, converted))
.map(result -> (T) template.support().applyUpdatedCas(o, result.cas()));
}).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
Expand All @@ -88,19 +87,17 @@ public Flux<? extends T> all(Collection<? extends T> objects) {
return Flux.fromIterable(objects).flatMap(this::one);
}

private ReplaceOptions buildReplaceOptions(T object) {
private ReplaceOptions buildReplaceOptions(T object, CouchbaseDocument doc) {
final ReplaceOptions options = ReplaceOptions.replaceOptions();
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
options.durability(persistTo, replicateTo);
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null && !expiry.isZero()) {
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));
} else if (doc.getExpiration() != 0) {
options.expiry(Duration.ofSeconds(doc.getExpiration()));
}
long cas = template.support().getCas(object);
options.cas(cas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.springframework.data.couchbase.core;

import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.mapping.Document;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -44,7 +41,7 @@ public ReactiveUpsertByIdOperationSupport(final ReactiveCouchbaseTemplate templa
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, Duration.ofSeconds(0));
DurabilityLevel.NONE, null);
}

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

private UpsertOptions buildUpsertOptions() {
private UpsertOptions buildUpsertOptions(CouchbaseDocument doc) {
final UpsertOptions options = UpsertOptions.upsertOptions();
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
options.durability(persistTo, replicateTo);
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null && !expiry.isZero()) {
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));
} else if (doc.getExpiration() != 0) {
options.expiry(Duration.ofSeconds(doc.getExpiration()));
}
return options;
}
Expand Down
Loading