Avoids using API without column families

pull/171/head
Tpt 3 years ago
parent 5973de3a73
commit 0bd512a14c
  1. 14
      lib/src/storage/mod.rs
  2. 46
      lib/src/storage/rocksdb_backend.rs

@ -33,6 +33,7 @@ const DSPO_CF: &str = "dspo";
const DPOS_CF: &str = "dpos"; const DPOS_CF: &str = "dpos";
const DOSP_CF: &str = "dosp"; const DOSP_CF: &str = "dosp";
const GRAPHS_CF: &str = "graphs"; const GRAPHS_CF: &str = "graphs";
const DEFAULT_CF: &str = "default";
const COLUMN_FAMILIES: [&str; 11] = [ const COLUMN_FAMILIES: [&str; 11] = [
ID2STR_CF, SPOG_CF, POSG_CF, OSPG_CF, GSPO_CF, GPOS_CF, GOSP_CF, DSPO_CF, DPOS_CF, DOSP_CF, ID2STR_CF, SPOG_CF, POSG_CF, OSPG_CF, GSPO_CF, GPOS_CF, GOSP_CF, DSPO_CF, DPOS_CF, DOSP_CF,
@ -42,7 +43,8 @@ const COLUMN_FAMILIES: [&str; 11] = [
/// Low level storage primitives /// Low level storage primitives
#[derive(Clone)] #[derive(Clone)]
pub struct Storage { pub struct Storage {
default: Db, db: Db,
default: Tree,
id2str: Tree, id2str: Tree,
spog: Tree, spog: Tree,
posg: Tree, posg: Tree,
@ -68,6 +70,7 @@ impl Storage {
fn setup(db: Db) -> std::io::Result<Self> { fn setup(db: Db) -> std::io::Result<Self> {
let this = Self { let this = Self {
default: db.open_tree(DEFAULT_CF)?,
id2str: db.open_tree(ID2STR_CF)?, id2str: db.open_tree(ID2STR_CF)?,
spog: db.open_tree(SPOG_CF)?, spog: db.open_tree(SPOG_CF)?,
posg: db.open_tree(POSG_CF)?, posg: db.open_tree(POSG_CF)?,
@ -79,7 +82,7 @@ impl Storage {
dpos: db.open_tree(DPOS_CF)?, dpos: db.open_tree(DPOS_CF)?,
dosp: db.open_tree(DOSP_CF)?, dosp: db.open_tree(DOSP_CF)?,
graphs: db.open_tree(GRAPHS_CF)?, graphs: db.open_tree(GRAPHS_CF)?,
default: db, db,
}; };
let mut version = this.ensure_version()?; let mut version = this.ensure_version()?;
@ -93,7 +96,7 @@ impl Storage {
} }
version = 1; version = 1;
this.set_version(version)?; this.set_version(version)?;
this.default.flush()?; this.db.flush()?;
} }
if version == 1 { if version == 1 {
// We migrate to v2 // We migrate to v2
@ -108,7 +111,7 @@ impl Storage {
iter.status()?; iter.status()?;
version = 2; version = 2;
this.set_version(version)?; this.set_version(version)?;
this.default.flush()?; this.db.flush()?;
} }
match version { match version {
@ -652,8 +655,7 @@ impl Storage {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub fn flush(&self) -> std::io::Result<()> { pub fn flush(&self) -> std::io::Result<()> {
self.default.flush()?; self.db.flush()
Ok(())
} }
pub fn get_str(&self, key: &StrHash) -> std::io::Result<Option<String>> { pub fn get_str(&self, key: &StrHash) -> std::io::Result<Option<String>> {

@ -196,52 +196,6 @@ impl Db {
r r
} }
} }
pub fn get(&self, key: &[u8]) -> Result<Option<PinnableSlice<'_>>> {
unsafe {
let options = rocksdb_readoptions_create();
assert!(
!options.is_null(),
"rocksdb_readoptions_create returned null"
);
let r = ffi_result!(rocksdb_get_pinned(
self.0.db,
options,
key.as_ptr() as *const c_char,
key.len()
));
rocksdb_readoptions_destroy(options);
let slice = r?;
Ok(if slice.is_null() {
None
} else {
Some(PinnableSlice {
slice,
lifetime: PhantomData::default(),
})
})
}
}
pub fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
unsafe {
let options = rocksdb_writeoptions_create();
assert!(
!options.is_null(),
"rocksdb_writeoptions_create returned null"
);
let r = ffi_result!(rocksdb_put(
self.0.db,
options,
key.as_ptr() as *const c_char,
key.len(),
value.as_ptr() as *const c_char,
value.len(),
));
rocksdb_writeoptions_destroy(options);
r
}
}
} }
#[derive(Clone)] #[derive(Clone)]

Loading…
Cancel
Save