Skip to main content

couchbase_core/options/
crud.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use std::sync::Arc;
20
21use crate::memdx::datatype::DataTypeFlag;
22use crate::memdx::durability_level::DurabilityLevel;
23use crate::memdx::subdoc::{LookupInOp, MutateInOp, SubdocDocFlag};
24use crate::retry::{RetryStrategy, DEFAULT_RETRY_STRATEGY};
25
26#[derive(Clone, Debug)]
27#[non_exhaustive]
28pub struct GetOptions<'a> {
29    pub key: &'a [u8],
30    pub scope_name: &'a str,
31    pub collection_name: &'a str,
32    pub retry_strategy: Arc<dyn RetryStrategy>,
33}
34
35impl<'a> GetOptions<'a> {
36    pub fn new(key: &'a [u8], scope_name: &'a str, collection_name: &'a str) -> Self {
37        Self {
38            key,
39            scope_name,
40            collection_name,
41            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
42        }
43    }
44
45    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
46        self.retry_strategy = retry_strategy;
47        self
48    }
49}
50
51#[derive(Clone, Debug)]
52#[non_exhaustive]
53pub struct GetMetaOptions<'a> {
54    pub key: &'a [u8],
55    pub scope_name: &'a str,
56    pub collection_name: &'a str,
57    pub retry_strategy: Arc<dyn RetryStrategy>,
58}
59
60impl<'a> GetMetaOptions<'a> {
61    pub fn new(key: &'a [u8], scope_name: &'a str, collection_name: &'a str) -> Self {
62        Self {
63            key,
64            scope_name,
65            collection_name,
66            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
67        }
68    }
69
70    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
71        self.retry_strategy = retry_strategy;
72        self
73    }
74}
75
76#[derive(Clone, Debug)]
77#[non_exhaustive]
78pub struct UpsertOptions<'a> {
79    pub key: &'a [u8],
80    pub scope_name: &'a str,
81    pub collection_name: &'a str,
82    pub value: &'a [u8],
83    pub flags: u32,
84    pub datatype: DataTypeFlag,
85    pub expiry: Option<u32>,
86    pub preserve_expiry: Option<bool>,
87    pub cas: Option<u64>,
88    pub durability_level: Option<DurabilityLevel>,
89    pub retry_strategy: Arc<dyn RetryStrategy>,
90}
91
92impl<'a> UpsertOptions<'a> {
93    pub fn new(
94        key: &'a [u8],
95        scope_name: &'a str,
96        collection_name: &'a str,
97        value: &'a [u8],
98    ) -> Self {
99        Self {
100            key,
101            scope_name,
102            collection_name,
103            value,
104            flags: 0,
105            datatype: DataTypeFlag::default(),
106            expiry: None,
107            preserve_expiry: None,
108            cas: None,
109            durability_level: None,
110            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
111        }
112    }
113
114    pub fn flags(mut self, flags: u32) -> Self {
115        self.flags = flags;
116        self
117    }
118
119    pub fn datatype(mut self, datatype: DataTypeFlag) -> Self {
120        self.datatype = datatype;
121        self
122    }
123
124    pub fn expiry(mut self, expiry: impl Into<Option<u32>>) -> Self {
125        self.expiry = expiry.into();
126        self
127    }
128
129    pub fn preserve_expiry(mut self, preserve_expiry: impl Into<Option<bool>>) -> Self {
130        self.preserve_expiry = preserve_expiry.into();
131        self
132    }
133
134    pub fn cas(mut self, cas: impl Into<Option<u64>>) -> Self {
135        self.cas = cas.into();
136        self
137    }
138
139    pub fn durability_level(
140        mut self,
141        durability_level: impl Into<Option<DurabilityLevel>>,
142    ) -> Self {
143        self.durability_level = durability_level.into();
144        self
145    }
146
147    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
148        self.retry_strategy = retry_strategy;
149        self
150    }
151}
152
153#[derive(Clone, Debug)]
154#[non_exhaustive]
155pub struct DeleteOptions<'a> {
156    pub key: &'a [u8],
157    pub scope_name: &'a str,
158    pub collection_name: &'a str,
159    pub cas: Option<u64>,
160    pub durability_level: Option<DurabilityLevel>,
161    pub retry_strategy: Arc<dyn RetryStrategy>,
162}
163
164impl<'a> DeleteOptions<'a> {
165    pub fn new(key: &'a [u8], scope_name: &'a str, collection_name: &'a str) -> Self {
166        Self {
167            key,
168            scope_name,
169            collection_name,
170            cas: None,
171            durability_level: None,
172            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
173        }
174    }
175
176    pub fn cas(mut self, cas: impl Into<Option<u64>>) -> Self {
177        self.cas = cas.into();
178        self
179    }
180
181    pub fn durability_level(
182        mut self,
183        durability_level: impl Into<Option<DurabilityLevel>>,
184    ) -> Self {
185        self.durability_level = durability_level.into();
186        self
187    }
188
189    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
190        self.retry_strategy = retry_strategy;
191        self
192    }
193}
194
195#[derive(Clone, Debug)]
196#[non_exhaustive]
197pub struct GetAndLockOptions<'a> {
198    pub key: &'a [u8],
199    pub scope_name: &'a str,
200    pub collection_name: &'a str,
201    pub lock_time: u32,
202    pub collection_id: Option<u32>,
203    pub retry_strategy: Arc<dyn RetryStrategy>,
204}
205
206impl<'a> GetAndLockOptions<'a> {
207    pub fn new(
208        key: &'a [u8],
209        scope_name: &'a str,
210        collection_name: &'a str,
211        lock_time: u32,
212    ) -> Self {
213        Self {
214            key,
215            scope_name,
216            collection_name,
217            lock_time,
218            collection_id: None,
219            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
220        }
221    }
222
223    pub fn collection_id(mut self, collection_id: impl Into<Option<u32>>) -> Self {
224        self.collection_id = collection_id.into();
225        self
226    }
227
228    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
229        self.retry_strategy = retry_strategy;
230        self
231    }
232}
233
234#[derive(Clone, Debug)]
235#[non_exhaustive]
236pub struct GetAndTouchOptions<'a> {
237    pub key: &'a [u8],
238    pub scope_name: &'a str,
239    pub collection_name: &'a str,
240    pub expiry: u32,
241    pub retry_strategy: Arc<dyn RetryStrategy>,
242}
243
244impl<'a> GetAndTouchOptions<'a> {
245    pub fn new(key: &'a [u8], scope_name: &'a str, collection_name: &'a str, expiry: u32) -> Self {
246        Self {
247            key,
248            scope_name,
249            collection_name,
250            expiry,
251            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
252        }
253    }
254
255    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
256        self.retry_strategy = retry_strategy;
257        self
258    }
259}
260
261#[derive(Clone, Debug)]
262#[non_exhaustive]
263pub struct UnlockOptions<'a> {
264    pub key: &'a [u8],
265    pub scope_name: &'a str,
266    pub collection_name: &'a str,
267    pub cas: u64,
268    pub retry_strategy: Arc<dyn RetryStrategy>,
269}
270
271impl<'a> UnlockOptions<'a> {
272    pub fn new(key: &'a [u8], scope_name: &'a str, collection_name: &'a str, cas: u64) -> Self {
273        Self {
274            key,
275            scope_name,
276            collection_name,
277            cas,
278            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
279        }
280    }
281
282    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
283        self.retry_strategy = retry_strategy;
284        self
285    }
286}
287
288#[derive(Clone, Debug)]
289#[non_exhaustive]
290pub struct TouchOptions<'a> {
291    pub key: &'a [u8],
292    pub scope_name: &'a str,
293    pub collection_name: &'a str,
294    pub expiry: u32,
295    pub retry_strategy: Arc<dyn RetryStrategy>,
296}
297
298impl<'a> TouchOptions<'a> {
299    pub fn new(key: &'a [u8], scope_name: &'a str, collection_name: &'a str, expiry: u32) -> Self {
300        Self {
301            key,
302            scope_name,
303            collection_name,
304            expiry,
305            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
306        }
307    }
308
309    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
310        self.retry_strategy = retry_strategy;
311        self
312    }
313}
314
315#[derive(Clone, Debug)]
316#[non_exhaustive]
317pub struct AddOptions<'a> {
318    pub key: &'a [u8],
319    pub scope_name: &'a str,
320    pub collection_name: &'a str,
321    pub value: &'a [u8],
322    pub flags: u32,
323    pub datatype: DataTypeFlag,
324    pub expiry: Option<u32>,
325    pub durability_level: Option<DurabilityLevel>,
326    pub retry_strategy: Arc<dyn RetryStrategy>,
327}
328
329impl<'a> AddOptions<'a> {
330    pub fn new(
331        key: &'a [u8],
332        scope_name: &'a str,
333        collection_name: &'a str,
334        value: &'a [u8],
335    ) -> Self {
336        Self {
337            key,
338            scope_name,
339            collection_name,
340            value,
341            flags: 0,
342            datatype: DataTypeFlag::default(),
343            expiry: None,
344            durability_level: None,
345            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
346        }
347    }
348
349    pub fn flags(mut self, flags: u32) -> Self {
350        self.flags = flags;
351        self
352    }
353
354    pub fn datatype(mut self, datatype: DataTypeFlag) -> Self {
355        self.datatype = datatype;
356        self
357    }
358
359    pub fn expiry(mut self, expiry: impl Into<Option<u32>>) -> Self {
360        self.expiry = expiry.into();
361        self
362    }
363
364    pub fn durability_level(
365        mut self,
366        durability_level: impl Into<Option<DurabilityLevel>>,
367    ) -> Self {
368        self.durability_level = durability_level.into();
369        self
370    }
371
372    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
373        self.retry_strategy = retry_strategy;
374        self
375    }
376}
377
378#[derive(Clone, Debug)]
379#[non_exhaustive]
380pub struct ReplaceOptions<'a> {
381    pub key: &'a [u8],
382    pub scope_name: &'a str,
383    pub collection_name: &'a str,
384    pub value: &'a [u8],
385    pub flags: u32,
386    pub datatype: DataTypeFlag,
387    pub expiry: Option<u32>,
388    pub preserve_expiry: Option<bool>,
389    pub cas: Option<u64>,
390    pub durability_level: Option<DurabilityLevel>,
391    pub retry_strategy: Arc<dyn RetryStrategy>,
392}
393
394impl<'a> ReplaceOptions<'a> {
395    pub fn new(
396        key: &'a [u8],
397        scope_name: &'a str,
398        collection_name: &'a str,
399        value: &'a [u8],
400    ) -> Self {
401        Self {
402            key,
403            scope_name,
404            collection_name,
405            value,
406            flags: 0,
407            datatype: DataTypeFlag::default(),
408            expiry: None,
409            preserve_expiry: None,
410            cas: None,
411            durability_level: None,
412            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
413        }
414    }
415
416    pub fn flags(mut self, flags: u32) -> Self {
417        self.flags = flags;
418        self
419    }
420
421    pub fn datatype(mut self, datatype: DataTypeFlag) -> Self {
422        self.datatype = datatype;
423        self
424    }
425
426    pub fn expiry(mut self, expiry: impl Into<Option<u32>>) -> Self {
427        self.expiry = expiry.into();
428        self
429    }
430
431    pub fn preserve_expiry(mut self, preserve_expiry: impl Into<Option<bool>>) -> Self {
432        self.preserve_expiry = preserve_expiry.into();
433        self
434    }
435
436    pub fn cas(mut self, cas: impl Into<Option<u64>>) -> Self {
437        self.cas = cas.into();
438        self
439    }
440
441    pub fn durability_level(
442        mut self,
443        durability_level: impl Into<Option<DurabilityLevel>>,
444    ) -> Self {
445        self.durability_level = durability_level.into();
446        self
447    }
448
449    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
450        self.retry_strategy = retry_strategy;
451        self
452    }
453}
454
455#[derive(Clone, Debug)]
456#[non_exhaustive]
457pub struct AppendOptions<'a> {
458    pub key: &'a [u8],
459    pub scope_name: &'a str,
460    pub collection_name: &'a str,
461    pub value: &'a [u8],
462    pub cas: Option<u64>,
463    pub durability_level: Option<DurabilityLevel>,
464    pub retry_strategy: Arc<dyn RetryStrategy>,
465}
466
467impl<'a> AppendOptions<'a> {
468    pub fn new(
469        key: &'a [u8],
470        scope_name: &'a str,
471        collection_name: &'a str,
472        value: &'a [u8],
473    ) -> Self {
474        Self {
475            key,
476            scope_name,
477            collection_name,
478            value,
479            cas: None,
480            durability_level: None,
481            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
482        }
483    }
484
485    pub fn cas(mut self, cas: impl Into<Option<u64>>) -> Self {
486        self.cas = cas.into();
487        self
488    }
489
490    pub fn durability_level(
491        mut self,
492        durability_level: impl Into<Option<DurabilityLevel>>,
493    ) -> Self {
494        self.durability_level = durability_level.into();
495        self
496    }
497
498    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
499        self.retry_strategy = retry_strategy;
500        self
501    }
502}
503
504#[derive(Clone, Debug)]
505#[non_exhaustive]
506pub struct PrependOptions<'a> {
507    pub key: &'a [u8],
508    pub scope_name: &'a str,
509    pub collection_name: &'a str,
510    pub value: &'a [u8],
511    pub cas: Option<u64>,
512    pub durability_level: Option<DurabilityLevel>,
513    pub retry_strategy: Arc<dyn RetryStrategy>,
514}
515
516impl<'a> PrependOptions<'a> {
517    pub fn new(
518        key: &'a [u8],
519        scope_name: &'a str,
520        collection_name: &'a str,
521        value: &'a [u8],
522    ) -> Self {
523        Self {
524            key,
525            scope_name,
526            collection_name,
527            value,
528            cas: None,
529            durability_level: None,
530            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
531        }
532    }
533
534    pub fn cas(mut self, cas: impl Into<Option<u64>>) -> Self {
535        self.cas = cas.into();
536        self
537    }
538
539    pub fn durability_level(
540        mut self,
541        durability_level: impl Into<Option<DurabilityLevel>>,
542    ) -> Self {
543        self.durability_level = durability_level.into();
544        self
545    }
546
547    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
548        self.retry_strategy = retry_strategy;
549        self
550    }
551}
552
553#[derive(Clone, Debug)]
554#[non_exhaustive]
555pub struct IncrementOptions<'a> {
556    pub key: &'a [u8],
557    pub scope_name: &'a str,
558    pub collection_name: &'a str,
559    pub initial: Option<u64>,
560    pub delta: u64,
561    pub expiry: Option<u32>,
562    pub durability_level: Option<DurabilityLevel>,
563    pub retry_strategy: Arc<dyn RetryStrategy>,
564}
565
566impl<'a> IncrementOptions<'a> {
567    pub fn new(key: &'a [u8], delta: u64, scope_name: &'a str, collection_name: &'a str) -> Self {
568        Self {
569            key,
570            scope_name,
571            collection_name,
572            initial: None,
573            delta,
574            expiry: None,
575            durability_level: None,
576            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
577        }
578    }
579
580    pub fn initial(mut self, initial: impl Into<Option<u64>>) -> Self {
581        self.initial = initial.into();
582        self
583    }
584
585    pub fn expiry(mut self, expiry: impl Into<Option<u32>>) -> Self {
586        self.expiry = expiry.into();
587        self
588    }
589
590    pub fn durability_level(
591        mut self,
592        durability_level: impl Into<Option<DurabilityLevel>>,
593    ) -> Self {
594        self.durability_level = durability_level.into();
595        self
596    }
597
598    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
599        self.retry_strategy = retry_strategy;
600        self
601    }
602}
603
604#[derive(Clone, Debug)]
605#[non_exhaustive]
606pub struct DecrementOptions<'a> {
607    pub key: &'a [u8],
608    pub scope_name: &'a str,
609    pub collection_name: &'a str,
610    pub initial: Option<u64>,
611    pub delta: u64,
612    pub expiry: Option<u32>,
613    pub durability_level: Option<DurabilityLevel>,
614    pub retry_strategy: Arc<dyn RetryStrategy>,
615}
616
617impl<'a> DecrementOptions<'a> {
618    pub fn new(key: &'a [u8], delta: u64, scope_name: &'a str, collection_name: &'a str) -> Self {
619        Self {
620            key,
621            scope_name,
622            collection_name,
623            initial: None,
624            delta,
625            expiry: None,
626            durability_level: None,
627            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
628        }
629    }
630
631    pub fn initial(mut self, initial: impl Into<Option<u64>>) -> Self {
632        self.initial = initial.into();
633        self
634    }
635
636    pub fn expiry(mut self, expiry: impl Into<Option<u32>>) -> Self {
637        self.expiry = expiry.into();
638        self
639    }
640
641    pub fn durability_level(
642        mut self,
643        durability_level: impl Into<Option<DurabilityLevel>>,
644    ) -> Self {
645        self.durability_level = durability_level.into();
646        self
647    }
648
649    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
650        self.retry_strategy = retry_strategy;
651        self
652    }
653}
654
655#[derive(Clone, Debug)]
656#[non_exhaustive]
657pub struct LookupInOptions<'a> {
658    pub key: &'a [u8],
659    pub scope_name: &'a str,
660    pub collection_name: &'a str,
661    pub ops: &'a [LookupInOp<'a>],
662    pub flags: SubdocDocFlag,
663    pub retry_strategy: Arc<dyn RetryStrategy>,
664}
665
666impl<'a> LookupInOptions<'a> {
667    pub fn new(
668        key: &'a [u8],
669        scope_name: &'a str,
670        collection_name: &'a str,
671        ops: &'a [LookupInOp<'a>],
672    ) -> Self {
673        Self {
674            key,
675            scope_name,
676            collection_name,
677            ops,
678            flags: SubdocDocFlag::empty(),
679            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
680        }
681    }
682
683    pub fn flags(mut self, flags: SubdocDocFlag) -> Self {
684        self.flags = flags;
685        self
686    }
687
688    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
689        self.retry_strategy = retry_strategy;
690        self
691    }
692}
693
694#[derive(Clone, Debug)]
695#[non_exhaustive]
696pub struct MutateInOptions<'a> {
697    pub key: &'a [u8],
698    pub scope_name: &'a str,
699    pub collection_name: &'a str,
700    pub ops: &'a [MutateInOp<'a>],
701    pub flags: SubdocDocFlag,
702    pub expiry: Option<u32>,
703    pub preserve_expiry: Option<bool>,
704    pub cas: Option<u64>,
705    pub durability_level: Option<DurabilityLevel>,
706    pub retry_strategy: Arc<dyn RetryStrategy>,
707}
708
709impl<'a> MutateInOptions<'a> {
710    pub fn new(
711        key: &'a [u8],
712        scope_name: &'a str,
713        collection_name: &'a str,
714        ops: &'a [MutateInOp<'a>],
715    ) -> Self {
716        Self {
717            key,
718            scope_name,
719            collection_name,
720            ops,
721            flags: SubdocDocFlag::empty(),
722            expiry: None,
723            preserve_expiry: None,
724            cas: None,
725            durability_level: None,
726            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
727        }
728    }
729
730    pub fn flags(mut self, flags: SubdocDocFlag) -> Self {
731        self.flags = flags;
732        self
733    }
734
735    pub fn expiry(mut self, expiry: impl Into<Option<u32>>) -> Self {
736        self.expiry = expiry.into();
737        self
738    }
739
740    pub fn preserve_expiry(mut self, preserve_expiry: impl Into<Option<bool>>) -> Self {
741        self.preserve_expiry = preserve_expiry.into();
742        self
743    }
744
745    pub fn cas(mut self, cas: impl Into<Option<u64>>) -> Self {
746        self.cas = cas.into();
747        self
748    }
749
750    pub fn durability_level(
751        mut self,
752        durability_level: impl Into<Option<DurabilityLevel>>,
753    ) -> Self {
754        self.durability_level = durability_level.into();
755        self
756    }
757
758    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
759        self.retry_strategy = retry_strategy;
760        self
761    }
762}
763
764#[derive(Clone, Debug)]
765#[non_exhaustive]
766pub struct GetCollectionIdOptions<'a> {
767    pub scope_name: &'a str,
768    pub collection_name: &'a str,
769    pub retry_strategy: Arc<dyn RetryStrategy>,
770}
771
772impl<'a> GetCollectionIdOptions<'a> {
773    pub fn new(scope_name: &'a str, collection_name: &'a str) -> Self {
774        Self {
775            scope_name,
776            collection_name,
777            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
778        }
779    }
780
781    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
782        self.retry_strategy = retry_strategy;
783        self
784    }
785}