foundation_db 0.0.1

Unified storage backend with Turso sync API, D1, R2, and in-memory fallback
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! `NamespacedStore` — `StateStore` wrapper with automatic key prefixing.
//!
//! WHY: Deployables need isolated state so one deployable's resources can't
//! collide with another's. The wrapper makes prefixing structural, not convention.
//!
//! WHAT: Thin wrapper around any `StateStore` that prefixes all keys with a namespace.
//!
//! HOW: All key-based operations prepend `self.prefix`. `list()` filters by prefix
//! and strips it from returned keys.

use super::traits::{StateStore, StateStoreStream};
use super::types::ResourceState;
use super::types::StateStatus;
use crate::core::errors::StorageError;
use foundation_core::valtron::ThreadedValue;
use std::sync::Arc;

/// `StateStore` wrapper that automatically prefixes all keys with a namespace.
///
/// WHY: Convention-based prefixing is easy to forget and impossible to enforce.
/// The wrapper makes it structural — once you have a `NamespacedStore`, every
/// operation is scoped. You can't accidentally write to another deployable's namespace.
///
/// WHAT: Implements `StateStore` by prepending `{prefix}/` to all keys.
///
/// HOW: `store(key, val)` → inner.store("{prefix}{key}", val), etc.
/// `list()` filters to prefix keys and strips the prefix from returned keys.
pub struct NamespacedStore<S: StateStore> {
    inner: Arc<S>,
    prefix: String,
}

impl<S: StateStore> NamespacedStore<S> {
    /// Create a new `NamespacedStore` wrapping the given `StateStore`.
    ///
    /// All operations on the returned store will be prefixed with `namespace`.
    #[must_use]
    pub fn new(inner: Arc<S>, namespace: &str) -> Self {
        Self {
            inner,
            prefix: format!("{namespace}/"),
        }
    }

    /// Get the namespace prefix used by this store.
    #[must_use]
    pub fn prefix(&self) -> &str {
        &self.prefix
    }

    /// Store a typed value under the given key.
    ///
    /// Serializes the value as JSON into the `ResourceState`'s `output` field.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails or the inner store fails.
    pub fn store_typed<T: serde::Serialize>(
        &self,
        key: &str,
        value: &T,
    ) -> Result<(), StorageError> {
        let full_key = format!("{prefix}{key}", prefix = self.prefix);
        let json_value = serde_json::to_value(value).map_err(|e| {
            StorageError::Io(std::io::Error::other(format!(
                "Failed to serialize state: {e}"
            )))
        })?;

        let state = ResourceState {
            id: full_key.clone(),
            kind: String::new(),
            provider: String::new(),
            status: StateStatus::Created,
            environment: None,
            config_hash: String::new(),
            output: json_value,
            config_snapshot: serde_json::json!(null),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        };

        let stream = self.inner.set(&full_key, &state)?;
        // Drive the stream to completion (sync backends yield one value)
        for item in stream {
            if let ThreadedValue::Value(Err(e)) = item {
                return Err(e);
            }
        }
        Ok(())
    }

    /// Get a typed value from the store.
    ///
    /// Deserializes the `ResourceState`'s `output` field into the requested type.
    ///
    /// # Errors
    ///
    /// Returns an error if deserialization fails or the inner store fails.
    pub fn get_typed<T: for<'de> serde::Deserialize<'de>>(
        &self,
        key: &str,
    ) -> Result<Option<T>, StorageError> {
        let full_key = format!("{prefix}{key}", prefix = self.prefix);
        let stream = self.inner.get(&full_key)?;

        if let Some(item) = stream.into_iter().next() {
            match item {
                ThreadedValue::Value(Ok(Some(state))) => {
                    let value: T = serde_json::from_value(state.output).map_err(|e| {
                        StorageError::Io(std::io::Error::other(format!(
                            "Failed to deserialize state: {e}"
                        )))
                    })?;
                    return Ok(Some(value));
                }
                ThreadedValue::Value(Ok(None)) => return Ok(None),
                ThreadedValue::Value(Err(e)) => return Err(e),
                ThreadedValue::Waiting => {}
            }
        }
        Ok(None)
    }

    /// Delete a key from the store.
    ///
    /// # Errors
    ///
    /// Returns an error if the inner store fails.
    pub fn remove(&self, key: &str) -> Result<(), StorageError> {
        let full_key = format!("{prefix}{key}", prefix = self.prefix);
        let stream = self.inner.delete(&full_key)?;
        for item in stream {
            if let ThreadedValue::Value(Err(e)) = item {
                return Err(e);
            }
        }
        Ok(())
    }
}

impl<S: StateStore> StateStore for NamespacedStore<S> {
    fn init(&self) -> Result<(), StorageError> {
        self.inner.init()
    }

    fn list(&self) -> Result<StateStoreStream<String>, StorageError> {
        let stream = self.inner.list_by_prefix(&self.prefix)?;
        let prefix = self.prefix.clone();
        Ok(Box::new(stream.map(move |item| match item {
            ThreadedValue::Value(Ok(id)) => {
                ThreadedValue::Value(Ok(id.strip_prefix(&prefix).unwrap().to_string()))
            }
            other => other,
        })))
    }

    fn count(&self) -> Result<StateStoreStream<usize>, StorageError> {
        self.inner.count_by_prefix(&self.prefix)
    }

    fn get(
        &self,
        resource_id: &str,
    ) -> Result<StateStoreStream<Option<ResourceState>>, StorageError> {
        let full_key = format!("{prefix}{resource_id}", prefix = self.prefix);
        self.inner.get(&full_key)
    }

    fn get_batch(&self, ids: &[&str]) -> Result<StateStoreStream<ResourceState>, StorageError> {
        let full_ids: Vec<String> = ids
            .iter()
            .map(|id| format!("{prefix}{id}", prefix = self.prefix))
            .collect();
        let full_id_refs: Vec<&str> = full_ids.iter().map(String::as_str).collect();
        self.inner.get_batch(&full_id_refs)
    }

    fn all(&self) -> Result<StateStoreStream<ResourceState>, StorageError> {
        self.inner.all_by_prefix(&self.prefix)
    }

    fn set(
        &self,
        resource_id: &str,
        state: &ResourceState,
    ) -> Result<StateStoreStream<()>, StorageError> {
        let full_key = format!("{prefix}{resource_id}", prefix = self.prefix);
        self.inner.set(&full_key, state)
    }

    fn delete(&self, resource_id: &str) -> Result<StateStoreStream<()>, StorageError> {
        let full_key = format!("{prefix}{resource_id}", prefix = self.prefix);
        self.inner.delete(&full_key)
    }

    fn sync_remote(&self) -> Result<(), StorageError> {
        self.inner.sync_remote()
    }

    fn list_by_prefix(&self, prefix: &str) -> Result<StateStoreStream<String>, StorageError> {
        let combined = format!("{ns}{prefix}", ns = self.prefix, prefix = prefix);
        let stream = self.inner.list_by_prefix(&combined)?;
        let ns_prefix = self.prefix.clone();
        Ok(Box::new(stream.map(move |item| match item {
            ThreadedValue::Value(Ok(id)) => {
                let stripped = id.strip_prefix(&ns_prefix).unwrap_or(&id);
                ThreadedValue::Value(Ok(stripped.to_string()))
            }
            other => other,
        })))
    }

    fn count_by_prefix(&self, prefix: &str) -> Result<StateStoreStream<usize>, StorageError> {
        let combined = format!("{ns}{prefix}", ns = self.prefix, prefix = prefix);
        self.inner.count_by_prefix(&combined)
    }

    fn all_by_prefix(&self, prefix: &str) -> Result<StateStoreStream<ResourceState>, StorageError> {
        // Combine namespace prefix with the given prefix filter.
        // The inner store uses list_by_prefix semantics on id.
        let combined = format!("{ns}{prefix}", ns = self.prefix, prefix = prefix);
        self.inner.all_by_prefix(&combined)
    }

    fn find_by_kind(&self, kind: &str) -> Result<StateStoreStream<ResourceState>, StorageError> {
        let prefix = self.prefix.clone();
        let kind = kind.to_string();
        let all = self.inner.all_by_prefix(&prefix)?;
        Ok(Box::new(all.filter_map(move |item| match item {
            ThreadedValue::Value(Ok(state)) if state.kind == kind => {
                Some(ThreadedValue::Value(Ok(state)))
            }
            ThreadedValue::Value(Ok(_)) => None,
            ThreadedValue::Value(Err(e)) => Some(ThreadedValue::Value(Err(e))),
            ThreadedValue::Waiting => None,
        })))
    }

    fn find_by_status(
        &self,
        status: &str,
    ) -> Result<StateStoreStream<ResourceState>, StorageError> {
        let prefix = self.prefix.clone();
        let status = status.to_string();
        let all = self.inner.all_by_prefix(&prefix)?;
        Ok(Box::new(all.filter_map(move |item| match item {
            ThreadedValue::Value(Ok(state)) => {
                let status_str = state.status.to_string();
                if status_str == status {
                    Some(ThreadedValue::Value(Ok(state)))
                } else {
                    None
                }
            }
            ThreadedValue::Value(Err(e)) => Some(ThreadedValue::Value(Err(e))),
            ThreadedValue::Waiting => None,
        })))
    }

    fn find_by_provider(
        &self,
        provider: &str,
    ) -> Result<StateStoreStream<ResourceState>, StorageError> {
        let prefix = self.prefix.clone();
        let provider = provider.to_string();
        let all = self.inner.all_by_prefix(&prefix)?;
        Ok(Box::new(all.filter_map(move |item| match item {
            ThreadedValue::Value(Ok(state)) if state.provider == provider => {
                Some(ThreadedValue::Value(Ok(state)))
            }
            ThreadedValue::Value(Ok(_)) => None,
            ThreadedValue::Value(Err(e)) => Some(ThreadedValue::Value(Err(e))),
            ThreadedValue::Waiting => None,
        })))
    }

    fn delete_by_prefix(&self, prefix: &str) -> Result<StateStoreStream<usize>, StorageError> {
        let combined = format!("{ns}{prefix}", ns = self.prefix, prefix = prefix);
        self.inner.delete_by_prefix(&combined)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::state::file::FileStateStore;
    use crate::core::state::helpers::drive_to_completion;

    fn temp_store() -> (tempfile::TempDir, FileStateStore) {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let store = FileStateStore::with_root(temp_dir.path().to_path_buf());
        store.init().expect("Failed to init store");
        (temp_dir, store)
    }

    #[test]
    fn test_prefix_isolation() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);

        let ns_a = NamespacedStore::new(Arc::clone(&store_arc), "namespace-a");
        let ns_b = NamespacedStore::new(store_arc, "namespace-b");

        ns_a.store_typed("key1", &"value-a").unwrap();
        ns_b.store_typed("key1", &"value-b").unwrap();

        let a_val: Option<String> = ns_a.get_typed("key1").unwrap();
        let b_val: Option<String> = ns_b.get_typed("key1").unwrap();

        assert_eq!(a_val, Some("value-a".to_string()));
        assert_eq!(b_val, Some("value-b".to_string()));
    }

    #[test]
    fn test_list_filters_by_prefix() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "test");

        ns.store_typed("resource-1", &1).unwrap();
        ns.store_typed("resource-2", &2).unwrap();
        drive_to_completion(
            store_arc
                .set(
                    "other-key",
                    &ResourceState {
                        id: "other-key".to_string(),
                        kind: String::new(),
                        provider: String::new(),
                        status: StateStatus::Created,
                        environment: None,
                        config_hash: String::new(),
                        output: serde_json::json!(99),
                        config_snapshot: serde_json::json!(null),
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                    },
                )
                .unwrap(),
        )
        .unwrap();

        let namespaced_ids: Vec<_> = ns
            .list()
            .unwrap()
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(id)) => Some(id),
                _ => None,
            })
            .collect();

        assert!(namespaced_ids.contains(&"resource-1".to_string()));
        assert!(namespaced_ids.contains(&"resource-2".to_string()));
        assert!(!namespaced_ids.contains(&"other-key".to_string()));
    }

    #[test]
    fn test_delete_removes_key() {
        let (_temp, store) = temp_store();
        let ns = NamespacedStore::new(Arc::new(store), "del-test");

        ns.store_typed("to-delete", &42).unwrap();
        assert!(ns.get_typed::<i32>("to-delete").unwrap().is_some());

        ns.remove("to-delete").unwrap();
        assert!(ns.get_typed::<i32>("to-delete").unwrap().is_none());
    }

    #[test]
    fn test_struct_serialization() {
        #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
        struct TestData {
            name: String,
            count: u32,
        }

        let (_temp, store) = temp_store();
        let ns = NamespacedStore::new(Arc::new(store), "struct-test");

        let data = TestData {
            name: "test".to_string(),
            count: 42,
        };
        ns.store_typed("data", &data).unwrap();

        let loaded: Option<TestData> = ns.get_typed("data").unwrap();
        assert_eq!(loaded, Some(data));
    }

    #[test]
    fn test_all_returns_only_namespaced() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "all-test");
        ns.store_typed("one", &1).unwrap();
        ns.store_typed("two", &2).unwrap();

        drive_to_completion(
            store_arc
                .set(
                    "unrelated",
                    &ResourceState {
                        id: "unrelated".to_string(),
                        kind: String::new(),
                        provider: String::new(),
                        status: StateStatus::Created,
                        environment: None,
                        config_hash: String::new(),
                        output: serde_json::json!(0),
                        config_snapshot: serde_json::json!(null),
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                    },
                )
                .unwrap(),
        )
        .unwrap();

        let all: Vec<_> = ns
            .all()
            .unwrap()
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(s)) => Some(s),
                _ => None,
            })
            .collect();

        assert_eq!(all.len(), 2);
        assert!(all.iter().any(|s| s.id.ends_with("one")));
        assert!(all.iter().any(|s| s.id.ends_with("two")));
        assert!(!all.iter().any(|s| s.id == "unrelated"));
    }

    #[test]
    fn test_count_only_namespaced() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "count-test");

        ns.store_typed("a", &1).unwrap();
        ns.store_typed("b", &2).unwrap();

        drive_to_completion(
            store_arc
                .set(
                    "other",
                    &ResourceState {
                        id: "other".to_string(),
                        kind: String::new(),
                        provider: String::new(),
                        status: StateStatus::Created,
                        environment: None,
                        config_hash: String::new(),
                        output: serde_json::json!(0),
                        config_snapshot: serde_json::json!(null),
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                    },
                )
                .unwrap(),
        )
        .unwrap();

        let count: Vec<_> = ns
            .count()
            .unwrap()
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(c)) => Some(c),
                _ => None,
            })
            .collect();

        assert_eq!(count, vec![2]);
    }

    #[test]
    fn test_list_by_prefix_delegates() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "prefix-test");

        ns.store_typed("alpha-1", &1).unwrap();
        ns.store_typed("alpha-2", &2).unwrap();
        ns.store_typed("beta-1", &3).unwrap();

        let alpha_ids: Vec<_> = ns
            .list_by_prefix("alpha-")
            .unwrap()
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(id)) => Some(id),
                _ => None,
            })
            .collect();

        assert_eq!(alpha_ids.len(), 2);
        assert!(alpha_ids.contains(&"alpha-1".to_string()));
        assert!(alpha_ids.contains(&"alpha-2".to_string()));
    }

    #[test]
    fn test_count_by_prefix_delegates() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "count-prefix");

        ns.store_typed("a", &1).unwrap();
        ns.store_typed("b", &2).unwrap();
        drive_to_completion(
            store_arc
                .set(
                    "other",
                    &ResourceState {
                        id: "other".to_string(),
                        kind: String::new(),
                        provider: String::new(),
                        status: StateStatus::Created,
                        environment: None,
                        config_hash: String::new(),
                        output: serde_json::json!(0),
                        config_snapshot: serde_json::json!(null),
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                    },
                )
                .unwrap(),
        )
        .unwrap();

        let count: Vec<_> = ns
            .count_by_prefix("")
            .unwrap()
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(c)) => Some(c),
                _ => None,
            })
            .collect();

        assert_eq!(count, vec![2]);
    }

    #[test]
    fn test_delete_by_prefix_deletes_only_namespaced() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "del-prefix");

        ns.store_typed("keep", &1).unwrap();
        ns.store_typed("remove-1", &2).unwrap();
        ns.store_typed("remove-2", &3).unwrap();

        // Insert non-namespaced key directly
        drive_to_completion(
            store_arc
                .set(
                    "unrelated",
                    &ResourceState {
                        id: "unrelated".to_string(),
                        kind: String::new(),
                        provider: String::new(),
                        status: StateStatus::Created,
                        environment: None,
                        config_hash: String::new(),
                        output: serde_json::json!(0),
                        config_snapshot: serde_json::json!(null),
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                    },
                )
                .unwrap(),
        )
        .unwrap();

        ns.remove("remove-1").unwrap();
        ns.remove("remove-2").unwrap();

        assert!(ns.get_typed::<i32>("keep").unwrap().is_some());
        // Verify unrelated is still there
        let stream = store_arc.get("unrelated").unwrap();
        let found: Vec<_> = stream
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(Some(s))) => Some(s),
                _ => None,
            })
            .collect();
        assert_eq!(found.len(), 1);
    }

    #[test]
    fn test_all_by_prefix_delegates() {
        let (_temp, store) = temp_store();
        let store_arc = Arc::new(store);
        let ns = NamespacedStore::new(Arc::clone(&store_arc), "all-prefix");

        ns.store_typed("one", &10).unwrap();
        ns.store_typed("two", &20).unwrap();

        // Insert non-namespaced key directly
        drive_to_completion(
            store_arc
                .set(
                    "unrelated",
                    &ResourceState {
                        id: "unrelated".to_string(),
                        kind: String::new(),
                        provider: String::new(),
                        status: StateStatus::Created,
                        environment: None,
                        config_hash: String::new(),
                        output: serde_json::json!(0),
                        config_snapshot: serde_json::json!(null),
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                    },
                )
                .unwrap(),
        )
        .unwrap();

        let all: Vec<_> = ns
            .all()
            .unwrap()
            .filter_map(|i| match i {
                ThreadedValue::Value(Ok(s)) => Some(s),
                _ => None,
            })
            .collect();

        assert_eq!(all.len(), 2);
        assert!(!all.iter().any(|s| s.id == "unrelated"));
    }
}