Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a binary heap instead of a vecdeque to make our priority list #1892

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions meilisearch-lib/src/tasks/task_store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod store;

use std::collections::{HashSet, VecDeque};
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};
use std::path::Path;
use std::sync::Arc;

Expand Down Expand Up @@ -43,7 +44,7 @@ impl TaskFilter {

pub struct TaskStore {
store: Arc<Store>,
pending_queue: Arc<RwLock<VecDeque<TaskId>>>,
pending_queue: Arc<RwLock<BinaryHeap<Reverse<TaskId>>>>,
}

impl Clone for TaskStore {
Expand Down Expand Up @@ -87,21 +88,17 @@ impl TaskStore {
})
.await??;

self.pending_queue.write().await.push_back(task.id);
self.pending_queue.write().await.push(Reverse(task.id));

Ok(task)
}

// Returns the next task to process.
pub async fn peek_pending(&self) -> Option<TaskId> {
self.pending_queue.read().await.front().copied()
self.pending_queue.read().await.peek().map(|rid| rid.0)
}

pub async fn get_task(
&self,
id: TaskId,
filter: Option<TaskFilter>,
) -> Result<Task> {
pub async fn get_task(&self, id: TaskId, filter: Option<TaskFilter>) -> Result<Task> {
let store = self.store.clone();
let task = tokio::task::spawn_blocking(move || -> Result<_> {
let txn = store.rtxn()?;
Expand All @@ -112,7 +109,10 @@ impl TaskStore {
.ok_or(TaskError::UnexistingTask(id))?;

match filter {
Some(filter) => filter.pass(&task).then(|| task).ok_or(TaskError::UnexistingTask(id)),
Some(filter) => filter
.pass(&task)
.then(|| task)
.ok_or(TaskError::UnexistingTask(id)),
None => Ok(task),
}
}
Expand All @@ -137,7 +137,14 @@ impl TaskStore {
.await??;

let mut pending_queue = pending_queue.write().await;
pending_queue.retain(|id| !to_remove.contains(id));

// currently retain is not stable: https://doc.rust-lang.org/stable/std/collections/struct.BinaryHeap.html#method.retain
// pending_queue.retain(|id| !to_remove.contains(&id.0));

*pending_queue = pending_queue
.drain()
.filter(|id| !to_remove.contains(&id.0))
.collect();

Ok(())
}
Expand Down Expand Up @@ -197,17 +204,10 @@ pub mod test {
}
}

pub async fn get_task(
&self,
id: TaskId,
filter: Option<TaskFilter>,
) -> Result<Task> {
pub async fn get_task(&self, id: TaskId, filter: Option<TaskFilter>) -> Result<Task> {
match self {
Self::Real(s) => s.get_task(id, filter).await,
Self::Mock(m) => unsafe {
m.get::<_, Result<Task>>("get_task")
.call((id, filter))
},
Self::Mock(m) => unsafe { m.get::<_, Result<Task>>("get_task").call((id, filter)) },
}
}

Expand Down