2019-10-03 22:39:30 +08:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use rkv::Value;
|
2019-10-03 23:33:15 +08:00
|
|
|
use crate::{update::Update, MResult};
|
2019-10-03 21:04:11 +08:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Updates {
|
|
|
|
pub(crate) updates: rkv::SingleStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Updates {
|
2019-10-03 22:39:30 +08:00
|
|
|
// TODO we should use the MDB_LAST op but
|
|
|
|
// it is not exposed by the rkv library
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn last_update_id<'a>(
|
2019-10-03 22:39:30 +08:00
|
|
|
&self,
|
2019-10-04 16:21:09 +08:00
|
|
|
reader: &'a impl rkv::Readable,
|
2019-10-03 22:39:30 +08:00
|
|
|
) -> Result<Option<(u64, Option<Value<'a>>)>, rkv::StoreError>
|
|
|
|
{
|
|
|
|
let mut last = None;
|
|
|
|
let iter = self.updates.iter_start(reader)?;
|
|
|
|
for result in iter {
|
|
|
|
let (key, data) = result?;
|
|
|
|
last = Some((key, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
let (last_key, last_data) = match last {
|
|
|
|
Some(entry) => entry,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let array = last_key.try_into().unwrap();
|
|
|
|
let number = u64::from_be_bytes(array);
|
|
|
|
|
|
|
|
Ok(Some((number, last_data)))
|
|
|
|
}
|
|
|
|
|
2019-10-07 21:00:28 +08:00
|
|
|
fn first_update_id<'a>(
|
|
|
|
&self,
|
|
|
|
reader: &'a impl rkv::Readable,
|
|
|
|
) -> Result<Option<(u64, Option<Value<'a>>)>, rkv::StoreError>
|
|
|
|
{
|
|
|
|
let mut iter = self.updates.iter_start(reader)?;
|
|
|
|
let (first_key, first_data) = match iter.next() {
|
|
|
|
Some(result) => result?,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let array = first_key.try_into().unwrap();
|
|
|
|
let number = u64::from_be_bytes(array);
|
|
|
|
|
|
|
|
Ok(Some((number, first_data)))
|
|
|
|
}
|
|
|
|
|
2019-10-04 16:21:09 +08:00
|
|
|
pub fn contains(
|
2019-10-03 22:54:37 +08:00
|
|
|
&self,
|
2019-10-04 16:21:09 +08:00
|
|
|
reader: &impl rkv::Readable,
|
2019-10-03 22:54:37 +08:00
|
|
|
update_id: u64,
|
|
|
|
) -> Result<bool, rkv::StoreError>
|
|
|
|
{
|
|
|
|
let update_id_bytes = update_id.to_be_bytes();
|
|
|
|
self.updates.get(reader, update_id_bytes).map(|v| v.is_some())
|
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn put_update(
|
2019-10-03 21:04:11 +08:00
|
|
|
&self,
|
|
|
|
writer: &mut rkv::Writer,
|
2019-10-07 22:16:04 +08:00
|
|
|
update_id: u64,
|
2019-10-03 21:04:11 +08:00
|
|
|
update: &Update,
|
2019-10-07 22:16:04 +08:00
|
|
|
) -> MResult<()>
|
2019-10-03 21:04:11 +08:00
|
|
|
{
|
2019-10-07 22:16:04 +08:00
|
|
|
let update_id_bytes = update_id.to_be_bytes();
|
2019-10-11 22:16:21 +08:00
|
|
|
let update = serde_json::to_vec(&update)?;
|
2019-10-03 22:39:30 +08:00
|
|
|
let blob = Value::Blob(&update);
|
2019-10-07 22:16:04 +08:00
|
|
|
self.updates.put(writer, update_id_bytes, &blob)?;
|
|
|
|
Ok(())
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 21:00:28 +08:00
|
|
|
pub fn pop_front(
|
2019-10-03 21:04:11 +08:00
|
|
|
&self,
|
2019-10-03 22:13:09 +08:00
|
|
|
writer: &mut rkv::Writer,
|
2019-10-03 23:33:15 +08:00
|
|
|
) -> MResult<Option<(u64, Update)>>
|
2019-10-03 21:04:11 +08:00
|
|
|
{
|
2019-10-07 22:16:04 +08:00
|
|
|
let (first_id, first_data) = match self.first_update_id(writer)? {
|
2019-10-03 22:39:30 +08:00
|
|
|
Some(entry) => entry,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
match first_data {
|
2019-10-03 22:39:30 +08:00
|
|
|
Some(Value::Blob(bytes)) => {
|
2019-10-11 22:16:21 +08:00
|
|
|
let update = serde_json::from_slice(&bytes)?;
|
2019-10-04 23:16:34 +08:00
|
|
|
// remove it from the database now
|
2019-10-07 22:16:04 +08:00
|
|
|
let first_id_bytes = first_id.to_be_bytes();
|
|
|
|
self.updates.delete(writer, first_id_bytes)?;
|
2019-10-04 23:16:34 +08:00
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
Ok(Some((first_id, update)))
|
2019-10-03 22:39:30 +08:00
|
|
|
},
|
|
|
|
Some(value) => panic!("invalid type {:?}", value),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
}
|