[common][server] Get returns Option
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Armin Friedl 2020-02-09 13:30:19 +01:00
parent 1e43bd5a7b
commit 7833fcd354
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
2 changed files with 15 additions and 17 deletions

View file

@ -13,9 +13,11 @@ use std::{
path::Path, path::Path,
io::BufReader, io::BufReader,
fs::File, fs::File,
ops::Deref
}; };
#[allow(unused_imports)]
use std::ops::Deref; // we use this but rustc doesn't know
use quick_error::quick_error; use quick_error::quick_error;
use seckey::SecKey; use seckey::SecKey;

View file

@ -29,7 +29,7 @@ impl CofferMap {
} }
impl Coffer for CofferMap { impl Coffer for CofferMap {
fn put(&mut self, key: CofferKey, value: CofferValue) -> CofferResult<()> { fn put(&mut self, key: CofferKey, value: CofferValue) -> CofferResult<()> {
let mut lock = self.write(); let mut lock = self.write();
match lock.get_mut(&key.shard) { match lock.get_mut(&key.shard) {
@ -61,33 +61,29 @@ impl Coffer for CofferMap {
} }
} }
fn get(&self, key: &CofferKey) -> CofferResult<CofferValue> { fn get(&self, key: &CofferKey) -> Option<CofferValue> {
let lock = self.read(); let lock = self.read();
let res = lock.get(&key.shard) lock.get(&key.shard)
.and_then( |shard| { shard.get(&key.key) } ) .and_then( |shard| { shard.get(&key.key) } )
.ok_or(CofferError::Msg("Key not found"))?; .map(|o| o.clone())
Ok(res.clone())
} }
fn get_shard<T>(&self, shard: T) -> CofferResult<CofferShard> fn get_shard<T>(&self, shard: T) -> Option<CofferShard>
where T: AsRef<str> where T: AsRef<str>
{ {
let lock = self.read(); let lock = self.read();
debug!{"Coffer {:?}", *lock} debug!{"Coffer {:?}", *lock}
let coffer_shard = lock.get(shard.as_ref()) let map_to_vec = |map: &HashMap<String, CofferValue>| {
.ok_or(CofferError::Msg("Shard not found"))?; map.iter()
.map(|(k,v)| (k.clone(), v.clone()))
.collect::<Vec<(String, CofferValue)>>()
};
let mut res = CofferShard(Vec::new()); lock.get(shard.as_ref())
.and_then(|s| Some(CofferShard(map_to_vec(s))))
for (k,v) in coffer_shard {
res.0.push((k.clone(), v.clone()));
}
Ok(res)
} }
} }