From 00ad6e634fc877a4069a2ba3c7dd6bde9832e767 Mon Sep 17 00:00:00 2001 From: Tpt Date: Sat, 6 Nov 2021 09:28:35 +0100 Subject: [PATCH] Cleaner storage backend architecture --- .../fallback.rs} | 26 +-------------- lib/src/storage/backend/mod.rs | 32 ++++++++++++++++++ .../rocksdb.rs} | 33 +++---------------- lib/src/storage/mod.rs | 13 ++------ 4 files changed, 39 insertions(+), 65 deletions(-) rename lib/src/storage/{fallback_backend.rs => backend/fallback.rs} (90%) create mode 100644 lib/src/storage/backend/mod.rs rename lib/src/storage/{rocksdb_backend.rs => backend/rocksdb.rs} (96%) diff --git a/lib/src/storage/fallback_backend.rs b/lib/src/storage/backend/fallback.rs similarity index 90% rename from lib/src/storage/fallback_backend.rs rename to lib/src/storage/backend/fallback.rs index 7ae80030..9e005e50 100644 --- a/lib/src/storage/fallback_backend.rs +++ b/lib/src/storage/backend/fallback.rs @@ -1,5 +1,6 @@ //! TODO: This storage is dramatically naive. +use crate::storage::backend::{ColumnFamilyDefinition, CompactionAction, CompactionFilter}; use std::collections::btree_map::Entry; use std::collections::BTreeMap; use std::ffi::CString; @@ -7,14 +8,6 @@ use std::io::Result; use std::iter::{once, Once}; use std::sync::{Arc, RwLock}; -pub struct ColumnFamilyDefinition { - pub name: &'static str, - pub merge_operator: Option, - pub compaction_filter: Option, - pub use_iter: bool, - pub min_prefix_size: usize, -} - #[derive(Clone)] pub struct Db(Arc>>); @@ -95,7 +88,6 @@ impl Db { match action { CompactionAction::Keep => tree.tree.insert(key.into(), value.into()), CompactionAction::Remove => tree.tree.remove(key), - CompactionAction::Replace(value) => tree.tree.insert(key.into(), value), }; Ok(()) } @@ -152,9 +144,6 @@ impl Db { e.insert(value); } CompactionAction::Remove => (), - CompactionAction::Replace(value) => { - e.insert(value); - } } } Entry::Occupied(mut e) => { @@ -171,7 +160,6 @@ impl Db { match action { CompactionAction::Keep => e.insert(value), CompactionAction::Remove => e.remove(), - CompactionAction::Replace(value) => e.insert(value), }; } } @@ -254,15 +242,3 @@ pub struct MergeOperator { } pub type SlicesIterator<'a> = Once<&'a [u8]>; - -pub struct CompactionFilter { - pub filter: fn(&[u8], &[u8]) -> CompactionAction, - pub name: CString, -} - -#[allow(dead_code)] -pub enum CompactionAction { - Keep, - Remove, - Replace(Vec), -} diff --git a/lib/src/storage/backend/mod.rs b/lib/src/storage/backend/mod.rs new file mode 100644 index 00000000..7bacbbe6 --- /dev/null +++ b/lib/src/storage/backend/mod.rs @@ -0,0 +1,32 @@ +//! A storage backend +//! RocksDB is available, if not in memory + +#[cfg(target_arch = "wasm32")] +pub use fallback::{ColumnFamily, Db, Iter, MergeOperator}; +#[cfg(not(target_arch = "wasm32"))] +pub use rocksdb::{ColumnFamily, Db, Iter, MergeOperator}; +use std::ffi::CString; + +#[cfg(target_arch = "wasm32")] +mod fallback; +#[cfg(not(target_arch = "wasm32"))] +mod rocksdb; + +pub struct ColumnFamilyDefinition { + pub name: &'static str, + pub merge_operator: Option, + pub compaction_filter: Option, + pub use_iter: bool, + pub min_prefix_size: usize, +} + +pub struct CompactionFilter { + pub filter: fn(&[u8], &[u8]) -> CompactionAction, + pub name: CString, +} + +#[warn(dead_code)] +pub enum CompactionAction { + Keep, + Remove, +} diff --git a/lib/src/storage/rocksdb_backend.rs b/lib/src/storage/backend/rocksdb.rs similarity index 96% rename from lib/src/storage/rocksdb_backend.rs rename to lib/src/storage/backend/rocksdb.rs index 8728d337..68694c48 100644 --- a/lib/src/storage/rocksdb_backend.rs +++ b/lib/src/storage/backend/rocksdb.rs @@ -5,6 +5,7 @@ #![allow(unsafe_code)] use crate::error::invalid_input_error; +use crate::storage::backend::{ColumnFamilyDefinition, CompactionAction, CompactionFilter}; use libc::{self, c_char, c_int, c_uchar, c_void, size_t}; use oxrocksdb_sys::*; use std::borrow::Borrow; @@ -39,14 +40,6 @@ macro_rules! ffi_result_impl { }} } -pub struct ColumnFamilyDefinition { - pub name: &'static str, - pub merge_operator: Option, - pub compaction_filter: Option, - pub use_iter: bool, - pub min_prefix_size: usize, -} - #[derive(Clone)] pub struct Db(Arc); @@ -728,18 +721,6 @@ impl<'a> Iterator for SlicesIterator<'a> { } } -pub struct CompactionFilter { - pub filter: fn(&[u8], &[u8]) -> CompactionAction, - pub name: CString, -} - -#[allow(dead_code)] -pub enum CompactionAction { - Keep, - Remove, - Replace(Vec), -} - unsafe extern "C" fn compactionfilter_destructor(filter: *mut c_void) { Box::from_raw(filter as *mut CompactionFilter); } @@ -751,9 +732,9 @@ unsafe extern "C" fn compactionfilter_filter( key_length: size_t, existing_value: *const c_char, value_length: size_t, - new_value: *mut *mut c_char, - new_value_length: *mut size_t, - value_changed: *mut c_uchar, + _new_value: *mut *mut c_char, + _new_value_length: *mut size_t, + _value_changed: *mut c_uchar, ) -> c_uchar { let filter = &*(filter as *const CompactionFilter); match (filter.filter)( @@ -762,12 +743,6 @@ unsafe extern "C" fn compactionfilter_filter( ) { CompactionAction::Keep => 0, CompactionAction::Remove => 1, - CompactionAction::Replace(new_val) => { - *new_value_length = new_val.len(); - *value_changed = 1_u8; - *new_value = Box::into_raw(new_val.into_boxed_slice()) as *mut c_char; - 0 - } } } diff --git a/lib/src/storage/mod.rs b/lib/src/storage/mod.rs index eee99a8a..38258100 100644 --- a/lib/src/storage/mod.rs +++ b/lib/src/storage/mod.rs @@ -9,13 +9,7 @@ use crate::storage::binary_encoder::{ use crate::storage::numeric_encoder::{ insert_term, remove_term, EncodedQuad, EncodedTerm, StrHash, StrLookup, }; -#[cfg(target_arch = "wasm32")] -use fallback_backend::{ - ColumnFamily, ColumnFamilyDefinition, CompactionAction, CompactionFilter, Db, Iter, - MergeOperator, -}; -#[cfg(not(target_arch = "wasm32"))] -use rocksdb_backend::{ +use backend::{ ColumnFamily, ColumnFamilyDefinition, CompactionAction, CompactionFilter, Db, Iter, MergeOperator, }; @@ -24,13 +18,10 @@ use std::io::Result; #[cfg(not(target_arch = "wasm32"))] use std::path::Path; +mod backend; mod binary_encoder; -#[cfg(target_arch = "wasm32")] -mod fallback_backend; pub mod io; pub mod numeric_encoder; -#[cfg(not(target_arch = "wasm32"))] -mod rocksdb_backend; pub mod small_string; const ID2STR_CF: &str = "id2str";