Tree

Struct Tree 

Source
pub struct Tree<S> { /* private fields */ }
Expand description

A merkle search tree data structure, backed by storage implementing AsyncBlockStoreRead and optionally AsyncBlockStoreWrite.

This data structure is merely a convenience structure that implements algorithms that handle certain common operations one may want to perform against a MST.

The structure does not actually load the merkle search tree into memory or perform any deep copies. The tree itself lives entirely inside of the provided backing storage. This also carries the implication that any operation performed against the tree will have performance that reflects that of accesses to the backing storage.

If your backing storage is implemented by a cloud service, such as a database or block storage service, you will likely want to insert a caching layer in your block storage to ensure that performance remains fast.


There are two factors that determine the placement of nodes inside of a merkle search tree:

  • The number of leading zeroes in the SHA256 hash of the key
  • The key’s lexicographic position inside of a layer

§Reference

  • Official documentation: https://atproto.com/guides/data-repos
  • Useful reading: https://interjectedfuture.com/crdts-turned-inside-out/

Implementations§

Source§

impl<S: AsyncBlockStoreRead + AsyncBlockStoreWrite> Tree<S>

Source

pub async fn create(storage: S) -> Result<Self, Error>

Create a new MST with an empty root node

Source

pub async fn add(&mut self, key: &str, value: Cid) -> Result<(), Error>

Add a new key with the specified value to the tree.

Source

pub async fn update(&mut self, key: &str, value: Cid) -> Result<(), Error>

Update an existing key with a new value.

Source

pub async fn delete(&mut self, key: &str) -> Result<(), Error>

Delete a key from the tree.

Source§

impl<S: AsyncBlockStoreRead> Tree<S>

Source

pub fn open(storage: S, root: Cid) -> Self

Open a pre-existing merkle search tree.

This is a very cheap operation that does not actually load the MST or check its validity. You should only use this with data from a trusted source.

Source

pub fn root(&self) -> Cid

Return the CID of the root node.

Source

pub async fn depth(&mut self, node: Option<Cid>) -> Result<Option<usize>, Error>

Compute the depth of the merkle search tree from either the specified node or the root

Source

pub fn export(&mut self) -> impl Stream<Item = Result<Cid, Error>> + '_

Returns a stream of all CIDs in the tree or referenced by the tree.

Source

pub fn entries( &mut self, ) -> impl Stream<Item = Result<(String, Cid), Error>> + '_

Returns a stream of all entries in this tree, in lexicographic order.

This function will not work with a partial MST, such as one received from a firehose record.

Source

pub fn entries_prefixed<'a>( &'a mut self, prefix: &'a str, ) -> impl Stream<Item = Result<(String, Cid), Error>> + 'a

Returns a stream of all keys starting with the specified prefix, in lexicographic order.

This function will not work with a partial MST, such as one received from a firehose record.

Source

pub fn keys(&mut self) -> impl Stream<Item = Result<String, Error>> + '_

Returns a stream of all keys in this tree, in lexicographic order.

This function will not work with a partial MST, such as one received from a firehose record.

Source

pub fn keys_prefixed<'a>( &'a mut self, prefix: &'a str, ) -> impl Stream<Item = Result<String, Error>> + 'a

Returns a stream of all keys in this tree with the specified prefix, in lexicographic order.

This function will not work with a partial MST, such as one received from a firehose record.

Source

pub async fn get(&mut self, key: &str) -> Result<Option<Cid>, Error>

Returns the specified record from the repository, or None if it does not exist.

Source

pub async fn extract_path( &mut self, key: &str, ) -> Result<impl Iterator<Item = Cid>, Error>

Returns the full path to a node that contains the specified key (including the containing node).

If the key is not present in the tree, this will return the path to the node that would’ve contained the key.

This is useful for exporting portions of the MST for e.g. generating firehose records.

Trait Implementations§

Source§

impl<S: Clone> Clone for Tree<S>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<S> Debug for Tree<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Tree<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Tree<S>
where S: RefUnwindSafe,

§

impl<S> Send for Tree<S>
where S: Send,

§

impl<S> Sync for Tree<S>
where S: Sync,

§

impl<S> Unpin for Tree<S>
where S: Unpin,

§

impl<S> UnwindSafe for Tree<S>
where S: UnwindSafe,

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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> TryInto<U> for T
where U: TryFrom<T>,

Source§

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.