Cleaner storage backend architecture

pull/171/head
Tpt 3 years ago
parent 73cd761229
commit 00ad6e634f
  1. 26
      lib/src/storage/backend/fallback.rs
  2. 32
      lib/src/storage/backend/mod.rs
  3. 33
      lib/src/storage/backend/rocksdb.rs
  4. 13
      lib/src/storage/mod.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<MergeOperator>,
pub compaction_filter: Option<CompactionFilter>,
pub use_iter: bool,
pub min_prefix_size: usize,
}
#[derive(Clone)]
pub struct Db(Arc<RwLock<BTreeMap<ColumnFamily, Tree>>>);
@ -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<u8>),
}

@ -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<MergeOperator>,
pub compaction_filter: Option<CompactionFilter>,
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,
}

@ -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<MergeOperator>,
pub compaction_filter: Option<CompactionFilter>,
pub use_iter: bool,
pub min_prefix_size: usize,
}
#[derive(Clone)]
pub struct Db(Arc<DbHandler>);
@ -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<u8>),
}
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
}
}
}

@ -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";

Loading…
Cancel
Save