2016-02-13 18:51:52 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
2016-07-16 16:09:30 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Display;
|
2016-02-13 18:51:52 +00:00
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
2016-03-13 15:33:30 +00:00
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
2016-02-13 18:51:52 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::storeid::StoreId;
|
|
|
|
use libimagstore::storeid::IntoStoreId;
|
2016-07-27 17:09:41 +00:00
|
|
|
use libimagerror::into::IntoError;
|
2016-02-13 18:51:52 +00:00
|
|
|
|
|
|
|
use module_path::ModuleEntryPath;
|
|
|
|
use result::Result;
|
|
|
|
use error::CounterError as CE;
|
|
|
|
use error::CounterErrorKind as CEK;
|
2016-07-18 16:53:54 +00:00
|
|
|
use error::error::MapErrInto;
|
2016-02-13 18:51:52 +00:00
|
|
|
|
|
|
|
pub type CounterName = String;
|
2016-07-16 16:09:30 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
|
2016-07-17 14:26:11 +00:00
|
|
|
pub struct CounterUnit(String);
|
2016-07-16 16:09:30 +00:00
|
|
|
|
|
|
|
impl Display for CounterUnit {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "({})", self.0)
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 18:51:52 +00:00
|
|
|
|
2016-07-17 14:26:11 +00:00
|
|
|
impl CounterUnit {
|
|
|
|
pub fn new<S: Into<String>>(unit: S) -> CounterUnit {
|
|
|
|
CounterUnit(unit.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 18:51:52 +00:00
|
|
|
pub struct Counter<'a> {
|
|
|
|
fle: FileLockEntry<'a>,
|
2016-07-18 15:05:02 +00:00
|
|
|
unit: Option<CounterUnit>,
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Counter<'a> {
|
|
|
|
|
2016-07-18 15:05:02 +00:00
|
|
|
pub fn new(store: &Store, name: CounterName, init: i64) -> Result<Counter> {
|
2016-02-13 18:51:52 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
|
|
debug!("Creating new counter: '{}' with value: {}", name, init);
|
|
|
|
let fle = {
|
2016-08-25 16:19:40 +00:00
|
|
|
let id = try!(ModuleEntryPath::new(name.clone())
|
|
|
|
.into_storeid()
|
|
|
|
.map_err_into(CEK::StoreWriteError));
|
|
|
|
let mut lockentry = try!(store.create(id).map_err_into(CEK::StoreWriteError));
|
2016-02-13 18:51:52 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
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() {
|
2016-07-27 17:09:41 +00:00
|
|
|
return Err(CEK::StoreWriteError.into_error());
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let setres = header.set("counter.name", Value::String(name));
|
|
|
|
if setres.is_err() {
|
2016-07-27 17:09:41 +00:00
|
|
|
return Err(CEK::StoreWriteError.into_error())
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 15:05:02 +00:00
|
|
|
let setres = header.set("counter.value", Value::Integer(init));
|
2016-07-14 17:25:58 +00:00
|
|
|
if setres.is_err() {
|
2016-07-27 17:09:41 +00:00
|
|
|
return Err(CEK::StoreWriteError.into_error())
|
2016-07-14 17:25:58 +00:00
|
|
|
}
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lockentry
|
|
|
|
};
|
|
|
|
|
2016-07-18 15:05:02 +00:00
|
|
|
Ok(Counter { fle: fle, unit: None })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_unit(mut self, unit: Option<CounterUnit>) -> Result<Counter<'a>> {
|
|
|
|
self.unit = unit;
|
|
|
|
|
|
|
|
if let Some(u) = self.unit.clone() {
|
|
|
|
let mut header = self.fle.deref_mut().get_header_mut();
|
|
|
|
let setres = header.set("counter.unit", Value::String(u.0));
|
|
|
|
if setres.is_err() {
|
2016-07-18 16:53:54 +00:00
|
|
|
self.unit = None;
|
2016-07-27 17:09:41 +00:00
|
|
|
return Err(CEK::StoreWriteError.into_error())
|
2016-07-18 15:05:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(self)
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inc(&mut self) -> Result<()> {
|
|
|
|
let mut header = self.fle.deref_mut().get_header_mut();
|
|
|
|
match header.read("counter.value") {
|
|
|
|
Ok(Some(Value::Integer(i))) => {
|
|
|
|
header.set("counter.value", Value::Integer(i + 1))
|
2016-07-27 17:09:41 +00:00
|
|
|
.map_err_into(CEK::StoreWriteError)
|
2016-02-13 18:51:52 +00:00
|
|
|
.map(|_| ())
|
|
|
|
},
|
|
|
|
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
|
|
|
_ => Err(CE::new(CEK::StoreReadError, None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dec(&mut self) -> Result<()> {
|
|
|
|
let mut header = self.fle.deref_mut().get_header_mut();
|
|
|
|
match header.read("counter.value") {
|
|
|
|
Ok(Some(Value::Integer(i))) => {
|
|
|
|
header.set("counter.value", Value::Integer(i - 1))
|
2016-07-27 17:09:41 +00:00
|
|
|
.map_err_into(CEK::StoreWriteError)
|
2016-02-13 18:51:52 +00:00
|
|
|
.map(|_| ())
|
|
|
|
},
|
|
|
|
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
|
|
|
_ => Err(CE::new(CEK::StoreReadError, None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self) -> Result<()> {
|
2016-07-27 16:54:09 +00:00
|
|
|
self.set(0)
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2016-07-27 17:09:41 +00:00
|
|
|
.map_err_into(CEK::StoreWriteError)
|
2016-02-13 18:51:52 +00:00
|
|
|
.map(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> Result<CounterName> {
|
2016-07-27 17:09:41 +00:00
|
|
|
self.read_header_at("counter.name", |v| match v {
|
|
|
|
Some(Value::String(s)) => Ok(s),
|
|
|
|
_ => Err(CEK::HeaderTypeError.into_error()),
|
|
|
|
})
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> Result<i64> {
|
2016-07-27 17:09:41 +00:00
|
|
|
self.read_header_at("counter.value", |v| match v {
|
|
|
|
Some(Value::Integer(i)) => Ok(i),
|
|
|
|
_ => Err(CEK::HeaderTypeError.into_error()),
|
|
|
|
})
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 16:53:54 +00:00
|
|
|
pub fn unit(&self) -> Option<&CounterUnit> {
|
|
|
|
self.unit.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_unit(&self) -> Result<Option<CounterUnit>> {
|
2016-07-27 17:09:41 +00:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-25 16:19:40 +00:00
|
|
|
fn read_header_at<T, F>(&self, name: &str, f: F) -> Result<T>
|
2016-07-27 17:09:41 +00:00
|
|
|
where F: FnOnce(Option<Value>) -> Result<T>
|
|
|
|
{
|
|
|
|
self.fle.get_header().read(name).map_err_into(CEK::StoreWriteError).and_then(f)
|
2016-07-14 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 18:51:52 +00:00
|
|
|
pub fn load(name: CounterName, store: &Store) -> Result<Counter> {
|
|
|
|
debug!("Loading counter: '{}'", name);
|
2016-08-25 16:19:40 +00:00
|
|
|
let id = try!(ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.map_err_into(CEK::StoreWriteError));
|
2016-07-18 15:05:02 +00:00
|
|
|
Counter::from_storeid(store, id)
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(name: CounterName, store: &Store) -> Result<()> {
|
|
|
|
debug!("Deleting counter: '{}'", name);
|
2016-08-25 16:19:40 +00:00
|
|
|
let id = try!(ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.map_err_into(CEK::StoreWriteError));
|
|
|
|
store.delete(id).map_err_into(CEK::StoreWriteError)
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
2016-03-13 15:33:30 +00:00
|
|
|
|
|
|
|
pub fn all_counters(store: &Store) -> Result<CounterIterator> {
|
|
|
|
store.retrieve_for_module("counter")
|
|
|
|
.map(|iter| CounterIterator::new(store, iter))
|
2016-07-27 17:09:41 +00:00
|
|
|
.map_err_into(CEK::StoreReadError)
|
2016-03-13 15:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
trait FromStoreId {
|
2016-05-02 22:57:41 +00:00
|
|
|
fn from_storeid(&Store, StoreId) -> Result<Counter>;
|
2016-03-13 15:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromStoreId for Counter<'a> {
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
fn from_storeid(store: &Store, id: StoreId) -> Result<Counter> {
|
2016-03-13 15:33:30 +00:00
|
|
|
debug!("Loading counter from storeid: '{:?}'", id);
|
|
|
|
match store.retrieve(id) {
|
|
|
|
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
|
2016-07-18 15:05:02 +00:00
|
|
|
Ok(c) => {
|
|
|
|
let mut counter = Counter { fle: c, unit: None };
|
2016-07-18 16:53:54 +00:00
|
|
|
counter.read_unit()
|
|
|
|
.map_err_into(CEK::StoreReadError)
|
|
|
|
.and_then(|u| {
|
|
|
|
counter.unit = u;
|
2016-08-25 16:19:40 +00:00
|
|
|
Ok(counter)
|
2016-07-18 16:53:54 +00:00
|
|
|
})
|
2016-07-18 15:05:02 +00:00
|
|
|
}
|
2016-03-13 15:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CounterIterator<'a> {
|
|
|
|
store: &'a Store,
|
|
|
|
iditer: StoreIdIterator,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CounterIterator<'a> {
|
|
|
|
|
|
|
|
pub fn new(store: &'a Store, iditer: StoreIdIterator) -> CounterIterator<'a> {
|
|
|
|
CounterIterator {
|
|
|
|
store: store,
|
|
|
|
iditer: iditer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for CounterIterator<'a> {
|
|
|
|
type Item = Result<Counter<'a>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<Counter<'a>>> {
|
|
|
|
self.iditer
|
|
|
|
.next()
|
|
|
|
.map(|id| Counter::from_storeid(self.store, id))
|
|
|
|
}
|
|
|
|
|
2016-02-13 18:51:52 +00:00
|
|
|
}
|
|
|
|
|