Skip to content

Commit

Permalink
build: add mock storage (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware authored Apr 16, 2024
1 parent 9cd4afe commit d145432
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/committer/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod errors;
pub(crate) mod map_storage;
pub(crate) mod storage_trait;
29 changes: 29 additions & 0 deletions crates/committer/src/storage/map_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashMap;

use crate::storage::storage_trait::{Storage, StorageKey, StorageValue};

pub(crate) struct MapStorage {
storage: HashMap<StorageKey, StorageValue>,
}

impl Storage for MapStorage {
fn get(&self, key: &StorageKey) -> Option<&StorageValue> {
self.storage.get(key)
}

fn set(&mut self, key: StorageKey, value: StorageValue) -> Option<StorageValue> {
self.storage.insert(key, value)
}

fn mget(&self, keys: &[StorageKey]) -> Vec<Option<&StorageValue>> {
keys.iter().map(|key| self.get(key)).collect::<Vec<_>>()
}

fn mset(&mut self, key_to_value: HashMap<StorageKey, StorageValue>) {
self.storage.extend(key_to_value);
}

fn delete(&mut self, key: &StorageKey) -> Option<StorageValue> {
self.storage.remove(key)
}
}
25 changes: 15 additions & 10 deletions crates/committer/src/storage/storage_trait.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use crate::storage::errors::StorageError;
use std::collections::HashMap;

#[allow(dead_code)]
#[derive(Eq, Hash, PartialEq)]
pub(crate) struct StorageKey(Vec<u8>);

#[allow(dead_code)]
pub(crate) struct StorageValue(Vec<u8>);

pub(crate) trait Storage {
/// Returns value from storage, if it exists.
fn get(&self, key: &StorageKey) -> Option<StorageValue>;
/// Sets value in storage.
fn set(&mut self, key: &StorageKey, value: &StorageValue);
/// Returns values from storage in same order of given keys. If key does not exist,
/// value is None.
fn mget(&self, keys: &[StorageKey]) -> [Option<StorageValue>];
fn get(&self, key: &StorageKey) -> Option<&StorageValue>;

/// Sets value in storage. If key already exists, its value is overwritten and the old value is
/// returned.
fn set(&mut self, key: StorageKey, value: StorageValue) -> Option<StorageValue>;

/// Returns values from storage in same order of given keys. Value is None for keys that do not
/// exist.
fn mget(&self, keys: &[StorageKey]) -> Vec<Option<&StorageValue>>;

/// Sets values in storage.
fn mset(&mut self, key_to_value: &HashMap<StorageKey, StorageValue>);
/// Deletes value from storage. Returns error if key does not exist.
fn delete(&mut self, key: &StorageKey) -> Result<(), StorageError>;
fn mset(&mut self, key_to_value: HashMap<StorageKey, StorageValue>);

/// Deletes value from storage and returns its value if it exists. Returns None if not.
fn delete(&mut self, key: &StorageKey) -> Option<StorageValue>;
}

0 comments on commit d145432

Please sign in to comment.