@@ -27,6 +27,12 @@ import com.couchbase.client.core.msg.kv.CodecFlags
2727import com .couchbase .client .core .transaction .CoreTransactionAttemptContext
2828import com .couchbase .client .core .transaction .support .SpanWrapper
2929import com .couchbase .client .scala .codec .JsonSerializer
30+ import com .couchbase .client .scala .transactions .config .{
31+ TransactionGetOptions ,
32+ TransactionGetReplicaFromPreferredServerGroupOptions ,
33+ TransactionInsertOptions ,
34+ TransactionReplaceOptions
35+ }
3036import com .couchbase .client .scala .transactions .internal .EncodingUtil .encode
3137import com .couchbase .client .scala .util .FutureConversions
3238import com .couchbase .client .scala .{ReactiveCollection , ReactiveScope }
@@ -56,9 +62,27 @@ class ReactiveTransactionAttemptContext private[scala] (
5662 * @return a <code>TransactionGetResult</code> containing the document
5763 */
5864 def get (collection : ReactiveCollection , id : String ): SMono [TransactionGetResult ] = {
65+ get(collection, id, TransactionGetOptions .Default )
66+ }
67+
68+ /**
69+ * Gets a document with the specified <code>id</code> and from the specified Couchbase <code>collection</code>.
70+ * <p>
71+ * If the document does not exist it will raise a [[com.couchbase.client.core.error.DocumentNotFoundException ]].
72+ *
73+ * @param collection the Couchbase collection the document exists on
74+ * @param id the document's ID
75+ * @param options options controlling the operation
76+ * @return a <code>TransactionGetResult</code> containing the document
77+ */
78+ def get (
79+ collection : ReactiveCollection ,
80+ id : String ,
81+ options : TransactionGetOptions
82+ ): SMono [TransactionGetResult ] = {
5983 FutureConversions
6084 .javaMonoToScalaMono(internal.get(collection.collectionIdentifier, id))
61- .map(TransactionGetResult )
85+ .map(result => TransactionGetResult (result, options.transcoder) )
6286 }
6387
6488 /** Gets a document from the specified Couchbase <code>collection</code> matching the specified <code>id</code>.
@@ -78,12 +102,38 @@ class ReactiveTransactionAttemptContext private[scala] (
78102 def getReplicaFromPreferredServerGroup (
79103 collection : ReactiveCollection ,
80104 id : String
105+ ): SMono [TransactionGetResult ] =
106+ getReplicaFromPreferredServerGroup(
107+ collection,
108+ id,
109+ TransactionGetReplicaFromPreferredServerGroupOptions .Default
110+ )
111+
112+ /** Gets a document from the specified Couchbase <code>collection</code> matching the specified <code>id</code>.
113+ * <p>
114+ * It will be fetched only from document copies that on nodes in the preferred server group, which can
115+ * be configured with [[com.couchbase.client.scala.env.ClusterEnvironment.Builder.preferredServerGroup ]].
116+ * <p>
117+ * If no replica can be retrieved, which can include for reasons such as this preferredServerGroup not being set,
118+ * and misconfigured server groups, then [[com.couchbase.client.core.error.DocumentUnretrievableException ]]
119+ * can be raised. It is strongly recommended that this method always be used with a fallback strategy to use
120+ * ctx.get() on failure.
121+ *
122+ * @param collection the Couchbase collection the document exists on
123+ * @param id the document's ID
124+ * @param options options controlling the operation
125+ * @return a <code>TransactionGetResult</code> containing the document
126+ */
127+ def getReplicaFromPreferredServerGroup (
128+ collection : ReactiveCollection ,
129+ id : String ,
130+ options : TransactionGetReplicaFromPreferredServerGroupOptions
81131 ): SMono [TransactionGetResult ] =
82132 FutureConversions
83133 .javaMonoToScalaMono(
84134 internal.getReplicaFromPreferredServerGroup(collection.collectionIdentifier, id)
85135 )
86- .map(TransactionGetResult )
136+ .map(result => TransactionGetResult (result, options.transcoder) )
87137
88138 /**
89139 * Inserts a new document into the specified Couchbase <code>collection</code>.
@@ -95,23 +145,43 @@ class ReactiveTransactionAttemptContext private[scala] (
95145 */
96146 def insert [T ](collection : ReactiveCollection , id : String , content : T )(
97147 implicit serializer : JsonSerializer [T ]
148+ ): SMono [TransactionGetResult ] = {
149+ insert(collection, id, content, TransactionInsertOptions .Default )
150+ }
151+
152+ /**
153+ * Inserts a new document into the specified Couchbase <code>collection</code>.
154+ *
155+ * @param collection the Couchbase collection in which to insert the doc
156+ * @param id the document's unique ID
157+ * @param content $SupportedTypes
158+ * @param options options controlling the operation
159+ * @return the doc, updated with its new CAS value and ID, and converted to a <code>TransactionGetResult</code>
160+ */
161+ def insert [T ](
162+ collection : ReactiveCollection ,
163+ id : String ,
164+ content : T ,
165+ options : TransactionInsertOptions
166+ )(
167+ implicit serializer : JsonSerializer [T ]
98168 ): SMono [TransactionGetResult ] = {
99169 val span = CbTracing .newSpan(internal.core().context(), TRANSACTION_OP_INSERT , internal.span())
100170 span.lowCardinalityAttribute(TracingIdentifiers .ATTR_OPERATION , TRANSACTION_OP_INSERT )
101- encode(content, span, serializer, internal.core.context) match {
171+ encode(content, span, serializer, options.transcoder, internal.core.context) match {
102172 case Failure (exception) => SMono .raiseError(exception)
103173 case Success (encoded) =>
104174 FutureConversions
105175 .javaMonoToScalaMono(
106176 internal.insert(
107177 collection.collectionIdentifier,
108178 id,
109- encoded,
110- CodecFlags . JSON_COMPAT_FLAGS ,
179+ encoded.encoded ,
180+ encoded.flags ,
111181 new SpanWrapper (span)
112182 )
113183 )
114- .map(TransactionGetResult )
184+ .map(result => TransactionGetResult (result, options.transcoder) )
115185 .doOnError(_ => span.status(RequestSpan .StatusCode .ERROR ))
116186 .doOnTerminate(() => span.end())
117187 }
@@ -127,18 +197,33 @@ class ReactiveTransactionAttemptContext private[scala] (
127197 */
128198 def replace [T ](doc : TransactionGetResult , content : T )(
129199 implicit serializer : JsonSerializer [T ]
200+ ): SMono [TransactionGetResult ] = {
201+ replace(doc, content, TransactionReplaceOptions .Default )
202+ }
203+
204+ /**
205+ * Mutates the specified <code>doc</code> with new content.
206+ *
207+ * @param doc the doc to be mutated
208+ * @param content $SupportedTypes
209+ * @param options options controlling the operation
210+ * @return the doc, updated with its new CAS value. For performance a copy is not created and the original doc
211+ * object is modified.
212+ */
213+ def replace [T ](doc : TransactionGetResult , content : T , options : TransactionReplaceOptions )(
214+ implicit serializer : JsonSerializer [T ]
130215 ): SMono [TransactionGetResult ] = {
131216 val span = CbTracing .newSpan(internal.core().context(), TRANSACTION_OP_REPLACE , internal.span())
132217 span.lowCardinalityAttribute(TracingIdentifiers .ATTR_OPERATION , TRANSACTION_OP_REPLACE )
133- encode(content, span, serializer, internal.core.context) match {
218+ encode(content, span, serializer, options.transcoder, internal.core.context) match {
134219 case Failure (exception) => SMono .raiseError(exception)
135220 case Success (encoded) =>
136221 FutureConversions
137222 .javaMonoToScalaMono(
138223 internal
139- .replace(doc.internal, encoded, CodecFlags . JSON_COMPAT_FLAGS , new SpanWrapper (span))
224+ .replace(doc.internal, encoded.encoded, encoded.flags , new SpanWrapper (span))
140225 )
141- .map(TransactionGetResult )
226+ .map(result => TransactionGetResult (result, options.transcoder) )
142227 .doOnError(_ => span.status(RequestSpan .StatusCode .ERROR ))
143228 .doOnTerminate(() => span.end())
144229 }
0 commit comments