-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cd4afe
commit d145432
Showing
3 changed files
with
45 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |