-
Notifications
You must be signed in to change notification settings - Fork 70
new trie #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
new trie #210
Changes from 40 commits
91681c8
6f097f6
d9ac5aa
00b4fa9
719aa79
bd9e3d1
1cf9d43
bac0cbc
ada1da6
5a9c3b4
0ff5025
d00240e
81446ab
599b0f1
314179d
269ef62
675ae01
87d60c8
e031dbf
7faf394
557b52d
ed42db6
8d334d2
a244144
f5bc93c
caf67f3
de3bc22
8edcc92
1f9b8ed
270aa81
d858765
b5076f8
39e81cb
04e7a18
49bc5a7
956ed99
37a0555
ac8a09f
bc76a21
5df1cd8
aeec4fa
f0d3662
e83c8f0
367c0e7
c7547c6
1a969ad
6cc8363
28a5476
b6e4393
93433ca
16f1cd5
04a1ee4
5e26628
554187b
10cc948
024e964
d176e42
7a575ef
fca9577
26e462d
8801370
bb561a0
e911ebc
bef6e0f
04ac7a0
e4b29e4
1b8cced
8ad4ee8
d352340
8f33c2c
8e2852f
71bcaf8
af1b48c
61c6261
2699e69
e00e121
96867d4
f46295a
52c9f52
8a63867
95c5917
3d6b21f
3698096
f240f4a
95b443b
62cef48
a6916c0
2f13772
4bb8717
51e97f4
ecc52d3
ed7c5f0
2c06f2e
7533461
620d676
6235f9c
a6a8fa6
4755c22
cf5a6e6
8543ca5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,11 @@ | |
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
#[cfg(not(feature = "std"))] | ||
extern crate alloc; | ||
|
||
#[cfg(not(feature = "std"))] | ||
use alloc::vec::Vec; | ||
#[cfg(not(feature = "std"))] | ||
use core::hash; | ||
#[cfg(feature = "std")] | ||
|
@@ -73,144 +78,19 @@ pub trait Hasher: Sync + Send { | |
fn hash(x: &[u8]) -> Self::Out; | ||
} | ||
|
||
/// Trait modelling a plain datastore whose key is a fixed type. | ||
/// The caller should ensure that a key only corresponds to | ||
/// one value. | ||
pub trait PlainDB<K, V>: Send + Sync + AsPlainDB<K, V> { | ||
/// Look up a given hash into the bytes that hash to it, returning None if the | ||
/// hash is not known. | ||
fn get(&self, key: &K) -> Option<V>; | ||
|
||
/// Check for the existence of a hash-key. | ||
fn contains(&self, key: &K) -> bool; | ||
|
||
/// Insert a datum item into the DB. Insertions are counted and the equivalent | ||
/// number of `remove()`s must be performed before the data is considered dead. | ||
/// The caller should ensure that a key only corresponds to one value. | ||
fn emplace(&mut self, key: K, value: V); | ||
|
||
/// Remove a datum previously inserted. Insertions can be "owed" such that the | ||
/// same number of `insert()`s may happen without the data being eventually | ||
/// being inserted into the DB. It can be "owed" more than once. | ||
/// The caller should ensure that a key only corresponds to one value. | ||
fn remove(&mut self, key: &K); | ||
} | ||
|
||
/// Trait for immutable reference of PlainDB. | ||
pub trait PlainDBRef<K, V> { | ||
/// Look up a given hash into the bytes that hash to it, returning None if the | ||
/// hash is not known. | ||
fn get(&self, key: &K) -> Option<V>; | ||
|
||
/// Check for the existance of a hash-key. | ||
fn contains(&self, key: &K) -> bool; | ||
} | ||
|
||
impl<'a, K, V> PlainDBRef<K, V> for &'a dyn PlainDB<K, V> { | ||
fn get(&self, key: &K) -> Option<V> { | ||
PlainDB::get(*self, key) | ||
} | ||
fn contains(&self, key: &K) -> bool { | ||
PlainDB::contains(*self, key) | ||
} | ||
} | ||
/// Trait modelling datastore keyed by a hash defined by the `Hasher` and optional location tag. | ||
pub trait HashDB<H: Hasher, T, L>: Send + Sync { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be a good idea to rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also change traits to return errors ? I mean if we return error I would go for NodeDB, if not NodeStore. (also wondering about renaming crate here) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Probably, but in a separate PR. |
||
/// Look up a trie node by hash and location. | ||
/// Returns the node bytes and the list of children node locations if any. | ||
fn get(&self, key: &H::Out, prefix: Prefix, location: L) -> Option<(T, Vec<L>)>; | ||
|
||
impl<'a, K, V> PlainDBRef<K, V> for &'a mut dyn PlainDB<K, V> { | ||
fn get(&self, key: &K) -> Option<V> { | ||
PlainDB::get(*self, key) | ||
/// Check for the existence of a hash-key at the location. | ||
fn contains(&self, key: &H::Out, prefix: Prefix, location: L) -> bool { | ||
self.get(key, prefix, location).is_some() | ||
} | ||
fn contains(&self, key: &K) -> bool { | ||
PlainDB::contains(*self, key) | ||
} | ||
} | ||
|
||
/// Trait modelling datastore keyed by a hash defined by the `Hasher`. | ||
pub trait HashDB<H: Hasher, T>: Send + Sync + AsHashDB<H, T> { | ||
/// Look up a given hash into the bytes that hash to it, returning None if the | ||
/// hash is not known. | ||
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>; | ||
|
||
/// Check for the existence of a hash-key. | ||
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool; | ||
|
||
/// Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions | ||
/// are counted and the equivalent number of `remove()`s must be performed before the data | ||
/// is considered dead. | ||
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H::Out; | ||
|
||
/// Like `insert()`, except you provide the key and the data is all moved. | ||
fn emplace(&mut self, key: H::Out, prefix: Prefix, value: T); | ||
|
||
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of | ||
/// `insert()`s may happen without the data being eventually being inserted into the DB. | ||
/// It can be "owed" more than once. | ||
fn remove(&mut self, key: &H::Out, prefix: Prefix); | ||
} | ||
|
||
/// Trait for immutable reference of HashDB. | ||
pub trait HashDBRef<H: Hasher, T> { | ||
/// Look up a given hash into the bytes that hash to it, returning None if the | ||
/// hash is not known. | ||
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>; | ||
|
||
/// Check for the existance of a hash-key. | ||
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool; | ||
} | ||
|
||
impl<'a, H: Hasher, T> HashDBRef<H, T> for &'a dyn HashDB<H, T> { | ||
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T> { | ||
HashDB::get(*self, key, prefix) | ||
} | ||
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool { | ||
HashDB::contains(*self, key, prefix) | ||
} | ||
} | ||
|
||
impl<'a, H: Hasher, T> HashDBRef<H, T> for &'a mut dyn HashDB<H, T> { | ||
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T> { | ||
HashDB::get(*self, key, prefix) | ||
} | ||
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool { | ||
HashDB::contains(*self, key, prefix) | ||
} | ||
} | ||
|
||
/// Upcast trait for HashDB. | ||
pub trait AsHashDB<H: Hasher, T> { | ||
/// Perform upcast to HashDB for anything that derives from HashDB. | ||
fn as_hash_db(&self) -> &dyn HashDB<H, T>; | ||
/// Perform mutable upcast to HashDB for anything that derives from HashDB. | ||
fn as_hash_db_mut<'a>(&'a mut self) -> &'a mut (dyn HashDB<H, T> + 'a); | ||
} | ||
|
||
/// Upcast trait for PlainDB. | ||
pub trait AsPlainDB<K, V> { | ||
/// Perform upcast to PlainDB for anything that derives from PlainDB. | ||
fn as_plain_db(&self) -> &dyn PlainDB<K, V>; | ||
/// Perform mutable upcast to PlainDB for anything that derives from PlainDB. | ||
fn as_plain_db_mut<'a>(&'a mut self) -> &'a mut (dyn PlainDB<K, V> + 'a); | ||
} | ||
|
||
// NOTE: There used to be a `impl<T> AsHashDB for T` but that does not work with generics. | ||
// See https://stackoverflow.com/questions/48432842/ | ||
// implementing-a-trait-for-reference-and-non-reference-types-causes-conflicting-im | ||
// This means we need concrete impls of AsHashDB in several places, which somewhat defeats | ||
// the point of the trait. | ||
impl<'a, H: Hasher, T> AsHashDB<H, T> for &'a mut dyn HashDB<H, T> { | ||
fn as_hash_db(&self) -> &dyn HashDB<H, T> { | ||
&**self | ||
} | ||
fn as_hash_db_mut<'b>(&'b mut self) -> &'b mut (dyn HashDB<H, T> + 'b) { | ||
&mut **self | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl<'a, K, V> AsPlainDB<K, V> for &'a mut dyn PlainDB<K, V> { | ||
fn as_plain_db(&self) -> &dyn PlainDB<K, V> { | ||
&**self | ||
} | ||
fn as_plain_db_mut<'b>(&'b mut self) -> &'b mut (dyn PlainDB<K, V> + 'b) { | ||
&mut **self | ||
/// Compute value hash. | ||
fn hash(&self, value: &[u8]) -> H::Out { | ||
H::hash(value) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.