use crate::error::StoreError;
use std::{fmt::Debug, path::Path, sync::Arc};
pub mod tables;
pub type PrefixResult = Result<(Box<[u8]>, Box<[u8]>), StoreError>;
#[derive(Debug, Clone, Default)]
pub struct CfStats {
pub name: String,
pub live_sst_bytes: u64,
pub total_sst_bytes: u64,
pub live_data_bytes: u64,
pub num_keys: u64,
pub num_files: u64,
pub blob_bytes: u64,
pub pending_compaction_bytes: u64,
pub memtable_bytes: u64,
}
#[derive(Debug, Clone, Default)]
pub struct RocksDbStats {
pub cfs: Vec<CfStats>,
pub block_cache_usage_bytes: u64,
pub block_cache_capacity_bytes: u64,
pub block_cache_pinned_bytes: u64,
pub running_compactions: u64,
pub block_cache_hits: u64,
pub block_cache_misses: u64,
}
pub trait StorageBackend: Debug + Send + Sync {
fn clear_table(&self, table: &'static str) -> Result<(), StoreError>;
fn begin_read(&self) -> Result<Arc<dyn StorageReadView>, StoreError>;
fn begin_write(&self) -> Result<Box<dyn StorageWriteBatch + 'static>, StoreError>;
fn begin_locked(
&self,
table_name: &'static str,
) -> Result<Box<dyn StorageLockedView + 'static>, StoreError>;
fn create_checkpoint(&self, path: &Path) -> Result<(), StoreError>;
fn flush(&self) -> Result<(), StoreError> {
Ok(())
}
fn db_stats(&self) -> Option<RocksDbStats> {
None
}
}
pub trait StorageReadView: Send + Sync {
fn get(&self, table: &'static str, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError>;
fn multi_get(
&self,
table: &'static str,
keys: &[&[u8]],
) -> Vec<Result<Option<Vec<u8>>, StoreError>> {
keys.iter().map(|k| self.get(table, k)).collect()
}
fn prefix_iterator(
&self,
table: &'static str,
prefix: &[u8],
) -> Result<Box<dyn Iterator<Item = PrefixResult> + '_>, StoreError>;
fn first_key(&self, table: &'static str) -> Result<Option<Vec<u8>>, StoreError>;
fn last_key(&self, table: &'static str) -> Result<Option<Vec<u8>>, StoreError>;
}
pub trait StorageWriteBatch: Send {
fn put(&mut self, table: &'static str, key: &[u8], value: &[u8]) -> Result<(), StoreError> {
self.put_batch(table, vec![(key.to_vec(), value.to_vec())])
}
fn put_batch(
&mut self,
table: &'static str,
batch: Vec<(Vec<u8>, Vec<u8>)>,
) -> Result<(), StoreError>;
fn delete(&mut self, table: &'static str, key: &[u8]) -> Result<(), StoreError>;
fn delete_range(
&mut self,
table: &'static str,
start: &[u8],
end: &[u8],
) -> Result<(), StoreError>;
fn merge(&mut self, table: &'static str, key: &[u8], operand: &[u8]) -> Result<(), StoreError>;
fn commit(&mut self) -> Result<(), StoreError>;
}
pub trait StorageLockedView: Send + Sync {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError>;
}