Skip to content

Commit

Permalink
jmt(tree_cache): conservative guards for version mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Mar 13, 2024
1 parent 1a79909 commit 5b8dca5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/tree_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use hashbrown::{hash_map::Entry, HashMap, HashSet};
#[cfg(feature = "std")]
use std::collections::{hash_map::Entry, HashMap, HashSet};

use anyhow::{bail, Result};
use anyhow::{bail, ensure, Result};

Check warning on line 75 in src/tree_cache.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `ensure`

Check warning on line 75 in src/tree_cache.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `ensure`

use crate::{
node_type::{Node, NodeKey},
Expand Down Expand Up @@ -189,8 +189,23 @@ where
}

#[cfg(feature = "migration")]
/// Instantiate a [`TreeCache`] over the a [`TreeReader`] that is defined
/// against a root node key at version `current_version`.
///
/// # Usage
/// This method is used to perform incremental addition to a tree without
/// increasing the tree version's number.
pub fn new_overwrite(reader: &'a R, current_version: Version) -> Result<Self> {
let node_cache = HashMap::new();
let Some((node_key, _)) = reader.get_rightmost_leaf()? else {
bail!("creating an overwrite cache for an empty tree is not supported")
};

ensure!(
node_key.version() == current_version,
"the supplied version is not the latest version of the tree"
);

let root_node_key = NodeKey::new_empty_path(current_version);
Ok(Self {
node_cache,
Expand Down

0 comments on commit 5b8dca5

Please sign in to comment.