pub fn load_ancestors<DB: Database + ?Sized>(
        &mut self,
        sorted_keys: &SortedKeys,
        db: &DB,
    ) -> LoadAncestorsResult {
        let Some(Node::Internal(_)) = self.get(&Nibbles::EMPTY) else {
            return LoadAncestorsResult {
                longest_prefixes: vec![Nibbles::EMPTY; sorted_keys.0.len()],
                db_reads: 0,
            };
        };

        // Longest prefix for each key in `key_value_pairs` (i.e., what we'll return from
        // this method). `None` indicates that the longest prefix for a key is not determined yet.
        let mut longest_prefixes = vec![None; sorted_keys.0.len()];
        // Previous encountered when iterating by `sorted_keys` below.
        let mut prev_nibbles = None;
        // Cumulative number of db reads.
        let mut db_reads = 0;
        for nibble_count in 1.. {
            // Extract `nibble_count` nibbles from each key for which we haven't found the parent
            // yet. Note that nibbles in `requested_keys` are sorted.
            let requested_keys = sorted_keys.0.iter().filter_map(|(idx, key)| {
                if longest_prefixes[*idx].is_some() {
                    return None;
                }
                if nibble_count > 2 * KEY_SIZE {
                    // We have traversed to the final tree level. There's nothing to load;
                    // we just need to record the longest prefix as the full key.
                    longest_prefixes[*idx] = Some(Nibbles::new(key, 2 * KEY_SIZE));
                    return None;
                }

                let nibbles = Nibbles::new(key, nibble_count);
                let (this_parent_nibbles, last_nibble) = nibbles.split_last().unwrap();
                // ^ `unwrap()` is safe by construction; `nibble_count` is positive
                let this_ref = self.child_ref(&this_parent_nibbles, last_nibble);
                let Some(this_ref) = this_ref else {
                    longest_prefixes[*idx] = Some(this_parent_nibbles);
                    return None;
                };

                // Deduplicate by `nibbles`. We do it at the end to properly
                // assign `parent_nibbles` for all keys, and before the version is updated
                // for `ChildRef`s, in order to update it only once.
                if prev_nibbles == Some(nibbles) {
                    return None;
                }
                prev_nibbles = Some(nibbles);

                Some((nibbles.with_version(this_ref.version), this_ref.is_leaf))
            });
            let requested_keys: Vec<_> = requested_keys.collect();

            if requested_keys.is_empty() {
                break;
            }
            let new_nodes = db.tree_nodes(&requested_keys);
            db_reads += new_nodes.len() as u64;

            // Since we load nodes level by level, we can update `patch_set` more efficiently
            // by pushing entire `HashMap`s into `changes_by_nibble_count`.
            let level = requested_keys
                .iter()
                .zip(new_nodes)
                .map(|((key, _), node)| {
                    (key, node.unwrap())
                    // ^ `unwrap()` is safe: all requested nodes are referenced by their parents
                });
            self.push_level_from_db(level);
        }

        // All parents must be set at this point.
        let longest_prefixes = longest_prefixes.into_iter().map(Option::unwrap).collect();

        LoadAncestorsResult {
            longest_prefixes,
            db_reads,
        }
    }
Loads ancestor nodes for all keys in `sorted_keys`.

This method works by traversing the tree level by level. It uses [`Database::tree_nodes()`]
(translating to multi-get in RocksDB) for each level to expedite node loading.
core/lib/merkle_tree/src/storage/patch.rs