2022-09-07 06:10:14 +08:00
|
|
|
//! Utility functions on the DBs. Mainly getter and setters.
|
|
|
|
|
2022-10-11 23:42:43 +08:00
|
|
|
use meilisearch_types::heed::{types::DecodeIgnore, RoTxn, RwTxn};
|
|
|
|
use meilisearch_types::milli::BEU32;
|
2022-10-18 17:02:46 +08:00
|
|
|
use roaring::{MultiOps, RoaringBitmap};
|
2022-09-07 06:10:14 +08:00
|
|
|
|
2022-10-12 06:43:24 +08:00
|
|
|
use crate::{Error, IndexScheduler, Result, Task, TaskId};
|
|
|
|
use meilisearch_types::tasks::{Kind, Status};
|
2022-09-07 06:10:14 +08:00
|
|
|
|
|
|
|
impl IndexScheduler {
|
2022-10-17 18:58:20 +08:00
|
|
|
pub(crate) fn all_task_ids(&self, rtxn: &RoTxn) -> Result<RoaringBitmap> {
|
2022-10-18 17:02:46 +08:00
|
|
|
enum_iterator::all()
|
|
|
|
.map(|s| self.get_status(&rtxn, s))
|
2022-10-19 17:34:05 +08:00
|
|
|
.union()
|
2022-10-17 18:58:20 +08:00
|
|
|
}
|
2022-10-18 17:02:46 +08:00
|
|
|
|
2022-09-07 17:21:53 +08:00
|
|
|
pub(crate) fn last_task_id(&self, rtxn: &RoTxn) -> Result<Option<TaskId>> {
|
|
|
|
Ok(self
|
|
|
|
.all_tasks
|
|
|
|
.remap_data_type::<DecodeIgnore>()
|
|
|
|
.last(rtxn)?
|
|
|
|
.map(|(k, _)| k.get() + 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn next_task_id(&self, rtxn: &RoTxn) -> Result<TaskId> {
|
|
|
|
Ok(self.last_task_id(rtxn)?.unwrap_or_default())
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:10:14 +08:00
|
|
|
pub(crate) fn get_task(&self, rtxn: &RoTxn, task_id: TaskId) -> Result<Option<Task>> {
|
|
|
|
Ok(self.all_tasks.get(rtxn, &BEU32::new(task_id))?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert an iterator to a `Vec` of tasks. The tasks MUST exist or a
|
|
|
|
/// `CorruptedTaskQueue` error will be throwed.
|
|
|
|
pub(crate) fn get_existing_tasks(
|
|
|
|
&self,
|
|
|
|
rtxn: &RoTxn,
|
|
|
|
tasks: impl IntoIterator<Item = TaskId>,
|
|
|
|
) -> Result<Vec<Task>> {
|
|
|
|
tasks
|
|
|
|
.into_iter()
|
|
|
|
.map(|task_id| {
|
|
|
|
self.get_task(rtxn, task_id)
|
|
|
|
.and_then(|task| task.ok_or(Error::CorruptedTaskQueue))
|
|
|
|
})
|
|
|
|
.collect::<Result<_>>()
|
|
|
|
}
|
|
|
|
|
2022-09-16 07:58:08 +08:00
|
|
|
pub(crate) fn update_task(&self, wtxn: &mut RwTxn, task: &Task) -> Result<()> {
|
2022-09-08 02:08:07 +08:00
|
|
|
let old_task = self
|
|
|
|
.get_task(wtxn, task.uid)?
|
|
|
|
.ok_or(Error::CorruptedTaskQueue)?;
|
|
|
|
|
2022-09-26 19:46:34 +08:00
|
|
|
debug_assert_eq!(old_task.uid, task.uid);
|
|
|
|
|
|
|
|
if old_task == *task {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-09-08 02:08:07 +08:00
|
|
|
if old_task.status != task.status {
|
2022-09-08 02:44:33 +08:00
|
|
|
self.update_status(wtxn, old_task.status, |bitmap| {
|
2022-09-08 02:08:07 +08:00
|
|
|
bitmap.remove(task.uid);
|
|
|
|
})?;
|
2022-09-08 02:44:33 +08:00
|
|
|
self.update_status(wtxn, task.status, |bitmap| {
|
2022-09-08 02:08:07 +08:00
|
|
|
bitmap.insert(task.uid);
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if old_task.kind.as_kind() != task.kind.as_kind() {
|
2022-09-08 02:44:33 +08:00
|
|
|
self.update_kind(wtxn, old_task.kind.as_kind(), |bitmap| {
|
2022-09-08 02:08:07 +08:00
|
|
|
bitmap.remove(task.uid);
|
|
|
|
})?;
|
2022-09-08 02:44:33 +08:00
|
|
|
self.update_kind(wtxn, task.kind.as_kind(), |bitmap| {
|
2022-09-08 02:08:07 +08:00
|
|
|
bitmap.insert(task.uid);
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:29:37 +08:00
|
|
|
self.all_tasks.put(wtxn, &BEU32::new(task.uid), task)?;
|
2022-09-08 02:08:07 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-13 17:07:36 +08:00
|
|
|
/// Returns the whole set of tasks that belongs to this index.
|
|
|
|
pub(crate) fn index_tasks(&self, rtxn: &RoTxn, index: &str) -> Result<RoaringBitmap> {
|
2022-10-03 21:29:37 +08:00
|
|
|
Ok(self.index_tasks.get(rtxn, index)?.unwrap_or_default())
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn put_index(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
index: &str,
|
|
|
|
bitmap: &RoaringBitmap,
|
|
|
|
) -> Result<()> {
|
|
|
|
Ok(self.index_tasks.put(wtxn, index, bitmap)?)
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:22:58 +08:00
|
|
|
pub(crate) fn update_index(
|
2022-09-07 06:10:14 +08:00
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
2022-09-07 06:22:58 +08:00
|
|
|
index: &str,
|
2022-09-08 02:44:33 +08:00
|
|
|
f: impl Fn(&mut RoaringBitmap),
|
2022-09-07 06:10:14 +08:00
|
|
|
) -> Result<()> {
|
2022-10-13 17:07:36 +08:00
|
|
|
let mut tasks = self.index_tasks(wtxn, index)?;
|
2022-09-08 02:44:33 +08:00
|
|
|
f(&mut tasks);
|
2022-09-07 06:22:58 +08:00
|
|
|
self.put_index(wtxn, index, &tasks)?;
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:22:58 +08:00
|
|
|
pub(crate) fn get_status(&self, rtxn: &RoTxn, status: Status) -> Result<RoaringBitmap> {
|
2022-10-03 21:29:37 +08:00
|
|
|
Ok(self.status.get(rtxn, &status)?.unwrap_or_default())
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:22:58 +08:00
|
|
|
pub(crate) fn put_status(
|
2022-09-07 06:10:14 +08:00
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
2022-09-07 06:22:58 +08:00
|
|
|
status: Status,
|
2022-09-07 06:10:14 +08:00
|
|
|
bitmap: &RoaringBitmap,
|
|
|
|
) -> Result<()> {
|
2022-09-07 06:22:58 +08:00
|
|
|
Ok(self.status.put(wtxn, &status, bitmap)?)
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn update_status(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
status: Status,
|
2022-09-08 02:44:33 +08:00
|
|
|
f: impl Fn(&mut RoaringBitmap),
|
2022-09-07 06:10:14 +08:00
|
|
|
) -> Result<()> {
|
2022-10-03 21:29:37 +08:00
|
|
|
let mut tasks = self.get_status(wtxn, status)?;
|
2022-09-08 02:44:33 +08:00
|
|
|
f(&mut tasks);
|
2022-09-07 06:10:14 +08:00
|
|
|
self.put_status(wtxn, status, &tasks)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:22:58 +08:00
|
|
|
pub(crate) fn get_kind(&self, rtxn: &RoTxn, kind: Kind) -> Result<RoaringBitmap> {
|
2022-10-03 21:29:37 +08:00
|
|
|
Ok(self.kind.get(rtxn, &kind)?.unwrap_or_default())
|
2022-09-07 06:22:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn put_kind(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
kind: Kind,
|
|
|
|
bitmap: &RoaringBitmap,
|
|
|
|
) -> Result<()> {
|
|
|
|
Ok(self.kind.put(wtxn, &kind, bitmap)?)
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:10:14 +08:00
|
|
|
pub(crate) fn update_kind(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
kind: Kind,
|
2022-09-08 02:44:33 +08:00
|
|
|
f: impl Fn(&mut RoaringBitmap),
|
2022-09-07 06:10:14 +08:00
|
|
|
) -> Result<()> {
|
2022-10-03 21:29:37 +08:00
|
|
|
let mut tasks = self.get_kind(wtxn, kind)?;
|
2022-09-08 02:44:33 +08:00
|
|
|
f(&mut tasks);
|
2022-09-07 06:10:14 +08:00
|
|
|
self.put_kind(wtxn, kind, &tasks)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|