Skip to main content

couchbase_core/options/
analytics.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 */
18use crate::analyticsx;
19use crate::analyticsx::query_options::{Format, PlanFormat, ScanConsistency};
20use crate::httpx::request::OnBehalfOfInfo;
21use crate::retry::{RetryStrategy, DEFAULT_RETRY_STRATEGY};
22use serde_json::Value;
23use std::collections::HashMap;
24use std::sync::Arc;
25use std::time::Duration;
26
27#[derive(Debug, Clone)]
28#[non_exhaustive]
29pub struct AnalyticsOptions {
30    pub args: Option<Vec<Value>>,
31    pub client_context_id: Option<String>,
32    pub format: Option<Format>,
33    pub pretty: Option<bool>,
34    pub query_context: Option<String>,
35    pub read_only: Option<bool>,
36    pub scan_consistency: Option<ScanConsistency>,
37    pub scan_wait: Option<Duration>,
38    pub statement: Option<String>,
39    pub timeout: Option<Duration>,
40    pub named_args: Option<HashMap<String, Value>>,
41    pub raw: Option<HashMap<String, Value>>,
42
43    pub plan_format: Option<PlanFormat>,
44    pub logical_plan: Option<bool>,
45    pub optimized_logical_plan: Option<bool>,
46    pub expression_tree: Option<bool>,
47    pub rewritten_expression_tree: Option<bool>,
48    pub job: Option<bool>,
49    pub max_warnings: Option<i32>,
50
51    pub on_behalf_of: Option<OnBehalfOfInfo>,
52    pub endpoint: Option<String>,
53    pub retry_strategy: Arc<dyn RetryStrategy>,
54}
55
56impl Default for AnalyticsOptions {
57    fn default() -> Self {
58        Self {
59            args: None,
60            client_context_id: None,
61            format: None,
62            pretty: None,
63            query_context: None,
64            read_only: None,
65            scan_consistency: None,
66            scan_wait: None,
67            statement: None,
68            timeout: None,
69            named_args: None,
70            raw: None,
71            plan_format: None,
72            logical_plan: None,
73            optimized_logical_plan: None,
74            expression_tree: None,
75            rewritten_expression_tree: None,
76            job: None,
77            max_warnings: None,
78            on_behalf_of: None,
79            endpoint: None,
80            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
81        }
82    }
83}
84
85impl AnalyticsOptions {
86    pub fn new() -> Self {
87        Default::default()
88    }
89
90    pub fn args(mut self, args: impl Into<Option<Vec<Value>>>) -> Self {
91        self.args = args.into();
92        self
93    }
94
95    pub fn client_context_id(mut self, client_context_id: impl Into<Option<String>>) -> Self {
96        self.client_context_id = client_context_id.into();
97        self
98    }
99
100    pub fn pretty(mut self, pretty: impl Into<Option<bool>>) -> Self {
101        self.pretty = pretty.into();
102        self
103    }
104
105    pub fn query_context(mut self, query_context: impl Into<Option<String>>) -> Self {
106        self.query_context = query_context.into();
107        self
108    }
109
110    pub fn read_only(mut self, read_only: impl Into<Option<bool>>) -> Self {
111        self.read_only = read_only.into();
112        self
113    }
114
115    pub fn scan_consistency(
116        mut self,
117        scan_consistency: impl Into<Option<ScanConsistency>>,
118    ) -> Self {
119        self.scan_consistency = scan_consistency.into();
120        self
121    }
122
123    pub fn scan_wait(mut self, scan_wait: impl Into<Option<Duration>>) -> Self {
124        self.scan_wait = scan_wait.into();
125        self
126    }
127
128    pub fn statement(mut self, statement: impl Into<Option<String>>) -> Self {
129        self.statement = statement.into();
130        self
131    }
132
133    pub fn timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
134        self.timeout = timeout.into();
135        self
136    }
137
138    pub fn named_args(mut self, named_args: impl Into<Option<HashMap<String, Value>>>) -> Self {
139        self.named_args = named_args.into();
140        self
141    }
142
143    pub fn raw(mut self, raw: impl Into<Option<HashMap<String, Value>>>) -> Self {
144        self.raw = raw.into();
145        self
146    }
147
148    pub fn plan_format(mut self, plan_format: impl Into<Option<PlanFormat>>) -> Self {
149        self.plan_format = plan_format.into();
150        self
151    }
152
153    pub fn logical_plan(mut self, logical_plan: impl Into<Option<bool>>) -> Self {
154        self.logical_plan = logical_plan.into();
155        self
156    }
157
158    pub fn optimized_logical_plan(
159        mut self,
160        optimized_logical_plan: impl Into<Option<bool>>,
161    ) -> Self {
162        self.optimized_logical_plan = optimized_logical_plan.into();
163        self
164    }
165
166    pub fn expression_tree(mut self, expression_tree: impl Into<Option<bool>>) -> Self {
167        self.expression_tree = expression_tree.into();
168        self
169    }
170
171    pub fn rewritten_expression_tree(
172        mut self,
173        rewritten_expression_tree: impl Into<Option<bool>>,
174    ) -> Self {
175        self.rewritten_expression_tree = rewritten_expression_tree.into();
176        self
177    }
178
179    pub fn job(mut self, job: impl Into<Option<bool>>) -> Self {
180        self.job = job.into();
181        self
182    }
183
184    pub fn max_warnings(mut self, max_warnings: impl Into<Option<i32>>) -> Self {
185        self.max_warnings = max_warnings.into();
186        self
187    }
188
189    pub fn on_behalf_of(mut self, on_behalf_of: impl Into<Option<OnBehalfOfInfo>>) -> Self {
190        self.on_behalf_of = on_behalf_of.into();
191        self
192    }
193
194    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
195        self.retry_strategy = retry_strategy;
196        self
197    }
198
199    pub fn endpoint(mut self, endpoint: impl Into<Option<String>>) -> Self {
200        self.endpoint = endpoint.into();
201        self
202    }
203}
204
205impl From<AnalyticsOptions> for analyticsx::query_options::QueryOptions {
206    fn from(opts: AnalyticsOptions) -> Self {
207        analyticsx::query_options::QueryOptions {
208            args: opts.args,
209            client_context_id: opts.client_context_id,
210            format: opts.format,
211            pretty: opts.pretty,
212            query_context: opts.query_context,
213            read_only: opts.read_only,
214            scan_consistency: opts.scan_consistency,
215            scan_wait: opts.scan_wait,
216            statement: opts.statement,
217            timeout: opts.timeout,
218            named_args: opts.named_args,
219            raw: opts.raw,
220            plan_format: opts.plan_format,
221            logical_plan: opts.logical_plan,
222            optimized_logical_plan: opts.optimized_logical_plan,
223            expression_tree: opts.expression_tree,
224            rewritten_expression_tree: opts.rewritten_expression_tree,
225            job: opts.job,
226            max_warnings: opts.max_warnings,
227            on_behalf_of: opts.on_behalf_of,
228        }
229    }
230}
231
232#[derive(Debug, Clone)]
233#[non_exhaustive]
234pub struct GetPendingMutationsOptions<'a> {
235    pub on_behalf_of: Option<&'a OnBehalfOfInfo>,
236
237    pub endpoint: Option<String>,
238    pub retry_strategy: Arc<dyn RetryStrategy>,
239}
240
241impl Default for GetPendingMutationsOptions<'_> {
242    fn default() -> Self {
243        Self {
244            on_behalf_of: None,
245            endpoint: None,
246            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
247        }
248    }
249}
250
251impl<'a> GetPendingMutationsOptions<'a> {
252    pub fn new() -> Self {
253        Default::default()
254    }
255
256    pub fn on_behalf_of(mut self, on_behalf_of: impl Into<Option<&'a OnBehalfOfInfo>>) -> Self {
257        self.on_behalf_of = on_behalf_of.into();
258        self
259    }
260
261    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
262        self.retry_strategy = retry_strategy;
263        self
264    }
265
266    pub fn endpoint(mut self, endpoint: impl Into<Option<String>>) -> Self {
267        self.endpoint = endpoint.into();
268        self
269    }
270}
271
272impl<'a> From<&GetPendingMutationsOptions<'a>>
273    for analyticsx::query_options::GetPendingMutationsOptions<'a>
274{
275    fn from(opts: &GetPendingMutationsOptions<'a>) -> Self {
276        analyticsx::query_options::GetPendingMutationsOptions {
277            on_behalf_of: opts.on_behalf_of,
278        }
279    }
280}