make Task index_uid an option

Not all task relate to an index. Tasks that don't have an index_uid set
to None
This commit is contained in:
ad hoc 2022-05-16 19:50:45 +02:00
parent 9935db86c7
commit aa50acb031
No known key found for this signature in database
GPG key ID: 4F00A782990CC643
8 changed files with 79 additions and 35 deletions

View file

@ -30,10 +30,14 @@ pub struct TaskFilter {
impl TaskFilter {
fn pass(&self, task: &Task) -> bool {
self.indexes
.as_ref()
.map(|indexes| indexes.contains(&*task.index_uid))
.unwrap_or(true)
match task.index_uid {
Some(ref index_uid) => self
.indexes
.as_ref()
.map(|indexes| indexes.contains(index_uid.as_str()))
.unwrap_or(true),
None => false,
}
}
/// Adds an index to the filter, so the filter must match this index.
@ -66,7 +70,11 @@ impl TaskStore {
Ok(Self { store })
}
pub async fn register(&self, index_uid: IndexUid, content: TaskContent) -> Result<Task> {
pub async fn register(
&self,
index_uid: Option<IndexUid>,
content: TaskContent,
) -> Result<Task> {
debug!("registering update: {:?}", content);
let store = self.store.clone();
let task = tokio::task::spawn_blocking(move || -> Result<Task> {
@ -305,7 +313,11 @@ pub mod test {
}
}
pub async fn register(&self, index_uid: IndexUid, content: TaskContent) -> Result<Task> {
pub async fn register(
&self,
index_uid: Option<IndexUid>,
content: TaskContent,
) -> Result<Task> {
match self {
Self::Real(s) => s.register(index_uid, content).await,
Self::Mock(_m) => todo!(),
@ -335,7 +347,7 @@ pub mod test {
let gen_task = |id: TaskId| Task {
id,
index_uid: IndexUid::new_unchecked("test"),
index_uid: Some(IndexUid::new_unchecked("test")),
content: TaskContent::IndexCreation { primary_key: None },
events: Vec::new(),
};

View file

@ -109,7 +109,8 @@ impl Store {
pub fn put(&self, txn: &mut RwTxn, task: &Task) -> Result<()> {
self.tasks.put(txn, &BEU64::new(task.id), task)?;
self.uids_task_ids
.put(txn, &(&task.index_uid, task.id), &())?;
// TODO(marin): The index uid should be remaped to a task queue identifier here
.put(txn, &(&task.index_uid.as_ref().unwrap(), task.id), &())?;
Ok(())
}
@ -325,7 +326,7 @@ pub mod test {
let tasks = (0..100)
.map(|_| Task {
id: rand::random(),
index_uid: IndexUid::new_unchecked("test"),
index_uid: Some(IndexUid::new_unchecked("test")),
content: TaskContent::IndexDeletion,
events: vec![],
})
@ -356,14 +357,14 @@ pub mod test {
let task_1 = Task {
id: 1,
index_uid: IndexUid::new_unchecked("test"),
index_uid: Some(IndexUid::new_unchecked("test")),
content: TaskContent::IndexDeletion,
events: vec![],
};
let task_2 = Task {
id: 0,
index_uid: IndexUid::new_unchecked("test1"),
index_uid: Some(IndexUid::new_unchecked("test1")),
content: TaskContent::IndexDeletion,
events: vec![],
};
@ -379,18 +380,28 @@ pub mod test {
txn.abort().unwrap();
assert_eq!(tasks.len(), 1);
assert_eq!(&*tasks.first().unwrap().index_uid, "test");
assert_eq!(
tasks
.first()
.as_ref()
.unwrap()
.index_uid
.as_ref()
.unwrap()
.as_str(),
"test"
);
// same thing but invert the ids
let task_1 = Task {
id: 0,
index_uid: IndexUid::new_unchecked("test"),
index_uid: Some(IndexUid::new_unchecked("test")),
content: TaskContent::IndexDeletion,
events: vec![],
};
let task_2 = Task {
id: 1,
index_uid: IndexUid::new_unchecked("test1"),
index_uid: Some(IndexUid::new_unchecked("test1")),
content: TaskContent::IndexDeletion,
events: vec![],
};
@ -405,7 +416,17 @@ pub mod test {
let tasks = store.list_tasks(&txn, None, Some(filter), None).unwrap();
assert_eq!(tasks.len(), 1);
assert_eq!(&*tasks.first().unwrap().index_uid, "test");
assert_eq!(
&*tasks
.first()
.as_ref()
.unwrap()
.index_uid
.as_ref()
.unwrap()
.as_str(),
"test"
);
}
proptest! {