pub trait BlockStore: CondSync {
    // Required methods
    fn get_block(
        &self,
        cid: &Cid
    ) -> impl Future<Output = Result<Bytes, BlockStoreError>> + CondSend;
    fn put_block_keyed(
        &self,
        cid: Cid,
        bytes: impl Into<Bytes> + CondSend
    ) -> impl Future<Output = Result<(), BlockStoreError>> + CondSend;
    fn has_block(
        &self,
        cid: &Cid
    ) -> impl Future<Output = Result<bool, BlockStoreError>> + CondSend;

    // Provided methods
    fn put_block(
        &self,
        bytes: impl Into<Bytes> + CondSend,
        codec: u64
    ) -> impl Future<Output = Result<Cid, BlockStoreError>> + CondSend { ... }
    fn create_cid(
        &self,
        bytes: &[u8],
        codec: u64
    ) -> Result<Cid, BlockStoreError> { ... }
}
Expand description

For types that implement block store operations like adding, getting content from the store.

Required Methods§

source

fn get_block( &self, cid: &Cid ) -> impl Future<Output = Result<Bytes, BlockStoreError>> + CondSend

Retrieve a block from this store via its hash (Cid).

If this store can’t find the block, it may raise an error like BlockNotFound.

source

fn put_block_keyed( &self, cid: Cid, bytes: impl Into<Bytes> + CondSend ) -> impl Future<Output = Result<(), BlockStoreError>> + CondSend

Put a block of data into this blockstore. The block’s CID needs to match the CID given.

It’s up to the blockstore whether to check this fact or assume it when this function is called.

The default implementation of put_block will use this function under the hood and use the correct CID provided by the create_cid function.

This is useful to be able to add blocks that were generated from other clients with differently configured hashing functions to this blockstore.

source

fn has_block( &self, cid: &Cid ) -> impl Future<Output = Result<bool, BlockStoreError>> + CondSend

Find out whether a call to get_block would return with a result or not.

This is useful for data exchange protocols to find out what needs to be fetched externally and what doesn’t.

Provided Methods§

source

fn put_block( &self, bytes: impl Into<Bytes> + CondSend, codec: u64 ) -> impl Future<Output = Result<Cid, BlockStoreError>> + CondSend

Put some bytes into the blockstore. These bytes should be encoded with the given codec.

E.g. CODEC_RAW for raw bytes blocks, CODEC_DAG_CBOR for dag-cbor, etc.

This codec will determine the codec encoded in the final Cid that’s returned.

If the codec is incorrect, this function won’t fail, but any tools that depend on the correctness of the codec may fail. (E.g. tools that follow the links of blocks).

This funciton allows the blockstore to choose the hashing function itself. The hashing function that was chosen will be readable from the Cid metadata.

If you need control over the concrete hashing function that’s used, see put_block_keyed.

source

fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid, BlockStoreError>

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<B: BlockStore> BlockStore for &B

source§

async fn get_block(&self, cid: &Cid) -> Result<Bytes, BlockStoreError>

source§

async fn put_block( &self, bytes: impl Into<Bytes> + CondSend, codec: u64 ) -> Result<Cid, BlockStoreError>

source§

async fn put_block_keyed( &self, cid: Cid, bytes: impl Into<Bytes> + CondSend ) -> Result<(), BlockStoreError>

source§

async fn has_block(&self, cid: &Cid) -> Result<bool, BlockStoreError>

source§

fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid, BlockStoreError>

source§

impl<B: BlockStore> BlockStore for Box<B>

source§

async fn get_block(&self, cid: &Cid) -> Result<Bytes, BlockStoreError>

source§

async fn put_block( &self, bytes: impl Into<Bytes> + CondSend, codec: u64 ) -> Result<Cid, BlockStoreError>

source§

async fn put_block_keyed( &self, cid: Cid, bytes: impl Into<Bytes> + CondSend ) -> Result<(), BlockStoreError>

source§

async fn has_block(&self, cid: &Cid) -> Result<bool, BlockStoreError>

source§

fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid, BlockStoreError>

Implementors§