mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 14:48:08 -05:00
sdk/monotree/tree: replaced SledDb struct with SledOverlayDb so we can open multiple instances for different trees over a single sled-overlay pointer
This commit is contained in:
@@ -41,7 +41,7 @@ pub mod bits;
|
||||
pub mod node;
|
||||
|
||||
pub mod tree;
|
||||
pub use tree::{MemoryDb, Monotree, SledDb};
|
||||
pub use tree::{MemoryDb, Monotree, SledOverlayDb};
|
||||
|
||||
pub mod utils;
|
||||
|
||||
|
||||
@@ -17,8 +17,10 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use sled_overlay::SledTreeOverlay;
|
||||
use sled_overlay::SledDbOverlay;
|
||||
|
||||
use super::{
|
||||
bits::Bits,
|
||||
@@ -149,24 +151,27 @@ impl MonotreeStorageAdapter for MemoryDb {
|
||||
}
|
||||
|
||||
/// sled-overlay-based storage for Monotree
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SledDb {
|
||||
db: SledTreeOverlay,
|
||||
#[derive(Clone)]
|
||||
pub struct SledOverlayDb {
|
||||
overlay: Arc<Mutex<SledDbOverlay>>,
|
||||
tree: [u8; 32],
|
||||
batch: MemCache,
|
||||
batch_on: bool,
|
||||
}
|
||||
|
||||
impl SledDb {
|
||||
pub fn new(db: SledTreeOverlay) -> Self {
|
||||
Self { db, batch: MemCache::new(), batch_on: false }
|
||||
impl SledOverlayDb {
|
||||
pub fn new(overlay: &Arc<Mutex<SledDbOverlay>>, tree: &[u8; 32]) -> Self {
|
||||
Self { overlay: overlay.clone(), tree: *tree, batch: MemCache::new(), batch_on: false }
|
||||
}
|
||||
}
|
||||
|
||||
impl MonotreeStorageAdapter for SledDb {
|
||||
impl MonotreeStorageAdapter for SledOverlayDb {
|
||||
fn put(&mut self, key: &Hash, value: Vec<u8>) -> GenericResult<()> {
|
||||
if self.batch_on {
|
||||
self.batch.put(key, value);
|
||||
} else if let Err(e) = self.db.insert(&slice_to_hash(key), &value) {
|
||||
} else if let Err(e) =
|
||||
self.overlay.lock().unwrap().insert(&self.tree, &slice_to_hash(key), &value)
|
||||
{
|
||||
return Err(ContractError::IoError(e.to_string()))
|
||||
}
|
||||
|
||||
@@ -178,7 +183,7 @@ impl MonotreeStorageAdapter for SledDb {
|
||||
return Ok(self.batch.get(key));
|
||||
}
|
||||
|
||||
match self.db.get(key) {
|
||||
match self.overlay.lock().unwrap().get(&self.tree, key) {
|
||||
Ok(Some(v)) => Ok(Some(v.to_vec())),
|
||||
Ok(None) => Ok(None),
|
||||
Err(e) => Err(ContractError::IoError(e.to_string())),
|
||||
@@ -188,7 +193,7 @@ impl MonotreeStorageAdapter for SledDb {
|
||||
fn del(&mut self, key: &Hash) -> GenericResult<()> {
|
||||
if self.batch_on {
|
||||
self.batch.del(key);
|
||||
} else if let Err(e) = self.db.remove(key) {
|
||||
} else if let Err(e) = self.overlay.lock().unwrap().remove(&self.tree, key) {
|
||||
return Err(ContractError::IoError(e.to_string()));
|
||||
}
|
||||
|
||||
@@ -207,12 +212,12 @@ impl MonotreeStorageAdapter for SledDb {
|
||||
fn finish_batch(&mut self) -> GenericResult<()> {
|
||||
if self.batch_on {
|
||||
for (key, value) in self.batch.map.drain() {
|
||||
if let Err(e) = self.db.insert(&key, &value) {
|
||||
if let Err(e) = self.overlay.lock().unwrap().insert(&self.tree, &key, &value) {
|
||||
return Err(ContractError::IoError(e.to_string()))
|
||||
}
|
||||
}
|
||||
for key in self.batch.set.drain() {
|
||||
if let Err(e) = self.db.remove(&key) {
|
||||
if let Err(e) = self.overlay.lock().unwrap().remove(&self.tree, &key) {
|
||||
return Err(ContractError::IoError(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user