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

source

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

source

pub fn encode(&self) -> Vec<u8>

Encode

source

pub fn encode_into(&self, dest: &mut Vec<u8>)

Encode to destination writer

source

pub fn encoding_length(&self) -> usize

Return length of encoding

source

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)

source

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

source

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<'a> TreeNode

source

pub fn iter(&'a self) -> Iter<'a>

Creates an iterator which yields (key, value) tuples for all of the tree’s nodes which are retained in memory (skipping pruned subtrees).

source§

impl TreeNode

source

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.

source

pub fn new_with_tree_inner(inner_tree: TreeNodeInner) -> Self

Creates a new Tree given an inner tree

source

pub fn is_sum_node(&self) -> bool

Is sum node?

source

pub fn storage_cost_for_update( current_value_byte_cost: u32, old_cost: u32, ) -> StorageCost

source

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

source

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

source

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/

source

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.

source

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)

source

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)

source

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.

source

pub fn key(&self) -> &[u8]

Returns the root node’s key as a slice.

source

pub fn feature_type(&self) -> TreeFeatureType

Returns the root node’s feature type

source

pub fn key_as_ref(&self) -> &Vec<u8>

Returns the root node’s key as a slice.

source

pub fn set_key(&mut self, key: Vec<u8>)

Set key of Tree

source

pub fn set_value(&mut self, value: Vec<u8>)

Set value of Tree

source

pub fn take_key(self) -> Vec<u8>

Consumes the tree and returns its root node’s key, without having to clone or allocate.

source

pub fn value_ref(&self) -> &Vec<u8>

Returns the root node’s value as a ref.

source

pub fn value_mut_ref(&mut self) -> &mut Vec<u8>

Returns the root node’s value as a ref.

source

pub fn value_as_slice(&self) -> &[u8]

Returns the root node’s value as a slice.

source

pub const fn kv_hash(&self) -> &CryptoHash

Returns the hash of the root node’s key/value pair.

source

pub const fn value_hash(&self) -> &CryptoHash

Returns the hash of the node’s valu

Returns a reference to the root node’s Link on the given side, if any. If there is no child, returns None.

Returns a mutable reference to the root node’s Link on the given side, if any. If there is no child, returns None.

source

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.

source

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.

source

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.

source

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).

source

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.

source

pub fn hash(&self) -> CostContext<CryptoHash>

Computes and returns the hash of the root node.

source

pub fn sum(&self) -> Result<Option<i64>, Error>

Computes and returns the hash of the root node.

source

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.

source

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.

source

pub const fn child_heights(&self) -> (u8, u8)

Return the child heights of self

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn walk<F>(self, left: bool, f: F) -> Self
where F: FnOnce(Option<Self>) -> Option<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.

source

pub fn walk_expect<F>(self, left: bool, f: F) -> Self
where F: FnOnce(Self) -> Option<Self>,

Like walk, but panics if there is no child on the given side.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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).

source§

impl TreeNode

source

pub fn average_case_encoded_tree_size( not_prefixed_key_len: u32, estimated_element_size: u32, is_sum_node: bool, ) -> u32

Return estimate of average encoded tree size

source§

impl TreeNode

source

pub fn worst_case_encoded_tree_size( not_prefixed_key_len: u32, max_element_size: u32, is_sum_node: bool, ) -> u32

Return worst case size of encoded tree

Trait Implementations§

source§

impl Clone for TreeNode

source§

fn clone(&self) -> TreeNode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TreeNode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<S> From<Walker<S>> for TreeNode
where S: Fetch + Sized + Clone,

source§

fn from(walker: Walker<S>) -> Self

Converts to this type from the input type.
source§

impl PartialEq for TreeNode

source§

fn eq(&self, other: &TreeNode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for TreeNode

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CostsExt for T

source§

fn wrap_with_cost(self, cost: OperationCost) -> CostContext<Self>
where Self: Sized,

Wraps any value into a CostContext object with provided costs.
source§

fn wrap_fn_cost( self, f: impl FnOnce(&Self) -> OperationCost, ) -> CostContext<Self>
where Self: Sized,

Wraps any value into CostContext object with costs computed using the value getting wrapped.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryFromVersioned<U> for T
where T: TryFrom<U>,

§

type Error = <T as TryFrom<U>>::Error

The type returned in the event of a conversion error.
source§

fn try_from_versioned( value: U, _grove_version: &GroveVersion, ) -> Result<T, <T as TryFromVersioned<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> TryIntoVersioned<U> for T
where U: TryFromVersioned<T>,

§

type Error = <U as TryFromVersioned<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into_versioned( self, grove_version: &GroveVersion, ) -> Result<U, <U as TryFromVersioned<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V