oasis_core_runtime/storage/mkvs/tree/
prefetch.rs

1use anyhow::Result;
2
3use crate::storage::mkvs::{
4    cache::{Cache, ReadSyncFetcher},
5    sync::{GetPrefixesRequest, Proof, ReadSync, TreeID},
6    tree::{NodePtrRef, Root, Tree},
7    Prefix,
8};
9
10pub(super) struct FetcherSyncGetPrefixes<'a> {
11    prefixes: &'a [Prefix],
12    limit: u16,
13}
14
15impl<'a> FetcherSyncGetPrefixes<'a> {
16    pub(super) fn new(prefixes: &'a [Prefix], limit: u16) -> Self {
17        Self { prefixes, limit }
18    }
19}
20
21impl<'a> ReadSyncFetcher for FetcherSyncGetPrefixes<'a> {
22    fn fetch(&self, root: Root, ptr: NodePtrRef, rs: &mut Box<dyn ReadSync>) -> Result<Proof> {
23        let rsp = rs.sync_get_prefixes(GetPrefixesRequest {
24            tree: TreeID {
25                root,
26                position: ptr.borrow().hash,
27            },
28            prefixes: self.prefixes.to_vec(),
29            limit: self.limit,
30        })?;
31        Ok(rsp.proof)
32    }
33}
34
35impl Tree {
36    /// Populate the in-memory tree with nodes for keys starting with given prefixes.
37    pub fn prefetch_prefixes(&self, prefixes: &[Prefix], limit: u16) -> Result<()> {
38        let pending_root = self.cache.borrow().get_pending_root();
39        self.cache
40            .borrow_mut()
41            .remote_sync(pending_root, FetcherSyncGetPrefixes::new(prefixes, limit))
42    }
43}