Skip to main content

hypercore/common/
store.rs

1/// The types of stores that can be created.
2#[derive(Debug, Clone, PartialEq)]
3pub enum Store {
4    /// Tree
5    Tree,
6    /// Data (block store)
7    Data,
8    /// Bitfield
9    Bitfield,
10    /// Oplog
11    Oplog,
12}
13
14impl std::fmt::Display for Store {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            Store::Tree => write!(f, "tree"),
18            Store::Data => write!(f, "data"),
19            Store::Bitfield => write!(f, "bitfield"),
20            Store::Oplog => write!(f, "oplog"),
21        }
22    }
23}
24
25/// Information type about a store.
26#[derive(Debug, PartialEq)]
27pub(crate) enum StoreInfoType {
28    /// Read/write content of the store
29    Content,
30    /// Size in bytes of the store. When flushed, truncates to the given index. `data` is `None`.
31    Size,
32}
33
34/// Piece of information about a store. Useful for indicating changes that should be made to random
35/// access storages or information read from them.
36#[derive(Debug)]
37pub(crate) struct StoreInfo {
38    pub(crate) store: Store,
39    pub(crate) info_type: StoreInfoType,
40    pub(crate) index: u64,
41    pub(crate) length: Option<u64>,
42    pub(crate) data: Option<Box<[u8]>>,
43    /// When reading, indicates missing value (can be true only if allow_miss is given as instruction).
44    /// When writing indicates that the value should be dropped.
45    pub(crate) miss: bool,
46}
47
48impl StoreInfo {
49    pub(crate) fn new_content(store: Store, index: u64, data: &[u8]) -> Self {
50        Self {
51            store,
52            info_type: StoreInfoType::Content,
53            index,
54            length: Some(data.len() as u64),
55            data: Some(data.into()),
56            miss: false,
57        }
58    }
59
60    pub(crate) fn new_content_miss(store: Store, index: u64) -> Self {
61        Self {
62            store,
63            info_type: StoreInfoType::Content,
64            index,
65            length: None,
66            data: None,
67            miss: true,
68        }
69    }
70
71    pub(crate) fn new_delete(store: Store, index: u64, length: u64) -> Self {
72        Self {
73            store,
74            info_type: StoreInfoType::Content,
75            index,
76            length: Some(length),
77            data: None,
78            miss: true,
79        }
80    }
81
82    pub(crate) fn new_truncate(store: Store, index: u64) -> Self {
83        Self {
84            store,
85            info_type: StoreInfoType::Size,
86            index,
87            length: None,
88            data: None,
89            miss: true,
90        }
91    }
92
93    pub(crate) fn new_size(store: Store, index: u64, length: u64) -> Self {
94        Self {
95            store,
96            info_type: StoreInfoType::Size,
97            index,
98            length: Some(length),
99            data: None,
100            miss: false,
101        }
102    }
103}
104
105/// Represents an instruction to obtain information about a store.
106#[derive(Debug)]
107pub(crate) struct StoreInfoInstruction {
108    pub(crate) store: Store,
109    pub(crate) info_type: StoreInfoType,
110    pub(crate) index: u64,
111    pub(crate) length: Option<u64>,
112    pub(crate) allow_miss: bool,
113}
114
115impl StoreInfoInstruction {
116    pub(crate) fn new_content(store: Store, index: u64, length: u64) -> Self {
117        Self {
118            store,
119            info_type: StoreInfoType::Content,
120            index,
121            length: Some(length),
122            allow_miss: false,
123        }
124    }
125
126    pub(crate) fn new_content_allow_miss(store: Store, index: u64, length: u64) -> Self {
127        Self {
128            store,
129            info_type: StoreInfoType::Content,
130            index,
131            length: Some(length),
132            allow_miss: true,
133        }
134    }
135
136    pub(crate) fn new_all_content(store: Store) -> Self {
137        Self {
138            store,
139            info_type: StoreInfoType::Content,
140            index: 0,
141            length: None,
142            allow_miss: false,
143        }
144    }
145
146    pub(crate) fn new_size(store: Store, index: u64) -> Self {
147        Self {
148            store,
149            info_type: StoreInfoType::Size,
150            index,
151            length: None,
152            allow_miss: false,
153        }
154    }
155}