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