surrealdb_datastore/
lib.rs1#![allow(clippy::absolute_paths)]
18#![recursion_limit = "256"]
20
21pub use surrealdb_catalog as catalog;
22pub use surrealdb_expr::{expr, val};
23
24#[macro_use]
25extern crate common;
26#[macro_use]
27extern crate tracing;
28
29#[macro_use]
30mod mac;
31
32pub(crate) mod obs {
38 #[cfg(feature = "observability")]
39 pub(crate) use tracing::Instrument;
40
41 #[cfg(not(feature = "observability"))]
42 pub(crate) trait Instrument: Sized {
43 fn instrument(self, _span: tracing::Span) -> Self {
45 self
46 }
47 }
48
49 #[cfg(not(feature = "observability"))]
50 impl<T> Instrument for T {}
51}
52
53pub mod absence;
54pub mod cache;
55pub mod catalog_reads;
56pub mod clock;
57pub mod close;
58pub mod config;
59pub mod creations;
60pub mod factory;
61pub mod feature;
62pub mod index_shapes;
63pub mod index_state;
64pub mod into;
65pub mod key;
66pub mod layouts;
67pub mod out_of_line;
68pub mod pending_guard;
69mod record_writes;
70pub mod sequences;
71pub mod speculation;
72pub mod tasklease;
73pub mod tr;
74pub mod triggers;
75pub mod tx;
76pub mod unseen_indexes;
77pub mod util;
78pub mod version;
79
80pub use error::DatastoreError;
81
82pub fn storage_error(err: &anyhow::Error) -> Option<&surrealdb_kvs::Error> {
98 err.chain().find_map(|e| e.downcast_ref::<surrealdb_kvs::Error>())
99}
100
101pub fn is_condition_not_met(err: &anyhow::Error) -> bool {
107 matches!(storage_error(err), Some(surrealdb_kvs::Error::TransactionConditionNotMet))
108}
109
110pub fn is_retryable_transaction_conflict(err: &anyhow::Error) -> bool {
112 storage_error(err).is_some_and(surrealdb_kvs::Error::is_retryable)
113}
114
115pub fn is_shutdown_error(err: &anyhow::Error) -> bool {
121 matches!(storage_error(err), Some(surrealdb_kvs::Error::Shutdown))
122}
123
124pub fn is_indeterminate_commit(err: &anyhow::Error) -> bool {
131 matches!(storage_error(err), Some(surrealdb_kvs::Error::CommitOutcomeUnknown(_)))
132}
133
134#[cfg(test)]
135mod storage_error_tests {
136 use super::{is_retryable_transaction_conflict, is_shutdown_error, storage_error};
137
138 #[derive(Debug, thiserror::Error)]
143 #[error("There was a problem with the key-value store: {0}")]
144 struct Wrapped(#[from] surrealdb_kvs::Error);
145
146 #[derive(Debug, thiserror::Error)]
147 #[error("not a storage failure")]
148 struct Foreign;
149
150 #[test]
151 fn either_shape_is_recovered() {
152 for err in [
153 anyhow::Error::new(surrealdb_kvs::Error::TransactionKeyAlreadyExists),
154 anyhow::Error::new(Wrapped(surrealdb_kvs::Error::TransactionKeyAlreadyExists)),
155 ] {
156 assert!(
157 matches!(
158 storage_error(&err),
159 Some(surrealdb_kvs::Error::TransactionKeyAlreadyExists)
160 ),
161 "a key-already-exists failure went unrecognised: {err}"
162 );
163 }
164 }
165
166 #[test]
167 fn either_shape_classifies_the_same() {
168 for err in [
169 anyhow::Error::new(surrealdb_kvs::Error::TransactionConflict("busy".to_string())),
170 anyhow::Error::new(Wrapped(surrealdb_kvs::Error::TransactionConflict(
171 "busy".to_string(),
172 ))),
173 ] {
174 assert!(is_retryable_transaction_conflict(&err), "conflict not retryable: {err}");
175 assert!(!is_shutdown_error(&err), "conflict misread as a shutdown: {err}");
176 }
177 for err in [
178 anyhow::Error::new(surrealdb_kvs::Error::Shutdown),
179 anyhow::Error::new(Wrapped(surrealdb_kvs::Error::Shutdown)),
180 ] {
181 assert!(is_shutdown_error(&err), "shutdown not recognised: {err}");
182 assert!(!is_retryable_transaction_conflict(&err), "shutdown misread as retryable");
183 }
184 }
185
186 #[test]
187 fn a_foreign_error_is_not_a_storage_failure() {
188 let err = anyhow::Error::new(Foreign);
189 assert!(storage_error(&err).is_none());
190 assert!(!is_retryable_transaction_conflict(&err));
191 assert!(!is_shutdown_error(&err));
192 }
193}
194
195pub use config::TransactionConfig;
196pub use factory::{ReadRequest, TransactionFactory};
197pub use feature::{FeatureLevelCell, FeatureLevelState, LevelledMode, Readable};
198pub use into::IntoBytes;
199pub use layouts::{Admission, LayoutSet, RowGate};
200pub use surrealdb_kvs::consts::NORMAL_BATCH_SIZE;
201pub use surrealdb_kvs::{
202 Direction, Error, Key, KeyRange, Result, TransactionType, Val, Version, api, consts, err,
203 timestamp,
204};
205pub use tr::Transactor;
206pub use tx::{DocIdsReclaimClaim, Transaction, WriteBatch};
207pub mod error;
208#[cfg(any(test, feature = "test-hooks"))]
209pub mod testing;
210pub mod values;