15
15
*/
16
16
package org .springframework .data .couchbase .core ;
17
17
18
- import static com .couchbase .client .java .kv .GetOptions . getOptions ;
18
+ import static com .couchbase .client .java .kv .GetAndTouchOptions . getAndTouchOptions ;
19
19
20
20
import reactor .core .publisher .Flux ;
21
21
import reactor .core .publisher .Mono ;
22
22
23
+ import java .time .Duration ;
23
24
import java .util .Arrays ;
24
25
import java .util .Collection ;
25
26
import java .util .List ;
30
31
import org .springframework .util .Assert ;
31
32
32
33
import com .couchbase .client .core .error .DocumentNotFoundException ;
34
+ import com .couchbase .client .java .CommonOptions ;
33
35
import com .couchbase .client .java .codec .RawJsonTranscoder ;
36
+ import com .couchbase .client .java .kv .GetAndTouchOptions ;
34
37
import com .couchbase .client .java .kv .GetOptions ;
35
38
36
39
public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperation {
@@ -44,7 +47,7 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
44
47
45
48
@ Override
46
49
public <T > ReactiveFindById <T > findById (Class <T > domainType ) {
47
- return new ReactiveFindByIdSupport <>(template , domainType , null , null , null , null , template .support ());
50
+ return new ReactiveFindByIdSupport <>(template , domainType , null , null , null , null , null , template .support ());
48
51
}
49
52
50
53
static class ReactiveFindByIdSupport <T > implements ReactiveFindById <T > {
@@ -53,36 +56,53 @@ static class ReactiveFindByIdSupport<T> implements ReactiveFindById<T> {
53
56
private final Class <T > domainType ;
54
57
private final String scope ;
55
58
private final String collection ;
56
- private final GetOptions options ;
59
+ private final CommonOptions <?> options ;
57
60
private final List <String > fields ;
58
61
private final ReactiveTemplateSupport support ;
62
+ private final Duration expiry ;
59
63
60
64
ReactiveFindByIdSupport (ReactiveCouchbaseTemplate template , Class <T > domainType , String scope , String collection ,
61
- GetOptions options , List <String > fields , ReactiveTemplateSupport support ) {
65
+ CommonOptions <?> options , List <String > fields , Duration expiry , ReactiveTemplateSupport support ) {
62
66
this .template = template ;
63
67
this .domainType = domainType ;
64
68
this .scope = scope ;
65
69
this .collection = collection ;
66
70
this .options = options ;
67
71
this .fields = fields ;
72
+ this .expiry = expiry ;
68
73
this .support = support ;
69
74
}
70
75
71
76
@ Override
72
77
public Mono <T > one (final String id ) {
73
- GetOptions gOptions = options != null ? options : getOptions ();
74
- if (gOptions .build ().transcoder () == null ) {
75
- gOptions .transcoder (RawJsonTranscoder .INSTANCE );
76
- }
77
- if (fields != null && !fields .isEmpty ()) {
78
- gOptions .project (fields );
78
+ PseudoArgs <?> pArgs ;
79
+ if (expiry != null ) {
80
+ GetAndTouchOptions getAndTouchOptions = options != null ? (GetAndTouchOptions ) options : getAndTouchOptions ();
81
+ if (getAndTouchOptions .build ().transcoder () == null ) {
82
+ getAndTouchOptions .transcoder (RawJsonTranscoder .INSTANCE );
83
+ }
84
+ pArgs = new PseudoArgs (template , scope , collection , getAndTouchOptions , domainType );
85
+ } else {
86
+ GetOptions getOptions = options != null ? (GetOptions ) options : GetOptions .getOptions ();
87
+ if (getOptions .build ().transcoder () == null ) {
88
+ getOptions .transcoder (RawJsonTranscoder .INSTANCE );
89
+ }
90
+ if (fields != null && !fields .isEmpty ()) {
91
+ getOptions .project (fields );
92
+ }
93
+ pArgs = new PseudoArgs (template , scope , collection , getOptions , domainType );
94
+
79
95
}
80
- PseudoArgs <GetOptions > pArgs = new PseudoArgs (template , scope , collection , gOptions , domainType );
81
96
LOG .trace ("findById {}" , pArgs );
82
- return Mono .just (id )
83
- .flatMap (docId -> template .getCouchbaseClientFactory ().withScope (pArgs .getScope ())
84
- .getCollection (pArgs .getCollection ()).reactive ().get (docId , pArgs .getOptions ()))
85
- .flatMap (result -> support .decodeEntity (id , result .contentAs (String .class ), result .cas (), domainType ))
97
+ return Mono .just (id ).flatMap (docId -> {
98
+ if (expiry != null ) {
99
+ return template .getCouchbaseClientFactory ().withScope (pArgs .getScope ()).getCollection (pArgs .getCollection ())
100
+ .reactive ().getAndTouch (docId , expiry , (GetAndTouchOptions ) pArgs .getOptions ());
101
+ } else {
102
+ return template .getCouchbaseClientFactory ().withScope (pArgs .getScope ()).getCollection (pArgs .getCollection ())
103
+ .reactive ().get (docId , (GetOptions ) pArgs .getOptions ());
104
+ }
105
+ }).flatMap (result -> support .decodeEntity (id , result .contentAs (String .class ), result .cas (), domainType ))
86
106
.onErrorResume (throwable -> {
87
107
if (throwable instanceof RuntimeException ) {
88
108
if (throwable instanceof DocumentNotFoundException ) {
@@ -107,25 +127,31 @@ public Flux<? extends T> all(final Collection<String> ids) {
107
127
@ Override
108
128
public TerminatingFindById <T > withOptions (final GetOptions options ) {
109
129
Assert .notNull (options , "Options must not be null." );
110
- return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , support );
130
+ return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , expiry , support );
111
131
}
112
132
113
133
@ Override
114
134
public FindByIdWithOptions <T > inCollection (final String collection ) {
115
- return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , support );
135
+ return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , expiry , support );
116
136
}
117
137
118
138
@ Override
119
139
public FindByIdInCollection <T > inScope (final String scope ) {
120
- return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , support );
140
+ return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , expiry , support );
121
141
}
122
142
123
143
@ Override
124
144
public FindByIdInScope <T > project (String ... fields ) {
125
145
Assert .notNull (fields , "Fields must not be null" );
126
146
return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , Arrays .asList (fields ),
127
- support );
147
+ expiry , support );
148
+ }
149
+
150
+ @ Override
151
+ public FindByIdWithProjection <T > withExpiry (final Duration expiry ) {
152
+ return new ReactiveFindByIdSupport <>(template , domainType , scope , collection , options , fields , expiry , support );
128
153
}
154
+
129
155
}
130
156
131
157
}
0 commit comments