pub trait TrieCache<NC: NodeCodec> {
// Required methods
fn lookup_value_for_key(
&mut self,
key: &[u8],
) -> Option<&CachedValue<NC::HashOut>>;
fn cache_value_for_key(
&mut self,
key: &[u8],
value: CachedValue<NC::HashOut>,
);
fn get_or_insert_node(
&mut self,
hash: NC::HashOut,
fetch_node: &mut dyn FnMut() -> Result<NodeOwned<NC::HashOut>, NC::HashOut, NC::Error>,
) -> Result<&NodeOwned<NC::HashOut>, NC::HashOut, NC::Error>;
fn get_node(
&mut self,
hash: &NC::HashOut,
) -> Option<&NodeOwned<NC::HashOut>>;
}
Expand description
A cache that can be used to speed-up certain operations when accessing the trie.
The TrieDB
/TrieDBMut
by default are working with the internal hash-db in a non-owning
mode. This means that for every lookup in the trie, every node is always fetched and decoded on
the fly. Fetching and decoding a node always takes some time and can kill the performance of any
application that is doing quite a lot of trie lookups. To circumvent this performance
degradation, a cache can be used when looking up something in the trie. Any cache that should be
used with the TrieDB
/TrieDBMut
needs to implement this trait.
The trait is laying out a two level cache, first the trie nodes cache and then the value cache.
The trie nodes cache, as the name indicates, is for caching trie nodes as NodeOwned
. These
trie nodes are referenced by their hash. The value cache is caching CachedValue
’s and these
are referenced by the key to look them up in the trie. As multiple different tries can have
different values under the same key, it up to the cache implementation to ensure that the
correct value is returned. As each trie has a different root, this root can be used to
differentiate values under the same key.
Required Methods§
sourcefn lookup_value_for_key(
&mut self,
key: &[u8],
) -> Option<&CachedValue<NC::HashOut>>
fn lookup_value_for_key( &mut self, key: &[u8], ) -> Option<&CachedValue<NC::HashOut>>
Lookup value for the given key
.
Returns the None
if the key
is unknown or otherwise Some(_)
with the associated
value.
[Self::cache_data_for_key
] is used to make the cache aware of data that is associated
to a key
.
§Attention
The cache can be used for different tries, aka with different roots. This means that the cache implementation needs to take care of always returning the correct value for the current trie root.
sourcefn cache_value_for_key(&mut self, key: &[u8], value: CachedValue<NC::HashOut>)
fn cache_value_for_key(&mut self, key: &[u8], value: CachedValue<NC::HashOut>)
Cache the given value
for the given key
.
§Attention
The cache can be used for different tries, aka with different roots. This means
that the cache implementation needs to take care of caching value
for the current
trie root.