Trait sp_trie::TrieCache

source ·
pub trait TrieCache<NC>
where NC: NodeCodec,
{ // Required methods fn lookup_value_for_key( &mut self, key: &[u8], ) -> Option<&CachedValue<<NC as NodeCodec>::HashOut>>; fn cache_value_for_key( &mut self, key: &[u8], value: CachedValue<<NC as NodeCodec>::HashOut>, ); fn get_or_insert_node( &mut self, hash: <NC as NodeCodec>::HashOut, fetch_node: &mut dyn FnMut() -> Result<NodeOwned<<NC as NodeCodec>::HashOut>, Box<TrieError<<NC as NodeCodec>::HashOut, <NC as NodeCodec>::Error>>>, ) -> Result<&NodeOwned<<NC as NodeCodec>::HashOut>, Box<TrieError<<NC as NodeCodec>::HashOut, <NC as NodeCodec>::Error>>>; fn get_node( &mut self, hash: &<NC as NodeCodec>::HashOut, ) -> Option<&NodeOwned<<NC as NodeCodec>::HashOut>>; }
Expand description

Various re-exports from the trie-db crate. 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§

source

fn lookup_value_for_key( &mut self, key: &[u8], ) -> Option<&CachedValue<<NC as NodeCodec>::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.

source

fn cache_value_for_key( &mut self, key: &[u8], value: CachedValue<<NC as NodeCodec>::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.

source

fn get_or_insert_node( &mut self, hash: <NC as NodeCodec>::HashOut, fetch_node: &mut dyn FnMut() -> Result<NodeOwned<<NC as NodeCodec>::HashOut>, Box<TrieError<<NC as NodeCodec>::HashOut, <NC as NodeCodec>::Error>>>, ) -> Result<&NodeOwned<<NC as NodeCodec>::HashOut>, Box<TrieError<<NC as NodeCodec>::HashOut, <NC as NodeCodec>::Error>>>

Get or insert a NodeOwned.

The cache implementation should look up based on the given hash if the node is already known. If the node is not yet known, the given fetch_node function can be used to fetch the particular node.

Returns the NodeOwned or an error that happened on fetching the node.

source

fn get_node( &mut self, hash: &<NC as NodeCodec>::HashOut, ) -> Option<&NodeOwned<<NC as NodeCodec>::HashOut>>

Get the NodeOwned that corresponds to the given hash.

Implementors§

source§

impl<'a, H: Hasher> TrieCache<NodeCodec<H>> for TrieCache<'a, H>