Merge pull request #588 from asuivelentine/libimagcounter/refactor

Libimagcounter/refactor
This commit is contained in:
Matthias Beyer 2016-07-28 14:41:53 +02:00 committed by GitHub
commit 4dce62030a

View file

@ -11,6 +11,7 @@ use libimagstore::storeid::StoreIdIterator;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagerror::into::IntoError;
use module_path::ModuleEntryPath;
use result::Result;
@ -47,28 +48,25 @@ impl<'a> Counter<'a> {
debug!("Creating new counter: '{}' with value: {}", name, init);
let fle = {
let lockentry = store.create(ModuleEntryPath::new(name.clone()).into_storeid());
if lockentry.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(lockentry.err().unwrap()))));
}
let mut lockentry = lockentry.unwrap();
let mut lockentry = try!(store.create(ModuleEntryPath::new(name.clone()).into_storeid())
.map_err_into(CEK::StoreWriteError));
{
let mut entry = lockentry.deref_mut();
let mut header = entry.get_header_mut();
let setres = header.set("counter", Value::Table(BTreeMap::new()));
if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
return Err(CEK::StoreWriteError.into_error());
}
let setres = header.set("counter.name", Value::String(name));
if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
return Err(CEK::StoreWriteError.into_error())
}
let setres = header.set("counter.value", Value::Integer(init));
if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
return Err(CEK::StoreWriteError.into_error())
}
}
@ -86,7 +84,7 @@ impl<'a> Counter<'a> {
let setres = header.set("counter.unit", Value::String(u.0));
if setres.is_err() {
self.unit = None;
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
return Err(CEK::StoreWriteError.into_error())
}
};
Ok(self)
@ -97,7 +95,7 @@ impl<'a> Counter<'a> {
match header.read("counter.value") {
Ok(Some(Value::Integer(i))) => {
header.set("counter.value", Value::Integer(i + 1))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.map_err_into(CEK::StoreWriteError)
.map(|_| ())
},
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
@ -110,7 +108,7 @@ impl<'a> Counter<'a> {
match header.read("counter.value") {
Ok(Some(Value::Integer(i))) => {
header.set("counter.value", Value::Integer(i - 1))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.map_err_into(CEK::StoreWriteError)
.map(|_| ())
},
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
@ -119,39 +117,28 @@ impl<'a> Counter<'a> {
}
pub fn reset(&mut self) -> Result<()> {
let mut header = self.fle.deref_mut().get_header_mut();
header.set("counter.value", Value::Integer(0))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.map(|_| ())
self.set(0)
}
pub fn set(&mut self, v: i64) -> Result<()> {
let mut header = self.fle.deref_mut().get_header_mut();
header.set("counter.value", Value::Integer(v))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.map_err_into(CEK::StoreWriteError)
.map(|_| ())
}
pub fn name(&self) -> Result<CounterName> {
self.fle.get_header().read("counter.name")
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.and_then(|v| {
match v {
Some(Value::String(s)) => Ok(s),
_ => Err(CE::new(CEK::HeaderTypeError, None)),
}
})
self.read_header_at("counter.name", |v| match v {
Some(Value::String(s)) => Ok(s),
_ => Err(CEK::HeaderTypeError.into_error()),
})
}
pub fn value(&self) -> Result<i64> {
self.fle.get_header().read("counter.value")
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.and_then(|v| {
match v {
Some(Value::Integer(i)) => Ok(i),
_ => Err(CE::new(CEK::HeaderTypeError, None)),
}
})
self.read_header_at("counter.value", |v| match v {
Some(Value::Integer(i)) => Ok(i),
_ => Err(CEK::HeaderTypeError.into_error()),
})
}
pub fn unit(&self) -> Option<&CounterUnit> {
@ -159,13 +146,17 @@ impl<'a> Counter<'a> {
}
pub fn read_unit(&self) -> Result<Option<CounterUnit>> {
self.fle.get_header().read("counter.unit")
.map_err_into(CEK::StoreReadError)
.and_then(|s| match s {
Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))),
Some(_) => Err(CE::new(CEK::HeaderTypeError, None)),
None => Ok(None),
})
self.read_header_at("counter.unit", |s| match s {
Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))),
Some(_) => Err(CEK::HeaderTypeError.into_error()),
None => Ok(None),
})
}
fn read_header_at<T, F>(&self, name: &str, f: F) -> Result<T>
where F: FnOnce(Option<Value>) -> Result<T>
{
self.fle.get_header().read(name).map_err_into(CEK::StoreWriteError).and_then(f)
}
pub fn load(name: CounterName, store: &Store) -> Result<Counter> {
@ -177,13 +168,13 @@ impl<'a> Counter<'a> {
pub fn delete(name: CounterName, store: &Store) -> Result<()> {
debug!("Deleting counter: '{}'", name);
store.delete(ModuleEntryPath::new(name).into_storeid())
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
.map_err_into(CEK::StoreWriteError)
}
pub fn all_counters(store: &Store) -> Result<CounterIterator> {
store.retrieve_for_module("counter")
.map(|iter| CounterIterator::new(store, iter))
.map_err(|e| CE::new(CEK::StoreReadError, Some(Box::new(e))))
.map_err_into(CEK::StoreReadError)
}
}