2019-10-18 19:05:28 +08:00
|
|
|
use super::BEU64;
|
|
|
|
use crate::update::Update;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2019-10-17 18:31:46 +08:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use zlmdb::types::OwnedType;
|
2019-10-18 19:05:28 +08:00
|
|
|
use zlmdb::{BytesDecode, BytesEncode, Result as ZResult};
|
2019-10-03 21:04:11 +08:00
|
|
|
|
2019-10-17 18:31:46 +08:00
|
|
|
pub struct SerdeJson<T>(std::marker::PhantomData<T>);
|
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
impl<T> BytesEncode for SerdeJson<T>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
2019-10-17 18:31:46 +08:00
|
|
|
type EItem = T;
|
|
|
|
|
|
|
|
fn bytes_encode(item: &Self::EItem) -> Option<Cow<[u8]>> {
|
|
|
|
serde_json::to_vec(item).map(Cow::Owned).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
impl<'a, T: 'a> BytesDecode<'a> for SerdeJson<T>
|
|
|
|
where
|
|
|
|
T: Deserialize<'a> + Clone,
|
|
|
|
{
|
2019-10-17 18:31:46 +08:00
|
|
|
type DItem = T;
|
|
|
|
|
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
|
|
|
|
serde_json::from_slice(bytes).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Updates {
|
2019-10-17 18:31:46 +08:00
|
|
|
pub(crate) updates: zlmdb::Database<OwnedType<BEU64>, SerdeJson<Update>>,
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Updates {
|
2019-10-16 23:05:24 +08:00
|
|
|
// TODO do not trigger deserialize if possible
|
|
|
|
pub fn last_update_id(&self, reader: &zlmdb::RoTxn) -> ZResult<Option<(u64, Update)>> {
|
|
|
|
match self.updates.last(reader)? {
|
|
|
|
Some((key, data)) => Ok(Some((key.get(), data))),
|
|
|
|
None => Ok(None),
|
2019-10-03 22:39:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
// TODO do not trigger deserialize if possible
|
|
|
|
fn first_update_id(&self, reader: &zlmdb::RoTxn) -> ZResult<Option<(u64, Update)>> {
|
|
|
|
match self.updates.first(reader)? {
|
|
|
|
Some((key, data)) => Ok(Some((key.get(), data))),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
2019-10-07 21:00:28 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
// TODO do not trigger deserialize if possible
|
|
|
|
pub fn contains(&self, reader: &zlmdb::RoTxn, update_id: u64) -> ZResult<bool> {
|
|
|
|
let update_id = BEU64::new(update_id);
|
|
|
|
self.updates.get(reader, &update_id).map(|v| v.is_some())
|
2019-10-03 22:54:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn put_update(
|
2019-10-03 21:04:11 +08:00
|
|
|
&self,
|
2019-10-16 23:05:24 +08:00
|
|
|
writer: &mut zlmdb::RwTxn,
|
2019-10-07 22:16:04 +08:00
|
|
|
update_id: u64,
|
2019-10-03 21:04:11 +08:00
|
|
|
update: &Update,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> ZResult<()> {
|
2019-10-16 23:05:24 +08:00
|
|
|
// TODO prefer using serde_json?
|
|
|
|
let update_id = BEU64::new(update_id);
|
|
|
|
self.updates.put(writer, &update_id, update)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
pub fn pop_front(&self, writer: &mut zlmdb::RwTxn) -> ZResult<Option<(u64, Update)>> {
|
|
|
|
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 19:05:28 +08:00
|
|
|
}
|
|
|
|
None => Ok(None),
|
2019-10-03 22:39:30 +08:00
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
}
|