meilisearch/meilidb-core/src/store/updates_results.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

use std::convert::TryInto;
2019-10-03 22:54:37 +08:00
use rkv::Value;
2019-10-03 23:33:15 +08:00
use crate::{update::UpdateResult, MResult};
2019-10-03 22:13:09 +08:00
#[derive(Copy, Clone)]
pub struct UpdatesResults {
pub(crate) updates_results: rkv::SingleStore,
}
impl UpdatesResults {
// TODO we should use the MDB_LAST op but
// it is not exposed by the rkv library
pub fn last_update_id<'a>(
&self,
reader: &'a impl rkv::Readable,
) -> Result<Option<(u64, Option<Value<'a>>)>, rkv::StoreError>
{
let mut last = None;
let iter = self.updates_results.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-03 22:13:09 +08:00
pub fn put_update_result(
&self,
writer: &mut rkv::Writer,
update_id: u64,
update_result: &UpdateResult,
2019-10-03 23:33:15 +08:00
) -> MResult<()>
2019-10-03 22:13:09 +08:00
{
2019-10-03 22:54:37 +08:00
let update_id_bytes = update_id.to_be_bytes();
2019-10-03 23:33:15 +08:00
let update_result = bincode::serialize(&update_result)?;
2019-10-03 22:54:37 +08:00
let blob = Value::Blob(&update_result);
2019-10-03 23:33:15 +08:00
self.updates_results.put(writer, update_id_bytes, &blob)?;
Ok(())
2019-10-03 22:13:09 +08:00
}
2019-10-04 16:21:09 +08:00
pub fn update_result(
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:13:09 +08:00
update_id: u64,
2019-10-03 23:33:15 +08:00
) -> MResult<Option<UpdateResult>>
2019-10-03 22:13:09 +08:00
{
2019-10-03 22:54:37 +08:00
let update_id_bytes = update_id.to_be_bytes();
match self.updates_results.get(reader, update_id_bytes)? {
Some(Value::Blob(bytes)) => {
2019-10-03 23:33:15 +08:00
let update_result = bincode::deserialize(&bytes)?;
2019-10-03 22:54:37 +08:00
Ok(Some(update_result))
},
Some(value) => panic!("invalid type {:?}", value),
None => Ok(None),
}
2019-10-03 22:13:09 +08:00
}
}