datafold 0.1.55

A personal database for data sovereignty with AI-powered ingestion
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
use super::NativeIndexManager;
use crate::schema::SchemaError;
use crate::storage::traits::*;
#[cfg(feature = "aws-backend")]
use crate::storage::DynamoDbNamespacedStore;
use crate::storage::{SledNamespacedStore, TypedKvStore};
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

/// Enhanced database operations with pluggable storage backend
///
/// This version uses the storage abstraction layer, allowing the same
/// DbOperations API to work with different backends (Sled, DynamoDB, etc.)
#[derive(Clone)]
pub struct DbOperations {
    /// Main storage namespace - using concrete type instead of trait object
    main_store: Arc<TypedKvStore<dyn KvStore>>,

    /// Named namespaces (like sled trees)
    metadata_store: Arc<TypedKvStore<dyn KvStore>>,
    permissions_store: Arc<TypedKvStore<dyn KvStore>>,
    transforms_store: Arc<TypedKvStore<dyn KvStore>>,
    orchestrator_store: Arc<TypedKvStore<dyn KvStore>>,
    schema_states_store: Arc<TypedKvStore<dyn KvStore>>,
    schemas_store: Arc<TypedKvStore<dyn KvStore>>,
    public_keys_store: Arc<TypedKvStore<dyn KvStore>>,
    transform_queue_store: Arc<TypedKvStore<dyn KvStore>>,

    /// Raw KV store for native index (doesn't need typed operations)
    _native_index_store: Arc<dyn KvStore>,

    /// Optional reference to underlying sled tree for NativeIndexManager
    /// This is a temporary bridge until NativeIndexManager is also abstracted
    native_index_tree: Option<sled::Tree>,

    native_index_manager: Option<NativeIndexManager>,

    /// Optional reference to underlying orchestrator tree for TransformOrchestrator
    /// This is a temporary bridge until TransformOrchestrator is abstracted
    pub orchestrator_tree: Option<sled::Tree>,
}

impl DbOperations {
    /// Create from a NamespacedStore (works with any backend)
    pub async fn from_namespaced_store(
        store: Arc<dyn NamespacedStore>,
    ) -> Result<Self, crate::storage::StorageError> {
        // Open all required namespaces
        let main_kv = store.open_namespace("main").await?;
        let metadata_kv = store.open_namespace("metadata").await?;
        let permissions_kv = store.open_namespace("node_id_schema_permissions").await?;
        let transforms_kv = store.open_namespace("transforms").await?;
        let orchestrator_kv = store.open_namespace("orchestrator_state").await?;
        let schema_states_kv = store.open_namespace("schema_states").await?;
        let schemas_kv = store.open_namespace("schemas").await?;
        let public_keys_kv = store.open_namespace("public_keys").await?;
        let transform_queue_kv = store.open_namespace("transform_queue_tree").await?;
        let native_index_kv = store.open_namespace("native_index").await?;

        // Wrap KvStores in TypedKvStore adapters
        let main_store = Arc::new(TypedKvStore::new(main_kv));
        let metadata_store = Arc::new(TypedKvStore::new(metadata_kv));
        let permissions_store = Arc::new(TypedKvStore::new(permissions_kv));
        let transforms_store = Arc::new(TypedKvStore::new(transforms_kv));
        let orchestrator_store = Arc::new(TypedKvStore::new(orchestrator_kv));
        let schema_states_store = Arc::new(TypedKvStore::new(schema_states_kv));
        let schemas_store = Arc::new(TypedKvStore::new(schemas_kv));
        let public_keys_store = Arc::new(TypedKvStore::new(public_keys_kv));
        let transform_queue_store = Arc::new(TypedKvStore::new(transform_queue_kv));

        // Create native index manager with the store
        let native_index_manager = NativeIndexManager::new_with_store(native_index_kv.clone());

        Ok(Self {
            main_store,
            metadata_store,
            permissions_store,
            transforms_store,
            orchestrator_store,
            schema_states_store,
            schemas_store,
            public_keys_store,
            transform_queue_store,
            _native_index_store: native_index_kv,
            native_index_tree: None,
            native_index_manager: Some(native_index_manager),
            orchestrator_tree: None,
        })
    }

    /// Convenience constructor for Sled backend (backward compatible)
    pub async fn from_sled(db: sled::Db) -> Result<Self, crate::storage::StorageError> {
        let native_index_tree = db
            .open_tree("native_index")
            .map_err(|e| crate::storage::StorageError::SledError(e.to_string()))?;
        let native_index_manager = NativeIndexManager::new(native_index_tree.clone());

        let orchestrator_tree = db
            .open_tree("orchestrator_state")
            .map_err(|e| crate::storage::StorageError::SledError(e.to_string()))?;

        let store = Arc::new(SledNamespacedStore::new(db)) as Arc<dyn NamespacedStore>;
        let mut db_ops = Self::from_namespaced_store(store).await?;

        // Set the native index and orchestrator components (temporary bridges)
        db_ops.native_index_tree = Some(native_index_tree);
        db_ops.native_index_manager = Some(native_index_manager);
        db_ops.orchestrator_tree = Some(orchestrator_tree);

        Ok(db_ops)
    }

    /// Convenience constructor for DynamoDB backend with simplified config
    /// Convenience constructor for DynamoDB backend with simplified config
    #[cfg(feature = "aws-backend")]
    pub async fn from_dynamodb(
        client: aws_sdk_dynamodb::Client,
        table_name: String,
        user_id: String,
    ) -> Result<Self, crate::storage::StorageError> {
        let store = DynamoDbNamespacedStore::new_with_prefix(client, table_name, user_id.clone());
        Self::from_namespaced_store(Arc::new(store)).await
    }

    /// Constructor for DynamoDB backend with detailed configuration
    #[cfg(feature = "aws-backend")]
    pub async fn from_dynamodb_flexible(
        client: aws_sdk_dynamodb::Client,
        resolver: crate::storage::TableNameResolver,
        auto_create: bool,
        user_id: String,
    ) -> Result<Self, crate::storage::StorageError> {
        let store = DynamoDbNamespacedStore::new(client, resolver, auto_create, user_id.clone());
        Self::from_namespaced_store(Arc::new(store)).await
    }

    // ===== Generic storage operations (async API) =====

    /// Store an item in the main namespace
    pub async fn store_item<T: Serialize + Send + Sync>(
        &self,
        key: &str,
        item: &T,
    ) -> Result<(), SchemaError> {
        self.main_store
            .put_item(key, item)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Get an item from the main namespace
    pub async fn get_item<T: DeserializeOwned + Send + Sync>(
        &self,
        key: &str,
    ) -> Result<Option<T>, SchemaError> {
        self.main_store
            .get_item(key)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Delete an item from the main namespace
    pub async fn delete_item(&self, key: &str) -> Result<bool, SchemaError> {
        self.main_store
            .delete_item(key)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// List keys with prefix
    pub async fn list_items_with_prefix(&self, prefix: &str) -> Result<Vec<String>, SchemaError> {
        self.main_store
            .list_keys_with_prefix(prefix)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Store an item in a specific namespace
    pub async fn store_in_namespace<T: Serialize + Send + Sync>(
        &self,
        namespace: &str,
        key: &str,
        item: &T,
    ) -> Result<(), SchemaError> {
        let store = self.get_namespace_store(namespace)?;
        store
            .put_item(key, item)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Get an item from a specific namespace
    pub async fn get_from_namespace<T: DeserializeOwned + Send + Sync>(
        &self,
        namespace: &str,
        key: &str,
    ) -> Result<Option<T>, SchemaError> {
        let store = self.get_namespace_store(namespace)?;
        store
            .get_item(key)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// List all keys in a namespace
    pub async fn list_keys_in_namespace(
        &self,
        namespace: &str,
    ) -> Result<Vec<String>, SchemaError> {
        let store = self.get_namespace_store(namespace)?;
        store
            .list_keys_with_prefix("")
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Delete an item from a specific namespace
    pub async fn delete_from_namespace(
        &self,
        namespace: &str,
        key: &str,
    ) -> Result<bool, SchemaError> {
        let store = self.get_namespace_store(namespace)?;
        store
            .delete_item(key)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Check if a key exists in a specific namespace
    pub async fn exists_in_namespace(
        &self,
        namespace: &str,
        key: &str,
    ) -> Result<bool, SchemaError> {
        let store = self.get_namespace_store(namespace)?;
        store
            .exists_item(key)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    // ===== Namespace-specific store getters =====

    pub fn metadata_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.metadata_store
    }

    pub fn permissions_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.permissions_store
    }

    pub fn transforms_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.transforms_store
    }

    pub fn orchestrator_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.orchestrator_store
    }

    pub fn schema_states_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.schema_states_store
    }

    pub fn schemas_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.schemas_store
    }

    pub fn public_keys_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.public_keys_store
    }

    pub fn transform_queue_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.transform_queue_store
    }

    pub fn native_index_manager(&self) -> Option<&NativeIndexManager> {
        self.native_index_manager.as_ref()
    }

    /// Get atoms/molecules store (same as main_store for backward compatibility)
    pub fn atoms_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.main_store
    }

    /// Get molecules store (same as main_store for backward compatibility)
    pub fn molecules_store(&self) -> &Arc<TypedKvStore<dyn KvStore>> {
        &self.main_store
    }

    /// Flush all pending writes to durable storage
    /// For Sled backends, this ensures data is written to disk
    /// For cloud backends like DynamoDB, this is typically a no-op (auto-flushed)
    pub async fn flush(&self) -> Result<(), SchemaError> {
        // Storage abstraction handles flushing internally
        // For Sled, this is done via the KvStore trait's flush method
        self.main_store
            .inner()
            .flush()
            .await
            .map_err(|e| SchemaError::InvalidData(format!("Flush failed: {}", e)))
    }

    // ===== Batch operations =====

    /// Batch store multiple items
    pub async fn batch_store_items<T: Serialize + Send + Sync + Clone>(
        &self,
        items: &[(String, T)],
    ) -> Result<(), SchemaError> {
        let items_vec: Vec<(String, T)> =
            items.iter().map(|(k, v)| (k.clone(), v.clone())).collect();

        self.main_store
            .batch_put_items(items_vec)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Batch store items in a specific namespace
    pub async fn batch_store_in_namespace<T: Serialize + Send + Sync + Clone>(
        &self,
        namespace: &str,
        items: &[(String, T)],
    ) -> Result<(), SchemaError> {
        let store = self.get_namespace_store(namespace)?;
        let items_vec: Vec<(String, T)> =
            items.iter().map(|(k, v)| (k.clone(), v.clone())).collect();

        store
            .batch_put_items(items_vec)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))
    }

    /// Get database statistics (approximate for non-Sled backends)
    pub async fn get_stats(&self) -> Result<HashMap<String, u64>, SchemaError> {
        let mut stats = HashMap::new();

        // Count items with prefixes in main store
        let atoms = self
            .main_store
            .list_keys_with_prefix("atom:")
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?;
        stats.insert("atoms".to_string(), atoms.len() as u64);

        let refs = self
            .main_store
            .list_keys_with_prefix("ref:")
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?;
        stats.insert("refs".to_string(), refs.len() as u64);

        // For other namespaces, count all keys
        let metadata_keys = self
            .metadata_store
            .list_keys_with_prefix("")
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?;
        stats.insert("metadata".to_string(), metadata_keys.len() as u64);

        let permissions_keys = self
            .permissions_store
            .list_keys_with_prefix("")
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?;
        stats.insert("permissions".to_string(), permissions_keys.len() as u64);

        let transforms_keys = self
            .transforms_store
            .list_keys_with_prefix("")
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?;
        stats.insert("transforms".to_string(), transforms_keys.len() as u64);

        Ok(stats)
    }

    // ===== Helper methods =====

    fn get_namespace_store(
        &self,
        namespace: &str,
    ) -> Result<&Arc<TypedKvStore<dyn KvStore>>, SchemaError> {
        match namespace {
            "metadata" => Ok(&self.metadata_store),
            "permissions" | "node_id_schema_permissions" => Ok(&self.permissions_store),
            "transforms" => Ok(&self.transforms_store),
            "orchestrator" | "orchestrator_state" => Ok(&self.orchestrator_store),
            "schema_states" => Ok(&self.schema_states_store),
            "schemas" => Ok(&self.schemas_store),
            "public_keys" => Ok(&self.public_keys_store),
            "transform_queue" | "transform_queue_tree" => Ok(&self.transform_queue_store),
            "main" => Ok(&self.main_store),
            _ => Err(SchemaError::InvalidData(format!(
                "Unknown namespace: {}",
                namespace
            ))),
        }
    }
}