Skip to content

Commit

Permalink
Split out storage engine module.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Sep 2, 2023
1 parent 786452e commit 9dadfd1
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 371 deletions.
30 changes: 15 additions & 15 deletions src/sql/engine/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ use std::borrow::Cow;
use std::clone::Clone;
use std::collections::HashSet;

/// A SQL engine based on an underlying MVCC key/value store
pub struct KV<S: kv::Store> {
/// The underlying key/value store
pub(super) kv: kv::MVCC<S>,
/// A SQL engine based on an underlying MVCC key/value store.
pub struct KV<E: kv::Engine> {
/// The underlying key/value store.
pub(super) kv: kv::MVCC<E>,
}

// FIXME Implement Clone manually due to https://github.com/rust-lang/rust/issues/26925
impl<S: kv::Store> Clone for KV<S> {
impl<E: kv::Engine> Clone for KV<E> {
fn clone(&self) -> Self {
KV::new(self.kv.clone())
}
}

impl<S: kv::Store> KV<S> {
impl<E: kv::Engine> KV<E> {
/// Creates a new key/value-based SQL engine
pub fn new(kv: kv::MVCC<S>) -> Self {
pub fn new(kv: kv::MVCC<E>) -> Self {
Self { kv }
}

Expand All @@ -39,8 +39,8 @@ impl<S: kv::Store> KV<S> {
}
}

impl<S: kv::Store> super::Engine for KV<S> {
type Transaction = Transaction<S>;
impl<E: kv::Engine> super::Engine for KV<E> {
type Transaction = Transaction<E>;

fn begin(&self, mode: super::Mode) -> Result<Self::Transaction> {
Ok(Self::Transaction::new(self.kv.begin_with_mode(mode)?))
Expand All @@ -62,13 +62,13 @@ fn deserialize<'a, V: Deserialize<'a>>(bytes: &'a [u8]) -> Result<V> {
}

/// An SQL transaction based on an MVCC key/value transaction
pub struct Transaction<S: kv::Store> {
txn: kv::mvcc::Transaction<S>,
pub struct Transaction<E: kv::Engine> {
txn: kv::mvcc::Transaction<E>,
}

impl<S: kv::Store> Transaction<S> {
impl<E: kv::Engine> Transaction<E> {
/// Creates a new SQL transaction from an MVCC transaction
fn new(txn: kv::mvcc::Transaction<S>) -> Self {
fn new(txn: kv::mvcc::Transaction<E>) -> Self {
Self { txn }
}

Expand Down Expand Up @@ -99,7 +99,7 @@ impl<S: kv::Store> Transaction<S> {
}
}

impl<S: kv::Store> super::Transaction for Transaction<S> {
impl<E: kv::Engine> super::Transaction for Transaction<E> {
fn id(&self) -> u64 {
self.txn.id()
}
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<S: kv::Store> super::Transaction for Transaction<S> {
}
}

impl<S: kv::Store> Catalog for Transaction<S> {
impl<E: kv::Engine> Catalog for Transaction<E> {
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)));
Expand Down
14 changes: 7 additions & 7 deletions src/sql/engine/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Raft {
}

/// Creates an underlying state machine for a Raft engine.
pub fn new_state<S: kv::Store>(kv: kv::MVCC<S>) -> Result<State<S>> {
pub fn new_state<E: kv::Engine>(kv: kv::MVCC<E>) -> Result<State<E>> {
State::new(kv)
}

Expand Down Expand Up @@ -260,17 +260,17 @@ impl Catalog for Transaction {
}

/// The Raft state machine for the Raft-based SQL engine, using a KV SQL engine
pub struct State<S: kv::Store> {
pub struct State<E: kv::Engine> {
/// The underlying KV SQL engine
engine: super::KV<S>,
engine: super::KV<E>,
/// The last applied index
applied_index: u64,
}

impl<S: kv::Store> State<S> {
impl<E: kv::Engine> State<E> {
/// Creates a new Raft state maching using the given MVCC key/value store
pub fn new(store: kv::MVCC<S>) -> Result<Self> {
let engine = super::KV::new(store);
pub fn new(engine: kv::MVCC<E>) -> Result<Self> {
let engine = super::KV::new(engine);
let applied_index = engine
.get_metadata(b"applied_index")?
.map(|b| Raft::deserialize(&b))
Expand Down Expand Up @@ -305,7 +305,7 @@ impl<S: kv::Store> State<S> {
}
}

impl<S: kv::Store> raft::State for State<S> {
impl<E: kv::Engine> raft::State for State<E> {
fn applied_index(&self) -> u64 {
self.applied_index
}
Expand Down
12 changes: 6 additions & 6 deletions src/storage/kv/bitcask.rs → src/storage/engine/bitcask.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::Store;
use super::Engine;
use crate::error::Result;

use fs4::FileExt;
use std::io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write};
use std::path::PathBuf;

/// A very simple variant of BitCask, itself a very simple log-structured
/// key-value store used e.g. by the Riak database. It is not compatible with
/// key-value engine used e.g. by the Riak database. It is not compatible with
/// BitCask databases generated by other implementations. See:
/// https://riak.com/assets/bitcask-intro.pdf
///
Expand Down Expand Up @@ -93,7 +93,7 @@ impl std::fmt::Display for BitCask {
}
}

impl Store for BitCask {
impl Engine for BitCask {
type ScanIterator<'a> = ScanIterator<'a>;

fn delete(&mut self, key: &[u8]) -> Result<()> {
Expand Down Expand Up @@ -380,14 +380,14 @@ impl Log {
mod tests {
use super::*;

const GOLDEN_DIR: &str = "src/storage/kv/golden/bitcask";
const GOLDEN_DIR: &str = "src/storage/engine/golden/bitcask";

super::super::tests::test_store!({
super::super::tests::test_engine!({
let path = tempdir::TempDir::new("toydb")?.path().join("toydb");
BitCask::new(path)?
});

/// Creates a new BitCask store for testing.
/// Creates a new BitCask engine for testing.
fn setup() -> Result<BitCask> {
BitCask::new(tempdir::TempDir::new("toydb")?.path().join("toydb"))
}
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/storage/kv/memory.rs → src/storage/engine/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Store;
use super::Engine;
use crate::error::Result;

/// An in-memory key/value store using the Rust standard library B-tree
/// An in-memory key/value storage engine using the Rust standard library B-tree
/// implementation. Data is not persisted.
pub struct Memory {
data: std::collections::BTreeMap<Vec<u8>, Vec<u8>>,
Expand All @@ -20,7 +20,7 @@ impl std::fmt::Display for Memory {
}
}

impl Store for Memory {
impl Engine for Memory {
type ScanIterator<'a> = ScanIterator<'a>;

fn flush(&mut self) -> Result<()> {
Expand Down Expand Up @@ -75,5 +75,5 @@ impl<'a> DoubleEndedIterator for ScanIterator<'a> {
mod tests {
use super::*;

super::super::tests::test_store!(Memory::new());
super::super::tests::test_engine!(Memory::new());
}
Loading

0 comments on commit 9dadfd1

Please sign in to comment.