Skip to content

Commit d531dbd

Browse files
committed
DATACOUCH-645 - Support Document(expiryExpression) annotation.
1) get the expiryExpression from the converted CouchbaseDocument. 2) added interfaces for apis on core objects which are common among many objects. For instance withExpiry and withExpirty on all the insert/replace/upsert/remove objects. The definition of an interface simplyfies testing of all the possible combinations - instead of having four (or eight, counting Reactive), there is one interface to test them all.
1 parent 0494247 commit d531dbd

22 files changed

+357
-229
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,32 @@ public interface ExecutableInsertByIdOperation {
2626

2727
<T> ExecutableInsertById<T> insertById(Class<T> domainType);
2828

29-
interface TerminatingInsertById<T> {
29+
interface TerminatingInsertById<T> extends OneAndAll<T>{
3030

31+
@Override
3132
T one(T object);
3233

34+
@Override
3335
Collection<? extends T> all(Collection<? extends T> objects);
3436

3537
}
3638

37-
interface InsertByIdWithCollection<T> extends TerminatingInsertById<T> {
39+
interface InsertByIdWithCollection<T> extends TerminatingInsertById<T>, InCollection<T> {
3840

3941
TerminatingInsertById<T> inCollection(String collection);
4042
}
4143

42-
interface InsertByIdWithDurability<T> extends InsertByIdWithCollection<T> {
44+
interface InsertByIdWithDurability<T> extends InsertByIdWithCollection<T>, WithDurability<T> {
4345

4446
InsertByIdWithCollection<T> withDurability(DurabilityLevel durabilityLevel);
4547

4648
InsertByIdWithCollection<T> withDurability(PersistTo persistTo, ReplicateTo replicateTo);
4749

4850
}
4951

50-
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
52+
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T>, WithExpiry<T> {
5153

54+
@Override
5255
InsertByIdWithDurability<T> withExpiry(Duration expiry);
5356
}
5457

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ interface TerminatingRemoveById {
3434

3535
}
3636

37-
interface RemoveByIdWithCollection extends TerminatingRemoveById {
37+
interface RemoveByIdWithCollection extends TerminatingRemoveById, InCollection {
3838

3939
TerminatingRemoveById inCollection(String collection);
4040
}
4141

42-
interface RemoveByIdWithDurability extends RemoveByIdWithCollection {
42+
interface RemoveByIdWithDurability extends RemoveByIdWithCollection, WithDurability {
4343

4444
RemoveByIdWithCollection withDurability(DurabilityLevel durabilityLevel);
4545

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,40 @@
1919
import java.util.Collection;
2020

2121
import com.couchbase.client.core.msg.kv.DurabilityLevel;
22+
import com.couchbase.client.java.kv.IncrementOptions;
2223
import com.couchbase.client.java.kv.PersistTo;
2324
import com.couchbase.client.java.kv.ReplicateTo;
2425

2526
public interface ExecutableReplaceByIdOperation {
2627

2728
<T> ExecutableReplaceById<T> replaceById(Class<T> domainType);
2829

29-
interface TerminatingReplaceById<T> {
30+
interface TerminatingReplaceById<T> extends OneAndAll<T> {
3031

32+
@Override
3133
T one(T object);
3234

35+
@Override
3336
Collection<? extends T> all(Collection<? extends T> objects);
3437

3538
}
3639

37-
interface ReplaceByIdWithCollection<T> extends TerminatingReplaceById<T> {
40+
interface ReplaceByIdWithCollection<T> extends TerminatingReplaceById<T> , InCollection<T> {
3841

3942
TerminatingReplaceById<T> inCollection(String collection);
4043
}
4144

42-
interface ReplaceByIdWithDurability<T> extends ReplaceByIdWithCollection<T> {
45+
interface ReplaceByIdWithDurability<T> extends ReplaceByIdWithCollection<T>, WithDurability<T> {
4346

4447
ReplaceByIdWithCollection<T> withDurability(DurabilityLevel durabilityLevel);
4548

4649
ReplaceByIdWithCollection<T> withDurability(PersistTo persistTo, ReplicateTo replicateTo);
4750

4851
}
4952

50-
interface ReplaceByIdWithExpiry<T> extends ReplaceByIdWithDurability<T> {
53+
interface ReplaceByIdWithExpiry<T> extends ReplaceByIdWithDurability<T>, WithExpiry<T> {
5154

55+
@Override
5256
ReplaceByIdWithDurability<T> withExpiry(final Duration expiry);
5357
}
5458

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,32 @@ public interface ExecutableUpsertByIdOperation {
2626

2727
<T> ExecutableUpsertById<T> upsertById(Class<T> domainType);
2828

29-
interface TerminatingUpsertById<T> {
29+
interface TerminatingUpsertById<T> extends OneAndAll<T>{
3030

31+
@Override
3132
T one(T object);
3233

34+
@Override
3335
Collection<? extends T> all(Collection<? extends T> objects);
3436

3537
}
3638

37-
interface UpsertByIdWithCollection<T> extends TerminatingUpsertById<T> {
39+
interface UpsertByIdWithCollection<T> extends TerminatingUpsertById<T>, InCollection<T> {
3840

3941
TerminatingUpsertById<T> inCollection(String collection);
4042
}
4143

42-
interface UpsertByIdWithDurability<T> extends UpsertByIdWithCollection<T> {
44+
interface UpsertByIdWithDurability<T> extends UpsertByIdWithCollection<T>, WithDurability<T> {
4345

4446
UpsertByIdWithCollection<T> withDurability(DurabilityLevel durabilityLevel);
4547

4648
UpsertByIdWithCollection<T> withDurability(PersistTo persistTo, ReplicateTo replicateTo);
4749

4850
}
4951

50-
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T> {
52+
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T>, WithExpiry<T> {
5153

54+
@Override
5255
UpsertByIdWithDurability<T> withExpiry(Duration expiry);
5356
}
5457

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.core;
17+
18+
/**
19+
* A common interface for all of Insert, Replace, Upsert that take collection
20+
*
21+
* @author Michael Reiche
22+
*
23+
* @param <T> - the entity class
24+
*/
25+
public interface InCollection<T> {
26+
Object inCollection(String collection);
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.core;
17+
18+
import java.util.Collection;
19+
20+
/**
21+
* A common interface for all of Insert, Replace, Upsert
22+
*
23+
* @author Michael Reiche
24+
*
25+
* @param <T> - the entity class
26+
*/
27+
public interface OneAndAll<T> {
28+
29+
T one(T object);
30+
31+
Collection<? extends T> all(Collection<? extends T> objects);
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.core;
17+
18+
import reactor.core.publisher.Flux;
19+
import reactor.core.publisher.Mono;
20+
21+
import java.util.Collection;
22+
23+
/**
24+
* A common interface for all of Insert, Replace, Upsert
25+
*
26+
* @author Michael Reiche
27+
*
28+
* @param <T> - the entity class
29+
*/
30+
31+
public interface OneAndAllReactive<T> {
32+
Mono<T> one(T object);
33+
34+
Flux<? extends T> all(Collection<? extends T> objects);
35+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ public interface ReactiveInsertByIdOperation {
2929

3030
<T> ReactiveInsertById<T> insertById(Class<T> domainType);
3131

32-
interface TerminatingInsertById<T> {
32+
interface TerminatingInsertById<T> extends OneAndAllReactive<T>{
3333

3434
Mono<T> one(T object);
3535

3636
Flux<? extends T> all(Collection<? extends T> objects);
3737

3838
}
3939

40-
interface InsertByIdWithCollection<T> extends TerminatingInsertById<T> {
40+
interface InsertByIdWithCollection<T> extends TerminatingInsertById<T>, InCollection<T> {
4141

4242
TerminatingInsertById<T> inCollection(String collection);
4343
}
4444

45-
interface InsertByIdWithDurability<T> extends InsertByIdWithCollection<T> {
45+
interface InsertByIdWithDurability<T> extends InsertByIdWithCollection<T>, WithDurability<T> {
4646

4747
InsertByIdWithCollection<T> withDurability(DurabilityLevel durabilityLevel);
4848

4949
InsertByIdWithCollection<T> withDurability(PersistTo persistTo, ReplicateTo replicateTo);
5050

5151
}
5252

53-
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
53+
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T>, WithExpiry<T>{
5454

5555
InsertByIdWithDurability<T> withExpiry(Duration expiry);
5656
}

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

Lines changed: 4 additions & 7 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

@@ -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,7 +89,7 @@ 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);
@@ -99,10 +98,8 @@ private InsertOptions buildInsertOptions() {
9998
}
10099
if (expiry != null && !expiry.isZero()) {
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/ReactiveRemoveByIdOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ interface TerminatingRemoveById {
3636

3737
}
3838

39-
interface RemoveByIdWithCollection extends TerminatingRemoveById {
39+
interface RemoveByIdWithCollection extends TerminatingRemoveById, InCollection {
4040

4141
TerminatingRemoveById inCollection(String collection);
4242
}
4343

44-
interface RemoveByIdWithDurability extends RemoveByIdWithCollection {
44+
interface RemoveByIdWithDurability extends RemoveByIdWithCollection, WithDurability {
4545

4646
RemoveByIdWithCollection withDurability(DurabilityLevel durabilityLevel);
4747

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ public interface ReactiveReplaceByIdOperation {
2929

3030
<T> ReactiveReplaceById<T> replaceById(Class<T> domainType);
3131

32-
interface TerminatingReplaceById<T> {
32+
interface TerminatingReplaceById<T> extends OneAndAllReactive<T> {
3333

3434
Mono<T> one(T object);
3535

3636
Flux<? extends T> all(Collection<? extends T> objects);
3737

3838
}
3939

40-
interface ReplaceByIdWithCollection<T> extends TerminatingReplaceById<T> {
40+
interface ReplaceByIdWithCollection<T> extends TerminatingReplaceById<T>, InCollection<T> {
4141

4242
TerminatingReplaceById<T> inCollection(String collection);
4343
}
4444

45-
interface ReplaceByIdWithDurability<T> extends ReplaceByIdWithCollection<T> {
45+
interface ReplaceByIdWithDurability<T> extends ReplaceByIdWithCollection<T>, WithDurability<T> {
4646

4747
ReplaceByIdWithCollection<T> withDurability(DurabilityLevel durabilityLevel);
4848

4949
ReplaceByIdWithCollection<T> withDurability(PersistTo persistTo, ReplicateTo replicateTo);
5050

5151
}
5252

53-
interface ReplaceByIdWithExpiry<T> extends ReplaceByIdWithDurability<T> {
53+
interface ReplaceByIdWithExpiry<T> extends ReplaceByIdWithDurability<T>, WithExpiry<T> {
5454

5555
ReplaceByIdWithDurability<T> withExpiry(final Duration expiry);
5656
}

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

Lines changed: 5 additions & 8 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;
@@ -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,7 +87,7 @@ 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);
@@ -97,10 +96,8 @@ private ReplaceOptions buildReplaceOptions(T object) {
9796
}
9897
if (expiry != null && !expiry.isZero()) {
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);

0 commit comments

Comments
 (0)