pub struct Hyperbee<M: CoreMem> { /* private fields */ }Expand description
An append only B-Tree built on Hypercore. It provides a key-value
store API, with methods for inserting, getting, and
deleting key-value pair. As well as creating sorted
iterators, and “sub” B-Trees for grouping related data.
Implementations§
source§impl<M: CoreMem> Hyperbee<M>
impl<M: CoreMem> Hyperbee<M>
sourcepub async fn version(&self) -> u64
pub async fn version(&self) -> u64
The number of blocks in the hypercore.
The first block is always the header block so:
version would be the seq of the next block
version - 1 is most recent block
sourcepub async fn get_root(
&self,
ensure_header: bool
) -> Result<Option<Arc<RwLock<Node<M>>>>, HyperbeeError>
pub async fn get_root( &self, ensure_header: bool ) -> Result<Option<Arc<RwLock<Node<M>>>>, HyperbeeError>
Gets the root of the tree.
When ensure_header == true write the hyperbee header onto the hypercore if it does not exist.
sourcepub async fn create_header(
&self,
metadata: Option<Metadata>
) -> Result<AppendOutcome, HyperbeeError>
pub async fn create_header( &self, metadata: Option<Metadata> ) -> Result<AppendOutcome, HyperbeeError>
Create the header for the Hyperbee. This must be done before writing anything else to the tree.
sourcepub async fn print(&self) -> Result<String, HyperbeeError>
pub async fn print(&self) -> Result<String, HyperbeeError>
Returs a string representing the structure of the tree showing the keys in each node
sourcepub async fn get(
&self,
key: &[u8]
) -> Result<Option<(u64, Option<Vec<u8>>)>, HyperbeeError>
pub async fn get( &self, key: &[u8] ) -> Result<Option<(u64, Option<Vec<u8>>)>, HyperbeeError>
Get the value corresponding to the provided key from the Hyperbee
§Errors
When Hyperbee.get_root fails
sourcepub async fn put(
&self,
key: &[u8],
value: Option<&[u8]>
) -> Result<(Option<u64>, u64), HyperbeeError>
pub async fn put( &self, key: &[u8], value: Option<&[u8]> ) -> Result<(Option<u64>, u64), HyperbeeError>
Insert the given key and value into the tree
Returs the seq of the new key, and Option<u64> which contains the seq of the old key
if it was replaced.
sourcepub async fn put_compare_and_swap(
&self,
key: &[u8],
value: Option<&[u8]>,
cas: impl FnOnce(Option<&KeyValueData>, &KeyValueData) -> bool
) -> Result<(Option<u64>, Option<u64>), HyperbeeError>
pub async fn put_compare_and_swap( &self, key: &[u8], value: Option<&[u8]>, cas: impl FnOnce(Option<&KeyValueData>, &KeyValueData) -> bool ) -> Result<(Option<u64>, Option<u64>), HyperbeeError>
Like Hyperbee::put but takes a compare_and_swap function.
The compared_and_swap function is called with the old key (if present), and the new key.
The new key is only inserted if compare_and_swap returns true.
Returs two Option<u64>s. The first is the old key, the second is the new key.
sourcepub async fn del(&self, key: &[u8]) -> Result<Option<u64>, HyperbeeError>
pub async fn del(&self, key: &[u8]) -> Result<Option<u64>, HyperbeeError>
Delete the given key from the tree.
Returns the seq from the key if it was deleted.
sourcepub async fn del_compare_and_swap(
&self,
key: &[u8],
cas: impl FnOnce(&KeyValueData) -> bool
) -> Result<Option<(bool, u64)>, HyperbeeError>
pub async fn del_compare_and_swap( &self, key: &[u8], cas: impl FnOnce(&KeyValueData) -> bool ) -> Result<Option<(bool, u64)>, HyperbeeError>
Like Hyperbee::del but takes a compare_and_swap function.
Before deleting the function is called with existing key’s KeyValueData.
The key is only deleted if compare_and_swap returs true.
Returns the bool representing the result of compare_and_swap, and the seq for the
key.
sourcepub fn sub(&self, prefix: &[u8]) -> Prefixed<M>
pub fn sub(&self, prefix: &[u8]) -> Prefixed<M>
Create a new tree with all it’s operation’s prefixed by the provided prefix.
sourcepub async fn traverse<'a>(
&self,
conf: TraverseConfig
) -> Result<Traverse<'a, M>, HyperbeeError>
pub async fn traverse<'a>( &self, conf: TraverseConfig ) -> Result<Traverse<'a, M>, HyperbeeError>
Traverse the tree based on the given TraverseConfig
source§impl Hyperbee<RandomAccessDisk>
impl Hyperbee<RandomAccessDisk>
sourcepub async fn from_storage_dir<T: AsRef<Path>>(
path_to_storage_dir: T
) -> Result<Hyperbee<RandomAccessDisk>, HyperbeeError>
pub async fn from_storage_dir<T: AsRef<Path>>( path_to_storage_dir: T ) -> Result<Hyperbee<RandomAccessDisk>, HyperbeeError>
source§impl Hyperbee<RandomAccessMemory>
impl Hyperbee<RandomAccessMemory>
sourcepub async fn from_ram() -> Result<Hyperbee<RandomAccessMemory>, HyperbeeError>
pub async fn from_ram() -> Result<Hyperbee<RandomAccessMemory>, HyperbeeError>
Helper for creating a Hyperbee in RAM