Struct grovedb_merk::tree::TreeNode
source · pub struct TreeNode { /* private fields */ }Expand description
A binary AVL tree data structure, with Merkle hashes.
Trees’ inner fields are stored on the heap so that nodes can recursively link to each other, and so we can detach nodes from their parents, then reattach without allocating or freeing heap memory.
Implementations§
source§impl TreeNode
impl TreeNode
sourcepub fn decode_raw(
bytes: &[u8],
key: Vec<u8>,
value_defined_cost_fn: Option<impl Fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
grove_version: &GroveVersion,
) -> Result<Self, Error>
pub fn decode_raw( bytes: &[u8], key: Vec<u8>, value_defined_cost_fn: Option<impl Fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>, grove_version: &GroveVersion, ) -> Result<Self, Error>
Decode given bytes and set as Tree fields. Set key to value of given key.
source§impl TreeNode
impl TreeNode
sourcepub fn encode_into(&self, dest: &mut Vec<u8>)
pub fn encode_into(&self, dest: &mut Vec<u8>)
Encode to destination writer
sourcepub fn encoding_length(&self) -> usize
pub fn encoding_length(&self) -> usize
Return length of encoding
sourcepub fn value_encoding_length_with_parent_to_child_reference(&self) -> u32
pub fn value_encoding_length_with_parent_to_child_reference(&self) -> u32
Get the cost (byte length) of the value including parent to child reference (or hook)
sourcepub fn decode_into(
&mut self,
key: Vec<u8>,
input: &[u8],
value_defined_cost_fn: Option<impl Fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
grove_version: &GroveVersion,
) -> Result<()>
pub fn decode_into( &mut self, key: Vec<u8>, input: &[u8], value_defined_cost_fn: Option<impl Fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>, grove_version: &GroveVersion, ) -> Result<()>
Decode bytes from reader, set as Tree fields and set key to given key
sourcepub fn decode(
key: Vec<u8>,
input: &[u8],
value_defined_cost_fn: Option<impl Fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
grove_version: &GroveVersion,
) -> Result<Self>
pub fn decode( key: Vec<u8>, input: &[u8], value_defined_cost_fn: Option<impl Fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>, grove_version: &GroveVersion, ) -> Result<Self>
Decode input and set as Tree fields. Set the key as the given key.
source§impl TreeNode
impl TreeNode
sourcepub fn new(
key: Vec<u8>,
value: Vec<u8>,
value_defined_cost: Option<ValueDefinedCostType>,
feature_type: TreeFeatureType,
) -> CostContext<Self>
pub fn new( key: Vec<u8>, value: Vec<u8>, value_defined_cost: Option<ValueDefinedCostType>, feature_type: TreeFeatureType, ) -> CostContext<Self>
Creates a new Tree with the given key and value, and no children.
Hashes the key/value pair and initializes the kv_hash field.
sourcepub fn new_with_tree_inner(inner_tree: TreeNodeInner) -> Self
pub fn new_with_tree_inner(inner_tree: TreeNodeInner) -> Self
Creates a new Tree given an inner tree
sourcepub fn is_sum_node(&self) -> bool
pub fn is_sum_node(&self) -> bool
Is sum node?
pub fn storage_cost_for_update( current_value_byte_cost: u32, old_cost: u32, ) -> StorageCost
sourcepub fn kv_with_parent_hook_size_and_storage_cost_from_old_cost(
&self,
current_value_byte_cost: u32,
old_cost: u32,
) -> Result<(u32, KeyValueStorageCost), Error>
pub fn kv_with_parent_hook_size_and_storage_cost_from_old_cost( &self, current_value_byte_cost: u32, old_cost: u32, ) -> Result<(u32, KeyValueStorageCost), Error>
Compare current value byte cost with old cost and return
current value byte cost with updated KeyValueStorageCost
sourcepub fn kv_with_parent_hook_size_and_storage_cost(
&self,
old_tree_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
) -> Result<(u32, KeyValueStorageCost), Error>
pub fn kv_with_parent_hook_size_and_storage_cost( &self, old_tree_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, ) -> Result<(u32, KeyValueStorageCost), Error>
Get current value byte cost and old value byte cost and
compare and return current value byte cost with updated
KeyValueStorageCost
sourcepub fn kv_with_parent_hook_size_and_storage_cost_change_for_value(
&self,
old_tree_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
value: Option<Vec<u8>>,
) -> Result<(u32, KeyValueStorageCost), Error>
pub fn kv_with_parent_hook_size_and_storage_cost_change_for_value( &self, old_tree_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, value: Option<Vec<u8>>, ) -> Result<(u32, KeyValueStorageCost), Error>
The point of this function is to get the cost change when we create a temp value that’s a partial merger between the old value and the new value. Basically it is the new value with the old values flags For example if we had an old value “Sam” with 40 bytes of flags and a new value “Samuel” with 2 bytes of flags, the cost is probably going to go up, As when we merge we will have Samuel with at least 40 bytes of flags/
sourcepub fn new_with_value_hash(
key: Vec<u8>,
value: Vec<u8>,
value_hash: CryptoHash,
feature_type: TreeFeatureType,
) -> CostContext<Self>
pub fn new_with_value_hash( key: Vec<u8>, value: Vec<u8>, value_hash: CryptoHash, feature_type: TreeFeatureType, ) -> CostContext<Self>
Creates a new Tree with the given key, value and value hash, and no
children.
Hashes the key/value pair and initializes the kv_hash field.
sourcepub fn new_with_combined_value_hash(
key: Vec<u8>,
value: Vec<u8>,
value_hash: CryptoHash,
feature_type: TreeFeatureType,
) -> CostContext<Self>
pub fn new_with_combined_value_hash( key: Vec<u8>, value: Vec<u8>, value_hash: CryptoHash, feature_type: TreeFeatureType, ) -> CostContext<Self>
Creates a new Tree with the given key, value and value hash, and no
children.
Sets the tree’s value_hash = hash(value, supplied_value_hash)
sourcepub fn new_with_layered_value_hash(
key: Vec<u8>,
value: Vec<u8>,
value_cost: u32,
value_hash: CryptoHash,
feature_type: TreeFeatureType,
) -> CostContext<Self>
pub fn new_with_layered_value_hash( key: Vec<u8>, value: Vec<u8>, value_cost: u32, value_hash: CryptoHash, feature_type: TreeFeatureType, ) -> CostContext<Self>
Creates a new Tree with the given key, value, value cost and value
hash, and no children.
Sets the tree’s value_hash = hash(value, supplied_value_hash)
sourcepub fn from_fields(
key: Vec<u8>,
value: Vec<u8>,
kv_hash: CryptoHash,
left: Option<Link>,
right: Option<Link>,
feature_type: TreeFeatureType,
) -> CostContext<Self>
pub fn from_fields( key: Vec<u8>, value: Vec<u8>, kv_hash: CryptoHash, left: Option<Link>, right: Option<Link>, feature_type: TreeFeatureType, ) -> CostContext<Self>
Creates a Tree by supplying all the raw struct fields (mainly useful
for testing). The kv_hash and Links are not ensured to be correct.
sourcepub fn feature_type(&self) -> TreeFeatureType
pub fn feature_type(&self) -> TreeFeatureType
Returns the root node’s feature type
sourcepub fn key_as_ref(&self) -> &Vec<u8> ⓘ
pub fn key_as_ref(&self) -> &Vec<u8> ⓘ
Returns the root node’s key as a slice.
sourcepub fn take_key(self) -> Vec<u8> ⓘ
pub fn take_key(self) -> Vec<u8> ⓘ
Consumes the tree and returns its root node’s key, without having to clone or allocate.
sourcepub fn value_mut_ref(&mut self) -> &mut Vec<u8> ⓘ
pub fn value_mut_ref(&mut self) -> &mut Vec<u8> ⓘ
Returns the root node’s value as a ref.
sourcepub fn value_as_slice(&self) -> &[u8] ⓘ
pub fn value_as_slice(&self) -> &[u8] ⓘ
Returns the root node’s value as a slice.
sourcepub const fn kv_hash(&self) -> &CryptoHash
pub const fn kv_hash(&self) -> &CryptoHash
Returns the hash of the root node’s key/value pair.
sourcepub const fn value_hash(&self) -> &CryptoHash
pub const fn value_hash(&self) -> &CryptoHash
Returns the hash of the node’s valu
sourcepub const fn link(&self, left: bool) -> Option<&Link>
pub const fn link(&self, left: bool) -> Option<&Link>
Returns a reference to the root node’s Link on the given side, if any.
If there is no child, returns None.
sourcepub fn link_mut(&mut self, left: bool) -> Option<&mut Link>
pub fn link_mut(&mut self, left: bool) -> Option<&mut Link>
Returns a mutable reference to the root node’s Link on the given side,
if any. If there is no child, returns None.
sourcepub fn child_ref_and_sum_size(&self, left: bool) -> Option<(u32, u32)>
pub fn child_ref_and_sum_size(&self, left: bool) -> Option<(u32, u32)>
Returns a the size of node’s child key and sum on the given side, if
any. If there is no child, returns None.
sourcepub const fn child(&self, left: bool) -> Option<&Self>
pub const fn child(&self, left: bool) -> Option<&Self>
Returns a reference to the root node’s child on the given side, if any.
If there is no child, returns None.
sourcepub fn child_mut(&mut self, left: bool) -> Option<&mut Self>
pub fn child_mut(&mut self, left: bool) -> Option<&mut Self>
Returns a mutable reference to the root node’s child on the given side,
if any. If there is no child, returns None.
sourcepub const fn child_hash(&self, left: bool) -> &CryptoHash
pub const fn child_hash(&self, left: bool) -> &CryptoHash
Returns the hash of the root node’s child on the given side, if any. If there is no child, returns the null hash (zero-filled).
sourcepub fn child_sum(&self, left: bool) -> i64
pub fn child_sum(&self, left: bool) -> i64
Returns the sum of the root node’s child on the given side, if any. If there is no child, returns 0.
sourcepub fn hash(&self) -> CostContext<CryptoHash>
pub fn hash(&self) -> CostContext<CryptoHash>
Computes and returns the hash of the root node.
sourcepub fn sum(&self) -> Result<Option<i64>, Error>
pub fn sum(&self) -> Result<Option<i64>, Error>
Computes and returns the hash of the root node.
sourcepub const fn child_pending_writes(&self, left: bool) -> usize
pub const fn child_pending_writes(&self, left: bool) -> usize
Returns the number of pending writes for the child on the given side, if any. If there is no child, returns 0.
sourcepub const fn child_height(&self, left: bool) -> u8
pub const fn child_height(&self, left: bool) -> u8
Returns the height of the child on the given side, if any. If there is no child, returns 0.
sourcepub const fn child_heights(&self) -> (u8, u8)
pub const fn child_heights(&self) -> (u8, u8)
Return the child heights of self
sourcepub fn height(&self) -> u8
pub fn height(&self) -> u8
Returns the height of the tree (the number of levels). For example, a single node has height 1, a node with a single descendant has height 2, etc.
sourcepub const fn balance_factor(&self) -> i8
pub const fn balance_factor(&self) -> i8
Returns the balance factor of the root node. This is the difference between the height of the right child (if any) and the height of the left child (if any). For example, a balance factor of 2 means the right subtree is 2 levels taller than the left subtree.
sourcepub fn attach(self, left: bool, maybe_child: Option<Self>) -> Self
pub fn attach(self, left: bool, maybe_child: Option<Self>) -> Self
Attaches the child (if any) to the root node on the given side. Creates
a Link of variant Link::Modified which contains the child.
Panics if there is already a child on the given side.
sourcepub fn detach(self, left: bool) -> (Self, Option<Self>)
pub fn detach(self, left: bool) -> (Self, Option<Self>)
Detaches the child on the given side (if any) from the root node, and
returns (root_node, maybe_child).
One will usually want to reattach (see attach) a child on the same
side after applying some operation to the detached child.
sourcepub fn detach_expect(self, left: bool) -> (Self, Self)
pub fn detach_expect(self, left: bool) -> (Self, Self)
Detaches the child on the given side from the root node, and
returns (root_node, child).
Panics if there is no child on the given side.
One will usually want to reattach (see attach) a child on the same
side after applying some operation to the detached child.
sourcepub fn walk<F>(self, left: bool, f: F) -> Self
pub fn walk<F>(self, left: bool, f: F) -> Self
Detaches the child on the given side and passes it into f, which must
return a new child (either the same child, a new child to take its
place, or None to explicitly keep the slot empty).
This is the same as detach, but with the function interface to enforce
at compile-time that an explicit final child value is returned. This is
less error prone that detaching with detach and reattaching with
attach.
sourcepub fn walk_expect<F>(self, left: bool, f: F) -> Self
pub fn walk_expect<F>(self, left: bool, f: F) -> Self
Like walk, but panics if there is no child on the given side.
sourcepub fn put_value(
self,
value: Vec<u8>,
feature_type: TreeFeatureType,
old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>,
update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>,
section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>,
) -> CostResult<Self, Error>
pub fn put_value( self, value: Vec<u8>, feature_type: TreeFeatureType, old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>, update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>, section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, ) -> CostResult<Self, Error>
Replaces the root node’s value with the given value and returns the
modified Tree.
sourcepub fn put_value_with_fixed_cost(
self,
value: Vec<u8>,
value_fixed_cost: u32,
feature_type: TreeFeatureType,
old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>,
update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>,
section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>,
) -> CostResult<Self, Error>
pub fn put_value_with_fixed_cost( self, value: Vec<u8>, value_fixed_cost: u32, feature_type: TreeFeatureType, old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>, update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>, section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, ) -> CostResult<Self, Error>
Replaces the root node’s value with the given value and returns the
modified Tree.
sourcepub fn put_value_and_reference_value_hash(
self,
value: Vec<u8>,
value_hash: CryptoHash,
feature_type: TreeFeatureType,
old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>,
update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>,
section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>,
) -> CostResult<Self, Error>
pub fn put_value_and_reference_value_hash( self, value: Vec<u8>, value_hash: CryptoHash, feature_type: TreeFeatureType, old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>, update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>, section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, ) -> CostResult<Self, Error>
Replaces the root node’s value with the given value and value hash
and returns the modified Tree.
sourcepub fn put_value_with_reference_value_hash_and_value_cost(
self,
value: Vec<u8>,
value_hash: CryptoHash,
value_cost: u32,
feature_type: TreeFeatureType,
old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>,
update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>,
section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>,
) -> CostResult<Self, Error>
pub fn put_value_with_reference_value_hash_and_value_cost( self, value: Vec<u8>, value_hash: CryptoHash, value_cost: u32, feature_type: TreeFeatureType, old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, get_temp_new_value_with_old_flags: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<Option<Vec<u8>>, Error>, update_tree_value_based_on_costs: &mut impl FnMut(&StorageCost, &Vec<u8>, &mut Vec<u8>) -> Result<(bool, Option<ValueDefinedCostType>), Error>, section_removal_bytes: &mut impl FnMut(&Vec<u8>, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, ) -> CostResult<Self, Error>
Replaces the root node’s value with the given value and value hash
and returns the modified Tree.
sourcepub fn commit<C: Commit>(
&mut self,
c: &mut C,
old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>,
) -> CostResult<(), Error>
pub fn commit<C: Commit>( &mut self, c: &mut C, old_specialized_cost: &impl Fn(&Vec<u8>, &Vec<u8>) -> Result<u32, Error>, ) -> CostResult<(), Error>
Called to finalize modifications to a tree, recompute its hashes, and write the updated nodes to a backing store.
Traverses through the tree, computing hashes for all modified links and
replacing them with Link::Loaded variants, writes out all changes to
the given Commit object’s write method, and calls the its prune
method to test whether or not to keep or prune nodes from memory.
sourcepub fn load<S: Fetch, V>(
&mut self,
left: bool,
source: &S,
value_defined_cost_fn: Option<&V>,
grove_version: &GroveVersion,
) -> CostResult<(), Error>
pub fn load<S: Fetch, V>( &mut self, left: bool, source: &S, value_defined_cost_fn: Option<&V>, grove_version: &GroveVersion, ) -> CostResult<(), Error>
Fetches the child on the given side using the given data source, and
places it in the child slot (upgrading the link from Link::Reference
to Link::Loaded).
Trait Implementations§
impl StructuralPartialEq for TreeNode
Auto Trait Implementations§
impl Freeze for TreeNode
impl RefUnwindSafe for TreeNode
impl Send for TreeNode
impl Sync for TreeNode
impl Unpin for TreeNode
impl UnwindSafe for TreeNode
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<T> CostsExt for T
impl<T> CostsExt for T
source§fn wrap_with_cost(self, cost: OperationCost) -> CostContext<Self>where
Self: Sized,
fn wrap_with_cost(self, cost: OperationCost) -> CostContext<Self>where
Self: Sized,
CostContext object with provided costs.source§fn wrap_fn_cost(
self,
f: impl FnOnce(&Self) -> OperationCost,
) -> CostContext<Self>where
Self: Sized,
fn wrap_fn_cost(
self,
f: impl FnOnce(&Self) -> OperationCost,
) -> CostContext<Self>where
Self: Sized,
CostContext object with costs computed using the
value getting wrapped.source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more