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>
impl<S: AsyncBlockStoreRead + AsyncBlockStoreWrite> Tree<S>
Sourcepub async fn create(storage: S) -> Result<Self, Error>
pub async fn create(storage: S) -> Result<Self, Error>
Create a new MST with an empty root node
Sourcepub async fn add(&mut self, key: &str, value: Cid) -> Result<(), Error>
pub async fn add(&mut self, key: &str, value: Cid) -> Result<(), Error>
Add a new key with the specified value to the tree.
Source§impl<S: AsyncBlockStoreRead> Tree<S>
impl<S: AsyncBlockStoreRead> Tree<S>
Sourcepub fn open(storage: S, root: Cid) -> Self
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.
Sourcepub async fn depth(&mut self, node: Option<Cid>) -> Result<Option<usize>, Error>
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
Sourcepub fn export(&mut self) -> impl Stream<Item = Result<Cid, Error>> + '_
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.
Sourcepub fn entries(
&mut self,
) -> impl Stream<Item = Result<(String, Cid), Error>> + '_
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.
Sourcepub fn entries_prefixed<'a>(
&'a mut self,
prefix: &'a str,
) -> impl Stream<Item = Result<(String, Cid), Error>> + 'a
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.
Sourcepub fn keys(&mut self) -> impl Stream<Item = Result<String, Error>> + '_
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.
Sourcepub fn keys_prefixed<'a>(
&'a mut self,
prefix: &'a str,
) -> impl Stream<Item = Result<String, Error>> + 'a
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.
Sourcepub async fn get(&mut self, key: &str) -> Result<Option<Cid>, Error>
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.
Sourcepub async fn extract_path(
&mut self,
key: &str,
) -> Result<impl Iterator<Item = Cid>, Error>
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.