2020-10-26 17:57:34 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2022-02-15 18:41:55 +08:00
|
|
|
use time::OffsetDateTime;
|
2021-06-14 22:46:19 +08:00
|
|
|
|
2022-09-05 23:31:26 +08:00
|
|
|
use crate::facet::FacetType;
|
|
|
|
use crate::{ExternalDocumentsIds, FieldDistribution, Index, Result};
|
2020-10-26 17:57:34 +08:00
|
|
|
|
|
|
|
pub struct ClearDocuments<'t, 'u, 'i> {
|
2020-10-30 18:42:00 +08:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
2020-10-26 17:57:34 +08:00
|
|
|
index: &'i Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, 'u, 'i> ClearDocuments<'t, 'u, 'i> {
|
2021-11-03 20:12:01 +08:00
|
|
|
pub fn new(wtxn: &'t mut heed::RwTxn<'i, 'u>, index: &'i Index) -> ClearDocuments<'t, 'u, 'i> {
|
|
|
|
ClearDocuments { wtxn, index }
|
2020-10-26 17:57:34 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn execute(self) -> Result<u64> {
|
2022-02-15 18:41:55 +08:00
|
|
|
self.index.set_updated_at(self.wtxn, &OffsetDateTime::now_utc())?;
|
2020-10-26 17:57:34 +08:00
|
|
|
let Index {
|
2020-10-30 17:56:35 +08:00
|
|
|
env: _env,
|
2020-10-26 17:57:34 +08:00
|
|
|
main: _main,
|
|
|
|
word_docids,
|
2022-03-24 22:22:57 +08:00
|
|
|
exact_word_docids,
|
2021-02-03 17:30:33 +08:00
|
|
|
word_prefix_docids,
|
2022-03-25 17:49:34 +08:00
|
|
|
exact_word_prefix_docids,
|
2020-10-26 17:57:34 +08:00
|
|
|
word_pair_proximity_docids,
|
2021-02-10 17:28:15 +08:00
|
|
|
word_prefix_pair_proximity_docids,
|
2022-09-14 21:33:13 +08:00
|
|
|
prefix_word_pair_proximity_docids,
|
2021-10-05 17:18:42 +08:00
|
|
|
word_position_docids,
|
2023-03-23 16:22:01 +08:00
|
|
|
word_fid_docids,
|
2021-05-27 21:27:41 +08:00
|
|
|
field_id_word_count_docids,
|
2021-10-05 17:18:42 +08:00
|
|
|
word_prefix_position_docids,
|
2023-03-23 16:22:01 +08:00
|
|
|
word_prefix_fid_docids,
|
2022-10-12 19:28:36 +08:00
|
|
|
script_language_docids,
|
2021-04-21 21:43:44 +08:00
|
|
|
facet_id_f64_docids,
|
|
|
|
facet_id_string_docids,
|
2022-07-19 15:30:19 +08:00
|
|
|
facet_id_exists_docids,
|
2023-03-08 23:14:00 +08:00
|
|
|
facet_id_is_null_docids,
|
2023-03-15 01:08:12 +08:00
|
|
|
facet_id_is_empty_docids,
|
2021-04-21 21:43:44 +08:00
|
|
|
field_id_docid_facet_f64s,
|
|
|
|
field_id_docid_facet_strings,
|
2020-10-26 17:57:34 +08:00
|
|
|
documents,
|
|
|
|
} = self.index;
|
|
|
|
|
2022-06-13 23:59:34 +08:00
|
|
|
let empty_roaring = RoaringBitmap::default();
|
|
|
|
|
2020-11-11 23:04:04 +08:00
|
|
|
// We retrieve the number of documents ids that we are deleting.
|
|
|
|
let number_of_documents = self.index.number_of_documents(self.wtxn)?;
|
2021-01-21 00:27:43 +08:00
|
|
|
let faceted_fields = self.index.faceted_fields_ids(self.wtxn)?;
|
2020-10-26 17:57:34 +08:00
|
|
|
|
2020-11-11 23:04:04 +08:00
|
|
|
// We clean some of the main engine datastructures.
|
|
|
|
self.index.put_words_fst(self.wtxn, &fst::Set::default())?;
|
2021-02-03 17:36:07 +08:00
|
|
|
self.index.put_words_prefixes_fst(self.wtxn, &fst::Set::default())?;
|
2020-11-23 00:53:33 +08:00
|
|
|
self.index.put_external_documents_ids(self.wtxn, &ExternalDocumentsIds::default())?;
|
2022-06-13 23:59:34 +08:00
|
|
|
self.index.put_documents_ids(self.wtxn, &empty_roaring)?;
|
|
|
|
self.index.put_soft_deleted_documents_ids(self.wtxn, &empty_roaring)?;
|
2021-06-21 21:57:41 +08:00
|
|
|
self.index.put_field_distribution(self.wtxn, &FieldDistribution::default())?;
|
2021-08-25 21:32:41 +08:00
|
|
|
self.index.delete_geo_rtree(self.wtxn)?;
|
2021-08-26 23:49:50 +08:00
|
|
|
self.index.delete_geo_faceted_documents_ids(self.wtxn)?;
|
2020-10-26 17:57:34 +08:00
|
|
|
|
2020-11-23 20:08:57 +08:00
|
|
|
// We clean all the faceted documents ids.
|
2021-04-28 23:58:16 +08:00
|
|
|
for field_id in faceted_fields {
|
2022-09-05 18:51:40 +08:00
|
|
|
self.index.put_faceted_documents_ids(
|
|
|
|
self.wtxn,
|
|
|
|
field_id,
|
|
|
|
FacetType::Number,
|
|
|
|
&empty_roaring,
|
|
|
|
)?;
|
|
|
|
self.index.put_faceted_documents_ids(
|
|
|
|
self.wtxn,
|
|
|
|
field_id,
|
|
|
|
FacetType::String,
|
|
|
|
&empty_roaring,
|
|
|
|
)?;
|
2020-11-23 20:08:57 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 23:04:04 +08:00
|
|
|
// Clear the other databases.
|
2020-10-26 17:57:34 +08:00
|
|
|
word_docids.clear(self.wtxn)?;
|
2022-03-24 22:22:57 +08:00
|
|
|
exact_word_docids.clear(self.wtxn)?;
|
2021-02-03 17:36:07 +08:00
|
|
|
word_prefix_docids.clear(self.wtxn)?;
|
2022-03-25 17:49:34 +08:00
|
|
|
exact_word_prefix_docids.clear(self.wtxn)?;
|
2020-10-26 17:57:34 +08:00
|
|
|
word_pair_proximity_docids.clear(self.wtxn)?;
|
2021-02-10 17:28:15 +08:00
|
|
|
word_prefix_pair_proximity_docids.clear(self.wtxn)?;
|
2022-09-14 21:33:13 +08:00
|
|
|
prefix_word_pair_proximity_docids.clear(self.wtxn)?;
|
2021-10-05 17:18:42 +08:00
|
|
|
word_position_docids.clear(self.wtxn)?;
|
2023-03-23 16:22:01 +08:00
|
|
|
word_fid_docids.clear(self.wtxn)?;
|
2021-05-27 21:27:41 +08:00
|
|
|
field_id_word_count_docids.clear(self.wtxn)?;
|
2021-10-05 17:18:42 +08:00
|
|
|
word_prefix_position_docids.clear(self.wtxn)?;
|
2023-03-23 16:22:01 +08:00
|
|
|
word_prefix_fid_docids.clear(self.wtxn)?;
|
2022-10-12 19:28:36 +08:00
|
|
|
script_language_docids.clear(self.wtxn)?;
|
2021-04-21 21:43:44 +08:00
|
|
|
facet_id_f64_docids.clear(self.wtxn)?;
|
2022-07-19 15:30:19 +08:00
|
|
|
facet_id_exists_docids.clear(self.wtxn)?;
|
2023-03-08 23:14:00 +08:00
|
|
|
facet_id_is_null_docids.clear(self.wtxn)?;
|
2023-03-15 01:08:12 +08:00
|
|
|
facet_id_is_empty_docids.clear(self.wtxn)?;
|
2021-04-21 21:43:44 +08:00
|
|
|
facet_id_string_docids.clear(self.wtxn)?;
|
|
|
|
field_id_docid_facet_f64s.clear(self.wtxn)?;
|
|
|
|
field_id_docid_facet_strings.clear(self.wtxn)?;
|
2020-10-26 17:57:34 +08:00
|
|
|
documents.clear(self.wtxn)?;
|
|
|
|
|
2020-11-11 23:04:04 +08:00
|
|
|
Ok(number_of_documents)
|
2020-10-26 17:57:34 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-01 15:07:16 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-08-02 21:13:06 +08:00
|
|
|
use crate::index::tests::TempIndex;
|
2021-04-01 15:07:16 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clear_documents() {
|
2022-08-02 21:13:06 +08:00
|
|
|
let index = TempIndex::new();
|
2021-04-01 15:07:16 +08:00
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2022-08-02 21:13:06 +08:00
|
|
|
index
|
|
|
|
.add_documents_using_wtxn(&mut wtxn, documents!([
|
|
|
|
{ "id": 0, "name": "kevin", "age": 20 },
|
|
|
|
{ "id": 1, "name": "kevina" },
|
|
|
|
{ "id": 2, "name": "benoit", "country": "France", "_geo": { "lng": 42, "lat": 35 } }
|
|
|
|
]))
|
|
|
|
.unwrap();
|
2021-04-01 15:07:16 +08:00
|
|
|
|
|
|
|
// Clear all documents from the database.
|
2021-11-03 20:12:01 +08:00
|
|
|
let builder = ClearDocuments::new(&mut wtxn, &index);
|
2021-04-01 15:07:16 +08:00
|
|
|
assert_eq!(builder.execute().unwrap(), 3);
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
|
2022-03-24 00:28:41 +08:00
|
|
|
// the value is 7 because there is `[id, name, age, country, _geo, _geo.lng, _geo.lat]`
|
|
|
|
assert_eq!(index.fields_ids_map(&rtxn).unwrap().len(), 7);
|
2021-04-01 15:07:16 +08:00
|
|
|
|
|
|
|
assert!(index.words_fst(&rtxn).unwrap().is_empty());
|
|
|
|
assert!(index.words_prefixes_fst(&rtxn).unwrap().is_empty());
|
|
|
|
assert!(index.external_documents_ids(&rtxn).unwrap().is_empty());
|
|
|
|
assert!(index.documents_ids(&rtxn).unwrap().is_empty());
|
2021-06-17 21:16:20 +08:00
|
|
|
assert!(index.field_distribution(&rtxn).unwrap().is_empty());
|
2021-08-25 21:32:41 +08:00
|
|
|
assert!(index.geo_rtree(&rtxn).unwrap().is_none());
|
2021-08-26 23:49:50 +08:00
|
|
|
assert!(index.geo_faceted_documents_ids(&rtxn).unwrap().is_empty());
|
2021-04-01 15:07:16 +08:00
|
|
|
|
|
|
|
assert!(index.word_docids.is_empty(&rtxn).unwrap());
|
|
|
|
assert!(index.word_prefix_docids.is_empty(&rtxn).unwrap());
|
|
|
|
assert!(index.word_pair_proximity_docids.is_empty(&rtxn).unwrap());
|
2021-05-27 21:27:41 +08:00
|
|
|
assert!(index.field_id_word_count_docids.is_empty(&rtxn).unwrap());
|
2021-04-01 15:07:16 +08:00
|
|
|
assert!(index.word_prefix_pair_proximity_docids.is_empty(&rtxn).unwrap());
|
2021-05-03 21:58:47 +08:00
|
|
|
assert!(index.facet_id_f64_docids.is_empty(&rtxn).unwrap());
|
|
|
|
assert!(index.facet_id_string_docids.is_empty(&rtxn).unwrap());
|
|
|
|
assert!(index.field_id_docid_facet_f64s.is_empty(&rtxn).unwrap());
|
|
|
|
assert!(index.field_id_docid_facet_strings.is_empty(&rtxn).unwrap());
|
2021-04-01 15:07:16 +08:00
|
|
|
assert!(index.documents.is_empty(&rtxn).unwrap());
|
|
|
|
}
|
|
|
|
}
|