2019-10-18 19:05:28 +08:00
|
|
|
use std::collections::{BTreeSet, HashMap, HashSet};
|
2019-10-03 21:04:11 +08:00
|
|
|
|
|
|
|
use fst::{SetBuilder, Streamer};
|
2019-10-18 19:05:28 +08:00
|
|
|
use sdset::{duo::DifferenceByKey, SetBuf, SetOperation};
|
2019-10-03 21:04:11 +08:00
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
use crate::database::{MainT, UpdateT};
|
2019-11-06 17:49:13 +08:00
|
|
|
use crate::database::{UpdateEvent, UpdateEventsEmitter};
|
2020-05-06 04:28:46 +08:00
|
|
|
use crate::facets;
|
2019-10-03 21:04:11 +08:00
|
|
|
use crate::store;
|
2020-01-16 23:19:04 +08:00
|
|
|
use crate::update::{next_update_id, compute_short_prefixes, Update};
|
2019-10-18 19:05:28 +08:00
|
|
|
use crate::{DocumentId, Error, MResult, RankedMap};
|
2019-10-03 21:04:11 +08:00
|
|
|
|
|
|
|
pub struct DocumentsDeletion {
|
|
|
|
updates_store: store::Updates,
|
2019-10-07 22:16:04 +08:00
|
|
|
updates_results_store: store::UpdatesResults,
|
2019-11-06 17:49:13 +08:00
|
|
|
updates_notifier: UpdateEventsEmitter,
|
2020-05-19 19:12:02 +08:00
|
|
|
documents: Vec<String>,
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentsDeletion {
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn new(
|
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
2019-11-06 17:49:13 +08:00
|
|
|
updates_notifier: UpdateEventsEmitter,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> DocumentsDeletion {
|
2019-10-07 22:16:04 +08:00
|
|
|
DocumentsDeletion {
|
|
|
|
updates_store,
|
|
|
|
updates_results_store,
|
|
|
|
updates_notifier,
|
|
|
|
documents: Vec::new(),
|
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:12:02 +08:00
|
|
|
pub fn delete_document_by_user_id(&mut self, document_id: String) {
|
2019-10-03 21:04:11 +08:00
|
|
|
self.documents.push(document_id);
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
pub fn finalize(self, writer: &mut heed::RwTxn<UpdateT>) -> MResult<u64> {
|
2019-11-06 17:49:13 +08:00
|
|
|
let _ = self.updates_notifier.send(UpdateEvent::NewUpdate);
|
2019-10-07 22:16:04 +08:00
|
|
|
let update_id = push_documents_deletion(
|
2019-10-11 17:29:47 +08:00
|
|
|
writer,
|
2019-10-07 22:16:04 +08:00
|
|
|
self.updates_store,
|
|
|
|
self.updates_results_store,
|
|
|
|
self.documents,
|
|
|
|
)?;
|
|
|
|
Ok(update_id)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:12:02 +08:00
|
|
|
impl Extend<String> for DocumentsDeletion {
|
|
|
|
fn extend<T: IntoIterator<Item=String>>(&mut self, iter: T) {
|
2019-10-03 21:04:11 +08:00
|
|
|
self.documents.extend(iter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 23:24:11 +08:00
|
|
|
pub fn push_documents_deletion(
|
2019-11-26 23:12:06 +08:00
|
|
|
writer: &mut heed::RwTxn<UpdateT>,
|
2019-10-08 23:24:11 +08:00
|
|
|
updates_store: store::Updates,
|
|
|
|
updates_results_store: store::UpdatesResults,
|
2020-05-19 19:12:02 +08:00
|
|
|
deletion: Vec<String>,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> MResult<u64> {
|
2019-10-08 23:24:11 +08:00
|
|
|
let last_update_id = next_update_id(writer, updates_store, updates_results_store)?;
|
|
|
|
|
2019-11-13 01:00:47 +08:00
|
|
|
let update = Update::documents_deletion(deletion);
|
2019-10-08 23:31:07 +08:00
|
|
|
updates_store.put_update(writer, last_update_id, &update)?;
|
2019-10-08 23:24:11 +08:00
|
|
|
|
|
|
|
Ok(last_update_id)
|
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
pub fn apply_documents_deletion(
|
2019-11-26 23:12:06 +08:00
|
|
|
writer: &mut heed::RwTxn<MainT>,
|
2020-01-16 23:29:50 +08:00
|
|
|
index: &store::Index,
|
2020-05-19 19:12:02 +08:00
|
|
|
deletion: Vec<String>,
|
2020-05-19 17:45:46 +08:00
|
|
|
) -> MResult<()>
|
|
|
|
{
|
2020-05-19 19:12:02 +08:00
|
|
|
let (user_ids, internal_ids) = {
|
|
|
|
let new_user_ids = SetBuf::from_dirty(deletion);
|
|
|
|
let mut internal_ids = Vec::new();
|
|
|
|
|
|
|
|
let user_ids = index.main.user_ids(writer)?;
|
|
|
|
for userid in new_user_ids.as_slice() {
|
|
|
|
if let Some(id) = user_ids.get(userid) {
|
2020-05-19 19:53:31 +08:00
|
|
|
internal_ids.push(DocumentId(id as u32));
|
2020-05-19 19:12:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_user_ids = fst::Map::from_iter(new_user_ids.into_iter().map(|k| (k, 0))).unwrap();
|
|
|
|
(new_user_ids, SetBuf::from_dirty(internal_ids))
|
|
|
|
};
|
2020-05-19 17:45:46 +08:00
|
|
|
|
2020-01-16 23:29:50 +08:00
|
|
|
let schema = match index.main.schema(writer)? {
|
2019-10-07 23:48:26 +08:00
|
|
|
Some(schema) => schema,
|
|
|
|
None => return Err(Error::SchemaMissing),
|
|
|
|
};
|
|
|
|
|
2020-01-16 23:29:50 +08:00
|
|
|
let mut ranked_map = match index.main.ranked_map(writer)? {
|
2019-10-21 23:33:52 +08:00
|
|
|
Some(ranked_map) => ranked_map,
|
|
|
|
None => RankedMap::default(),
|
|
|
|
};
|
|
|
|
|
2020-05-06 04:28:46 +08:00
|
|
|
// facet filters deletion
|
|
|
|
if let Some(attributes_for_facetting) = index.main.attributes_for_faceting(writer)? {
|
2020-05-19 19:12:02 +08:00
|
|
|
let facet_map = facets::facet_map_from_docids(writer, &index, &internal_ids, &attributes_for_facetting)?;
|
2020-05-06 04:28:46 +08:00
|
|
|
index.facets.remove(writer, facet_map)?;
|
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
// collect the ranked attributes according to the schema
|
2020-01-30 01:30:21 +08:00
|
|
|
let ranked_fields = schema.ranked();
|
2019-10-03 21:04:11 +08:00
|
|
|
|
|
|
|
let mut words_document_ids = HashMap::new();
|
2020-05-19 19:12:02 +08:00
|
|
|
for id in internal_ids.iter().cloned() {
|
2019-10-03 21:04:11 +08:00
|
|
|
// remove all the ranked attributes from the ranked_map
|
2020-02-11 22:16:02 +08:00
|
|
|
for ranked_attr in ranked_fields {
|
2019-10-03 21:04:11 +08:00
|
|
|
ranked_map.remove(id, *ranked_attr);
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:29:50 +08:00
|
|
|
if let Some(words) = index.docs_words.doc_words(writer, id)? {
|
2019-10-03 21:04:11 +08:00
|
|
|
let mut stream = words.stream();
|
|
|
|
while let Some(word) = stream.next() {
|
|
|
|
let word = word.to_vec();
|
2019-10-18 19:05:28 +08:00
|
|
|
words_document_ids
|
|
|
|
.entry(word)
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(id);
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut deleted_documents = HashSet::new();
|
|
|
|
let mut removed_words = BTreeSet::new();
|
|
|
|
for (word, document_ids) in words_document_ids {
|
|
|
|
let document_ids = SetBuf::from_dirty(document_ids);
|
|
|
|
|
2020-01-16 23:29:50 +08:00
|
|
|
if let Some(postings) = index.postings_lists.postings_list(writer, &word)? {
|
2020-01-08 22:30:43 +08:00
|
|
|
let op = DifferenceByKey::new(&postings.matches, &document_ids, |d| d.document_id, |id| *id);
|
2019-10-03 21:04:11 +08:00
|
|
|
let doc_indexes = op.into_set_buf();
|
|
|
|
|
|
|
|
if !doc_indexes.is_empty() {
|
2020-01-16 23:29:50 +08:00
|
|
|
index.postings_lists.put_postings_list(writer, &word, &doc_indexes)?;
|
2019-10-03 21:04:11 +08:00
|
|
|
} else {
|
2020-01-16 23:29:50 +08:00
|
|
|
index.postings_lists.del_postings_list(writer, &word)?;
|
2019-10-03 21:04:11 +08:00
|
|
|
removed_words.insert(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id in document_ids {
|
2020-01-16 23:29:50 +08:00
|
|
|
index.documents_fields_counts.del_all_document_fields_counts(writer, id)?;
|
|
|
|
if index.documents_fields.del_all_document_fields(writer, id)? != 0 {
|
2019-10-03 21:04:11 +08:00
|
|
|
deleted_documents.insert(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:53:35 +08:00
|
|
|
let deleted_documents_len = deleted_documents.len() as u64;
|
|
|
|
for id in deleted_documents {
|
2020-01-16 23:29:50 +08:00
|
|
|
index.docs_words.del_doc_words(writer, id)?;
|
2019-10-08 20:53:35 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
let removed_words = fst::Set::from_iter(removed_words).unwrap();
|
2020-01-16 23:29:50 +08:00
|
|
|
let words = match index.main.words_fst(writer)? {
|
2019-10-03 21:04:11 +08:00
|
|
|
Some(words_set) => {
|
|
|
|
let op = fst::set::OpBuilder::new()
|
|
|
|
.add(words_set.stream())
|
|
|
|
.add(removed_words.stream())
|
|
|
|
.difference();
|
|
|
|
|
|
|
|
let mut words_builder = SetBuilder::memory();
|
|
|
|
words_builder.extend_stream(op).unwrap();
|
|
|
|
words_builder
|
|
|
|
.into_inner()
|
|
|
|
.and_then(fst::Set::from_bytes)
|
|
|
|
.unwrap()
|
2019-10-18 19:05:28 +08:00
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
None => fst::Set::default(),
|
|
|
|
};
|
|
|
|
|
2020-01-16 23:29:50 +08:00
|
|
|
index.main.put_words_fst(writer, &words)?;
|
|
|
|
index.main.put_ranked_map(writer, &ranked_map)?;
|
|
|
|
index.main.put_number_of_documents(writer, |old| old - deleted_documents_len)?;
|
2019-10-03 21:04:11 +08:00
|
|
|
|
2020-05-19 19:12:02 +08:00
|
|
|
// We apply the changes to the user and internal ids
|
|
|
|
index.main.remove_user_ids(writer, &user_ids)?;
|
|
|
|
index.main.remove_internal_ids(writer, &internal_ids)?;
|
|
|
|
|
2020-01-16 23:29:50 +08:00
|
|
|
compute_short_prefixes(writer, index)?;
|
2020-01-16 23:19:04 +08:00
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
Ok(())
|
|
|
|
}
|