Skip to main content

typedb_driver/common/
options.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::time::Duration;
21
22/// TypeDB session and transaction options.
23/// `TypeDBOptions` object can be used to override the default server behaviour.
24/// Options are specified using properties assignment.
25///
26/// # Examples
27///
28/// ```rust
29/// let options = Options::new().infer(true).explain(true);
30/// ```
31#[derive(Clone, Copy, Debug, Default)]
32pub struct Options {
33    /// If set to `True`, enables inference for queries. Only settable at transaction level and above. Only affects read transactions.
34    pub infer: Option<bool>,
35    /// If set to `True`, reasoning tracing graphs are output in the logging directory. Should be used with `parallel = False`.
36    pub trace_inference: Option<bool>,
37    /// If set to `True`, enables explanations for queries. Only affects read transactions.
38    pub explain: Option<bool>,
39    /// If set to `True`, the server uses parallel instead of single-threaded execution.
40    pub parallel: Option<bool>,
41    /// If set to `True`, the first batch of answers is streamed to the driver even without an explicit request for it.
42    pub prefetch: Option<bool>,
43    /// If set, specifies a guideline number of answers that the server should send before the driver issues a fresh request.
44    pub prefetch_size: Option<i32>,
45    /// If set, specifies a timeout that allows the server to close sessions if the driver terminates or becomes unresponsive.
46    pub session_idle_timeout: Option<Duration>,
47    /// If set, specifies a timeout for killing transactions automatically, preventing memory leaks in unclosed transactions.
48    pub transaction_timeout: Option<Duration>,
49    /// If set, specifies how long the driver should wait if opening a session or transaction is blocked by a schema write lock.
50    pub schema_lock_acquire_timeout: Option<Duration>,
51    /// If set to `True`, enables reading data from any replica, potentially boosting read throughput. Only settable in TypeDB Cloud.
52    pub read_any_replica: Option<bool>,
53}
54
55impl Options {
56    pub fn new() -> Self {
57        Self::default()
58    }
59
60    /// If set to `True`, enables inference for queries. Only settable at transaction level and above. Only affects read transactions.
61    pub fn infer(self, infer: bool) -> Self {
62        Self { infer: Some(infer), ..self }
63    }
64
65    /// If set to `True`, reasoning tracing graphs are output in the logging directory. Should be used with `parallel = False`.
66    pub fn trace_inference(self, trace_inference: bool) -> Self {
67        Self { trace_inference: Some(trace_inference), ..self }
68    }
69
70    /// If set to `True`, enables explanations for queries. Only affects read transactions.
71    pub fn explain(self, explain: bool) -> Self {
72        Self { explain: Some(explain), ..self }
73    }
74
75    /// If set to `True`, the server uses parallel instead of single-threaded execution.
76    pub fn parallel(self, parallel: bool) -> Self {
77        Self { parallel: Some(parallel), ..self }
78    }
79
80    /// If set to `True`, the first batch of answers is streamed to the driver even without an explicit request for it.
81    pub fn prefetch(self, prefetch: bool) -> Self {
82        Self { prefetch: Some(prefetch), ..self }
83    }
84
85    /// If set, specifies a guideline number of answers that the server should send before the driver issues a fresh request.
86    pub fn prefetch_size(self, prefetch_size: i32) -> Self {
87        Self { prefetch_size: Some(prefetch_size), ..self }
88    }
89
90    /// If set, specifies a timeout that allows the server to close sessions if the driver terminates or becomes unresponsive.
91    pub fn session_idle_timeout(self, timeout: Duration) -> Self {
92        Self { session_idle_timeout: Some(timeout), ..self }
93    }
94
95    /// If set, specifies a timeout for killing transactions automatically, preventing memory leaks in unclosed transactions.
96    pub fn transaction_timeout(self, timeout: Duration) -> Self {
97        Self { transaction_timeout: Some(timeout), ..self }
98    }
99
100    /// If set, specifies how long the driver should wait if opening a session or transaction is blocked by a schema write lock.
101    pub fn schema_lock_acquire_timeout(self, timeout: Duration) -> Self {
102        Self { schema_lock_acquire_timeout: Some(timeout), ..self }
103    }
104
105    /// If set to `True`, enables reading data from any replica, potentially boosting read throughput. Only settable in TypeDB Cloud.
106    pub fn read_any_replica(self, read_any_replica: bool) -> Self {
107        Self { read_any_replica: Some(read_any_replica), ..self }
108    }
109}