Struct chain_storage::block_store::BlockStore
source · pub struct BlockStore {
permanent: PermanentStore,
root_id: Value,
id_length: usize,
blocks_tree: Tree,
info_tree: Tree,
chain_length_index_tree: Tree,
branches_tips_tree: Tree,
tags_tree: Tree,
_db: Db,
}
Fields§
§permanent: PermanentStore
§root_id: Value
§id_length: usize
§blocks_tree: Tree
§info_tree: Tree
§chain_length_index_tree: Tree
§branches_tips_tree: Tree
§_db: Db
Implementations§
source§impl BlockStore
impl BlockStore
sourcepub fn file<P: AsRef<Path>, I: Into<Value> + Clone>(
path: P,
root_id: I
) -> Result<Self, Error>
pub fn file<P: AsRef<Path>, I: Into<Value> + Clone>( path: P, root_id: I ) -> Result<Self, Error>
Create a new storage handle. The path must not exist or should be a directory. The directory will be created if it does not exist.
Arguments
path
- a path to the storage directory.root_id
- the ID of the root block which the first block in this block chain should refer to as a parent.
sourcepub fn memory<I: Into<Value> + Clone>(root_id: I) -> Result<Self, Error>
pub fn memory<I: Into<Value> + Clone>(root_id: I) -> Result<Self, Error>
Open a temporary in-memory database.
Arguments
root_id
- the ID of the root block which the first block in this block chain should refer to as a parent.
fn new<I: Into<Value>>( root_id: I, volatile: Db, permanent: PermanentStore ) -> Result<Self, Error>
sourcepub fn put_block(
&self,
block: &[u8],
block_info: BlockInfo
) -> Result<(), Error>
pub fn put_block( &self, block: &[u8], block_info: BlockInfo ) -> Result<(), Error>
Write a block to the store. The parent of the block must exist (unless it’s the root id).
Arguments
block
- a serialized representation of a block.block_info
- block metadata for internal needs (indexing, linking between blocks, etc)
sourcepub fn get_block_info(&self, block_id: &[u8]) -> Result<BlockInfo, Error>
pub fn get_block_info(&self, block_id: &[u8]) -> Result<BlockInfo, Error>
Get the BlockInfo
instance for the requested block.
Arguments
block_id
- the serialized block identifier.
fn get_block_info_volatile(&self, block_id: &[u8]) -> Result<BlockInfo, Error>
sourcepub fn get_blocks_by_chain_length(
&self,
chain_length: u32
) -> Result<Vec<Value>, Error>
pub fn get_blocks_by_chain_length( &self, chain_length: u32 ) -> Result<Vec<Value>, Error>
Get multiple serialized blocks from the given chain length. This will return block contents, not their IDs. If there is a block at the given chain length in the permanent storage, only this block is returned. Other branches are considered to be ready of removal if there are any.
sourcepub fn put_tag(&self, tag_name: &str, block_id: &[u8]) -> Result<(), Error>
pub fn put_tag(&self, tag_name: &str, block_id: &[u8]) -> Result<(), Error>
Add a tag for a given block. The block id can be later retrieved by this tag.
sourcepub fn get_tag(&self, tag_name: &str) -> Result<Option<Value>, Error>
pub fn get_tag(&self, tag_name: &str) -> Result<Option<Value>, Error>
Get the block ID for the given tag.
sourcepub fn prune_branch(&self, tip_id: &[u8]) -> Result<(), Error>
pub fn prune_branch(&self, tip_id: &[u8]) -> Result<(), Error>
Prune a branch with the given tip id from the storage.
sourcepub fn block_exists(&self, block_id: &[u8]) -> Result<bool, Error>
pub fn block_exists(&self, block_id: &[u8]) -> Result<bool, Error>
Check if the block with the given id exists.
fn block_exists_volatile(&self, block_id: &[u8]) -> Result<bool, Error>
sourcepub fn is_ancestor(
&self,
ancestor_id: &[u8],
descendant_id: &[u8]
) -> Result<Option<u32>, Error>
pub fn is_ancestor( &self, ancestor_id: &[u8], descendant_id: &[u8] ) -> Result<Option<u32>, Error>
Determine whether block identified by ancestor_id
is an ancestor of
block identified by descendant_id
.
Returned values:
Ok(Some(dist))
-ancestor
is ancestor ofdescendant
and there aredist
blocks between themOk(None)
-ancestor
is not ancestor ofdescendant
Err(error)
-ancestor
ordescendant
was not found
sourcepub fn get_nth_ancestor(
&self,
block_id: &[u8],
distance: u32
) -> Result<BlockInfo, Error>
pub fn get_nth_ancestor( &self, block_id: &[u8], distance: u32 ) -> Result<BlockInfo, Error>
Get n-th (n = distance
) ancestor of the block, identified by
block_id
.
sourcepub fn find_lowest_common_ancestor(
&self,
block1: &[u8],
block2: &[u8]
) -> Result<Option<BlockInfo>, Error>
pub fn find_lowest_common_ancestor( &self, block1: &[u8], block2: &[u8] ) -> Result<Option<BlockInfo>, Error>
Find the lowest common ancestor block (commonly referred to as lca) of two blocks in the chain
In case there are more than one common ancestor, pick the one with the biggest chain length.
sourcepub fn flush_to_permanent_store(
&self,
to_block: &[u8],
min_number: usize
) -> Result<usize, Error>
pub fn flush_to_permanent_store( &self, to_block: &[u8], min_number: usize ) -> Result<usize, Error>
Move all blocks up to the provided block ID to the permanent block storage.
Arguments
to_block
- the ID of the block until which we want to flush blocks to the permanent storage.min_number
- the minimum number of blocks to be flushed. If less than this number of blocks is available for flushing the function will not trigger.
Panics
If min_number
is less than 1.
Returns
The number of blocks that were flushed.
Trait Implementations§
source§impl Clone for BlockStore
impl Clone for BlockStore
source§fn clone(&self) -> BlockStore
fn clone(&self) -> BlockStore
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more