Skip to main content

sql_rs/storage/
mod.rs

1mod page;
2mod btree;
3mod file;
4mod wal;
5mod transaction;
6mod stats;
7
8pub use btree::BTree;
9pub use page::{Page, PageId};
10pub use wal::{WriteAheadLog, WalEntry};
11pub use transaction::{Transaction, TransactionManager, TransactionOp, TransactionState};
12pub use stats::{DatabaseStats, TableStats};
13
14use crate::Result;
15use std::path::Path;
16
17pub trait Storage: Send + Sync {
18    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
19    fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
20    fn delete(&mut self, key: &[u8]) -> Result<()>;
21    fn scan(&self, start: &[u8], end: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
22    fn flush(&mut self) -> Result<()>;
23}
24
25pub struct StorageEngine {
26    btree: BTree,
27}
28
29impl StorageEngine {
30    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
31        let btree = BTree::open(path)?;
32        Ok(Self { btree })
33    }
34
35    pub fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
36        let btree = BTree::create(path)?;
37        Ok(Self { btree })
38    }
39
40    pub fn get_stats(&self, vector_collections: usize, tables: Vec<TableStats>) -> Result<DatabaseStats> {
41        DatabaseStats::collect(self.btree.get_file_manager(), vector_collections, tables)
42    }
43}
44
45impl Storage for StorageEngine {
46    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
47        self.btree.get(key)
48    }
49    
50    fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
51        self.btree.insert(key, value)
52    }
53    
54    fn delete(&mut self, key: &[u8]) -> Result<()> {
55        self.btree.delete(key)
56    }
57    
58    fn scan(&self, start: &[u8], end: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
59        self.btree.scan(start, end)
60    }
61    
62    fn flush(&mut self) -> Result<()> {
63        self.btree.flush()
64    }
65}