Skip to content

Commit

Permalink
fix: unwrap on root when tree is persisted
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jul 25, 2023
1 parent 46a39a3 commit 4521c44
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,22 @@ where
// Load existing db instance
let db = D::load(db_config)?;

// Load root
let root = H::deserialize(db.get(Key(0, 0).into())?.unwrap());
// Load root
let root = match db.get(Key(0, 0).into())? {
Some(root) => H::deserialize(root),
None => H::default_leaf(),
};

// Load depth & next_index values from db
let depth = db.get(DEPTH_KEY)?.unwrap().try_into().unwrap();
let depth = usize::from_be_bytes(depth);

let next_index = db.get(NEXT_INDEX_KEY)?.unwrap().try_into().unwrap();
let next_index = usize::from_be_bytes(next_index);
let depth = match db.get(DEPTH_KEY)? {
Some(depth) => usize::from_be_bytes(depth.try_into().unwrap()),
None => 20
};

let next_index = match db.get(NEXT_INDEX_KEY)? {
Some(next_index) => usize::from_be_bytes(next_index.try_into().unwrap()),
None => 0,
};

// Load cache vec
let mut cache = vec![H::default_leaf(); depth + 1];
Expand Down

0 comments on commit 4521c44

Please sign in to comment.