Return key index on trie set.

This commit is contained in:
yukirij 2025-04-18 16:58:44 -07:00
parent 2bb32e75b7
commit 16c4707d6c

View File

@ -107,7 +107,7 @@ impl<const Z:usize> TrieFile<Z> {
}
}
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<usize,std::io::Error>
{
let mut node = Node::<Z>::new();
@ -115,11 +115,13 @@ impl<const Z:usize> TrieFile<Z> {
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<const Z:usize> TrieFile<Z> {
}
self.write_node(node_index, &node).await?;
result = node_index as usize;
}
// Continue to child node.
@ -173,6 +176,7 @@ impl<const Z:usize> TrieFile<Z> {
//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<const Z:usize> TrieFile<Z> {
// 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<const Z:usize> TrieFile<Z> {
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<const Z:usize> TrieFile<Z> {
// 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<Option<usize>, std::io::Error>