2019-10-03 15:04:11 +02:00
|
|
|
mod documents_addition;
|
|
|
|
mod documents_deletion;
|
2019-10-07 17:48:26 +02:00
|
|
|
mod schema_update;
|
2019-10-08 17:06:56 +02:00
|
|
|
mod synonyms_addition;
|
2019-10-08 17:16:48 +02:00
|
|
|
mod synonyms_deletion;
|
2019-10-03 15:04:11 +02:00
|
|
|
|
|
|
|
pub use self::documents_addition::{DocumentsAddition, apply_documents_addition};
|
|
|
|
pub use self::documents_deletion::{DocumentsDeletion, apply_documents_deletion};
|
2019-10-07 17:48:26 +02:00
|
|
|
pub use self::schema_update::apply_schema_update;
|
2019-10-08 17:06:56 +02:00
|
|
|
pub use self::synonyms_addition::{SynonymsAddition, apply_synonyms_addition};
|
2019-10-08 17:16:48 +02:00
|
|
|
pub use self::synonyms_deletion::{SynonymsDeletion, apply_synonyms_deletion};
|
2019-10-03 15:04:11 +02:00
|
|
|
|
2019-10-04 17:17:43 +02:00
|
|
|
use std::time::{Duration, Instant};
|
2019-10-08 17:06:56 +02:00
|
|
|
use std::collections::BTreeMap;
|
2019-10-07 17:48:26 +02:00
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
use log::debug;
|
2019-10-03 15:04:11 +02:00
|
|
|
use serde::{Serialize, Deserialize};
|
2019-10-07 17:48:26 +02:00
|
|
|
|
2019-10-04 17:17:43 +02:00
|
|
|
use crate::{store, Error, MResult, DocumentId, RankedMap};
|
2019-10-07 17:48:26 +02:00
|
|
|
use crate::error::UnsupportedOperation;
|
|
|
|
use meilidb_schema::Schema;
|
2019-10-03 15:04:11 +02:00
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-10-03 15:04:11 +02:00
|
|
|
pub enum Update {
|
2019-10-07 17:48:26 +02:00
|
|
|
SchemaUpdate(Schema),
|
2019-10-03 15:04:11 +02:00
|
|
|
DocumentsAddition(Vec<rmpv::Value>),
|
|
|
|
DocumentsDeletion(Vec<DocumentId>),
|
2019-10-08 17:06:56 +02:00
|
|
|
SynonymsAddition(BTreeMap<String, Vec<String>>),
|
2019-10-08 17:16:48 +02:00
|
|
|
SynonymsDeletion(BTreeMap<String, Option<Vec<String>>>),
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2019-10-03 16:13:09 +02:00
|
|
|
pub enum UpdateType {
|
2019-10-07 17:48:26 +02:00
|
|
|
SchemaUpdate { schema: Schema },
|
2019-10-03 16:13:09 +02:00
|
|
|
DocumentsAddition { number: usize },
|
|
|
|
DocumentsDeletion { number: usize },
|
2019-10-08 17:06:56 +02:00
|
|
|
SynonymsAddition { number: usize },
|
2019-10-08 17:16:48 +02:00
|
|
|
SynonymsDeletion { number: usize },
|
2019-10-03 16:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
|
|
pub struct DetailedDuration {
|
|
|
|
pub main: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
|
|
pub struct UpdateResult {
|
|
|
|
pub update_id: u64,
|
|
|
|
pub update_type: UpdateType,
|
|
|
|
pub result: Result<(), String>,
|
|
|
|
pub detailed_duration: DetailedDuration,
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:54:37 +02:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
|
|
pub enum UpdateStatus {
|
|
|
|
Enqueued,
|
|
|
|
Processed(UpdateResult),
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_status<T: rkv::Readable>(
|
|
|
|
reader: &T,
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
|
|
|
update_id: u64,
|
2019-10-03 17:33:15 +02:00
|
|
|
) -> MResult<UpdateStatus>
|
2019-10-03 16:54:37 +02:00
|
|
|
{
|
|
|
|
match updates_results_store.update_result(reader, update_id)? {
|
|
|
|
Some(result) => Ok(UpdateStatus::Processed(result)),
|
|
|
|
None => {
|
|
|
|
if updates_store.contains(reader, update_id)? {
|
|
|
|
Ok(UpdateStatus::Enqueued)
|
|
|
|
} else {
|
|
|
|
Ok(UpdateStatus::Unknown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:06:56 +02:00
|
|
|
fn biggest_update_id(
|
2019-10-07 16:16:04 +02:00
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
|
|
|
) -> MResult<Option<u64>>
|
|
|
|
{
|
|
|
|
let last_update_id = updates_store.last_update_id(writer)?;
|
|
|
|
let last_update_id = last_update_id.map(|(n, _)| n);
|
|
|
|
|
|
|
|
let last_update_results_id = updates_results_store.last_update_id(writer)?;
|
|
|
|
let last_update_results_id = last_update_results_id.map(|(n, _)| n);
|
|
|
|
|
|
|
|
let max = last_update_id.max(last_update_results_id);
|
|
|
|
|
|
|
|
Ok(max)
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:06:56 +02:00
|
|
|
pub fn next_update_id(
|
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
|
|
|
) -> MResult<u64>
|
|
|
|
{
|
|
|
|
let last_update_id = biggest_update_id(
|
|
|
|
writer,
|
|
|
|
updates_store,
|
|
|
|
updates_results_store
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(last_update_id.map_or(0, |n| n + 1))
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:48:26 +02:00
|
|
|
pub fn push_schema_update(
|
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
|
|
|
schema: Schema,
|
|
|
|
) -> MResult<u64>
|
|
|
|
{
|
2019-10-08 17:06:56 +02:00
|
|
|
let last_update_id = next_update_id(writer, updates_store, updates_results_store)?;
|
2019-10-07 17:48:26 +02:00
|
|
|
|
|
|
|
let update = Update::SchemaUpdate(schema);
|
|
|
|
let update_id = updates_store.put_update(writer, last_update_id, &update)?;
|
|
|
|
|
|
|
|
Ok(last_update_id)
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:04:11 +02:00
|
|
|
pub fn push_documents_addition<D: serde::Serialize>(
|
2019-10-07 15:00:28 +02:00
|
|
|
writer: &mut rkv::Writer,
|
2019-10-03 15:04:11 +02:00
|
|
|
updates_store: store::Updates,
|
2019-10-07 16:16:04 +02:00
|
|
|
updates_results_store: store::UpdatesResults,
|
2019-10-03 15:04:11 +02:00
|
|
|
addition: Vec<D>,
|
2019-10-07 16:16:04 +02:00
|
|
|
) -> MResult<u64>
|
2019-10-03 15:04:11 +02:00
|
|
|
{
|
|
|
|
let mut values = Vec::with_capacity(addition.len());
|
|
|
|
for add in addition {
|
|
|
|
let vec = rmp_serde::to_vec_named(&add)?;
|
|
|
|
let add = rmp_serde::from_read(&vec[..])?;
|
|
|
|
values.push(add);
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:06:56 +02:00
|
|
|
let last_update_id = next_update_id(writer, updates_store, updates_results_store)?;
|
2019-10-07 16:16:04 +02:00
|
|
|
|
2019-10-03 15:04:11 +02:00
|
|
|
let update = Update::DocumentsAddition(values);
|
2019-10-07 16:16:04 +02:00
|
|
|
let update_id = updates_store.put_update(writer, last_update_id, &update)?;
|
2019-10-07 10:52:45 +02:00
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
Ok(last_update_id)
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_documents_deletion(
|
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
updates_store: store::Updates,
|
2019-10-07 16:16:04 +02:00
|
|
|
updates_results_store: store::UpdatesResults,
|
2019-10-03 15:04:11 +02:00
|
|
|
deletion: Vec<DocumentId>,
|
2019-10-07 16:16:04 +02:00
|
|
|
) -> MResult<u64>
|
2019-10-03 15:04:11 +02:00
|
|
|
{
|
2019-10-08 17:06:56 +02:00
|
|
|
let last_update_id = next_update_id(writer, updates_store, updates_results_store)?;
|
2019-10-07 16:16:04 +02:00
|
|
|
|
2019-10-03 15:04:11 +02:00
|
|
|
let update = Update::DocumentsDeletion(deletion);
|
2019-10-07 16:16:04 +02:00
|
|
|
let update_id = updates_store.put_update(writer, last_update_id, &update)?;
|
2019-10-07 10:52:45 +02:00
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
Ok(last_update_id)
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 17:06:56 +02:00
|
|
|
pub fn push_synonyms_addition(
|
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
|
|
|
addition: BTreeMap<String, Vec<String>>,
|
|
|
|
) -> MResult<u64>
|
|
|
|
{
|
|
|
|
let last_update_id = next_update_id(writer, updates_store, updates_results_store)?;
|
|
|
|
|
|
|
|
let update = Update::SynonymsAddition(addition);
|
|
|
|
let update_id = updates_store.put_update(writer, last_update_id, &update)?;
|
|
|
|
|
|
|
|
Ok(last_update_id)
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:16:48 +02:00
|
|
|
pub fn push_synonyms_deletion(
|
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
|
|
|
deletion: BTreeMap<String, Option<Vec<String>>>,
|
|
|
|
) -> MResult<u64>
|
|
|
|
{
|
|
|
|
let last_update_id = next_update_id(writer, updates_store, updates_results_store)?;
|
|
|
|
|
|
|
|
let update = Update::SynonymsDeletion(deletion);
|
|
|
|
let update_id = updates_store.put_update(writer, last_update_id, &update)?;
|
|
|
|
|
|
|
|
Ok(last_update_id)
|
|
|
|
}
|
|
|
|
|
2019-10-04 17:17:43 +02:00
|
|
|
pub fn update_task(
|
2019-10-07 10:52:45 +02:00
|
|
|
writer: &mut rkv::Writer,
|
2019-10-04 17:17:43 +02:00
|
|
|
index: store::Index,
|
|
|
|
mut callback: Option<impl FnOnce(UpdateResult)>,
|
2019-10-07 15:00:28 +02:00
|
|
|
) -> MResult<bool>
|
2019-10-03 15:04:11 +02:00
|
|
|
{
|
2019-10-07 15:00:28 +02:00
|
|
|
let (update_id, update) = match index.updates.pop_front(writer)? {
|
|
|
|
Some(value) => value,
|
|
|
|
None => return Ok(false),
|
|
|
|
};
|
|
|
|
|
2019-10-08 14:53:35 +02:00
|
|
|
debug!("Processing update number {}", update_id);
|
|
|
|
|
2019-10-07 15:00:28 +02:00
|
|
|
let (update_type, result, duration) = match update {
|
2019-10-07 17:48:26 +02:00
|
|
|
Update::SchemaUpdate(schema) => {
|
|
|
|
let start = Instant::now();
|
|
|
|
let update_type = UpdateType::SchemaUpdate { schema: schema.clone() };
|
|
|
|
let result = apply_schema_update(writer, index.main, &schema);
|
|
|
|
|
|
|
|
(update_type, result, start.elapsed())
|
|
|
|
},
|
2019-10-07 15:00:28 +02:00
|
|
|
Update::DocumentsAddition(documents) => {
|
2019-10-07 16:16:04 +02:00
|
|
|
let start = Instant::now();
|
2019-10-07 15:00:28 +02:00
|
|
|
|
|
|
|
let ranked_map = match index.main.ranked_map(writer)? {
|
|
|
|
Some(ranked_map) => ranked_map,
|
|
|
|
None => RankedMap::default(),
|
|
|
|
};
|
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
let update_type = UpdateType::DocumentsAddition { number: documents.len() };
|
|
|
|
|
2019-10-07 17:48:26 +02:00
|
|
|
let result = apply_documents_addition(
|
|
|
|
writer,
|
|
|
|
index.main,
|
|
|
|
index.documents_fields,
|
|
|
|
index.postings_lists,
|
|
|
|
index.docs_words,
|
|
|
|
ranked_map,
|
|
|
|
documents,
|
|
|
|
);
|
2019-10-07 15:00:28 +02:00
|
|
|
|
|
|
|
(update_type, result, start.elapsed())
|
|
|
|
},
|
|
|
|
Update::DocumentsDeletion(documents) => {
|
2019-10-07 16:16:04 +02:00
|
|
|
let start = Instant::now();
|
2019-10-07 15:00:28 +02:00
|
|
|
|
|
|
|
let ranked_map = match index.main.ranked_map(writer)? {
|
|
|
|
Some(ranked_map) => ranked_map,
|
|
|
|
None => RankedMap::default(),
|
|
|
|
};
|
|
|
|
|
2019-10-07 16:16:04 +02:00
|
|
|
let update_type = UpdateType::DocumentsDeletion { number: documents.len() };
|
|
|
|
|
2019-10-07 17:48:26 +02:00
|
|
|
let result = apply_documents_deletion(
|
|
|
|
writer,
|
|
|
|
index.main,
|
|
|
|
index.documents_fields,
|
|
|
|
index.postings_lists,
|
|
|
|
index.docs_words,
|
|
|
|
ranked_map,
|
|
|
|
documents,
|
|
|
|
);
|
2019-10-07 15:00:28 +02:00
|
|
|
|
2019-10-08 17:06:56 +02:00
|
|
|
(update_type, result, start.elapsed())
|
|
|
|
},
|
|
|
|
Update::SynonymsAddition(synonyms) => {
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let update_type = UpdateType::SynonymsAddition { number: synonyms.len() };
|
|
|
|
|
|
|
|
let result = apply_synonyms_addition(
|
|
|
|
writer,
|
|
|
|
index.main,
|
|
|
|
index.synonyms,
|
|
|
|
synonyms,
|
|
|
|
);
|
|
|
|
|
2019-10-08 17:16:48 +02:00
|
|
|
(update_type, result, start.elapsed())
|
|
|
|
},
|
|
|
|
Update::SynonymsDeletion(synonyms) => {
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let update_type = UpdateType::SynonymsDeletion { number: synonyms.len() };
|
|
|
|
|
|
|
|
let result = apply_synonyms_deletion(
|
|
|
|
writer,
|
|
|
|
index.main,
|
|
|
|
index.synonyms,
|
|
|
|
synonyms,
|
|
|
|
);
|
|
|
|
|
2019-10-07 15:00:28 +02:00
|
|
|
(update_type, result, start.elapsed())
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-10-07 17:48:26 +02:00
|
|
|
debug!("Processed update number {} {:?} {:?}", update_id, update_type, result);
|
|
|
|
|
2019-10-07 15:00:28 +02:00
|
|
|
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(writer, update_id, &status)?;
|
|
|
|
|
|
|
|
if let Some(callback) = callback.take() {
|
|
|
|
(callback)(status);
|
2019-10-04 17:17:43 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 15:00:28 +02:00
|
|
|
Ok(true)
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|