Skip to content

Commit c12acf6

Browse files
authored
Populates expiry property from result.getExpiry (#2078)
Also passes options into save() Closes #1639. Signed-off-by: mikereiche <[email protected]>
1 parent f9f5dac commit c12acf6

File tree

39 files changed

+319
-144
lines changed

39 files changed

+319
-144
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.couchbase.core;
1717

1818
import java.lang.reflect.InaccessibleObjectException;
19+
import java.time.Instant;
1920
import java.util.Map;
2021
import java.util.Set;
2122

@@ -67,8 +68,8 @@ public AbstractTemplateSupport(ReactiveCouchbaseTemplate template, CouchbaseConv
6768

6869
abstract ReactiveCouchbaseTemplate getReactiveTemplate();
6970

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

7374
// this is the entity class defined for the repository. It may not be the class of the document that was read
7475
// we will reset it after reading the document
@@ -127,6 +128,11 @@ public <T> T decodeEntityBase(Object id, String source, Long cas, Class<T> entit
127128
if (cas != null && cas != 0 && persistentEntity.getVersionProperty() != null) {
128129
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
129130
}
131+
132+
if (expiryTime != null && persistentEntity.getExpiryProperty() != null) {
133+
accessor.setProperty(persistentEntity.getExpiryProperty(), expiryTime);
134+
}
135+
130136
N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id.toString(), scope, collection);
131137

132138
if (holder != null) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.data.couchbase.core;
1818

19+
import com.couchbase.client.java.CommonOptions;
1920
import org.springframework.data.couchbase.CouchbaseClientFactory;
2021
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
2122
import org.springframework.data.couchbase.core.query.Query;
@@ -54,6 +55,22 @@ public interface CouchbaseOperations extends FluentCouchbaseOperations {
5455
*/
5556
QueryScanConsistency getConsistency();
5657

58+
/**
59+
* Save the entity to couchbase.<br>
60+
* If there is no version property on the entity class, and this is in a transaction, use insert. <br>
61+
* If there is no version property on the entity class, and this is not in a transaction, use upsert. <br>
62+
* If there is a version property on the entity class, and it is non-zero, then this is an existing document, use
63+
* replace.<br>
64+
* Otherwise, there is a version property for the entity, but it is zero or null, use insert. <br>
65+
*
66+
* @param entity the entity to save in couchbase
67+
* @param options options
68+
* @param scopeAndCollection for use by repositories only. these are varargs for the scope and collection.
69+
* @param <T> the entity class
70+
* @return
71+
*/
72+
<T> T save(T entity, CommonOptions<?> options, String... scopeAndCollection);
73+
5774
/**
5875
* Save the entity to couchbase.<br>
5976
* If there is no version property on the entity class, and this is in a transaction, use insert. <br>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.data.couchbase.core;
1818

19+
import com.couchbase.client.java.CommonOptions;
20+
import com.couchbase.client.java.kv.InsertOptions;
21+
import com.couchbase.client.java.kv.ReplaceOptions;
22+
import com.couchbase.client.java.kv.UpsertOptions;
1923
import org.springframework.beans.BeansException;
2024
import org.springframework.context.ApplicationContext;
2125
import org.springframework.context.ApplicationContextAware;
@@ -86,6 +90,11 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch
8690

8791
@Override
8892
public <T> T save(T entity, String... scopeAndCollection) {
93+
return save(entity, null, scopeAndCollection);
94+
}
95+
96+
@Override
97+
public <T> T save(T entity, CommonOptions<?> options, String... scopeAndCollection) {
8998
Assert.notNull(entity, "Entity must not be null!");
9099

91100
String scope = scopeAndCollection.length > 0 ? scopeAndCollection[0] : null;
@@ -109,10 +118,12 @@ public <T> T save(T entity, String... scopeAndCollection) {
109118
if (ctx.isPresent()) {
110119
return (T) insertById(clazz).inScope(scope)
111120
.inCollection(collection)
121+
.withOptions((InsertOptions) options)
112122
.one(entity);
113123
} else { // if not in a tx, then upsert will work
114124
return (T) upsertById(clazz).inScope(scope)
115125
.inCollection(collection)
126+
.withOptions((UpsertOptions) options)
116127
.one(entity);
117128
}
118129
}).block();
@@ -121,11 +132,13 @@ public <T> T save(T entity, String... scopeAndCollection) {
121132
// Updating existing document with cas
122133
return (T)replaceById(clazz).inScope(scope)
123134
.inCollection(collection)
135+
.withOptions((ReplaceOptions) options)
124136
.one(entity);
125137
} else { // there is a version property, but it's zero or not set.
126138
// Creating new document
127139
return (T)insertById(clazz).inScope(scope)
128140
.inCollection(collection)
141+
.withOptions((InsertOptions) options)
129142
.one(entity);
130143
}
131144
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.data.mapping.callback.EntityCallbacks;
3131
import org.springframework.util.Assert;
3232

33+
import java.time.Instant;
34+
3335
/**
3436
* Internal encode/decode support for CouchbaseTemplate.
3537
*
@@ -62,9 +64,9 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
6264
}
6365

6466
@Override
65-
public <T> T decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
66-
Object txHolder, CouchbaseResourceHolder holder) {
67-
return decodeEntityBase(id, source, cas, entityClass, scope, collection, txHolder, holder);
67+
public <T> T decodeEntity(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass,
68+
String scope, String collection, Object txHolder, CouchbaseResourceHolder holder) {
69+
return decodeEntityBase(id, source, cas, expiryTime, entityClass, scope, collection, txHolder, holder);
6870
}
6971

7072
@Override

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.springframework.data.couchbase.core.ReactiveExistsByIdOperationSupport.ReactiveExistsByIdSupport;
2222
import org.springframework.data.couchbase.core.query.OptionsBuilder;
23-
import org.springframework.util.Assert;
2423

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

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

8382
@Override
8483
public TerminatingExistsById withOptions(final ExistsOptions options) {
85-
Assert.notNull(options, "Options must not be null.");
86-
return new ExecutableExistsByIdSupport(template, domainType, scope, collection, options);
84+
return new ExecutableExistsByIdSupport(template, domainType, scope, collection,
85+
options != null ? options : this.options);
8786
}
8887

8988
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ public TerminatingFindByAnalytics<T> matching(final AnalyticsQuery query) {
9292

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

10099
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public Collection<? extends T> all(final Collection<String> ids) {
7878

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

8585
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ public boolean exists() {
168168

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

176175
@Override

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.springframework.data.couchbase.core.ReactiveFindFromReplicasByIdOperationSupport.ReactiveFindFromReplicasByIdSupport;
2121
import org.springframework.data.couchbase.core.query.OptionsBuilder;
22-
import org.springframework.util.Assert;
2322

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

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

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

7877
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public Collection<? extends T> all(Collection<? extends T> objects) {
8585

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

0 commit comments

Comments
 (0)