Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(storage): use dynamic trait for storage interfaces #846

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ impl<T: NativeType> Array for PrimitiveArray<T> {
}
}

impl<T: NativeType> PrimitiveArray<T> {
/// Return the values of the array.
pub fn values(&self) -> &[T] {
assert!(self.valid.all(), "not all values are non-null");
&self.data
}
}

impl<T: NativeType> ArrayValidExt for PrimitiveArray<T> {
fn get_valid_bitmap(&self) -> &BitVec {
&self.valid
Expand Down
36 changes: 12 additions & 24 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ use crate::catalog::{RootCatalog, RootCatalogRef, TableRefId};
use crate::parser::{parse, ParserError, Statement};
use crate::planner::Statistics;
use crate::storage::{
InMemoryStorage, SecondaryStorage, SecondaryStorageOptions, Storage, StorageColumnRef,
StorageImpl, Table,
InMemoryStorage, SecondaryStorage, SecondaryStorageOptions, StorageColumnRef, StorageRef,
};

/// The database instance.
pub struct Database {
catalog: RootCatalogRef,
storage: StorageImpl,
storage: StorageRef,
config: Mutex<Config>,
}

Expand All @@ -32,10 +31,10 @@ struct Config {
impl Database {
/// Create a new in-memory database instance.
pub fn new_in_memory() -> Self {
let storage = InMemoryStorage::new();
let storage = Arc::new(InMemoryStorage::new());
Database {
catalog: storage.catalog().clone(),
storage: StorageImpl::InMemoryStorage(Arc::new(storage)),
storage,
config: Default::default(),
}
}
Expand All @@ -46,15 +45,13 @@ impl Database {
storage.spawn_compactor().await;
Database {
catalog: storage.catalog().clone(),
storage: StorageImpl::SecondaryStorage(storage),
storage,
config: Default::default(),
}
}

pub async fn shutdown(&self) -> Result<(), Error> {
if let StorageImpl::SecondaryStorage(storage) = &self.storage {
storage.shutdown().await?;
}
self.storage.shutdown().await?;
Ok(())
}

Expand Down Expand Up @@ -104,14 +101,7 @@ impl Database {
if !self.config.lock().unwrap().disable_optimizer {
plan = optimizer.optimize(plan);
}
let executor = match self.storage.clone() {
StorageImpl::InMemoryStorage(s) => {
crate::executor::build(optimizer.clone(), s, &plan)
}
StorageImpl::SecondaryStorage(s) => {
crate::executor::build(optimizer.clone(), s, &plan)
}
};
let executor = crate::executor::build(optimizer.clone(), self.storage.clone(), &plan);
let output = executor.try_collect().await?;
let mut chunk = Chunk::new(output);
chunk = bind_header(chunk, &stmt);
Expand All @@ -126,9 +116,9 @@ impl Database {
}
let mut stat = Statistics::default();
// only secondary storage supports statistics
let StorageImpl::SecondaryStorage(storage) = self.storage.clone() else {
if !self.storage.as_any().is::<SecondaryStorage>() {
return Ok(stat);
};
}
for schema in self.catalog.all_schemas().values() {
// skip internal schema
if schema.name() == RootCatalog::SYSTEM_SCHEMA_NAME {
Expand All @@ -139,12 +129,10 @@ impl Database {
continue;
}
let table_id = TableRefId::new(schema.id(), table.id());
let table = storage.get_table(table_id)?;
let table = self.storage.get_table(table_id).await?;
let txn = table.read().await?;
let values = txn.aggreagate_block_stat(&[(
BlockStatisticsType::RowCount,
StorageColumnRef::Idx(0),
)]);
let values =
txn.get_stats(&[(BlockStatisticsType::RowCount, StorageColumnRef::Idx(0))]);
stat.add_row_count(table_id, values[0].as_usize().unwrap().unwrap() as u32);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/executor/create_table.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.

use std::sync::Arc;

use super::*;
use crate::binder::CreateTable;
use crate::storage::Storage;

/// The executor of `create table` statement.
pub struct CreateTableExecutor<S: Storage> {
pub struct CreateTableExecutor {
pub table: Box<CreateTable>,
pub storage: Arc<S>,
pub storage: StorageRef,
}

impl<S: Storage> CreateTableExecutor<S> {
impl CreateTableExecutor {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self) {
self.storage
Expand Down
26 changes: 10 additions & 16 deletions src/executor/delete.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.

use std::sync::Arc;

use super::*;
use crate::array::DataChunk;
use crate::array::{ArrayImpl, DataChunk};
use crate::catalog::TableRefId;
use crate::storage::{RowHandler, Storage, Table, Transaction};
use crate::storage::StorageRef;

/// The executor of `delete` statement.
///
/// The last column of the input data chunk should be `_row_id_`.
pub struct DeleteExecutor<S: Storage> {
pub struct DeleteExecutor {
pub table_id: TableRefId,
pub storage: Arc<S>,
pub storage: StorageRef,
}

impl<S: Storage> DeleteExecutor<S> {
impl DeleteExecutor {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self, child: BoxedExecutor) {
let table = self.storage.get_table(self.table_id)?;
let table = self.storage.get_table(self.table_id).await?;
let mut txn = table.update().await?;
let mut cnt = 0;
#[for_await]
for chunk in child {
let chunk = chunk?;
let row_handlers = chunk.array_at(chunk.column_count() - 1);
for row_handler_idx in 0..row_handlers.len() {
let row_handler = <S::Transaction as Transaction>::RowHandlerType::from_column(
row_handlers,
row_handler_idx,
);
txn.delete(&row_handler).await?;
}
let ArrayImpl::Int64(rowids) = chunk.array_at(chunk.column_count() - 1) else {
panic!("column _row_id_ should be i64 type");
};
txn.delete(rowids.values()).await?;
cnt += chunk.cardinality();
}
txn.commit().await?;
Expand Down
9 changes: 3 additions & 6 deletions src/executor/drop.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.

use std::sync::Arc;

use super::*;
use crate::catalog::{RootCatalogRef, TableRefId};
use crate::storage::Storage;

/// The executor of `drop` statement.
pub struct DropExecutor<S: Storage> {
pub struct DropExecutor {
pub tables: Vec<TableRefId>,
pub catalog: RootCatalogRef,
pub storage: Arc<S>,
pub storage: StorageRef,
}

impl<S: Storage> DropExecutor<S> {
impl DropExecutor {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self) {
for table in self.tables {
Expand Down
20 changes: 8 additions & 12 deletions src/executor/insert.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.

use std::sync::Arc;

use super::*;
use crate::array::DataChunk;
use crate::catalog::{ColumnId, TableRefId};
use crate::storage::{Storage, Table, Transaction};
use crate::types::ColumnIndex;

/// The executor of `insert` statement.
pub struct InsertExecutor<S: Storage> {
pub struct InsertExecutor {
pub table_id: TableRefId,
pub column_ids: Vec<ColumnId>,
pub storage: Arc<S>,
pub storage: StorageRef,
}

impl<S: Storage> InsertExecutor<S> {
impl InsertExecutor {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self, child: BoxedExecutor) {
let table = self.storage.get_table(self.table_id)?;
let table = self.storage.get_table(self.table_id).await?;
let columns = table.columns()?;

// construct an expression
Expand Down Expand Up @@ -59,7 +56,7 @@ mod tests {
use super::*;
use crate::array::ArrayImpl;
use crate::catalog::{ColumnCatalog, ColumnDesc, TableRefId};
use crate::storage::{InMemoryStorage, StorageImpl};
use crate::storage::{InMemoryStorage, Storage, StorageRef};
use crate::types::DataType;

#[tokio::test]
Expand All @@ -68,7 +65,7 @@ mod tests {
let executor = InsertExecutor {
table_id: TableRefId::new(1, 0),
column_ids: vec![0, 1],
storage: storage.as_in_memory_storage(),
storage,
};
let source = async_stream::try_stream! {
yield [
Expand All @@ -82,10 +79,9 @@ mod tests {
executor.execute(source).next().await.unwrap().unwrap();
}

async fn create_table() -> StorageImpl {
let storage = StorageImpl::InMemoryStorage(Arc::new(InMemoryStorage::new()));
async fn create_table() -> StorageRef {
let storage = Arc::new(InMemoryStorage::new());
storage
.as_in_memory_storage()
.create_table(
1,
"t",
Expand Down
12 changes: 6 additions & 6 deletions src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use self::window::*;
use crate::array::DataChunk;
use crate::catalog::{RootCatalog, RootCatalogRef, TableRefId};
use crate::planner::{Expr, ExprAnalysis, Optimizer, RecExpr, TypeSchemaAnalysis};
use crate::storage::Storage;
use crate::storage::StorageRef;
use crate::types::{ColumnIndex, DataType};

mod copy_from_file;
Expand Down Expand Up @@ -95,13 +95,13 @@ const PROCESSING_WINDOW_SIZE: usize = 1024;
/// and produces a stream to its parent.
pub type BoxedExecutor = BoxStream<'static, Result<DataChunk>>;

pub fn build(optimizer: Optimizer, storage: Arc<impl Storage>, plan: &RecExpr) -> BoxedExecutor {
pub fn build(optimizer: Optimizer, storage: StorageRef, plan: &RecExpr) -> BoxedExecutor {
Builder::new(optimizer, storage, plan).build()
}

/// The builder of executor.
struct Builder<S: Storage> {
storage: Arc<S>,
struct Builder {
storage: StorageRef,
optimizer: Optimizer,
egraph: egg::EGraph<Expr, TypeSchemaAnalysis>,
root: Id,
Expand All @@ -110,9 +110,9 @@ struct Builder<S: Storage> {
views: HashMap<TableRefId, StreamSubscriber>,
}

impl<S: Storage> Builder<S> {
impl Builder {
/// Create a new executor builder.
fn new(optimizer: Optimizer, storage: Arc<S>, plan: &RecExpr) -> Self {
fn new(optimizer: Optimizer, storage: StorageRef, plan: &RecExpr) -> Self {
let mut egraph = egg::EGraph::new(TypeSchemaAnalysis {
catalog: optimizer.catalog().clone(),
});
Expand Down
Loading
Loading