Skip to content

Commit ff06fb5

Browse files
committed
DATACOUCH-608 - propagate expiry option and annotation to insert and upsert calls
1 parent 0b79dab commit ff06fb5

10 files changed

+257
-26
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18+
import java.time.Duration;
1819
import java.util.Collection;
1920

2021
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -46,6 +47,11 @@ interface InsertByIdWithDurability<T> extends InsertByIdWithCollection<T> {
4647

4748
}
4849

49-
interface ExecutableInsertById<T> extends InsertByIdWithDurability<T> {}
50+
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
51+
52+
InsertByIdWithDurability<T> withExpiry(Duration expiry);
53+
}
54+
55+
interface ExecutableInsertById<T> extends InsertByIdWithExpiry<T> {}
5056

5157
}

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18+
import java.time.Duration;
1819
import java.util.Collection;
1920

2021
import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public ExecutableInsertByIdOperationSupport(final CouchbaseTemplate template) {
3536
public <T> ExecutableInsertById<T> insertById(final Class<T> domainType) {
3637
Assert.notNull(domainType, "DomainType must not be null!");
3738
return new ExecutableInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
38-
DurabilityLevel.NONE);
39+
DurabilityLevel.NONE, null);
3940
}
4041

4142
static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {
@@ -46,18 +47,21 @@ static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {
4647
private final PersistTo persistTo;
4748
private final ReplicateTo replicateTo;
4849
private final DurabilityLevel durabilityLevel;
50+
private final Duration expiry;
4951
private final ReactiveInsertByIdOperationSupport.ReactiveInsertByIdSupport<T> reactiveSupport;
5052

5153
ExecutableInsertByIdSupport(final CouchbaseTemplate template, final Class<T> domainType, final String collection,
52-
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel) {
54+
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel,
55+
final Duration expiry) {
5356
this.template = template;
5457
this.domainType = domainType;
5558
this.collection = collection;
5659
this.persistTo = persistTo;
5760
this.replicateTo = replicateTo;
5861
this.durabilityLevel = durabilityLevel;
62+
this.expiry = expiry;
5963
this.reactiveSupport = new ReactiveInsertByIdOperationSupport.ReactiveInsertByIdSupport<>(template.reactive(),
60-
domainType, collection, persistTo, replicateTo, durabilityLevel);
64+
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
6165
}
6266

6367
@Override
@@ -74,22 +78,28 @@ public Collection<? extends T> all(Collection<? extends T> objects) {
7478
public TerminatingInsertById<T> inCollection(final String collection) {
7579
Assert.hasText(collection, "Collection must not be null nor empty.");
7680
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
77-
durabilityLevel);
81+
durabilityLevel, expiry);
7882
}
7983

8084
@Override
8185
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
8286
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
8387
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
84-
durabilityLevel);
88+
durabilityLevel, expiry);
8589
}
8690

8791
@Override
8892
public InsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
8993
Assert.notNull(persistTo, "PersistTo must not be null.");
9094
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
9195
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
92-
durabilityLevel);
96+
durabilityLevel, expiry);
97+
}
98+
99+
@Override
100+
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
101+
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
102+
durabilityLevel, expiry);
93103
}
94104

95105
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18+
import java.time.Duration;
1819
import java.util.Collection;
1920

2021
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -46,6 +47,11 @@ interface UpsertByIdWithDurability<T> extends UpsertByIdWithCollection<T> {
4647

4748
}
4849

49-
interface ExecutableUpsertById<T> extends UpsertByIdWithDurability<T> {}
50+
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T> {
51+
52+
UpsertByIdWithDurability<T> withExpiry(Duration expiry);
53+
}
54+
55+
interface ExecutableUpsertById<T> extends UpsertByIdWithExpiry<T> {}
5056

5157
}

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18+
import java.time.Duration;
1819
import java.util.Collection;
1920

2021
import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public ExecutableUpsertByIdOperationSupport(final CouchbaseTemplate template) {
3536
public <T> ExecutableUpsertById<T> upsertById(final Class<T> domainType) {
3637
Assert.notNull(domainType, "DomainType must not be null!");
3738
return new ExecutableUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
38-
DurabilityLevel.NONE);
39+
DurabilityLevel.NONE, null);
3940
}
4041

4142
static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {
@@ -46,18 +47,21 @@ static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {
4647
private final PersistTo persistTo;
4748
private final ReplicateTo replicateTo;
4849
private final DurabilityLevel durabilityLevel;
50+
private final Duration expiry;
4951
private final ReactiveUpsertByIdOperationSupport.ReactiveUpsertByIdSupport<T> reactiveSupport;
5052

5153
ExecutableUpsertByIdSupport(final CouchbaseTemplate template, final Class<T> domainType, final String collection,
52-
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel) {
54+
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel,
55+
final Duration expiry) {
5356
this.template = template;
5457
this.domainType = domainType;
5558
this.collection = collection;
5659
this.persistTo = persistTo;
5760
this.replicateTo = replicateTo;
5861
this.durabilityLevel = durabilityLevel;
62+
this.expiry = expiry;
5963
this.reactiveSupport = new ReactiveUpsertByIdOperationSupport.ReactiveUpsertByIdSupport<>(template.reactive(),
60-
domainType, collection, persistTo, replicateTo, durabilityLevel);
64+
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
6165
}
6266

6367
@Override
@@ -74,22 +78,30 @@ public Collection<? extends T> all(Collection<? extends T> objects) {
7478
public TerminatingUpsertById<T> inCollection(final String collection) {
7579
Assert.hasText(collection, "Collection must not be null nor empty.");
7680
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
77-
durabilityLevel);
81+
durabilityLevel, expiry);
7882
}
7983

8084
@Override
8185
public UpsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
8286
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
8387
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
84-
durabilityLevel);
88+
durabilityLevel, expiry);
8589
}
8690

8791
@Override
8892
public UpsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
8993
Assert.notNull(persistTo, "PersistTo must not be null.");
9094
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
9195
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
92-
durabilityLevel);
96+
durabilityLevel, expiry);
97+
}
98+
99+
@Override
100+
public UpsertByIdWithDurability<T> withExpiry(final Duration expiry) {
101+
Assert.notNull(persistTo, "PersistTo must not be null.");
102+
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
103+
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
104+
durabilityLevel, expiry);
93105
}
94106

95107
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import reactor.core.publisher.Flux;
1919
import reactor.core.publisher.Mono;
2020

21+
import java.time.Duration;
2122
import java.util.Collection;
2223

2324
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ interface InsertByIdWithDurability<T> extends InsertByIdWithCollection<T> {
4950

5051
}
5152

52-
interface ReactiveInsertById<T> extends InsertByIdWithDurability<T> {}
53+
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
54+
55+
InsertByIdWithDurability<T> withExpiry(Duration expiry);
56+
}
57+
58+
interface ReactiveInsertById<T> extends InsertByIdWithExpiry<T> {}
5359

5460
}

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

+21-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18+
import com.couchbase.client.java.kv.UpsertOptions;
19+
import org.springframework.data.couchbase.core.mapping.Document;
1820
import reactor.core.publisher.Flux;
1921
import reactor.core.publisher.Mono;
2022

23+
import java.time.Duration;
2124
import java.util.Collection;
2225

2326
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
@@ -40,7 +43,7 @@ public ReactiveInsertByIdOperationSupport(final ReactiveCouchbaseTemplate templa
4043
public <T> ReactiveInsertById<T> insertById(final Class<T> domainType) {
4144
Assert.notNull(domainType, "DomainType must not be null!");
4245
return new ReactiveInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
43-
DurabilityLevel.NONE);
46+
DurabilityLevel.NONE, null);
4447
}
4548

4649
static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
@@ -51,16 +54,18 @@ static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
5154
private final PersistTo persistTo;
5255
private final ReplicateTo replicateTo;
5356
private final DurabilityLevel durabilityLevel;
57+
private final Duration expiry;
5458

5559
ReactiveInsertByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
5660
final String collection, final PersistTo persistTo, final ReplicateTo replicateTo,
57-
final DurabilityLevel durabilityLevel) {
61+
final DurabilityLevel durabilityLevel, Duration expiry) {
5862
this.template = template;
5963
this.domainType = domainType;
6064
this.collection = collection;
6165
this.persistTo = persistTo;
6266
this.replicateTo = replicateTo;
6367
this.durabilityLevel = durabilityLevel;
68+
this.expiry = expiry;
6469
}
6570

6671
@Override
@@ -93,28 +98,39 @@ private InsertOptions buildInsertOptions() {
9398
} else if (durabilityLevel != DurabilityLevel.NONE) {
9499
options.durability(durabilityLevel);
95100
}
101+
if (expiry != null) {
102+
options.expiry(expiry);
103+
} else if (domainType.isAnnotationPresent(Document.class)) {
104+
Document documentAnn = domainType.getAnnotation(Document.class);
105+
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
106+
options.expiry(Duration.ofSeconds(durationSeconds));
107+
}
96108
return options;
97109
}
98110

99111
@Override
100112
public TerminatingInsertById<T> inCollection(final String collection) {
101113
Assert.hasText(collection, "Collection must not be null nor empty.");
102-
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
114+
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
103115
}
104116

105117
@Override
106118
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
107119
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
108-
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
120+
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
109121
}
110122

111123
@Override
112124
public InsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
113125
Assert.notNull(persistTo, "PersistTo must not be null.");
114126
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
115-
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
127+
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
116128
}
117129

130+
@Override
131+
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
132+
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
133+
}
118134
}
119135

120136
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import reactor.core.publisher.Flux;
1919
import reactor.core.publisher.Mono;
2020

21+
import java.time.Duration;
2122
import java.util.Collection;
2223

2324
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ interface UpsertByIdWithDurability<T> extends UpsertByIdWithCollection<T> {
4950

5051
}
5152

52-
interface ReactiveUpsertById<T> extends UpsertByIdWithDurability<T> {}
53+
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T> {
54+
55+
UpsertByIdWithDurability<T> withExpiry(Duration expiry);
56+
}
57+
58+
interface ReactiveUpsertById<T> extends UpsertByIdWithExpiry<T> {}
5359

5460
}

0 commit comments

Comments
 (0)