diff --git a/src/triefile/mod.rs b/src/triefile/mod.rs index 18fa433..c5d7c91 100644 --- a/src/triefile/mod.rs +++ b/src/triefile/mod.rs @@ -107,7 +107,7 @@ impl TrieFile { } } - pub async fn set(&mut self, key:&[u8], data:&[u8]) -> Result<(),std::io::Error> + pub async fn set(&mut self, key:&[u8], data:&[u8]) -> Result { let mut node = Node::::new(); @@ -115,11 +115,13 @@ impl TrieFile { let mut key_index = 0; let mut parent_index = u32::MAX; + let mut result = 0; + // Allocate first chain of nodes if none exist. if self.block_count().await? == 0 { //println!("originate"); - self.trailing_nodes(key, &mut key_index, data, u32::MAX).await?; - return Ok(()); + result = self.trailing_nodes(key, &mut key_index, data, u32::MAX).await? as usize; + return Ok(result); } else { //println!("traverse"); @@ -158,6 +160,7 @@ impl TrieFile { } self.write_node(node_index, &node).await?; + result = node_index as usize; } // Continue to child node. @@ -173,6 +176,7 @@ impl TrieFile { //println!(" - new child"); node.child = self.trailing_nodes(key, &mut key_index, data, node_index).await?; + result = node.child as usize; self.write_node(node_index, &node).await?; } } @@ -213,6 +217,7 @@ impl TrieFile { // Write data to trailing nodes. if key_index < key.len() { let new_branch = self.trailing_nodes(key, &mut key_index, data, node_index).await?; + result = new_branch as usize; child_node.next = new_branch; } @@ -222,6 +227,7 @@ impl TrieFile { for i in 0..Z.min(data.len()) { node.data[i] = data[i]; } + result = node_index as usize; } self.write_node(node_index, &node).await?; @@ -242,13 +248,14 @@ impl TrieFile { // Allocate and initialize subsequent nodes until key is resolved. node.next = self.trailing_nodes(key, &mut key_index, data, parent_index).await?; + result = node.next as usize; self.write_node(node_index, &node).await?; } } } } - Ok(()) + Ok(result) } pub async fn find(&self, key:&[u8]) -> Result, std::io::Error>