Explorar o código

sdk/monotree/tree: replaced SledDb struct with SledOverlayDb so we can open multiple instances for different trees over a single sled-overlay pointer

skoupidi hai 1 ano
pai
achega
37efe70543
Modificáronse 2 ficheiros con 19 adicións e 14 borrados
  1. 1 1
      src/sdk/src/monotree/mod.rs
  2. 18 13
      src/sdk/src/monotree/tree.rs

+ 1 - 1
src/sdk/src/monotree/mod.rs

@@ -41,7 +41,7 @@ pub mod bits;
 pub mod node;
 pub mod node;
 
 
 pub mod tree;
 pub mod tree;
-pub use tree::{MemoryDb, Monotree, SledDb};
+pub use tree::{MemoryDb, Monotree, SledOverlayDb};
 
 
 pub mod utils;
 pub mod utils;
 
 

+ 18 - 13
src/sdk/src/monotree/tree.rs

@@ -17,8 +17,10 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
  */
 
 
+use std::sync::{Arc, Mutex};
+
 use hashbrown::{HashMap, HashSet};
 use hashbrown::{HashMap, HashSet};
-use sled_overlay::SledTreeOverlay;
+use sled_overlay::SledDbOverlay;
 
 
 use super::{
 use super::{
     bits::Bits,
     bits::Bits,
@@ -149,24 +151,27 @@ impl MonotreeStorageAdapter for MemoryDb {
 }
 }
 
 
 /// sled-overlay-based storage for Monotree
 /// 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: MemCache,
     batch_on: bool,
     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<()> {
     fn put(&mut self, key: &Hash, value: Vec<u8>) -> GenericResult<()> {
         if self.batch_on {
         if self.batch_on {
             self.batch.put(key, value);
             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()))
             return Err(ContractError::IoError(e.to_string()))
         }
         }
 
 
@@ -178,7 +183,7 @@ impl MonotreeStorageAdapter for SledDb {
             return Ok(self.batch.get(key));
             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(Some(v)) => Ok(Some(v.to_vec())),
             Ok(None) => Ok(None),
             Ok(None) => Ok(None),
             Err(e) => Err(ContractError::IoError(e.to_string())),
             Err(e) => Err(ContractError::IoError(e.to_string())),
@@ -188,7 +193,7 @@ impl MonotreeStorageAdapter for SledDb {
     fn del(&mut self, key: &Hash) -> GenericResult<()> {
     fn del(&mut self, key: &Hash) -> GenericResult<()> {
         if self.batch_on {
         if self.batch_on {
             self.batch.del(key);
             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()));
             return Err(ContractError::IoError(e.to_string()));
         }
         }
 
 
@@ -207,12 +212,12 @@ impl MonotreeStorageAdapter for SledDb {
     fn finish_batch(&mut self) -> GenericResult<()> {
     fn finish_batch(&mut self) -> GenericResult<()> {
         if self.batch_on {
         if self.batch_on {
             for (key, value) in self.batch.map.drain() {
             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()))
                     return Err(ContractError::IoError(e.to_string()))
                 }
                 }
             }
             }
             for key in self.batch.set.drain() {
             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()))
                     return Err(ContractError::IoError(e.to_string()))
                 }
                 }
             }
             }