Skip to main content

hypercore/data/
mod.rs

1use crate::common::{NodeByteRange, Store, StoreInfo, StoreInfoInstruction};
2use futures::future::Either;
3
4/// Block store
5#[derive(Debug, Default)]
6pub(crate) struct BlockStore {}
7
8impl BlockStore {
9    pub(crate) fn append_batch<A: AsRef<[u8]>, B: AsRef<[A]>>(
10        &self,
11        batch: B,
12        batch_length: usize,
13        byte_length: u64,
14    ) -> StoreInfo {
15        let mut buffer: Vec<u8> = Vec::with_capacity(batch_length);
16        for data in batch.as_ref().iter() {
17            buffer.extend_from_slice(data.as_ref());
18        }
19        StoreInfo::new_content(Store::Data, byte_length, &buffer)
20    }
21
22    pub(crate) fn put(&self, value: &[u8], offset: u64) -> StoreInfo {
23        StoreInfo::new_content(Store::Data, offset, value)
24    }
25
26    pub(crate) fn read(
27        &self,
28        byte_range: &NodeByteRange,
29        info: Option<StoreInfo>,
30    ) -> Either<StoreInfoInstruction, Box<[u8]>> {
31        if let Some(info) = info {
32            Either::Right(info.data.unwrap())
33        } else {
34            Either::Left(StoreInfoInstruction::new_content(
35                Store::Data,
36                byte_range.index,
37                byte_range.length,
38            ))
39        }
40    }
41
42    /// Clears a segment, returns infos to write to storage.
43    pub(crate) fn clear(&self, start: u64, length: u64) -> StoreInfo {
44        StoreInfo::new_delete(Store::Data, start, length)
45    }
46}