15
15
*/
16
16
package org .springframework .data .couchbase .core ;
17
17
18
+ import com .couchbase .client .java .kv .UpsertOptions ;
19
+ import org .springframework .data .couchbase .core .mapping .Document ;
18
20
import reactor .core .publisher .Flux ;
19
21
import reactor .core .publisher .Mono ;
20
22
23
+ import java .time .Duration ;
21
24
import java .util .Collection ;
22
25
23
26
import org .springframework .data .couchbase .core .mapping .CouchbaseDocument ;
@@ -40,7 +43,7 @@ public ReactiveInsertByIdOperationSupport(final ReactiveCouchbaseTemplate templa
40
43
public <T > ReactiveInsertById <T > insertById (final Class <T > domainType ) {
41
44
Assert .notNull (domainType , "DomainType must not be null!" );
42
45
return new ReactiveInsertByIdSupport <>(template , domainType , null , PersistTo .NONE , ReplicateTo .NONE ,
43
- DurabilityLevel .NONE );
46
+ DurabilityLevel .NONE , null );
44
47
}
45
48
46
49
static class ReactiveInsertByIdSupport <T > implements ReactiveInsertById <T > {
@@ -51,16 +54,18 @@ static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
51
54
private final PersistTo persistTo ;
52
55
private final ReplicateTo replicateTo ;
53
56
private final DurabilityLevel durabilityLevel ;
57
+ private final Duration expiry ;
54
58
55
59
ReactiveInsertByIdSupport (final ReactiveCouchbaseTemplate template , final Class <T > domainType ,
56
60
final String collection , final PersistTo persistTo , final ReplicateTo replicateTo ,
57
- final DurabilityLevel durabilityLevel ) {
61
+ final DurabilityLevel durabilityLevel , Duration expiry ) {
58
62
this .template = template ;
59
63
this .domainType = domainType ;
60
64
this .collection = collection ;
61
65
this .persistTo = persistTo ;
62
66
this .replicateTo = replicateTo ;
63
67
this .durabilityLevel = durabilityLevel ;
68
+ this .expiry = expiry ;
64
69
}
65
70
66
71
@ Override
@@ -93,28 +98,39 @@ private InsertOptions buildInsertOptions() {
93
98
} else if (durabilityLevel != DurabilityLevel .NONE ) {
94
99
options .durability (durabilityLevel );
95
100
}
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
+ }
96
108
return options ;
97
109
}
98
110
99
111
@ Override
100
112
public TerminatingInsertById <T > inCollection (final String collection ) {
101
113
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 );
103
115
}
104
116
105
117
@ Override
106
118
public InsertByIdWithCollection <T > withDurability (final DurabilityLevel durabilityLevel ) {
107
119
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 );
109
121
}
110
122
111
123
@ Override
112
124
public InsertByIdWithCollection <T > withDurability (final PersistTo persistTo , final ReplicateTo replicateTo ) {
113
125
Assert .notNull (persistTo , "PersistTo must not be null." );
114
126
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 );
116
128
}
117
129
130
+ @ Override
131
+ public InsertByIdWithDurability <T > withExpiry (final Duration expiry ) {
132
+ return new ReactiveInsertByIdSupport <>(template , domainType , collection , persistTo , replicateTo , durabilityLevel , expiry );
133
+ }
118
134
}
119
135
120
136
}
0 commit comments