Merge pull request #588 from asuivelentine/libimagcounter/refactor
Libimagcounter/refactor
This commit is contained in:
commit
4dce62030a
1 changed files with 32 additions and 41 deletions
|
@ -11,6 +11,7 @@ use libimagstore::storeid::StoreIdIterator;
|
||||||
use libimagstore::store::FileLockEntry;
|
use libimagstore::store::FileLockEntry;
|
||||||
use libimagstore::storeid::StoreId;
|
use libimagstore::storeid::StoreId;
|
||||||
use libimagstore::storeid::IntoStoreId;
|
use libimagstore::storeid::IntoStoreId;
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
use module_path::ModuleEntryPath;
|
use module_path::ModuleEntryPath;
|
||||||
use result::Result;
|
use result::Result;
|
||||||
|
@ -47,28 +48,25 @@ impl<'a> Counter<'a> {
|
||||||
|
|
||||||
debug!("Creating new counter: '{}' with value: {}", name, init);
|
debug!("Creating new counter: '{}' with value: {}", name, init);
|
||||||
let fle = {
|
let fle = {
|
||||||
let lockentry = store.create(ModuleEntryPath::new(name.clone()).into_storeid());
|
let mut lockentry = try!(store.create(ModuleEntryPath::new(name.clone()).into_storeid())
|
||||||
if lockentry.is_err() {
|
.map_err_into(CEK::StoreWriteError));
|
||||||
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(lockentry.err().unwrap()))));
|
|
||||||
}
|
|
||||||
let mut lockentry = lockentry.unwrap();
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut entry = lockentry.deref_mut();
|
let mut entry = lockentry.deref_mut();
|
||||||
let mut header = entry.get_header_mut();
|
let mut header = entry.get_header_mut();
|
||||||
let setres = header.set("counter", Value::Table(BTreeMap::new()));
|
let setres = header.set("counter", Value::Table(BTreeMap::new()));
|
||||||
if setres.is_err() {
|
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));
|
let setres = header.set("counter.name", Value::String(name));
|
||||||
if setres.is_err() {
|
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));
|
let setres = header.set("counter.value", Value::Integer(init));
|
||||||
if setres.is_err() {
|
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));
|
let setres = header.set("counter.unit", Value::String(u.0));
|
||||||
if setres.is_err() {
|
if setres.is_err() {
|
||||||
self.unit = None;
|
self.unit = None;
|
||||||
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
|
return Err(CEK::StoreWriteError.into_error())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
@ -97,7 +95,7 @@ impl<'a> Counter<'a> {
|
||||||
match header.read("counter.value") {
|
match header.read("counter.value") {
|
||||||
Ok(Some(Value::Integer(i))) => {
|
Ok(Some(Value::Integer(i))) => {
|
||||||
header.set("counter.value", Value::Integer(i + 1))
|
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(|_| ())
|
.map(|_| ())
|
||||||
},
|
},
|
||||||
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
||||||
|
@ -110,7 +108,7 @@ impl<'a> Counter<'a> {
|
||||||
match header.read("counter.value") {
|
match header.read("counter.value") {
|
||||||
Ok(Some(Value::Integer(i))) => {
|
Ok(Some(Value::Integer(i))) => {
|
||||||
header.set("counter.value", Value::Integer(i - 1))
|
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(|_| ())
|
.map(|_| ())
|
||||||
},
|
},
|
||||||
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
||||||
|
@ -119,38 +117,27 @@ impl<'a> Counter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) -> Result<()> {
|
pub fn reset(&mut self) -> Result<()> {
|
||||||
let mut header = self.fle.deref_mut().get_header_mut();
|
self.set(0)
|
||||||
header.set("counter.value", Value::Integer(0))
|
|
||||||
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
|
|
||||||
.map(|_| ())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(&mut self, v: i64) -> Result<()> {
|
pub fn set(&mut self, v: i64) -> Result<()> {
|
||||||
let mut header = self.fle.deref_mut().get_header_mut();
|
let mut header = self.fle.deref_mut().get_header_mut();
|
||||||
header.set("counter.value", Value::Integer(v))
|
header.set("counter.value", Value::Integer(v))
|
||||||
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
|
.map_err_into(CEK::StoreWriteError)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> Result<CounterName> {
|
pub fn name(&self) -> Result<CounterName> {
|
||||||
self.fle.get_header().read("counter.name")
|
self.read_header_at("counter.name", |v| match v {
|
||||||
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
|
|
||||||
.and_then(|v| {
|
|
||||||
match v {
|
|
||||||
Some(Value::String(s)) => Ok(s),
|
Some(Value::String(s)) => Ok(s),
|
||||||
_ => Err(CE::new(CEK::HeaderTypeError, None)),
|
_ => Err(CEK::HeaderTypeError.into_error()),
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> Result<i64> {
|
pub fn value(&self) -> Result<i64> {
|
||||||
self.fle.get_header().read("counter.value")
|
self.read_header_at("counter.value", |v| match v {
|
||||||
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
|
|
||||||
.and_then(|v| {
|
|
||||||
match v {
|
|
||||||
Some(Value::Integer(i)) => Ok(i),
|
Some(Value::Integer(i)) => Ok(i),
|
||||||
_ => Err(CE::new(CEK::HeaderTypeError, None)),
|
_ => Err(CEK::HeaderTypeError.into_error()),
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,15 +146,19 @@ impl<'a> Counter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_unit(&self) -> Result<Option<CounterUnit>> {
|
pub fn read_unit(&self) -> Result<Option<CounterUnit>> {
|
||||||
self.fle.get_header().read("counter.unit")
|
self.read_header_at("counter.unit", |s| match s {
|
||||||
.map_err_into(CEK::StoreReadError)
|
|
||||||
.and_then(|s| match s {
|
|
||||||
Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))),
|
Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))),
|
||||||
Some(_) => Err(CE::new(CEK::HeaderTypeError, None)),
|
Some(_) => Err(CEK::HeaderTypeError.into_error()),
|
||||||
None => Ok(None),
|
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> {
|
pub fn load(name: CounterName, store: &Store) -> Result<Counter> {
|
||||||
debug!("Loading counter: '{}'", name);
|
debug!("Loading counter: '{}'", name);
|
||||||
let id = ModuleEntryPath::new(name).into_storeid();
|
let id = ModuleEntryPath::new(name).into_storeid();
|
||||||
|
@ -177,13 +168,13 @@ impl<'a> Counter<'a> {
|
||||||
pub fn delete(name: CounterName, store: &Store) -> Result<()> {
|
pub fn delete(name: CounterName, store: &Store) -> Result<()> {
|
||||||
debug!("Deleting counter: '{}'", name);
|
debug!("Deleting counter: '{}'", name);
|
||||||
store.delete(ModuleEntryPath::new(name).into_storeid())
|
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> {
|
pub fn all_counters(store: &Store) -> Result<CounterIterator> {
|
||||||
store.retrieve_for_module("counter")
|
store.retrieve_for_module("counter")
|
||||||
.map(|iter| CounterIterator::new(store, iter))
|
.map(|iter| CounterIterator::new(store, iter))
|
||||||
.map_err(|e| CE::new(CEK::StoreReadError, Some(Box::new(e))))
|
.map_err_into(CEK::StoreReadError)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue