storage-kit 0.0.1

Portable database and transactional state-store Kit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::{collections::BTreeMap, sync::Arc};

use serde_json::{Map, Value};
use surrealdb::types::SurrealValue;
use surrealdb::{IndexedResults as Response, Surreal, engine::any::Any};

use crate::{CollectionInfo, IndexInfo, ListCollectionsRequest, StorageError, StorageLocator};

use super::{manager::SurrealContextKey, manager::SurrealManager, mapper};

#[derive(Clone, Debug, PartialEq, Eq)]
struct ResolvedContext {
    namespace: String,
    database: String,
    key: Option<SurrealContextKey>,
}

impl ResolvedContext {
    fn resolve(
        db: &SurrealManager,
        locator: Option<&StorageLocator>,
    ) -> Result<Self, StorageError> {
        let mut locator = locator.cloned().unwrap_or_default();
        if let Some(default) = db.common_config().default_locator.as_ref() {
            locator = locator.with_defaults_from(default);
        }

        let namespace = locator_namespace(db, &locator)?
            .or_else(|| db.config().namespace.clone())
            .ok_or_else(|| missing_context("namespace"))?;
        let database = locator
            .database
            .or_else(|| db.config().database.clone())
            .ok_or_else(|| missing_context("database"))?;

        let key = SurrealContextKey {
            namespace: namespace.clone(),
            database: database.clone(),
        };
        Ok(Self {
            namespace,
            database,
            key: (db.config().context_key().as_ref() != Some(&key)).then_some(key),
        })
    }

    fn collection_locator(&self, collection: impl Into<String>) -> StorageLocator {
        StorageLocator::new(collection.into())
            .with_namespace([self.namespace.clone()])
            .with_database(self.database.clone())
    }
}

fn locator_namespace(
    db: &SurrealManager,
    locator: &StorageLocator,
) -> Result<Option<String>, StorageError> {
    match locator.namespace.as_slice() {
        [] => Ok(None),
        [namespace] if namespace.trim().is_empty() => Err(StorageError::InvalidConfig(
            "SurrealDB namespace segment must not be empty".into(),
        )),
        [namespace] => Ok(Some(namespace.clone())),
        namespace => Err(db.unsupported(
            "NAMESPACE_PATHS",
            format!(
                "SurrealDB exposes a single native namespace value, not a portable namespace path: {namespace:?}"
            ),
        )),
    }
}

fn missing_context(field: &str) -> StorageError {
    StorageError::InvalidConfig(format!(
        "SurrealDB operations require a {field} either in SurrealConfig.{field} or StorageLocator.{field}"
    ))
}

impl SurrealManager {
    pub(crate) fn scope<'a>(
        &'a self,
        locator: &'a StorageLocator,
    ) -> Result<SurrealScope<'a>, StorageError> {
        Ok(SurrealScope {
            db: self,
            locator,
            table: self.resolve_table(locator)?,
        })
    }

    pub(crate) fn resolve_table(&self, locator: &StorageLocator) -> Result<String, StorageError> {
        let table = mapper::table_name(locator);
        let table = table.trim();
        if table == Self::PARTITION_REPLACEMENT_GUARD_TABLE {
            return Err(StorageError::InvalidRequest(format!(
                "SurrealDB collection name `{table}` is reserved for atomic partition replacement coordination"
            )));
        }
        mapper::sanitize_identifier(table)
    }

    async fn client_for_locator(
        &self,
        locator: Option<&StorageLocator>,
    ) -> Result<Arc<Surreal<Any>>, StorageError> {
        match locator {
            None => Ok(Arc::clone(self.root_client())),
            Some(_) => {
                let resolved = ResolvedContext::resolve(self, locator)?;
                self.client_for_resolved_context(&resolved).await
            }
        }
    }

    async fn client_for_resolved_context(
        &self,
        resolved: &ResolvedContext,
    ) -> Result<Arc<Surreal<Any>>, StorageError> {
        let Some(key) = resolved.key.as_ref() else {
            return Ok(Arc::clone(self.root_client()));
        };

        {
            let cache = self
                .context_clients()
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            if let Some(client) = cache.get(key) {
                return Ok(Arc::clone(client));
            }
        }

        let client = Arc::new(self.root_client().as_ref().clone());
        SurrealManager::select_client_context(client.as_ref(), self.common_config(), key).await?;

        let mut cache = self
            .context_clients()
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        Ok(Arc::clone(cache.entry(key.clone()).or_insert(client)))
    }

    pub(crate) async fn ensure_namespace_database(
        &self,
        locator: Option<&StorageLocator>,
    ) -> Result<(), StorageError> {
        let resolved = ResolvedContext::resolve(self, locator)?;
        let namespace = mapper::quote_ident(&resolved.namespace);
        let database = mapper::quote_ident(&resolved.database);
        let sql = format!(
            "DEFINE NAMESPACE IF NOT EXISTS {namespace}; USE NS {namespace}; DEFINE DATABASE IF NOT EXISTS {database};"
        );
        let _ = self.run_response(None, sql, Default::default()).await?;
        Ok(())
    }

    async fn table_inventory_for_context(
        &self,
        resolved: &ResolvedContext,
    ) -> Result<Map<String, Value>, StorageError> {
        let client = self.client_for_resolved_context(resolved).await?;
        let mut response = self
            .execute_response(client, "INFO FOR DB;".to_owned(), BTreeMap::new())
            .await?;
        let raw = Self::take_query_value(&mut response, 0)?;
        raw.get("tables")
            .and_then(Value::as_object)
            .cloned()
            .ok_or_else(|| {
                self.map_err(
                    "info_for_db",
                    format!("unexpected INFO FOR DB response: {raw}"),
                )
            })
    }

    pub(crate) async fn table_exists(
        &self,
        locator: &StorageLocator,
    ) -> Result<bool, StorageError> {
        let resolved = ResolvedContext::resolve(self, Some(locator))?;
        Ok(self
            .table_inventory_for_context(&resolved)
            .await?
            .contains_key(&mapper::table_name(locator)))
    }

    fn build_collection_info(
        &self,
        locator: StorageLocator,
        record_count: Option<u64>,
    ) -> CollectionInfo {
        CollectionInfo {
            locator,
            schema: None,
            record_count,
        }
    }

    pub(crate) fn extract_count(&self, value: &Value) -> Result<u64, StorageError> {
        extract_count_value(value)
    }

    pub(crate) fn affected_row_count(&self, value: &Value) -> Result<u64, StorageError> {
        affected_row_count_value(value)
    }

    pub(crate) async fn open_collection_info(
        &self,
        locator: StorageLocator,
    ) -> Result<CollectionInfo, StorageError> {
        Ok(self.build_collection_info(locator.clone(), Some(self.count_rows(&locator).await?)))
    }

    pub(crate) async fn list_collection_infos(
        &self,
        req: &ListCollectionsRequest,
    ) -> Result<Vec<CollectionInfo>, StorageError> {
        let scope = (!req.namespace.is_empty())
            .then(|| StorageLocator::default().with_namespace(req.namespace.clone()));
        let resolved = ResolvedContext::resolve(self, scope.as_ref())?;
        let mut entries = self
            .table_inventory_for_context(&resolved)
            .await?
            .into_iter()
            .filter(|(table_name, _)| table_name != Self::PARTITION_REPLACEMENT_GUARD_TABLE)
            .collect::<Vec<_>>();
        entries.sort_by(|left, right| left.0.cmp(&right.0));
        Ok(req.apply_to_collections(
            entries
                .into_iter()
                .map(|(table_name, _)| {
                    self.build_collection_info(
                        resolved.collection_locator(table_name.clone()),
                        None,
                    )
                })
                .collect(),
        ))
    }

    pub(crate) async fn list_index_infos(
        &self,
        locator: &StorageLocator,
    ) -> Result<Vec<IndexInfo>, StorageError> {
        let table = self.resolve_table(locator)?;
        let sql = format!("INFO FOR TABLE {table};");
        let raw = self.run_query_value(Some(locator), sql).await?;
        index_infos_from_table_info(&raw)
    }

    pub(crate) async fn count_rows(&self, locator: &StorageLocator) -> Result<u64, StorageError> {
        let table = self.resolve_table(locator)?;
        let sql = format!("SELECT count() AS count FROM {table} GROUP ALL;");
        let value = self.run_query_value(Some(locator), sql).await?;
        self.extract_count(&value)
    }

    pub(crate) fn consume_response_errors(
        op: &'static str,
        response: &mut Response,
    ) -> Result<(), StorageError> {
        let errors = response.take_errors();
        if errors.is_empty() {
            Ok(())
        } else {
            Err(StorageError::backend_op(op, format!("{errors:?}")))
        }
    }

    pub(crate) async fn run_response(
        &self,
        locator: Option<&StorageLocator>,
        sql: impl Into<String>,
        bindings: BTreeMap<String, Value>,
    ) -> Result<Response, StorageError> {
        self.execute_response(
            self.client_for_locator(locator).await?,
            sql.into(),
            bindings,
        )
        .await
    }

    pub(super) async fn run_consuming_statement_errors(
        &self,
        locator: Option<&StorageLocator>,
        sql: impl Into<String>,
        bindings: BTreeMap<String, Value>,
    ) -> Result<Vec<(usize, surrealdb::Error)>, StorageError> {
        let mut response = self
            .execute_response_unchecked(
                self.client_for_locator(locator).await?,
                sql.into(),
                bindings,
            )
            .await?;
        let mut errors = response.take_errors().into_iter().collect::<Vec<_>>();
        errors.sort_by_key(|(statement_index, _)| *statement_index);
        Ok(errors)
    }

    async fn execute_response(
        &self,
        client: Arc<Surreal<Any>>,
        sql: String,
        bindings: BTreeMap<String, Value>,
    ) -> Result<Response, StorageError> {
        let mut response = self
            .execute_response_unchecked(client, sql, bindings)
            .await?;
        Self::consume_response_errors("query", &mut response)?;
        Ok(response)
    }

    async fn execute_response_unchecked(
        &self,
        client: Arc<Surreal<Any>>,
        sql: String,
        bindings: BTreeMap<String, Value>,
    ) -> Result<Response, StorageError> {
        self.with_request_timeout("query", "executing SurrealDB query", async move {
            let query = client.query(sql).bind(bindings);
            query
                .await
                .map_err(|err| StorageError::backend_op("query", err.to_string()))
        })
        .await
    }

    pub(crate) async fn run_query_take(
        &self,
        locator: Option<&StorageLocator>,
        sql: impl Into<String>,
        bindings: BTreeMap<String, Value>,
        statement_index: usize,
    ) -> Result<Value, StorageError> {
        let mut response = self.run_response(locator, sql, bindings).await?;
        Self::take_query_value(&mut response, statement_index)
    }

    fn take_query_value(
        response: &mut Response,
        statement_index: usize,
    ) -> Result<Value, StorageError> {
        let value = response
            .take::<surrealdb::types::Value>(statement_index)
            .map_err(|err| StorageError::backend_op("query.take", err.to_string()))?;
        serde_json::Value::from_value(value)
            .map_err(|err| StorageError::backend_op("query.take", err.to_string()))
    }

    pub(crate) async fn run_query_value(
        &self,
        locator: Option<&StorageLocator>,
        sql: impl Into<String>,
    ) -> Result<Value, StorageError> {
        self.run_query_take(locator, sql, BTreeMap::new(), 0).await
    }
}

fn extract_count_value(value: &Value) -> Result<u64, StorageError> {
    match value {
        Value::Array(rows) => rows
            .last()
            .and_then(|row| row.get("count"))
            .and_then(Value::as_u64)
            .ok_or_else(|| {
                StorageError::Serialization(format!(
                    "Surreal count response is missing numeric `count`: {value}"
                ))
            }),
        Value::Object(obj) => obj.get("count").and_then(Value::as_u64).ok_or_else(|| {
            StorageError::Serialization(format!(
                "Surreal count response object is missing numeric `count`: {value}"
            ))
        }),
        _ => Err(StorageError::Serialization(format!(
            "Surreal count response must be an array or object, got {value}"
        ))),
    }
}

fn affected_row_count_value(value: &Value) -> Result<u64, StorageError> {
    match value {
        Value::Array(values) => Ok(values.len() as u64),
        _ => Err(StorageError::Serialization(format!(
            "Surreal mutation response must be an array, got {value}"
        ))),
    }
}

fn index_infos_from_table_info(raw: &Value) -> Result<Vec<IndexInfo>, StorageError> {
    raw.get("indexes")
        .and_then(Value::as_object)
        .ok_or_else(|| {
            StorageError::Serialization(format!(
                "Surreal `INFO FOR TABLE` response is missing `indexes`: {raw}"
            ))
        })
        .map(|map| {
            map.iter()
                .map(|(name, _value)| IndexInfo {
                    name: Some(name.clone()),
                    columns: Vec::new(),
                    ready: None,
                })
                .collect()
        })
}

pub(crate) struct SurrealScope<'a> {
    db: &'a SurrealManager,
    locator: &'a StorageLocator,
    table: String,
}

impl SurrealScope<'_> {
    pub(crate) fn table(&self) -> &str {
        &self.table
    }

    pub(crate) async fn run_value(&self, sql: String) -> Result<Value, StorageError> {
        self.db.run_query_value(Some(self.locator), sql).await
    }

    pub(crate) async fn run_take(
        &self,
        sql: String,
        binds: BTreeMap<String, Value>,
        take: usize,
    ) -> Result<Value, StorageError> {
        self.db
            .run_query_take(Some(self.locator), sql, binds, take)
            .await
    }
}