From 4521c4406551fecd8f2e8543233e73e5061686e7 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Tue, 25 Jul 2023 17:05:39 +0530 Subject: [PATCH] fix: unwrap on root when tree is persisted --- src/tree.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/tree.rs b/src/tree.rs index 88b6df8..e14cda6 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -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];