mod memory;
pub use memory::InMemoryStorage;
mod secondary;
pub use secondary::{SecondaryStorage, StorageOptions as SecondaryStorageOptions};
mod error;
pub use error::{StorageError, StorageResult, TracedStorageError};
mod chunk;
use std::future::Future;
use std::sync::Arc;
pub use chunk::*;
use enum_dispatch::enum_dispatch;
use crate::array::{ArrayImpl, DataChunk};
use crate::catalog::{ColumnCatalog, ColumnId, DatabaseId, SchemaId, TableRefId};
use crate::types::DataValue;
use crate::v1::binder::BoundExpr;
#[enum_dispatch(StorageDispatch)]
#[derive(Clone)]
pub enum StorageImpl {
InMemoryStorage(Arc<InMemoryStorage>),
SecondaryStorage(Arc<SecondaryStorage>),
}
#[enum_dispatch]
pub trait StorageDispatch {}
impl<S: Storage> StorageDispatch for S {}
#[cfg(test)]
impl StorageImpl {
pub fn as_in_memory_storage(&self) -> Arc<InMemoryStorage> {
self.clone().try_into().unwrap()
}
}
impl StorageImpl {
pub fn enable_filter_scan(&self) -> bool {
match self {
Self::SecondaryStorage(_) => true,
Self::InMemoryStorage(_) => false,
}
}
}
pub trait Storage: Sync + Send + 'static {
type Transaction: Transaction;
type Table: Table<Transaction = Self::Transaction>;
fn create_table<'a>(
&'a self,
database_id: DatabaseId,
schema_id: SchemaId,
table_name: &'a str,
column_descs: &'a [ColumnCatalog],
ordered_pk_ids: &'a [ColumnId],
) -> impl Future<Output = StorageResult<()>> + Send + 'a;
fn get_table(&self, table_id: TableRefId) -> StorageResult<Self::Table>;
fn drop_table(
&self,
table_id: TableRefId,
) -> impl Future<Output = StorageResult<()>> + Send + '_;
}
pub trait Table: Sync + Send + Clone + 'static {
type Transaction: Transaction;
fn columns(&self) -> StorageResult<Arc<[ColumnCatalog]>>;
fn write(&self) -> impl Future<Output = StorageResult<Self::Transaction>> + Send + '_;
fn read(&self) -> impl Future<Output = StorageResult<Self::Transaction>> + Send + '_;
fn update(&self) -> impl Future<Output = StorageResult<Self::Transaction>> + Send + '_;
fn table_id(&self) -> TableRefId;
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum StorageColumnRef {
RowHandler,
Idx(u32),
}
pub trait RowHandler: Sync + Send + 'static {
fn from_column(column: &ArrayImpl, idx: usize) -> Self;
}
pub trait Transaction: Sync + Send + 'static {
type TxnIteratorType: TxnIterator;
type RowHandlerType: RowHandler;
fn scan<'a>(
&'a self,
begin_sort_key: &'a [DataValue],
end_sort_key: &'a [DataValue],
col_idx: &'a [StorageColumnRef],
is_sorted: bool,
reversed: bool,
expr: Option<BoundExpr>,
) -> impl Future<Output = StorageResult<Self::TxnIteratorType>> + Send + 'a;
fn append(&mut self, columns: DataChunk)
-> impl Future<Output = StorageResult<()>> + Send + '_;
fn delete<'a>(
&'a mut self,
id: &'a Self::RowHandlerType,
) -> impl Future<Output = StorageResult<()>> + Send + 'a;
fn commit(self) -> impl Future<Output = StorageResult<()>> + Send;
fn abort(self) -> impl Future<Output = StorageResult<()>> + Send;
}
pub trait TxnIterator: Send {
fn next_batch(
&mut self,
expected_size: Option<usize>,
) -> impl Future<Output = StorageResult<Option<DataChunk>>> + Send + '_;
}