Skip to content

Populates expiry property from result.getExpiry #2078

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
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 @@ -16,6 +16,7 @@
package org.springframework.data.couchbase.core;

import java.lang.reflect.InaccessibleObjectException;
import java.time.Instant;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -67,8 +68,8 @@ public AbstractTemplateSupport(ReactiveCouchbaseTemplate template, CouchbaseConv

abstract ReactiveCouchbaseTemplate getReactiveTemplate();

public <T> T decodeEntityBase(Object id, String source, Long cas, Class<T> entityClass, String scope,
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
public <T> T decodeEntityBase(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass,
String scope, String collection, Object txResultHolder, CouchbaseResourceHolder holder) {

// this is the entity class defined for the repository. It may not be the class of the document that was read
// we will reset it after reading the document
Expand Down Expand Up @@ -127,6 +128,11 @@ public <T> T decodeEntityBase(Object id, String source, Long cas, Class<T> entit
if (cas != null && cas != 0 && persistentEntity.getVersionProperty() != null) {
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
}

if (expiryTime != null && persistentEntity.getExpiryProperty() != null) {
accessor.setProperty(persistentEntity.getExpiryProperty(), expiryTime);
}

N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id.toString(), scope, collection);

if (holder != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.data.couchbase.core;

import com.couchbase.client.java.CommonOptions;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.query.Query;
Expand Down Expand Up @@ -54,6 +55,22 @@ public interface CouchbaseOperations extends FluentCouchbaseOperations {
*/
QueryScanConsistency getConsistency();

/**
* Save the entity to couchbase.<br>
* If there is no version property on the entity class, and this is in a transaction, use insert. <br>
* If there is no version property on the entity class, and this is not in a transaction, use upsert. <br>
* If there is a version property on the entity class, and it is non-zero, then this is an existing document, use
* replace.<br>
* Otherwise, there is a version property for the entity, but it is zero or null, use insert. <br>
*
* @param entity the entity to save in couchbase
* @param options options
* @param scopeAndCollection for use by repositories only. these are varargs for the scope and collection.
* @param <T> the entity class
* @return
*/
<T> T save(T entity, CommonOptions<?> options, String... scopeAndCollection);

/**
* Save the entity to couchbase.<br>
* If there is no version property on the entity class, and this is in a transaction, use insert. <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package org.springframework.data.couchbase.core;

import com.couchbase.client.java.CommonOptions;
import com.couchbase.client.java.kv.InsertOptions;
import com.couchbase.client.java.kv.ReplaceOptions;
import com.couchbase.client.java.kv.UpsertOptions;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -86,6 +90,11 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch

@Override
public <T> T save(T entity, String... scopeAndCollection) {
return save(entity, null, scopeAndCollection);
}

@Override
public <T> T save(T entity, CommonOptions<?> options, String... scopeAndCollection) {
Assert.notNull(entity, "Entity must not be null!");

String scope = scopeAndCollection.length > 0 ? scopeAndCollection[0] : null;
Expand All @@ -109,10 +118,12 @@ public <T> T save(T entity, String... scopeAndCollection) {
if (ctx.isPresent()) {
return (T) insertById(clazz).inScope(scope)
.inCollection(collection)
.withOptions((InsertOptions) options)
.one(entity);
} else { // if not in a tx, then upsert will work
return (T) upsertById(clazz).inScope(scope)
.inCollection(collection)
.withOptions((UpsertOptions) options)
.one(entity);
}
}).block();
Expand All @@ -121,11 +132,13 @@ public <T> T save(T entity, String... scopeAndCollection) {
// Updating existing document with cas
return (T)replaceById(clazz).inScope(scope)
.inCollection(collection)
.withOptions((ReplaceOptions) options)
.one(entity);
} else { // there is a version property, but it's zero or not set.
// Creating new document
return (T)insertById(clazz).inScope(scope)
.inCollection(collection)
.withOptions((InsertOptions) options)
.one(entity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.util.Assert;

import java.time.Instant;

/**
* Internal encode/decode support for CouchbaseTemplate.
*
Expand Down Expand Up @@ -62,9 +64,9 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
}

@Override
public <T> T decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
Object txHolder, CouchbaseResourceHolder holder) {
return decodeEntityBase(id, source, cas, entityClass, scope, collection, txHolder, holder);
public <T> T decodeEntity(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass,
String scope, String collection, Object txHolder, CouchbaseResourceHolder holder) {
return decodeEntityBase(id, source, cas, expiryTime, entityClass, scope, collection, txHolder, holder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.springframework.data.couchbase.core.ReactiveExistsByIdOperationSupport.ReactiveExistsByIdSupport;
import org.springframework.data.couchbase.core.query.OptionsBuilder;
import org.springframework.util.Assert;

import com.couchbase.client.java.kv.ExistsOptions;

Expand Down Expand Up @@ -82,8 +81,8 @@ public ExistsByIdWithOptions inCollection(final String collection) {

@Override
public TerminatingExistsById withOptions(final ExistsOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableExistsByIdSupport(template, domainType, scope, collection, options);
return new ExecutableExistsByIdSupport(template, domainType, scope, collection,
options != null ? options : this.options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public TerminatingFindByAnalytics<T> matching(final AnalyticsQuery query) {

@Override
public FindByAnalyticsWithQuery<T> withOptions(final AnalyticsOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
collection, options);
collection, options != null ? options : this.options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Collection<? extends T> all(final Collection<String> ids) {

@Override
public TerminatingFindById<T> withOptions(final GetOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableFindByIdSupport<>(template, domainType, scope, collection, options, fields, expiry, lockDuration);
return new ExecutableFindByIdSupport<>(template, domainType, scope, collection,
options != null ? options : this.options, fields, expiry, lockDuration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ public boolean exists() {

@Override
public TerminatingFindByQuery<T> withOptions(final QueryOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableFindByQuerySupport<>(template, domainType, returnType, query, scanConsistency, scope,
collection, options, distinctFields, fields);
collection, options != null ? options : this.options, distinctFields, fields);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.springframework.data.couchbase.core.ReactiveFindFromReplicasByIdOperationSupport.ReactiveFindFromReplicasByIdSupport;
import org.springframework.data.couchbase.core.query.OptionsBuilder;
import org.springframework.util.Assert;

import com.couchbase.client.java.kv.GetAnyReplicaOptions;

Expand Down Expand Up @@ -71,8 +70,8 @@ public Collection<? extends T> any(Collection<String> ids) {

@Override
public TerminatingFindFromReplicasById<T> withOptions(final GetAnyReplicaOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableFindFromReplicasByIdSupport<>(template, domainType, returnType, scope, collection, options);
return new ExecutableFindFromReplicasByIdSupport<>(template, domainType, returnType, scope, collection,
options != null ? options : this.options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public Collection<? extends T> all(Collection<? extends T> objects) {

@Override
public TerminatingInsertById<T> withOptions(final InsertOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, scope, collection, options, persistTo, replicateTo,
return new ExecutableInsertByIdSupport<>(template, domainType, scope, collection,
options != null ? options : this.options, persistTo, replicateTo,
durabilityLevel, expiry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
*/
package org.springframework.data.couchbase.core;

import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.kv.MutateInOptions;
import com.couchbase.client.java.kv.MutateInSpec;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.query.OptionsBuilder;
import org.springframework.data.couchbase.core.support.PseudoArgs;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.*;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.kv.MutateInOptions;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;

/**
* {@link ExecutableMutateInByIdOperation} implementations for Couchbase.
Expand All @@ -49,7 +49,7 @@ public ExecutableMutateInByIdOperationSupport(final CouchbaseTemplate template)
@Override
public <T> ExecutableMutateInById<T> mutateInById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableMutateInByIdSupport(template, domainType, OptionsBuilder.getScopeFrom(domainType),
return new ExecutableMutateInByIdSupport<>(template, domainType, OptionsBuilder.getScopeFrom(domainType),
OptionsBuilder.getCollectionFrom(domainType), null, OptionsBuilder.getPersistTo(domainType),
OptionsBuilder.getReplicateTo(domainType), OptionsBuilder.getDurabilityLevel(domainType, template.getConverter()),
null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(),
Expand Down Expand Up @@ -109,8 +109,8 @@ public Collection<? extends T> all(Collection<? extends T> objects) {

@Override
public TerminatingMutateInById<T> withOptions(final MutateInOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableMutateInByIdSupport(template, domainType, scope, collection, options, persistTo, replicateTo,
return new ExecutableMutateInByIdSupport<>(template, domainType, scope, collection,
options != null ? options : this.options, persistTo, replicateTo,
durabilityLevel, expiry, removePaths, upsertPaths, insertPaths, replacePaths, provideCas);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.springframework.data.couchbase.core.ReactiveRangeScanOperationSupport.ReactiveRangeScanSupport;
import org.springframework.data.couchbase.core.query.OptionsBuilder;
import org.springframework.util.Assert;

import com.couchbase.client.java.kv.MutationState;
import com.couchbase.client.java.kv.ScanOptions;
Expand Down Expand Up @@ -70,8 +69,8 @@ static class ExecutableRangeScanSupport<T> implements ExecutableRangeScan<T> {

@Override
public TerminatingRangeScan<T> withOptions(final ScanOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableRangeScanSupport<>(template, domainType, scope, collection, options, sort,
return new ExecutableRangeScanSupport<>(template, domainType, scope, collection,
options != null ? options : this.options, sort,
mutationState, batchItemLimit, batchByteLimit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public RemoveByIdInScope withDurability(final PersistTo persistTo, final Replica

@Override
public TerminatingRemoveById withOptions(final RemoveOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableRemoveByIdSupport(template, domainType, scope, collection, options, persistTo, replicateTo,
return new ExecutableRemoveByIdSupport(template, domainType, scope, collection,
options != null ? options : this.options, persistTo, replicateTo,
durabilityLevel, cas);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.data.couchbase.core.ReactiveRemoveByQueryOperationSupport.ReactiveRemoveByQuerySupport;
import org.springframework.data.couchbase.core.query.OptionsBuilder;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.util.Assert;

import com.couchbase.client.java.query.QueryOptions;
import com.couchbase.client.java.query.QueryScanConsistency;
Expand Down Expand Up @@ -97,9 +96,8 @@ public RemoveByQueryWithConsistency<T> inCollection(final String collection) {

@Override
public RemoveByQueryWithQuery<T> withOptions(final QueryOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency, scope, collection,
options);
options != null ? options : this.options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public ReplaceByIdWithDurability<T> withExpiry(final Duration expiry) {

@Override
public TerminatingReplaceById<T> withOptions(final ReplaceOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableReplaceByIdSupport<>(template, domainType, scope, collection, options, persistTo,
return new ExecutableReplaceByIdSupport<>(template, domainType, scope, collection,
options != null ? options : this.options, persistTo,
replicateTo, durabilityLevel, expiry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public Collection<? extends T> all(Collection<? extends T> objects) {

@Override
public TerminatingUpsertById<T> withOptions(final UpsertOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableUpsertByIdSupport<>(template, domainType, scope, collection, options, persistTo, replicateTo,
return new ExecutableUpsertByIdSupport<>(template, domainType, scope, collection,
options != null ? options : this.options, persistTo, replicateTo,
durabilityLevel, expiry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.transaction.CouchbaseResourceHolder;

import java.time.Instant;

/**
* Wrapper of {@link TemplateSupport} methods to adapt them to {@link ReactiveTemplateSupport}.
*
Expand All @@ -42,9 +44,10 @@ public Mono<CouchbaseDocument> encodeEntity(Object entityToEncode) {
}

@Override
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
Object txResultHolder, CouchbaseResourceHolder holder) {
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass, scope, collection, txResultHolder, holder));
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass,
String scope, String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, expiryTime, entityClass, scope, collection,
txResultHolder, holder));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import reactor.core.publisher.Mono;

import java.time.Instant;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -69,10 +71,11 @@ ReactiveCouchbaseTemplate getReactiveTemplate() {
}

@Override
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope,
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass,
String scope, String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
return Mono
.fromSupplier(() -> decodeEntityBase(id, source, cas, entityClass, scope, collection, txResultHolder, holder));
.fromSupplier(() -> decodeEntityBase(id, source, cas, expiryTime, entityClass, scope, collection,
txResultHolder, holder));
}

@Override
Expand Down
Loading
Loading