Skip to main content

sql_rs/storage/
mod.rs

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