2022-09-07 00:10:14 +02:00
|
|
|
//! Utility functions on the DBs. Mainly getter and setters.
|
|
|
|
|
2022-10-19 12:59:12 +02:00
|
|
|
use std::ops::Bound;
|
|
|
|
|
2022-10-20 18:00:07 +02:00
|
|
|
use meilisearch_types::heed::types::{DecodeIgnore, OwnedType};
|
|
|
|
use meilisearch_types::heed::{Database, RoTxn, RwTxn};
|
2022-10-19 12:59:12 +02:00
|
|
|
use meilisearch_types::milli::{CboRoaringBitmapCodec, BEU32};
|
2022-10-20 18:00:07 +02:00
|
|
|
use meilisearch_types::tasks::{Kind, KindWithContent, Status};
|
2022-10-18 11:02:46 +02:00
|
|
|
use roaring::{MultiOps, RoaringBitmap};
|
2022-10-19 12:59:12 +02:00
|
|
|
use time::OffsetDateTime;
|
2022-09-07 00:10:14 +02:00
|
|
|
|
2022-10-19 12:59:12 +02:00
|
|
|
use crate::{Error, IndexScheduler, Result, Task, TaskId, BEI128};
|
2022-09-07 00:10:14 +02:00
|
|
|
|
|
|
|
impl IndexScheduler {
|
2022-10-17 12:58:20 +02:00
|
|
|
pub(crate) fn all_task_ids(&self, rtxn: &RoTxn) -> Result<RoaringBitmap> {
|
2022-10-22 16:35:42 +02:00
|
|
|
enum_iterator::all().map(|s| self.get_status(rtxn, s)).union()
|
2022-10-17 12:58:20 +02:00
|
|
|
}
|
2022-10-18 11:02:46 +02:00
|
|
|
|
2022-09-07 11:21:53 +02:00
|
|
|
pub(crate) fn last_task_id(&self, rtxn: &RoTxn) -> Result<Option<TaskId>> {
|
2022-10-20 18:00:07 +02:00
|
|
|
Ok(self.all_tasks.remap_data_type::<DecodeIgnore>().last(rtxn)?.map(|(k, _)| k.get() + 1))
|
2022-09-07 11:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn next_task_id(&self, rtxn: &RoTxn) -> Result<TaskId> {
|
|
|
|
Ok(self.last_task_id(rtxn)?.unwrap_or_default())
|
|
|
|
}
|
|
|
|
|
2022-09-07 00:10:14 +02: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| {
|
2022-10-20 18:00:07 +02:00
|
|
|
self.get_task(rtxn, task_id).and_then(|task| task.ok_or(Error::CorruptedTaskQueue))
|
2022-09-07 00:10:14 +02:00
|
|
|
})
|
|
|
|
.collect::<Result<_>>()
|
|
|
|
}
|
|
|
|
|
2022-09-16 01:58:08 +02:00
|
|
|
pub(crate) fn update_task(&self, wtxn: &mut RwTxn, task: &Task) -> Result<()> {
|
2022-10-20 18:00:07 +02:00
|
|
|
let old_task = self.get_task(wtxn, task.uid)?.ok_or(Error::CorruptedTaskQueue)?;
|
2022-09-07 20:08:07 +02:00
|
|
|
|
2022-09-26 13:46:34 +02:00
|
|
|
debug_assert_eq!(old_task.uid, task.uid);
|
|
|
|
|
|
|
|
if old_task == *task {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:08:07 +02:00
|
|
|
if old_task.status != task.status {
|
2022-09-07 20:44:33 +02:00
|
|
|
self.update_status(wtxn, old_task.status, |bitmap| {
|
2022-09-07 20:08:07 +02:00
|
|
|
bitmap.remove(task.uid);
|
|
|
|
})?;
|
2022-09-07 20:44:33 +02:00
|
|
|
self.update_status(wtxn, task.status, |bitmap| {
|
2022-09-07 20:08:07 +02:00
|
|
|
bitmap.insert(task.uid);
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if old_task.kind.as_kind() != task.kind.as_kind() {
|
2022-09-07 20:44:33 +02:00
|
|
|
self.update_kind(wtxn, old_task.kind.as_kind(), |bitmap| {
|
2022-09-07 20:08:07 +02:00
|
|
|
bitmap.remove(task.uid);
|
|
|
|
})?;
|
2022-09-07 20:44:33 +02:00
|
|
|
self.update_kind(wtxn, task.kind.as_kind(), |bitmap| {
|
2022-09-07 20:08:07 +02:00
|
|
|
bitmap.insert(task.uid);
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2022-10-19 17:29:51 +02:00
|
|
|
assert_eq!(
|
|
|
|
old_task.enqueued_at, task.enqueued_at,
|
2022-10-19 16:18:00 +02:00
|
|
|
"Cannot update a task's enqueued_at time"
|
|
|
|
);
|
2022-10-19 12:59:12 +02:00
|
|
|
if old_task.started_at != task.started_at {
|
2022-10-20 18:00:07 +02:00
|
|
|
assert!(old_task.started_at.is_none(), "Cannot update a task's started_at time");
|
2022-10-19 12:59:12 +02:00
|
|
|
if let Some(started_at) = task.started_at {
|
|
|
|
insert_task_datetime(wtxn, self.started_at, started_at, task.uid)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if old_task.finished_at != task.finished_at {
|
2022-10-20 18:00:07 +02:00
|
|
|
assert!(old_task.finished_at.is_none(), "Cannot update a task's finished_at time");
|
2022-10-19 12:59:12 +02:00
|
|
|
if let Some(finished_at) = task.finished_at {
|
|
|
|
insert_task_datetime(wtxn, self.finished_at, finished_at, task.uid)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 15:29:37 +02:00
|
|
|
self.all_tasks.put(wtxn, &BEU32::new(task.uid), task)?;
|
2022-09-07 20:08:07 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-13 11:07:36 +02: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 15:29:37 +02:00
|
|
|
Ok(self.index_tasks.get(rtxn, index)?.unwrap_or_default())
|
2022-09-07 00:10:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 00:22:58 +02:00
|
|
|
pub(crate) fn update_index(
|
2022-09-07 00:10:14 +02:00
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
2022-09-07 00:22:58 +02:00
|
|
|
index: &str,
|
2022-09-07 20:44:33 +02:00
|
|
|
f: impl Fn(&mut RoaringBitmap),
|
2022-09-07 00:10:14 +02:00
|
|
|
) -> Result<()> {
|
2022-10-13 11:07:36 +02:00
|
|
|
let mut tasks = self.index_tasks(wtxn, index)?;
|
2022-09-07 20:44:33 +02:00
|
|
|
f(&mut tasks);
|
2022-10-20 09:56:43 +02:00
|
|
|
if tasks.is_empty() {
|
|
|
|
self.index_tasks.delete(wtxn, index)?;
|
|
|
|
} else {
|
|
|
|
self.index_tasks.put(wtxn, index, &tasks)?;
|
|
|
|
}
|
2022-09-07 00:22:58 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-09-07 00:10:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 00:22:58 +02:00
|
|
|
pub(crate) fn get_status(&self, rtxn: &RoTxn, status: Status) -> Result<RoaringBitmap> {
|
2022-10-03 15:29:37 +02:00
|
|
|
Ok(self.status.get(rtxn, &status)?.unwrap_or_default())
|
2022-09-07 00:10:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 00:22:58 +02:00
|
|
|
pub(crate) fn put_status(
|
2022-09-07 00:10:14 +02:00
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
2022-09-07 00:22:58 +02:00
|
|
|
status: Status,
|
2022-09-07 00:10:14 +02:00
|
|
|
bitmap: &RoaringBitmap,
|
|
|
|
) -> Result<()> {
|
2022-09-07 00:22:58 +02:00
|
|
|
Ok(self.status.put(wtxn, &status, bitmap)?)
|
2022-09-07 00:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn update_status(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
status: Status,
|
2022-09-07 20:44:33 +02:00
|
|
|
f: impl Fn(&mut RoaringBitmap),
|
2022-09-07 00:10:14 +02:00
|
|
|
) -> Result<()> {
|
2022-10-03 15:29:37 +02:00
|
|
|
let mut tasks = self.get_status(wtxn, status)?;
|
2022-09-07 20:44:33 +02:00
|
|
|
f(&mut tasks);
|
2022-09-07 00:10:14 +02:00
|
|
|
self.put_status(wtxn, status, &tasks)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-07 00:22:58 +02:00
|
|
|
pub(crate) fn get_kind(&self, rtxn: &RoTxn, kind: Kind) -> Result<RoaringBitmap> {
|
2022-10-03 15:29:37 +02:00
|
|
|
Ok(self.kind.get(rtxn, &kind)?.unwrap_or_default())
|
2022-09-07 00:22:58 +02: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 00:10:14 +02:00
|
|
|
pub(crate) fn update_kind(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
kind: Kind,
|
2022-09-07 20:44:33 +02:00
|
|
|
f: impl Fn(&mut RoaringBitmap),
|
2022-09-07 00:10:14 +02:00
|
|
|
) -> Result<()> {
|
2022-10-03 15:29:37 +02:00
|
|
|
let mut tasks = self.get_kind(wtxn, kind)?;
|
2022-09-07 20:44:33 +02:00
|
|
|
f(&mut tasks);
|
2022-09-07 00:10:14 +02:00
|
|
|
self.put_kind(wtxn, kind, &tasks)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 12:59:12 +02:00
|
|
|
|
|
|
|
pub(crate) fn insert_task_datetime(
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
database: Database<OwnedType<BEI128>, CboRoaringBitmapCodec>,
|
|
|
|
time: OffsetDateTime,
|
|
|
|
task_id: TaskId,
|
|
|
|
) -> Result<()> {
|
|
|
|
let timestamp = BEI128::new(time.unix_timestamp_nanos());
|
2022-10-22 16:35:42 +02:00
|
|
|
let mut task_ids = database.get(wtxn, ×tamp)?.unwrap_or_default();
|
2022-10-19 12:59:12 +02:00
|
|
|
task_ids.insert(task_id);
|
|
|
|
database.put(wtxn, ×tamp, &RoaringBitmap::from_iter([task_id]))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-19 16:13:14 +02:00
|
|
|
|
2022-10-19 12:59:12 +02:00
|
|
|
pub(crate) fn remove_task_datetime(
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
database: Database<OwnedType<BEI128>, CboRoaringBitmapCodec>,
|
|
|
|
time: OffsetDateTime,
|
|
|
|
task_id: TaskId,
|
|
|
|
) -> Result<()> {
|
|
|
|
let timestamp = BEI128::new(time.unix_timestamp_nanos());
|
2022-10-22 16:35:42 +02:00
|
|
|
if let Some(mut existing) = database.get(wtxn, ×tamp)? {
|
2022-10-19 12:59:12 +02:00
|
|
|
existing.remove(task_id);
|
|
|
|
if existing.is_empty() {
|
|
|
|
database.delete(wtxn, ×tamp)?;
|
|
|
|
} else {
|
|
|
|
database.put(wtxn, ×tamp, &RoaringBitmap::from_iter([task_id]))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-19 16:13:14 +02:00
|
|
|
|
2022-10-19 12:59:12 +02:00
|
|
|
pub(crate) fn keep_tasks_within_datetimes(
|
|
|
|
rtxn: &RoTxn,
|
|
|
|
tasks: &mut RoaringBitmap,
|
|
|
|
database: Database<OwnedType<BEI128>, CboRoaringBitmapCodec>,
|
|
|
|
after: Option<OffsetDateTime>,
|
|
|
|
before: Option<OffsetDateTime>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let (start, end) = match (&after, &before) {
|
|
|
|
(None, None) => return Ok(()),
|
|
|
|
(None, Some(before)) => (Bound::Unbounded, Bound::Excluded(*before)),
|
|
|
|
(Some(after), None) => (Bound::Excluded(*after), Bound::Unbounded),
|
|
|
|
(Some(after), Some(before)) => (Bound::Excluded(*after), Bound::Excluded(*before)),
|
|
|
|
};
|
|
|
|
let mut collected_task_ids = RoaringBitmap::new();
|
|
|
|
let start = map_bound(start, |b| BEI128::new(b.unix_timestamp_nanos()));
|
|
|
|
let end = map_bound(end, |b| BEI128::new(b.unix_timestamp_nanos()));
|
2022-10-22 16:35:42 +02:00
|
|
|
let iter = database.range(rtxn, &(start, end))?;
|
2022-10-19 12:59:12 +02:00
|
|
|
for r in iter {
|
|
|
|
let (_timestamp, task_ids) = r?;
|
|
|
|
collected_task_ids |= task_ids;
|
|
|
|
}
|
|
|
|
*tasks &= collected_task_ids;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove when Bound::map ( https://github.com/rust-lang/rust/issues/86026 ) is available on stable
|
|
|
|
fn map_bound<T, U>(bound: Bound<T>, map: impl FnOnce(T) -> U) -> Bound<U> {
|
|
|
|
match bound {
|
|
|
|
Bound::Included(x) => Bound::Included(map(x)),
|
|
|
|
Bound::Excluded(x) => Bound::Excluded(map(x)),
|
|
|
|
Bound::Unbounded => Bound::Unbounded,
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 16:30:18 +02:00
|
|
|
|
|
|
|
pub fn swap_index_uid_in_task(task: &mut Task, swap: (&str, &str)) {
|
|
|
|
use KindWithContent as K;
|
|
|
|
let mut index_uids = vec![];
|
|
|
|
match &mut task.kind {
|
2022-10-21 18:03:10 +02:00
|
|
|
K::DocumentAdditionOrUpdate { index_uid, .. } => index_uids.push(index_uid),
|
2022-10-17 16:30:18 +02:00
|
|
|
K::DocumentDeletion { index_uid, .. } => index_uids.push(index_uid),
|
|
|
|
K::DocumentClear { index_uid } => index_uids.push(index_uid),
|
2022-10-21 18:03:10 +02:00
|
|
|
K::SettingsUpdate { index_uid, .. } => index_uids.push(index_uid),
|
2022-10-17 16:30:18 +02:00
|
|
|
K::IndexDeletion { index_uid } => index_uids.push(index_uid),
|
|
|
|
K::IndexCreation { index_uid, .. } => index_uids.push(index_uid),
|
|
|
|
K::IndexUpdate { index_uid, .. } => index_uids.push(index_uid),
|
|
|
|
K::IndexSwap { swaps } => {
|
|
|
|
for (lhs, rhs) in swaps.iter_mut() {
|
2022-10-22 16:35:42 +02:00
|
|
|
if lhs == swap.0 || lhs == swap.1 {
|
2022-10-17 16:30:18 +02:00
|
|
|
index_uids.push(lhs);
|
|
|
|
}
|
2022-10-22 16:35:42 +02:00
|
|
|
if rhs == swap.0 || rhs == swap.1 {
|
2022-10-17 16:30:18 +02:00
|
|
|
index_uids.push(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 19:08:15 +02:00
|
|
|
K::TaskCancelation { .. }
|
|
|
|
| K::TaskDeletion { .. }
|
|
|
|
| K::DumpCreation { .. }
|
|
|
|
| K::Snapshot => {}
|
2022-10-17 16:30:18 +02:00
|
|
|
};
|
|
|
|
for index_uid in index_uids {
|
2022-10-22 16:35:42 +02:00
|
|
|
if index_uid == swap.0 {
|
2022-10-17 16:30:18 +02:00
|
|
|
*index_uid = swap.1.to_owned();
|
2022-10-22 16:35:42 +02:00
|
|
|
} else if index_uid == swap.1 {
|
2022-10-17 16:30:18 +02:00
|
|
|
*index_uid = swap.0.to_owned();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|