[common][server] Get returns Option
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
1e43bd5a7b
commit
7833fcd354
2 changed files with 15 additions and 17 deletions
|
@ -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;
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue