From 46b000d7468976d08f0b9becb7c52842418f9693 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sat, 2 Sep 2023 22:25:41 +0200 Subject: [PATCH] Move storage::kv down into storage root module. --- src/bin/toydb.rs | 9 +++++---- src/sql/engine/kv.rs | 34 ++++++++++++++++---------------- src/sql/engine/mod.rs | 2 +- src/sql/engine/raft.rs | 18 ++++++++--------- src/storage/{kv => }/encoding.rs | 0 src/storage/kv/mod.rs | 7 ------- src/storage/mod.rs | 5 ++++- src/storage/{kv => }/mvcc.rs | 2 +- tests/client/mod.rs | 4 ++-- tests/setup.rs | 2 +- tests/sql/mod.rs | 6 +++--- 11 files changed, 43 insertions(+), 46 deletions(-) rename src/storage/{kv => }/encoding.rs (100%) delete mode 100644 src/storage/kv/mod.rs rename src/storage/{kv => }/mvcc.rs (99%) diff --git a/src/bin/toydb.rs b/src/bin/toydb.rs index a501f420d..c0a910df7 100644 --- a/src/bin/toydb.rs +++ b/src/bin/toydb.rs @@ -42,12 +42,13 @@ async fn main() -> Result<()> { }; let raft_state: Box = match cfg.storage_sql.as_str() { "bitcask" | "" => { - let db = storage::kv::BitCask::new_compact(path.join("state"), cfg.compact_threshold)?; - Box::new(sql::engine::Raft::new_state(storage::kv::MVCC::new(db))?) + let engine = + storage::engine::BitCask::new_compact(path.join("state"), cfg.compact_threshold)?; + Box::new(sql::engine::Raft::new_state(engine)?) } "memory" => { - let db = storage::kv::Memory::new(); - Box::new(sql::engine::Raft::new_state(storage::kv::MVCC::new(db))?) + let engine = storage::engine::Memory::new(); + Box::new(sql::engine::Raft::new_state(engine)?) } name => return Err(Error::Config(format!("Unknown SQL storage engine {}", name))), }; diff --git a/src/sql/engine/kv.rs b/src/sql/engine/kv.rs index 330ecf36c..bff8c9fa7 100644 --- a/src/sql/engine/kv.rs +++ b/src/sql/engine/kv.rs @@ -2,7 +2,7 @@ use super::super::schema::{Catalog, Table, Tables}; use super::super::types::{Expression, Row, Value}; use super::Transaction as _; use crate::error::{Error, Result}; -use crate::storage::kv; +use crate::storage; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -10,22 +10,22 @@ use std::clone::Clone; use std::collections::HashSet; /// A SQL engine based on an underlying MVCC key/value store. -pub struct KV { +pub struct KV { /// The underlying key/value store. - pub(super) kv: kv::MVCC, + pub(super) kv: storage::mvcc::MVCC, } // FIXME Implement Clone manually due to https://github.com/rust-lang/rust/issues/26925 -impl Clone for KV { +impl Clone for KV { fn clone(&self) -> Self { - KV::new(self.kv.clone()) + KV { kv: self.kv.clone() } } } -impl KV { +impl KV { /// Creates a new key/value-based SQL engine - pub fn new(kv: kv::MVCC) -> Self { - Self { kv } + pub fn new(engine: E) -> Self { + Self { kv: storage::mvcc::MVCC::new(engine) } } /// Fetches an unversioned metadata value @@ -39,7 +39,7 @@ impl KV { } } -impl super::Engine for KV { +impl super::Engine for KV { type Transaction = Transaction; fn begin(&self, mode: super::Mode) -> Result { @@ -62,13 +62,13 @@ fn deserialize<'a, V: Deserialize<'a>>(bytes: &'a [u8]) -> Result { } /// An SQL transaction based on an MVCC key/value transaction -pub struct Transaction { - txn: kv::mvcc::Transaction, +pub struct Transaction { + txn: storage::mvcc::Transaction, } -impl Transaction { +impl Transaction { /// Creates a new SQL transaction from an MVCC transaction - fn new(txn: kv::mvcc::Transaction) -> Self { + fn new(txn: storage::mvcc::Transaction) -> Self { Self { txn } } @@ -99,7 +99,7 @@ impl Transaction { } } -impl super::Transaction for Transaction { +impl super::Transaction for Transaction { fn id(&self) -> u64 { self.txn.id() } @@ -270,7 +270,7 @@ impl super::Transaction for Transaction { } } -impl Catalog for Transaction { +impl Catalog for Transaction { fn create_table(&mut self, table: Table) -> Result<()> { if self.read_table(&table.name)?.is_some() { return Err(Error::Value(format!("Table {} already exists", table.name))); @@ -325,7 +325,7 @@ enum Key<'a> { impl<'a> Key<'a> { /// Encodes the key as a byte vector fn encode(self) -> Vec { - use kv::encoding::*; + use storage::encoding::*; match self { Self::Table(None) => vec![0x01], Self::Table(Some(name)) => [&[0x01][..], &encode_string(&name)].concat(), @@ -348,7 +348,7 @@ impl<'a> Key<'a> { /// Decodes a key from a byte vector fn decode(mut bytes: &[u8]) -> Result { - use kv::encoding::*; + use storage::encoding::*; let bytes = &mut bytes; let key = match take_byte(bytes)? { 0x01 => Self::Table(Some(take_string(bytes)?.into())), diff --git a/src/sql/engine/mod.rs b/src/sql/engine/mod.rs index d705dad74..6d8560c22 100644 --- a/src/sql/engine/mod.rs +++ b/src/sql/engine/mod.rs @@ -173,7 +173,7 @@ impl Session { } /// The transaction mode -pub type Mode = crate::storage::kv::mvcc::Mode; +pub type Mode = crate::storage::mvcc::Mode; /// A row scan iterator pub type Scan = Box> + Send>; diff --git a/src/sql/engine/raft.rs b/src/sql/engine/raft.rs index 0b27460d1..8fb7a1f15 100644 --- a/src/sql/engine/raft.rs +++ b/src/sql/engine/raft.rs @@ -3,7 +3,7 @@ use super::super::types::{Expression, Row, Value}; use super::{Engine as _, IndexScan, Mode, Scan, Transaction as _}; use crate::error::{Error, Result}; use crate::raft; -use crate::storage::kv; +use crate::storage; use serde::{Deserialize, Serialize}; use std::collections::HashSet; @@ -58,7 +58,7 @@ enum Query { #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Status { pub raft: raft::Status, - pub mvcc: kv::mvcc::Status, + pub mvcc: storage::mvcc::Status, } /// An SQL engine that wraps a Raft cluster. @@ -74,8 +74,8 @@ impl Raft { } /// Creates an underlying state machine for a Raft engine. - pub fn new_state(kv: kv::MVCC) -> Result> { - State::new(kv) + pub fn new_state(engine: E) -> Result> { + State::new(engine) } /// Returns Raft SQL engine status. @@ -260,16 +260,16 @@ impl Catalog for Transaction { } /// The Raft state machine for the Raft-based SQL engine, using a KV SQL engine -pub struct State { +pub struct State { /// The underlying KV SQL engine engine: super::KV, /// The last applied index applied_index: u64, } -impl State { - /// Creates a new Raft state maching using the given MVCC key/value store - pub fn new(engine: kv::MVCC) -> Result { +impl State { + /// Creates a new Raft state maching using the given storage engine. + pub fn new(engine: E) -> Result { let engine = super::KV::new(engine); let applied_index = engine .get_metadata(b"applied_index")? @@ -305,7 +305,7 @@ impl State { } } -impl raft::State for State { +impl raft::State for State { fn applied_index(&self) -> u64 { self.applied_index } diff --git a/src/storage/kv/encoding.rs b/src/storage/encoding.rs similarity index 100% rename from src/storage/kv/encoding.rs rename to src/storage/encoding.rs diff --git a/src/storage/kv/mod.rs b/src/storage/kv/mod.rs deleted file mode 100644 index ca92b871f..000000000 --- a/src/storage/kv/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub mod encoding; -pub mod mvcc; - -pub use super::engine::BitCask; -pub use super::engine::Engine; -pub use super::engine::Memory; -pub use mvcc::MVCC; diff --git a/src/storage/mod.rs b/src/storage/mod.rs index bea47e2f6..3553d35b5 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1,3 +1,6 @@ +pub mod encoding; pub mod engine; -pub mod kv; pub mod log; +pub mod mvcc; + +pub use engine::Engine; diff --git a/src/storage/kv/mvcc.rs b/src/storage/mvcc.rs similarity index 99% rename from src/storage/kv/mvcc.rs rename to src/storage/mvcc.rs index fa7338f02..c030e9c7d 100644 --- a/src/storage/kv/mvcc.rs +++ b/src/storage/mvcc.rs @@ -507,7 +507,7 @@ impl<'a> DoubleEndedIterator for Scan<'a> { #[cfg(test)] pub mod tests { - use super::super::Memory; + use super::super::engine::Memory; use super::*; fn setup() -> MVCC { diff --git a/tests/client/mod.rs b/tests/client/mod.rs index 6809b0ce2..e1ffbf2c1 100644 --- a/tests/client/mod.rs +++ b/tests/client/mod.rs @@ -8,7 +8,7 @@ use toydb::sql::engine::{Mode, Status}; use toydb::sql::execution::ResultSet; use toydb::sql::schema; use toydb::sql::types::{Column, DataType, Value}; -use toydb::storage::kv; +use toydb::storage::mvcc; use toydb::Client; use pretty_assertions::assert_eq; @@ -131,7 +131,7 @@ async fn status() -> Result<()> { storage: "hybrid".into(), storage_size: 3239, }, - mvcc: kv::mvcc::Status { txns: 1, txns_active: 0, storage: "memory".into() }, + mvcc: mvcc::Status { txns: 1, txns_active: 0, storage: "memory".into() }, } ); Ok(()) diff --git a/tests/setup.rs b/tests/setup.rs index 469d1cde0..b84a820c7 100644 --- a/tests/setup.rs +++ b/tests/setup.rs @@ -79,7 +79,7 @@ pub async fn server( id, peers, Box::new(storage::log::Hybrid::new(dir.path(), false)?), - Box::new(sql::engine::Raft::new_state(storage::kv::MVCC::new(storage::kv::Memory::new()))?), + Box::new(sql::engine::Raft::new_state(storage::engine::Memory::new())?), ) .await?; diff --git a/tests/sql/mod.rs b/tests/sql/mod.rs index df87f9120..069b2e014 100644 --- a/tests/sql/mod.rs +++ b/tests/sql/mod.rs @@ -5,11 +5,11 @@ mod schema; use toydb::error::Result; use toydb::sql::engine::{Engine, KV}; -use toydb::storage::kv; +use toydb::storage; /// Sets up a basic in-memory SQL engine with an initial dataset. -fn setup(queries: Vec<&str>) -> Result> { - let engine = KV::new(kv::MVCC::new(kv::Memory::new())); +fn setup(queries: Vec<&str>) -> Result> { + let engine = KV::new(storage::engine::Memory::new()); let mut session = engine.session()?; session.execute("BEGIN")?; for query in queries {