-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c7f710
commit 9a1718f
Showing
6 changed files
with
74 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use core::cell::RefCell; | ||
|
||
use alloc::collections::BTreeMap; | ||
|
||
use crate::{Branch, Leaf}; | ||
|
||
use super::{DatabaseGet, DatabaseSet, Node, NodeHash}; | ||
|
||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] | ||
pub struct MemoryDb<V> { | ||
leaves: RefCell<BTreeMap<NodeHash, Node<Branch<NodeHash>, Leaf<V>>>>, | ||
} | ||
|
||
impl<V> MemoryDb<V> { | ||
pub fn empty() -> Self { | ||
Self { | ||
leaves: RefCell::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<V: Clone> DatabaseGet<V> for MemoryDb<V> { | ||
type GetError = String; | ||
|
||
fn get(&self, hash: &NodeHash) -> Result<Node<Branch<NodeHash>, Leaf<V>>, Self::GetError> { | ||
self.leaves | ||
.borrow() | ||
.get(hash) | ||
.cloned() | ||
.ok_or_else(|| format!("Hash: `{}` not found", hash)) | ||
} | ||
} | ||
|
||
impl<V: Clone> DatabaseSet<V> for MemoryDb<V> { | ||
type SetError = String; | ||
|
||
fn set( | ||
&self, | ||
hash: NodeHash, | ||
node: Node<Branch<NodeHash>, Leaf<V>>, | ||
) -> Result<(), Self::SetError> { | ||
self.leaves.borrow_mut().insert(hash, node); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters