Add model for new store

This commit is contained in:
Julien Cretin
2020-10-27 14:17:50 +01:00
parent 55ceec2a21
commit e7303ed7c6
3 changed files with 222 additions and 1 deletions

View File

@@ -13,7 +13,9 @@
// limitations under the License.
use crate::StorageError;
use alloc::vec::Vec;
/// Errors returned by store operations.
#[derive(Debug, PartialEq, Eq)]
pub enum StoreError {
/// Invalid argument.
@@ -60,4 +62,55 @@ impl From<StorageError> for StoreError {
}
}
/// Result of store operations.
pub type StoreResult<T> = Result<T, StoreError>;
/// Progression ratio for store metrics.
///
/// This is used for the [capacity] and [lifetime] metrics. Those metrics are measured in words.
///
/// [capacity]: struct.Store.html#method.capacity
/// [lifetime]: struct.Store.html#method.lifetime
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct StoreRatio {
/// How much of the metric is used.
pub used: usize,
/// How much of the metric can be used at most.
pub total: usize,
}
impl StoreRatio {
/// How much of the metric is remaining.
pub fn remaining(self) -> usize {
self.total - self.used
}
}
/// Represents an update to the store as part of a transaction.
#[derive(Clone, Debug)]
pub enum StoreUpdate {
/// Inserts or replaces an entry in the store.
Insert { key: usize, value: Vec<u8> },
/// Removes an entry from the store.
Remove { key: usize },
}
impl StoreUpdate {
/// Returns the key affected by the update.
pub fn key(&self) -> usize {
match *self {
StoreUpdate::Insert { key, .. } => key,
StoreUpdate::Remove { key } => key,
}
}
/// Returns the value written by the update.
pub fn value(&self) -> Option<&[u8]> {
match self {
StoreUpdate::Insert { value, .. } => Some(value),
StoreUpdate::Remove { .. } => None,
}
}
}