2019-10-18 13:05:28 +02:00
|
|
|
use super::BEU64;
|
|
|
|
use crate::update::Update;
|
2019-10-21 12:05:53 +02:00
|
|
|
use heed::types::{OwnedType, SerdeJson};
|
|
|
|
use heed::Result as ZResult;
|
2019-10-17 12:31:46 +02:00
|
|
|
|
2019-10-03 15:04:11 +02:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Updates {
|
2019-10-21 12:05:53 +02:00
|
|
|
pub(crate) updates: heed::Database<OwnedType<BEU64>, SerdeJson<Update>>,
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Updates {
|
2019-10-16 17:05:24 +02:00
|
|
|
// TODO do not trigger deserialize if possible
|
2019-10-21 12:05:53 +02:00
|
|
|
pub fn last_update_id(self, reader: &heed::RoTxn) -> ZResult<Option<(u64, Update)>> {
|
2019-10-16 17:05:24 +02:00
|
|
|
match self.updates.last(reader)? {
|
|
|
|
Some((key, data)) => Ok(Some((key.get(), data))),
|
|
|
|
None => Ok(None),
|
2019-10-03 16:39:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
// TODO do not trigger deserialize if possible
|
2019-10-21 12:05:53 +02:00
|
|
|
fn first_update_id(self, reader: &heed::RoTxn) -> ZResult<Option<(u64, Update)>> {
|
2019-10-16 17:05:24 +02:00
|
|
|
match self.updates.first(reader)? {
|
|
|
|
Some((key, data)) => Ok(Some((key.get(), data))),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
2019-10-07 15:00:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
// TODO do not trigger deserialize if possible
|
2019-10-31 11:13:37 +01:00
|
|
|
pub fn get(self, reader: &heed::RoTxn, update_id: u64) -> ZResult<Option<Update>> {
|
2019-10-16 17:05:24 +02:00
|
|
|
let update_id = BEU64::new(update_id);
|
2019-10-31 11:13:37 +01:00
|
|
|
self.updates.get(reader, &update_id)
|
2019-10-03 16:54:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
pub fn put_update(
|
2019-10-18 13:21:41 +02:00
|
|
|
self,
|
2019-10-21 12:05:53 +02:00
|
|
|
writer: &mut heed::RwTxn,
|
2019-10-07 16:16:04 +02:00
|
|
|
update_id: u64,
|
2019-10-03 15:04:11 +02:00
|
|
|
update: &Update,
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<()> {
|
2019-10-16 17:05:24 +02:00
|
|
|
// TODO prefer using serde_json?
|
|
|
|
let update_id = BEU64::new(update_id);
|
|
|
|
self.updates.put(writer, &update_id, update)
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:05:53 +02:00
|
|
|
pub fn pop_front(self, writer: &mut heed::RwTxn) -> ZResult<Option<(u64, Update)>> {
|
2019-10-16 17:05:24 +02:00
|
|
|
match self.first_update_id(writer)? {
|
|
|
|
Some((update_id, update)) => {
|
|
|
|
let key = BEU64::new(update_id);
|
|
|
|
self.updates.delete(writer, &key)?;
|
|
|
|
Ok(Some((update_id, update)))
|
2019-10-18 13:05:28 +02:00
|
|
|
}
|
|
|
|
None => Ok(None),
|
2019-10-03 16:39:30 +02:00
|
|
|
}
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
}
|