meilisearch/meilisearch-core/src/store/updates.rs

66 lines
2.1 KiB
Rust
Raw Normal View History

2019-10-18 19:05:28 +08:00
use super::BEU64;
use crate::database::UpdateT;
2019-10-18 19:05:28 +08:00
use crate::update::Update;
2019-10-21 18:05:53 +08:00
use heed::types::{OwnedType, SerdeJson};
use heed::Result as ZResult;
2019-10-03 21:04:11 +08:00
#[derive(Copy, Clone)]
pub struct Updates {
2019-10-21 18:05:53 +08:00
pub(crate) updates: heed::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(self, reader: &heed::RoTxn<UpdateT>) -> ZResult<Option<(u64, Update)>> {
2019-10-16 23:05:24 +08:00
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
pub fn first_update(self, reader: &heed::RoTxn<UpdateT>) -> ZResult<Option<(u64, Update)>> {
2019-10-16 23:05:24 +08:00
match self.updates.first(reader)? {
Some((key, data)) => Ok(Some((key.get(), data))),
None => Ok(None),
}
}
2019-10-16 23:05:24 +08:00
// TODO do not trigger deserialize if possible
pub fn get(self, reader: &heed::RoTxn<UpdateT>, update_id: u64) -> ZResult<Option<Update>> {
2019-10-16 23:05:24 +08:00
let update_id = BEU64::new(update_id);
self.updates.get(reader, &update_id)
2019-10-03 22:54:37 +08:00
}
pub fn put_update(
2019-10-18 19:21:41 +08:00
self,
writer: &mut heed::RwTxn<UpdateT>,
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
}
pub fn del_update(self, writer: &mut heed::RwTxn<UpdateT>, update_id: u64) -> ZResult<bool> {
let update_id = BEU64::new(update_id);
self.updates.delete(writer, &update_id)
}
pub fn pop_front(self, writer: &mut heed::RwTxn<UpdateT>) -> ZResult<Option<(u64, Update)>> {
match self.first_update(writer)? {
2019-10-16 23:05:24 +08:00
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
}
2019-11-06 17:49:13 +08:00
pub fn clear(self, writer: &mut heed::RwTxn<UpdateT>) -> ZResult<()> {
2019-11-06 17:49:13 +08:00
self.updates.clear(writer)
}
2019-10-03 21:04:11 +08:00
}