Skip to content

Commit

Permalink
store: return raw bytes from Trie::retrieve_node (#7535)
Browse files Browse the repository at this point in the history
This will be needed in future commit where we will need access to the
raw bytes of the node.  See #7509.
  • Loading branch information
blasrodri authored Sep 6, 2022
1 parent 167a129 commit a34992e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/store/src/trie/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> TrieIterator<'a> {
///
/// The node is stored as the last [`Crumb`] in the trail.
fn descend_into_node(&mut self, hash: &CryptoHash) -> Result<(), StorageError> {
let node = self.trie.retrieve_node(hash)?;
let node = self.trie.retrieve_node(hash)?.1;
self.trail.push(Crumb { status: CrumbStatus::Entering, node });
Ok(())
}
Expand Down
17 changes: 13 additions & 4 deletions core/store/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl Trie {
}
let TrieNodeWithSize { node, memory_usage } = match handle {
NodeHandle::InMemory(h) => memory.node_ref(h).clone(),
NodeHandle::Hash(h) => self.retrieve_node(&h).expect("storage failure"),
NodeHandle::Hash(h) => self.retrieve_node(&h).expect("storage failure").1,
};

let mut memory_usage_naive = node.memory_usage_direct(memory);
Expand Down Expand Up @@ -667,10 +667,19 @@ impl Trie {
}
}

fn retrieve_node(&self, hash: &CryptoHash) -> Result<TrieNodeWithSize, StorageError> {
/// Retrieves decoded node alongside with its raw bytes representation.
///
/// Note that because Empty nodes (those which are referenced by
/// [`Self::EMPTY_ROOT`] hash) aren’t stored in the database, they don’t
/// have a bytes representation. For those nodes the first return value
/// will be `None`.
fn retrieve_node(
&self,
hash: &CryptoHash,
) -> Result<(Option<std::sync::Arc<[u8]>>, TrieNodeWithSize), StorageError> {
match self.retrieve_raw_node(hash)? {
None => Ok(TrieNodeWithSize::empty()),
Some((_bytes, node)) => Ok(TrieNodeWithSize::from_raw(node)),
None => Ok((None, TrieNodeWithSize::empty())),
Some((bytes, node)) => Ok((Some(bytes), TrieNodeWithSize::from_raw(node))),
}
}

Expand Down
16 changes: 8 additions & 8 deletions core/store/src/trie/state_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Trie {
if part_id == num_parts {
return Ok(vec![16]);
}
let root_node = self.retrieve_node(&self.root)?;
let root_node = self.retrieve_node(&self.root)?.1;
let total_size = root_node.memory_usage;
let size_start = (total_size + num_parts - 1) / num_parts * part_id;
self.find_path(&root_node, size_start)
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Trie {
}
TrieNode::Branch(children, _) => {
for child_index in 0..children.len() {
let child = match &children[child_index] {
let (_, child) = match &children[child_index] {
None => {
continue;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Trie {
Ok(false)
}
TrieNode::Extension(key, child_handle) => {
let child = match child_handle {
let (_, child) = match child_handle {
NodeHandle::InMemory(_) => unreachable!("only possible while mutating"),
NodeHandle::Hash(h) => self.retrieve_node(h)?,
};
Expand Down Expand Up @@ -312,7 +312,7 @@ mod tests {
return Ok(());
}
let mut stack: Vec<(CryptoHash, TrieNodeWithSize, CrumbStatus)> = Vec::new();
let root_node = self.retrieve_node(&self.root)?;
let root_node = self.retrieve_node(&self.root)?.1;
stack.push((self.root.clone(), root_node, CrumbStatus::Entering));
while let Some((hash, node, position)) = stack.pop() {
if let CrumbStatus::Entering = position {
Expand Down Expand Up @@ -353,7 +353,7 @@ mod tests {
}
if i < 16 {
if let Some(NodeHandle::Hash(h)) = children[i].clone() {
let child = self.retrieve_node(&h)?;
let (_, child) = self.retrieve_node(&h)?;
stack.push((hash, node, CrumbStatus::AtChild(i + 1)));
stack.push((h, child, CrumbStatus::Entering));
} else {
Expand All @@ -377,7 +377,7 @@ mod tests {
unreachable!("only possible while mutating")
}
NodeHandle::Hash(h) => {
let child = self.retrieve_node(&h)?;
let (_, child) = self.retrieve_node(&h)?;
stack.push((hash, node, CrumbStatus::Exiting));
stack.push((h, child, CrumbStatus::Entering));
}
Expand All @@ -394,7 +394,7 @@ mod tests {
size_start: u64,
size_end: u64,
) -> Result<(), StorageError> {
let root_node = self.retrieve_node(&self.root)?;
let root_node = self.retrieve_node(&self.root)?.1;
let path_begin = self.find_path(&root_node, size_start)?;
let path_end = self.find_path(&root_node, size_end)?;

Expand Down Expand Up @@ -424,7 +424,7 @@ mod tests {
part_id: PartId,
) -> Result<PartialState, StorageError> {
assert!(self.storage.as_caching_storage().is_some());
let root_node = self.retrieve_node(&self.root)?;
let root_node = self.retrieve_node(&self.root)?.1;
let total_size = root_node.memory_usage;
let size_start = (total_size + part_id.total - 1) / part_id.total * part_id.idx;
let size_end = std::cmp::min(
Expand Down

0 comments on commit a34992e

Please sign in to comment.