dbx_core/storage/mod.rs
1//! Storage module — 5-Tier Hybrid Storage architecture.
2//!
3//! All storage engines implement the [`StorageBackend`] trait.
4//! The SQL layer depends only on this trait (Dependency Inversion Principle).
5
6pub mod arrow_ipc;
7pub mod backup;
8pub mod cache;
9pub mod columnar;
10pub mod columnar_cache;
11pub mod columnar_delta;
12pub mod compression;
13pub mod delta_store;
14pub mod encryption;
15pub mod gpu;
16pub mod index;
17pub mod kv_adapter;
18pub mod memory_wos;
19pub mod parquet_io;
20pub mod versioned_batch;
21pub mod wos;
22
23use crate::error::DbxResult;
24use std::ops::RangeBounds;
25
26/// Core storage interface — all tiers implement this trait.
27///
28/// # Design Principles
29///
30/// - **DIP**: SQL layer depends on this trait, never on concrete types.
31/// - **Strategy**: New storage tiers are added by implementing this trait.
32/// - **Thread Safety**: `Send + Sync` required for concurrent access.
33///
34/// # Contract
35///
36/// - `insert`: Upsert semantics — overwrites existing key.
37/// - `get`: Returns `None` for non-existent keys, never errors.
38/// - `delete`: Returns `true` if key existed, `false` otherwise.
39/// - `scan`: Returns key-value pairs in key order within range.
40/// - `flush`: Persists buffered data to durable storage.
41/// - `count`: Returns the number of keys in a table.
42/// - `table_names`: Returns all table names.
43pub trait StorageBackend: Send + Sync {
44 /// Insert a key-value pair.
45 fn insert(&self, table: &str, key: &[u8], value: &[u8]) -> DbxResult<()>;
46
47 /// Insert multiple key-value pairs in a batch (optimized).
48 ///
49 /// Default implementation calls insert() sequentially.
50 /// Implementations should override this for better performance.
51 fn insert_batch(&self, table: &str, rows: Vec<(Vec<u8>, Vec<u8>)>) -> DbxResult<()> {
52 for (key, value) in rows {
53 self.insert(table, &key, &value)?;
54 }
55 Ok(())
56 }
57
58 /// Get a value by key.
59 fn get(&self, table: &str, key: &[u8]) -> DbxResult<Option<Vec<u8>>>;
60
61 /// Delete a key-value pair.
62 fn delete(&self, table: &str, key: &[u8]) -> DbxResult<bool>;
63
64 /// Scan a range of keys.
65 fn scan<R: RangeBounds<Vec<u8>> + Clone>(
66 &self,
67 table: &str,
68 range: R,
69 ) -> DbxResult<Vec<(Vec<u8>, Vec<u8>)>>;
70
71 /// Scan a single key-value pair in a range (optimized).
72 fn scan_one<R: RangeBounds<Vec<u8>> + Clone>(
73 &self,
74 table: &str,
75 range: R,
76 ) -> DbxResult<Option<(Vec<u8>, Vec<u8>)>>;
77
78 /// Flush any buffered data to durable storage.
79 fn flush(&self) -> DbxResult<()>;
80
81 /// Return the number of keys in the given table.
82 fn count(&self, table: &str) -> DbxResult<usize>;
83
84 /// Return all table names managed by this backend.
85 fn table_names(&self) -> DbxResult<Vec<String>>;
86}