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