From 4231cf218de3d3d6f5f6aed3b952e3aa6b6ff4c9 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Sun, 14 Jul 2024 10:40:11 -0700 Subject: [PATCH] make a submodule for rocksdbstorage --- src/core/src/index/revindex/disk_revindex.rs | 2 +- src/core/src/{storage.rs => storage/mod.rs} | 54 +------------------ src/core/src/storage/rocksdb.rs | 57 ++++++++++++++++++++ 3 files changed, 60 insertions(+), 53 deletions(-) rename src/core/src/{storage.rs => storage/mod.rs} (91%) create mode 100644 src/core/src/storage/rocksdb.rs diff --git a/src/core/src/index/revindex/disk_revindex.rs b/src/core/src/index/revindex/disk_revindex.rs index 2d02d3cfc7..21dc3b11c3 100644 --- a/src/core/src/index/revindex/disk_revindex.rs +++ b/src/core/src/index/revindex/disk_revindex.rs @@ -19,7 +19,7 @@ use crate::manifest::Manifest; use crate::prelude::*; use crate::sketch::minhash::{KmerMinHash, KmerMinHashBTree}; use crate::sketch::Sketch; -use crate::storage::{InnerStorage, Storage, STORAGE}; +use crate::storage::{rocksdb::STORAGE, InnerStorage, Storage}; use crate::Result; const DB_VERSION: u8 = 1; diff --git a/src/core/src/storage.rs b/src/core/src/storage/mod.rs similarity index 91% rename from src/core/src/storage.rs rename to src/core/src/storage/mod.rs index 70329ac2bf..6efefbee8b 100644 --- a/src/core/src/storage.rs +++ b/src/core/src/storage/mod.rs @@ -137,15 +137,10 @@ pub struct MemStorage { } #[cfg(feature = "branchwater")] -// Column family for using rocksdb as a Storage -pub(crate) const STORAGE: &str = "storage"; +pub mod rocksdb; #[cfg(feature = "branchwater")] -/// Store data in RocksDB -#[derive(Debug, Clone)] -pub struct RocksDBStorage { - db: Arc, -} +pub use rocksdb::RocksDBStorage; pub type Metadata<'a> = BTreeMap<&'a OsStr, &'a piz::read::FileMetadata<'a>>; @@ -662,48 +657,3 @@ impl Storage for MemStorage { } } -#[cfg(feature = "branchwater")] -impl RocksDBStorage { - pub fn from_path(path: &str) -> Self { - let mut opts = crate::index::revindex::RevIndex::db_options(); - opts.create_if_missing(true); - opts.create_missing_column_families(true); - opts.prepare_for_bulk_load(); - - // prepare column family descriptors - let cfs = crate::index::revindex::disk_revindex::cf_descriptors(); - - let db = - Arc::new(crate::index::revindex::DB::open_cf_descriptors(&opts, path, cfs).unwrap()); - - Self { db } - } - - pub fn from_db(db: Arc) -> Self { - Self { db: db.clone() } - } -} - -#[cfg(feature = "branchwater")] -impl Storage for RocksDBStorage { - fn save(&self, path: &str, content: &[u8]) -> Result { - let cf_storage = self.db.cf_handle(STORAGE).unwrap(); - // TODO(lirber): deal with conflict for path? - self.db.put_cf(&cf_storage, path.as_bytes(), &content[..])?; - Ok(path.into()) - } - - fn load(&self, path: &str) -> Result> { - let cf_storage = self.db.cf_handle(STORAGE).unwrap(); - let data = self.db.get_cf(&cf_storage, path.as_bytes())?; - data.ok_or_else(|| StorageError::DataReadError(path.into()).into()) - } - - fn args(&self) -> StorageArgs { - unimplemented!() - } - - fn spec(&self) -> String { - "rocksdb://".into() - } -} diff --git a/src/core/src/storage/rocksdb.rs b/src/core/src/storage/rocksdb.rs new file mode 100644 index 0000000000..4fc3a40234 --- /dev/null +++ b/src/core/src/storage/rocksdb.rs @@ -0,0 +1,57 @@ +use std::sync::Arc; + +use crate::storage::{Storage, StorageArgs, StorageError}; +use crate::{Error, Result}; + +// Column family for using rocksdb as a Storage +pub(crate) const STORAGE: &str = "storage"; + +/// Store data in RocksDB +#[derive(Debug, Clone)] +pub struct RocksDBStorage { + db: Arc, +} + +impl RocksDBStorage { + pub fn from_path(path: &str) -> Self { + let mut opts = crate::index::revindex::RevIndex::db_options(); + opts.create_if_missing(true); + opts.create_missing_column_families(true); + opts.prepare_for_bulk_load(); + + // prepare column family descriptors + let cfs = crate::index::revindex::disk_revindex::cf_descriptors(); + + let db = + Arc::new(crate::index::revindex::DB::open_cf_descriptors(&opts, path, cfs).unwrap()); + + Self { db } + } + + pub fn from_db(db: Arc) -> Self { + Self { db: db.clone() } + } +} + +impl Storage for RocksDBStorage { + fn save(&self, path: &str, content: &[u8]) -> Result { + let cf_storage = self.db.cf_handle(STORAGE).unwrap(); + // TODO(lirber): deal with conflict for path? + self.db.put_cf(&cf_storage, path.as_bytes(), &content[..])?; + Ok(path.into()) + } + + fn load(&self, path: &str) -> Result> { + let cf_storage = self.db.cf_handle(STORAGE).unwrap(); + let data = self.db.get_cf(&cf_storage, path.as_bytes())?; + data.ok_or_else(|| StorageError::DataReadError(path.into()).into()) + } + + fn args(&self) -> StorageArgs { + unimplemented!() + } + + fn spec(&self) -> String { + "rocksdb://".into() + } +}