Introduce update_task, popping an update and pushing the result of it

This commit is contained in:
Clément Renault 2019-10-04 17:17:43 +02:00
parent 29229b2137
commit df2ef8d2e1

View File

@ -4,25 +4,23 @@ mod documents_deletion;
pub use self::documents_addition::{DocumentsAddition, apply_documents_addition}; pub use self::documents_addition::{DocumentsAddition, apply_documents_addition};
pub use self::documents_deletion::{DocumentsDeletion, apply_documents_deletion}; pub use self::documents_deletion::{DocumentsDeletion, apply_documents_deletion};
use std::time::Duration;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::{store, Error, MResult, DocumentId}; use crate::{store, Error, MResult, DocumentId, RankedMap};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub enum Update { pub enum Update {
DocumentsAddition(Vec<rmpv::Value>), DocumentsAddition(Vec<rmpv::Value>),
DocumentsDeletion(Vec<DocumentId>), DocumentsDeletion(Vec<DocumentId>),
SynonymsAddition(BTreeMap<String, Vec<String>>),
SynonymsDeletion(BTreeMap<String, Option<Vec<String>>>),
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
pub enum UpdateType { pub enum UpdateType {
DocumentsAddition { number: usize }, DocumentsAddition { number: usize },
DocumentsDeletion { number: usize }, DocumentsDeletion { number: usize },
SynonymsAddition { number: usize },
SynonymsDeletion { number: usize },
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
@ -91,22 +89,87 @@ pub fn push_documents_deletion(
Ok(updates_store.push_back(writer, &update)?) Ok(updates_store.push_back(writer, &update)?)
} }
pub fn push_synonyms_addition( pub fn update_task(
writer: &mut rkv::Writer, rkv: Arc<RwLock<rkv::Rkv>>,
updates_store: store::Updates, index: store::Index,
addition: BTreeMap<String, Vec<String>>, mut callback: Option<impl FnOnce(UpdateResult)>,
) -> Result<u64, Error> ) -> MResult<()>
{ {
let update = Update::SynonymsAddition(addition); let rkv = rkv.read().unwrap();
Ok(updates_store.push_back(writer, &update)?) let mut writer = rkv.write()?;
}
pub fn push_synonyms_deletion( if let Some((update_id, update)) = index.updates.pop_back(&mut writer)? {
writer: &mut rkv::Writer, let (update_type, result, duration) = match update {
updates_store: store::Updates, Update::DocumentsAddition(documents) => {
deletion: BTreeMap<String, Option<Vec<String>>>, let update_type = UpdateType::DocumentsAddition { number: documents.len() };
) -> Result<u64, Error>
{ let schema = match index.main.schema(&writer)? {
let update = Update::SynonymsDeletion(deletion); Some(schema) => schema,
Ok(updates_store.push_back(writer, &update)?) None => return Err(Error::SchemaMissing),
};
let ranked_map = match index.main.ranked_map(&writer)? {
Some(ranked_map) => ranked_map,
None => RankedMap::default(),
};
let start = Instant::now();
let result = apply_documents_addition(
&mut writer,
index.main,
index.documents_fields,
index.postings_lists,
index.docs_words,
&schema,
ranked_map,
documents,
);
(update_type, result, start.elapsed())
},
Update::DocumentsDeletion(documents) => {
let update_type = UpdateType::DocumentsDeletion { number: documents.len() };
let schema = match index.main.schema(&writer)? {
Some(schema) => schema,
None => return Err(Error::SchemaMissing),
};
let ranked_map = match index.main.ranked_map(&writer)? {
Some(ranked_map) => ranked_map,
None => RankedMap::default(),
};
let start = Instant::now();
let result = apply_documents_deletion(
&mut writer,
index.main,
index.documents_fields,
index.postings_lists,
index.docs_words,
&schema,
ranked_map,
documents,
);
(update_type, result, start.elapsed())
},
};
let detailed_duration = DetailedDuration { main: duration };
let status = UpdateResult {
update_id,
update_type,
result: result.map_err(|e| e.to_string()),
detailed_duration,
};
index.updates_results.put_update_result(&mut writer, update_id, &status)?;
if let Some(callback) = callback.take() {
(callback)(status);
}
}
writer.commit()?;
Ok(())
} }