2021-08-16 19:36:30 +08:00
|
|
|
mod extract;
|
|
|
|
mod helpers;
|
|
|
|
mod transform;
|
|
|
|
mod typed_chunk;
|
|
|
|
|
2020-11-03 20:42:29 +08:00
|
|
|
use std::collections::HashSet;
|
2021-08-16 19:36:30 +08:00
|
|
|
use std::io::{self, BufRead, BufReader};
|
|
|
|
use std::iter::FromIterator;
|
2021-03-24 22:06:54 +08:00
|
|
|
use std::num::{NonZeroU32, NonZeroUsize};
|
2020-10-27 03:18:10 +08:00
|
|
|
use std::time::Instant;
|
|
|
|
|
2021-03-12 01:42:21 +08:00
|
|
|
use chrono::Utc;
|
2021-08-16 19:36:30 +08:00
|
|
|
use crossbeam_channel::{Receiver, Sender};
|
|
|
|
use grenad::{self, CompressionType};
|
|
|
|
use log::{debug, info};
|
2021-03-25 18:10:12 +08:00
|
|
|
use rayon::ThreadPool;
|
2021-08-16 19:36:30 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2021-06-17 00:33:33 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-16 19:36:30 +08:00
|
|
|
use typed_chunk::{write_typed_chunk_into_index, TypedChunk};
|
2020-11-03 20:20:11 +08:00
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
pub use self::helpers::{
|
|
|
|
create_sorter, create_writer, merge_cbo_roaring_bitmaps, merge_roaring_bitmaps,
|
2021-08-19 00:04:24 +08:00
|
|
|
sorter_into_lmdb_database, write_into_lmdb_database, writer_into_reader, MergeFn,
|
2020-10-27 03:18:10 +08:00
|
|
|
};
|
2021-08-16 19:36:30 +08:00
|
|
|
use self::helpers::{grenad_obkv_into_chunks, GrenadParameters};
|
2020-10-27 03:18:10 +08:00
|
|
|
pub use self::transform::{Transform, TransformOutput};
|
2021-06-17 00:33:33 +08:00
|
|
|
use crate::update::{
|
2021-08-16 19:36:30 +08:00
|
|
|
Facets, UpdateBuilder, UpdateIndexingStep, WordPrefixDocids, WordPrefixPairProximityDocids,
|
2021-06-17 00:33:33 +08:00
|
|
|
WordsLevelPositions, WordsPrefixesFst,
|
|
|
|
};
|
2021-08-16 19:36:30 +08:00
|
|
|
use crate::{Index, Result};
|
2020-10-27 03:18:10 +08:00
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
static MERGED_DATABASE_COUNT: usize = 7;
|
|
|
|
static PREFIX_DATABASE_COUNT: usize = 5;
|
|
|
|
static TOTAL_POSTING_DATABASE_COUNT: usize = MERGED_DATABASE_COUNT + PREFIX_DATABASE_COUNT;
|
|
|
|
|
2020-12-31 01:43:50 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct DocumentAdditionResult {
|
2021-04-30 02:05:07 +08:00
|
|
|
pub nb_documents: usize,
|
2020-12-31 01:43:50 +08:00
|
|
|
}
|
|
|
|
|
2020-12-23 01:17:35 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
2020-10-31 23:10:15 +08:00
|
|
|
#[non_exhaustive]
|
2020-10-26 18:02:44 +08:00
|
|
|
pub enum IndexDocumentsMethod {
|
|
|
|
/// Replace the previous document with the new one,
|
|
|
|
/// removing all the already known attributes.
|
|
|
|
ReplaceDocuments,
|
|
|
|
|
|
|
|
/// Merge the previous version of the document with the new version,
|
|
|
|
/// replacing old attributes values with the new ones and add the new attributes.
|
|
|
|
UpdateDocuments,
|
|
|
|
}
|
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum WriteMethod {
|
|
|
|
Append,
|
|
|
|
GetMergePut,
|
|
|
|
}
|
|
|
|
|
2020-12-23 01:17:35 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
2020-10-31 23:10:15 +08:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum UpdateFormat {
|
|
|
|
/// The given update is a real **comma seperated** CSV with headers on the first line.
|
|
|
|
Csv,
|
|
|
|
/// The given update is a JSON array with documents inside.
|
|
|
|
Json,
|
2020-11-01 18:50:10 +08:00
|
|
|
/// The given update is a JSON stream with a document on each line.
|
|
|
|
JsonStream,
|
2020-10-31 23:10:15 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 02:11:22 +08:00
|
|
|
pub struct IndexDocuments<'t, 'u, 'i, 'a> {
|
2020-10-30 18:42:00 +08:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
2020-10-26 18:02:44 +08:00
|
|
|
index: &'i Index,
|
2020-11-01 21:55:06 +08:00
|
|
|
pub(crate) log_every_n: Option<usize>,
|
2021-08-16 19:36:30 +08:00
|
|
|
pub(crate) documents_chunk_size: Option<usize>,
|
2020-11-01 21:55:06 +08:00
|
|
|
pub(crate) max_nb_chunks: Option<usize>,
|
|
|
|
pub(crate) max_memory: Option<usize>,
|
|
|
|
pub(crate) chunk_compression_type: CompressionType,
|
|
|
|
pub(crate) chunk_compression_level: Option<u32>,
|
2020-11-03 02:11:22 +08:00
|
|
|
pub(crate) thread_pool: Option<&'a ThreadPool>,
|
2020-11-28 19:43:43 +08:00
|
|
|
facet_level_group_size: Option<NonZeroUsize>,
|
|
|
|
facet_min_level_size: Option<NonZeroUsize>,
|
2021-08-19 00:04:24 +08:00
|
|
|
words_prefix_threshold: Option<u32>,
|
2021-02-10 18:53:13 +08:00
|
|
|
max_prefix_length: Option<usize>,
|
2021-03-24 22:06:54 +08:00
|
|
|
words_positions_level_group_size: Option<NonZeroU32>,
|
|
|
|
words_positions_min_level_size: Option<NonZeroU32>,
|
2020-10-26 18:02:44 +08:00
|
|
|
update_method: IndexDocumentsMethod,
|
2020-10-31 23:10:15 +08:00
|
|
|
update_format: UpdateFormat,
|
2020-11-01 04:46:55 +08:00
|
|
|
autogenerate_docids: bool,
|
2020-12-22 23:21:07 +08:00
|
|
|
update_id: u64,
|
2020-10-26 18:02:44 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 02:11:22 +08:00
|
|
|
impl<'t, 'u, 'i, 'a> IndexDocuments<'t, 'u, 'i, 'a> {
|
2020-12-22 23:21:07 +08:00
|
|
|
pub fn new(
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
|
|
|
update_id: u64,
|
|
|
|
) -> IndexDocuments<'t, 'u, 'i, 'a> {
|
2020-10-27 03:18:10 +08:00
|
|
|
IndexDocuments {
|
|
|
|
wtxn,
|
|
|
|
index,
|
|
|
|
log_every_n: None,
|
2021-08-16 19:36:30 +08:00
|
|
|
documents_chunk_size: None,
|
2020-10-27 03:18:10 +08:00
|
|
|
max_nb_chunks: None,
|
|
|
|
max_memory: None,
|
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
2020-11-03 02:11:22 +08:00
|
|
|
thread_pool: None,
|
2020-11-28 19:43:43 +08:00
|
|
|
facet_level_group_size: None,
|
|
|
|
facet_min_level_size: None,
|
2021-02-10 18:53:13 +08:00
|
|
|
words_prefix_threshold: None,
|
|
|
|
max_prefix_length: None,
|
2021-03-17 20:55:24 +08:00
|
|
|
words_positions_level_group_size: None,
|
|
|
|
words_positions_min_level_size: None,
|
2020-10-31 23:10:15 +08:00
|
|
|
update_method: IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
update_format: UpdateFormat::Json,
|
2021-05-01 02:34:29 +08:00
|
|
|
autogenerate_docids: false,
|
2020-12-22 23:21:07 +08:00
|
|
|
update_id,
|
2020-10-27 03:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:55:06 +08:00
|
|
|
pub fn index_documents_method(&mut self, method: IndexDocumentsMethod) {
|
2020-10-26 18:02:44 +08:00
|
|
|
self.update_method = method;
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:55:06 +08:00
|
|
|
pub fn update_format(&mut self, format: UpdateFormat) {
|
2020-10-31 23:10:15 +08:00
|
|
|
self.update_format = format;
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:55:06 +08:00
|
|
|
pub fn enable_autogenerate_docids(&mut self) {
|
2020-11-01 04:46:55 +08:00
|
|
|
self.autogenerate_docids = true;
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:55:06 +08:00
|
|
|
pub fn disable_autogenerate_docids(&mut self) {
|
2020-11-01 04:46:55 +08:00
|
|
|
self.autogenerate_docids = false;
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:55:53 +08:00
|
|
|
#[logging_timer::time("IndexDocuments::{}")]
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn execute<R, F>(self, reader: R, progress_callback: F) -> Result<DocumentAdditionResult>
|
2020-10-27 03:18:10 +08:00
|
|
|
where
|
|
|
|
R: io::Read,
|
2020-12-22 23:21:07 +08:00
|
|
|
F: Fn(UpdateIndexingStep, u64) + Sync,
|
2020-10-27 03:18:10 +08:00
|
|
|
{
|
2021-05-07 00:14:16 +08:00
|
|
|
let mut reader = BufReader::new(reader);
|
|
|
|
reader.fill_buf()?;
|
|
|
|
|
2021-05-07 03:16:40 +08:00
|
|
|
// Early return when there is no document to add
|
2021-05-07 00:14:16 +08:00
|
|
|
if reader.buffer().is_empty() {
|
2021-06-17 00:33:33 +08:00
|
|
|
return Ok(DocumentAdditionResult { nb_documents: 0 });
|
2021-05-07 00:14:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 01:42:21 +08:00
|
|
|
self.index.set_updated_at(self.wtxn, &Utc::now())?;
|
2020-11-03 20:20:11 +08:00
|
|
|
let before_transform = Instant::now();
|
2020-12-22 23:21:07 +08:00
|
|
|
let update_id = self.update_id;
|
|
|
|
let progress_callback = |step| progress_callback(step, update_id);
|
2020-10-27 03:18:10 +08:00
|
|
|
let transform = Transform {
|
|
|
|
rtxn: &self.wtxn,
|
|
|
|
index: self.index,
|
2020-11-11 19:39:09 +08:00
|
|
|
log_every_n: self.log_every_n,
|
2020-10-27 03:18:10 +08:00
|
|
|
chunk_compression_type: self.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.chunk_compression_level,
|
|
|
|
max_nb_chunks: self.max_nb_chunks,
|
|
|
|
max_memory: self.max_memory,
|
|
|
|
index_documents_method: self.update_method,
|
2020-11-01 04:46:55 +08:00
|
|
|
autogenerate_docids: self.autogenerate_docids,
|
2020-10-27 03:18:10 +08:00
|
|
|
};
|
|
|
|
|
2020-10-31 23:10:15 +08:00
|
|
|
let output = match self.update_format {
|
2020-12-01 21:25:17 +08:00
|
|
|
UpdateFormat::Csv => transform.output_from_csv(reader, &progress_callback)?,
|
|
|
|
UpdateFormat::Json => transform.output_from_json(reader, &progress_callback)?,
|
2021-06-17 00:33:33 +08:00
|
|
|
UpdateFormat::JsonStream => {
|
|
|
|
transform.output_from_json_stream(reader, &progress_callback)?
|
|
|
|
}
|
2020-10-31 23:10:15 +08:00
|
|
|
};
|
|
|
|
|
2020-12-31 01:43:50 +08:00
|
|
|
let nb_documents = output.documents_count;
|
|
|
|
|
2020-11-03 20:20:11 +08:00
|
|
|
info!("Update transformed in {:.02?}", before_transform.elapsed());
|
|
|
|
|
2020-12-31 01:43:50 +08:00
|
|
|
self.execute_raw(output, progress_callback)?;
|
|
|
|
Ok(DocumentAdditionResult { nb_documents })
|
2020-11-03 20:20:11 +08:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:55:53 +08:00
|
|
|
#[logging_timer::time("IndexDocuments::{}")]
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn execute_raw<F>(self, output: TransformOutput, progress_callback: F) -> Result<()>
|
2020-11-03 20:20:11 +08:00
|
|
|
where
|
2021-06-17 00:33:33 +08:00
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
2020-11-03 20:20:11 +08:00
|
|
|
{
|
2020-10-27 03:18:10 +08:00
|
|
|
let TransformOutput {
|
2020-10-31 19:54:43 +08:00
|
|
|
primary_key,
|
2020-10-27 03:18:10 +08:00
|
|
|
fields_ids_map,
|
2021-06-17 21:16:20 +08:00
|
|
|
field_distribution,
|
2020-11-22 18:54:04 +08:00
|
|
|
external_documents_ids,
|
2020-10-27 03:18:10 +08:00
|
|
|
new_documents_ids,
|
|
|
|
replaced_documents_ids,
|
|
|
|
documents_count,
|
|
|
|
documents_file,
|
2020-10-31 23:10:15 +08:00
|
|
|
} = output;
|
2020-10-27 03:18:10 +08:00
|
|
|
|
2021-07-27 22:24:21 +08:00
|
|
|
// The fields_ids_map is put back to the store now so the rest of the transaction sees an
|
|
|
|
// up to date field map.
|
|
|
|
self.index.put_fields_ids_map(self.wtxn, &fields_ids_map)?;
|
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
let backup_pool;
|
|
|
|
let pool = match self.thread_pool {
|
|
|
|
Some(pool) => pool,
|
|
|
|
#[cfg(not(test))]
|
|
|
|
None => {
|
|
|
|
// We initialize a bakcup pool with the default
|
|
|
|
// settings if none have already been set.
|
|
|
|
backup_pool = rayon::ThreadPoolBuilder::new().build()?;
|
|
|
|
&backup_pool
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
None => {
|
|
|
|
// We initialize a bakcup pool with the default
|
|
|
|
// settings if none have already been set.
|
|
|
|
backup_pool = rayon::ThreadPoolBuilder::new().num_threads(1).build()?;
|
|
|
|
&backup_pool
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let documents_file = grenad::Reader::new(documents_file)?;
|
|
|
|
|
|
|
|
// create LMDB writer channel
|
2021-08-24 19:01:31 +08:00
|
|
|
let (lmdb_writer_sx, lmdb_writer_rx): (
|
|
|
|
Sender<Result<TypedChunk>>,
|
|
|
|
Receiver<Result<TypedChunk>>,
|
|
|
|
) = crossbeam_channel::unbounded();
|
2021-08-16 19:36:30 +08:00
|
|
|
|
|
|
|
// get searchable fields for word databases
|
|
|
|
let searchable_fields =
|
|
|
|
self.index.searchable_fields_ids(self.wtxn)?.map(HashSet::from_iter);
|
|
|
|
// get filterable fields for facet databases
|
|
|
|
let faceted_fields = self.index.faceted_fields_ids(self.wtxn)?;
|
2021-08-24 00:41:48 +08:00
|
|
|
// get the fid of the `_geo` field.
|
|
|
|
let geo_field_id = self.index.fields_ids_map(self.wtxn)?.id("_geo");
|
2021-08-16 19:36:30 +08:00
|
|
|
|
2021-08-17 18:25:07 +08:00
|
|
|
let stop_words = self.index.stop_words(self.wtxn)?;
|
|
|
|
// let stop_words = stop_words.as_ref();
|
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
// Run extraction pipeline in parallel.
|
|
|
|
pool.install(|| {
|
|
|
|
let params = GrenadParameters {
|
|
|
|
chunk_compression_type: self.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.chunk_compression_level,
|
|
|
|
max_memory: self.max_memory,
|
|
|
|
max_nb_chunks: self.max_nb_chunks, // default value, may be chosen.
|
|
|
|
};
|
|
|
|
|
|
|
|
// split obkv file into several chuncks
|
2021-08-24 19:01:31 +08:00
|
|
|
let chunk_iter = grenad_obkv_into_chunks(
|
2021-08-16 19:36:30 +08:00
|
|
|
documents_file,
|
|
|
|
params.clone(),
|
2021-09-02 21:17:52 +08:00
|
|
|
self.documents_chunk_size.unwrap_or(1024 * 1024 * 128), // 128MiB
|
2021-08-24 19:01:31 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
let result = chunk_iter.map(|chunk_iter| {
|
|
|
|
// extract all databases from the chunked obkv douments
|
|
|
|
extract::data_from_obkv_documents(
|
|
|
|
chunk_iter,
|
|
|
|
params,
|
|
|
|
lmdb_writer_sx.clone(),
|
|
|
|
searchable_fields,
|
|
|
|
faceted_fields,
|
2021-08-24 00:41:48 +08:00
|
|
|
geo_field_id,
|
2021-08-24 19:01:31 +08:00
|
|
|
stop_words,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(e) = result {
|
2021-08-26 17:01:30 +08:00
|
|
|
let _ = lmdb_writer_sx.send(Err(e));
|
2021-08-24 19:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// needs to be droped to avoid channel waiting lock.
|
|
|
|
drop(lmdb_writer_sx)
|
2021-08-16 19:36:30 +08:00
|
|
|
});
|
|
|
|
|
2020-10-27 03:18:10 +08:00
|
|
|
// We delete the documents that this document addition replaces. This way we are
|
|
|
|
// able to simply insert all the documents even if they already exist in the database.
|
|
|
|
if !replaced_documents_ids.is_empty() {
|
|
|
|
let update_builder = UpdateBuilder {
|
|
|
|
log_every_n: self.log_every_n,
|
|
|
|
max_nb_chunks: self.max_nb_chunks,
|
|
|
|
max_memory: self.max_memory,
|
2021-09-02 21:25:39 +08:00
|
|
|
documents_chunk_size: self.documents_chunk_size,
|
2020-10-27 03:18:10 +08:00
|
|
|
chunk_compression_type: self.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.chunk_compression_level,
|
2020-11-03 02:11:22 +08:00
|
|
|
thread_pool: self.thread_pool,
|
2020-12-22 23:21:07 +08:00
|
|
|
update_id: self.update_id,
|
2020-10-27 03:18:10 +08:00
|
|
|
};
|
|
|
|
let mut deletion_builder = update_builder.delete_documents(self.wtxn, self.index)?;
|
2020-11-19 18:18:52 +08:00
|
|
|
debug!("documents to delete {:?}", replaced_documents_ids);
|
2020-10-27 03:18:10 +08:00
|
|
|
deletion_builder.delete_documents(&replaced_documents_ids);
|
2020-11-19 18:18:52 +08:00
|
|
|
let deleted_documents_count = deletion_builder.execute()?;
|
|
|
|
debug!("{} documents actually deleted", deleted_documents_count);
|
2020-10-27 03:18:10 +08:00
|
|
|
}
|
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
let index_documents_ids = self.index.documents_ids(self.wtxn)?;
|
|
|
|
let index_is_empty = index_documents_ids.len() == 0;
|
|
|
|
let mut final_documents_ids = RoaringBitmap::new();
|
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
let mut databases_seen = 0;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
for typed_chunk in lmdb_writer_rx {
|
2021-08-17 16:56:06 +08:00
|
|
|
let (docids, is_merged_database) =
|
2021-08-24 19:01:31 +08:00
|
|
|
write_typed_chunk_into_index(typed_chunk?, &self.index, self.wtxn, index_is_empty)?;
|
2021-08-17 16:56:06 +08:00
|
|
|
if !docids.is_empty() {
|
|
|
|
final_documents_ids |= docids;
|
|
|
|
let documents_seen_count = final_documents_ids.len();
|
|
|
|
progress_callback(UpdateIndexingStep::IndexDocuments {
|
|
|
|
documents_seen: documents_seen_count as usize,
|
|
|
|
total_documents: documents_count,
|
|
|
|
});
|
|
|
|
debug!(
|
|
|
|
"We have seen {} documents on {} total document so far",
|
|
|
|
documents_seen_count, documents_count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if is_merged_database {
|
|
|
|
databases_seen += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
}
|
2020-10-27 03:18:10 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:16:20 +08:00
|
|
|
// We write the field distribution into the main database
|
|
|
|
self.index.put_field_distribution(self.wtxn, &field_distribution)?;
|
2021-03-31 23:14:23 +08:00
|
|
|
|
2020-10-31 19:54:43 +08:00
|
|
|
// We write the primary key field id into the main database
|
2021-01-21 00:27:43 +08:00
|
|
|
self.index.put_primary_key(self.wtxn, &primary_key)?;
|
2020-10-31 19:54:43 +08:00
|
|
|
|
2020-11-22 18:54:04 +08:00
|
|
|
// We write the external documents ids into the main database.
|
|
|
|
self.index.put_external_documents_ids(self.wtxn, &external_documents_ids)?;
|
2020-10-27 03:18:10 +08:00
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
let all_documents_ids = index_documents_ids | new_documents_ids | replaced_documents_ids;
|
2021-08-16 19:36:30 +08:00
|
|
|
self.index.put_documents_ids(self.wtxn, &all_documents_ids)?;
|
2020-11-11 19:39:09 +08:00
|
|
|
|
2021-08-16 19:36:30 +08:00
|
|
|
self.execute_prefix_databases(progress_callback)
|
|
|
|
}
|
2020-10-27 03:18:10 +08:00
|
|
|
|
2021-08-24 19:55:53 +08:00
|
|
|
#[logging_timer::time("IndexDocuments::{}")]
|
2021-08-17 16:56:06 +08:00
|
|
|
pub fn execute_prefix_databases<F>(self, progress_callback: F) -> Result<()>
|
2021-08-16 19:36:30 +08:00
|
|
|
where
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2021-08-17 16:56:06 +08:00
|
|
|
// Merged databases are already been indexed, we start from this count;
|
|
|
|
let mut databases_seen = MERGED_DATABASE_COUNT;
|
|
|
|
|
2021-02-10 18:53:13 +08:00
|
|
|
// Run the facets update operation.
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = Facets::new(self.wtxn, self.index, self.update_id);
|
2020-11-18 04:19:25 +08:00
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
2020-11-28 19:43:43 +08:00
|
|
|
if let Some(value) = self.facet_level_group_size {
|
|
|
|
builder.level_group_size(value);
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
2020-11-28 19:43:43 +08:00
|
|
|
if let Some(value) = self.facet_min_level_size {
|
|
|
|
builder.min_level_size(value);
|
2020-11-18 23:29:07 +08:00
|
|
|
}
|
2020-11-18 04:19:25 +08:00
|
|
|
builder.execute()?;
|
2021-03-12 00:23:46 +08:00
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
databases_seen += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-02-10 18:53:13 +08:00
|
|
|
// Run the words prefixes update operation.
|
2021-03-25 18:10:12 +08:00
|
|
|
let mut builder = WordsPrefixesFst::new(self.wtxn, self.index, self.update_id);
|
2021-02-10 18:53:13 +08:00
|
|
|
if let Some(value) = self.words_prefix_threshold {
|
|
|
|
builder.threshold(value);
|
|
|
|
}
|
|
|
|
if let Some(value) = self.max_prefix_length {
|
|
|
|
builder.max_prefix_length(value);
|
|
|
|
}
|
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
databases_seen += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-03-25 18:10:12 +08:00
|
|
|
// Run the word prefix docids update operation.
|
|
|
|
let mut builder = WordPrefixDocids::new(self.wtxn, self.index);
|
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
|
|
|
builder.max_nb_chunks = self.max_nb_chunks;
|
|
|
|
builder.max_memory = self.max_memory;
|
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
databases_seen += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-03-25 18:10:12 +08:00
|
|
|
// Run the word prefix pair proximity docids update operation.
|
|
|
|
let mut builder = WordPrefixPairProximityDocids::new(self.wtxn, self.index);
|
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
|
|
|
builder.max_nb_chunks = self.max_nb_chunks;
|
|
|
|
builder.max_memory = self.max_memory;
|
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
databases_seen += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-03-17 20:55:24 +08:00
|
|
|
// Run the words level positions update operation.
|
2021-03-25 18:10:12 +08:00
|
|
|
let mut builder = WordsLevelPositions::new(self.wtxn, self.index);
|
2021-03-17 20:55:24 +08:00
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
|
|
|
if let Some(value) = self.words_positions_level_group_size {
|
|
|
|
builder.level_group_size(value);
|
|
|
|
}
|
|
|
|
if let Some(value) = self.words_positions_min_level_size {
|
|
|
|
builder.min_level_size(value);
|
|
|
|
}
|
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 16:56:06 +08:00
|
|
|
databases_seen += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2020-10-27 03:18:10 +08:00
|
|
|
Ok(())
|
2020-10-26 18:02:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 19:14:25 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-24 00:35:44 +08:00
|
|
|
use std::io::Cursor;
|
|
|
|
|
2021-07-06 17:40:45 +08:00
|
|
|
use big_s::S;
|
2020-10-30 19:14:25 +08:00
|
|
|
use heed::EnvOpenOptions;
|
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
use super::*;
|
2021-06-30 17:23:29 +08:00
|
|
|
use crate::update::DeleteDocuments;
|
2021-07-06 17:40:45 +08:00
|
|
|
use crate::HashMap;
|
2021-06-17 00:33:33 +08:00
|
|
|
|
2020-10-30 19:14:25 +08:00
|
|
|
#[test]
|
2020-10-30 20:46:56 +08:00
|
|
|
fn simple_document_replacement() {
|
2020-10-30 19:14:25 +08:00
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,name\n1,kevin\n2,kevina\n3,benoit\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 19:14:25 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document with id 1, to erase the previous ones.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,name\n1,updated kevin\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 19:14:25 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2020-10-30 20:46:56 +08:00
|
|
|
// Check that there is **always** 3 documents.
|
2020-10-30 19:14:25 +08:00
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Third we send 3 documents again to replace the existing ones.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,name\n1,updated second kevin\n2,updated kevina\n3,updated benoit\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 2);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 19:14:25 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2020-10-30 20:46:56 +08:00
|
|
|
// Check that there is **always** 3 documents.
|
2020-10-30 19:14:25 +08:00
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-30 20:46:56 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_document_merge() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with duplicate ids and
|
|
|
|
// change the index method to merge documents.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,name\n1,kevin\n1,kevina\n1,benoit\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-10-30 20:46:56 +08:00
|
|
|
builder.index_documents_method(IndexDocumentsMethod::UpdateDocuments);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 20:46:56 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is only 1 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
|
|
|
|
// Check that we get only one document from the database.
|
|
|
|
let docs = index.documents(&rtxn, Some(0)).unwrap();
|
|
|
|
assert_eq!(docs.len(), 1);
|
|
|
|
let (id, doc) = docs[0];
|
|
|
|
assert_eq!(id, 0);
|
|
|
|
|
|
|
|
// Check that this document is equal to the last one sent.
|
|
|
|
let mut doc_iter = doc.iter();
|
|
|
|
assert_eq!(doc_iter.next(), Some((0, &br#""1""#[..])));
|
|
|
|
assert_eq!(doc_iter.next(), Some((1, &br#""benoit""#[..])));
|
|
|
|
assert_eq!(doc_iter.next(), None);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document with id 1, to force it to be merged with the previous one.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,age\n1,25\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-10-30 20:46:56 +08:00
|
|
|
builder.index_documents_method(IndexDocumentsMethod::UpdateDocuments);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 20:46:56 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is **always** 1 document.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
|
|
|
|
// Check that we get only one document from the database.
|
|
|
|
let docs = index.documents(&rtxn, Some(0)).unwrap();
|
|
|
|
assert_eq!(docs.len(), 1);
|
|
|
|
let (id, doc) = docs[0];
|
|
|
|
assert_eq!(id, 0);
|
|
|
|
|
|
|
|
// Check that this document is equal to the last one sent.
|
|
|
|
let mut doc_iter = doc.iter();
|
|
|
|
assert_eq!(doc_iter.next(), Some((0, &br#""1""#[..])));
|
|
|
|
assert_eq!(doc_iter.next(), Some((1, &br#""benoit""#[..])));
|
|
|
|
assert_eq!(doc_iter.next(), Some((2, &br#""25""#[..])));
|
|
|
|
assert_eq!(doc_iter.next(), None);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-31 19:54:43 +08:00
|
|
|
|
2020-11-01 04:46:55 +08:00
|
|
|
#[test]
|
|
|
|
fn not_auto_generated_csv_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"name\nkevin\nkevina\nbenoit\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-01 04:46:55 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
assert!(builder.execute(content, |_, _| ()).is_err());
|
2020-11-01 04:46:55 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is no document.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 0);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_auto_generated_json_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents and 2 without ids.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &br#"[
|
|
|
|
{ "name": "kevina", "id": 21 },
|
|
|
|
{ "name": "kevin" },
|
|
|
|
{ "name": "benoit" }
|
|
|
|
]"#[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-01 04:46:55 +08:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 23:21:07 +08:00
|
|
|
assert!(builder.execute(content, |_, _| ()).is_err());
|
2020-11-01 04:46:55 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is no document.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 0);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
2020-10-31 19:54:43 +08:00
|
|
|
#[test]
|
|
|
|
fn simple_auto_generated_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"name\nkevin\nkevina\nbenoit\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2021-05-01 02:34:29 +08:00
|
|
|
builder.enable_autogenerate_docids();
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 19:54:43 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
|
|
|
|
let docs = index.documents(&rtxn, vec![0, 1, 2]).unwrap();
|
|
|
|
let (_id, obkv) = docs.iter().find(|(_id, kv)| kv.get(0) == Some(br#""kevin""#)).unwrap();
|
|
|
|
let kevin_uuid: String = serde_json::from_slice(&obkv.get(1).unwrap()).unwrap();
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document with the generated uuid, to erase the previous ones.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = format!("id,name\n{},updated kevin", kevin_uuid);
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content.as_bytes(), |_, _| ()).unwrap();
|
2020-10-31 19:54:43 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is **always** 3 documents.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
2020-11-01 19:14:44 +08:00
|
|
|
|
|
|
|
let docs = index.documents(&rtxn, vec![0, 1, 2]).unwrap();
|
2021-06-17 00:33:33 +08:00
|
|
|
let (kevin_id, _) =
|
|
|
|
docs.iter().find(|(_, d)| d.get(0).unwrap() == br#""updated kevin""#).unwrap();
|
2020-11-01 19:14:44 +08:00
|
|
|
let (id, doc) = docs[*kevin_id as usize];
|
|
|
|
assert_eq!(id, *kevin_id);
|
|
|
|
|
|
|
|
// Check that this document is equal to the last
|
|
|
|
// one sent and that an UUID has been generated.
|
2020-11-01 23:43:12 +08:00
|
|
|
assert_eq!(doc.get(0), Some(&br#""updated kevin""#[..]));
|
2020-11-01 19:14:44 +08:00
|
|
|
// This is an UUID, it must be 36 bytes long plus the 2 surrounding string quotes (").
|
2021-03-31 23:14:23 +08:00
|
|
|
assert_eq!(doc.get(1).unwrap().len(), 36 + 2);
|
2020-10-31 19:54:43 +08:00
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reordered_auto_generated_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,name\n1,kevin\n2,kevina\n3,benoit\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 19:54:43 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document without specifying the id.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"name\nnew kevin"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 19:54:43 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 4 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 4);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-31 23:10:15 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_csv_update() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 0 documents and only headers.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"id,name\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 23:10:15 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is no documents.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 0);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn json_documents() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with an id for only one of them.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &br#"[
|
|
|
|
{ "name": "kevin" },
|
2020-11-01 00:25:07 +08:00
|
|
|
{ "name": "kevina", "id": 21 },
|
2020-10-31 23:10:15 +08:00
|
|
|
{ "name": "benoit" }
|
|
|
|
]"#[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2021-05-01 02:34:29 +08:00
|
|
|
builder.enable_autogenerate_docids();
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 23:10:15 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_json_update() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 0 documents.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &b"[]"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2021-05-01 02:34:29 +08:00
|
|
|
builder.enable_autogenerate_docids();
|
2020-10-31 23:10:15 +08:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 23:10:15 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is no documents.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 0);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-11-01 18:50:10 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn json_stream_documents() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with an id for only one of them.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &br#"
|
|
|
|
{ "name": "kevin" }
|
|
|
|
{ "name": "kevina", "id": 21 }
|
|
|
|
{ "name": "benoit" }
|
|
|
|
"#[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2021-05-01 02:34:29 +08:00
|
|
|
builder.enable_autogenerate_docids();
|
2020-11-01 18:50:10 +08:00
|
|
|
builder.update_format(UpdateFormat::JsonStream);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-11-01 18:50:10 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-11-01 23:43:12 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 1 document with an invalid id.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
// There is a space in the document id.
|
|
|
|
let content = &b"id,name\nbrume bleue,kevin\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-01 23:43:12 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
assert!(builder.execute(content, |_, _| ()).is_err());
|
2020-11-01 23:43:12 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// First we send 1 document with a valid id.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
// There is a space in the document id.
|
|
|
|
let content = &b"id,name\n32,kevin\n"[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-11-01 23:43:12 +08:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-11-01 23:43:12 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 1 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-11-06 23:15:07 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_json_documents() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with an id for only one of them.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &br#"[
|
|
|
|
{ "id": 0, "name": "kevin", "object": { "key1": "value1", "key2": "value2" } },
|
|
|
|
{ "id": 1, "name": "kevina", "array": ["I", "am", "fine"] },
|
|
|
|
{ "id": 2, "name": "benoit", "array_of_object": [{ "wow": "amazing" }] }
|
|
|
|
]"#[..];
|
2020-12-22 23:21:07 +08:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-06 23:15:07 +08:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 23:21:07 +08:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-11-06 23:15:07 +08:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 1 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
// Search for a sub object value
|
|
|
|
let result = index.search(&rtxn).query(r#""value2""#).execute().unwrap();
|
|
|
|
assert_eq!(result.documents_ids, vec![0]);
|
|
|
|
|
|
|
|
// Search for a sub array value
|
|
|
|
let result = index.search(&rtxn).query(r#""fine""#).execute().unwrap();
|
|
|
|
assert_eq!(result.documents_ids, vec![1]);
|
|
|
|
|
|
|
|
// Search for a sub array sub object key
|
|
|
|
let result = index.search(&rtxn).query(r#""wow""#).execute().unwrap();
|
|
|
|
assert_eq!(result.documents_ids, vec![2]);
|
|
|
|
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2021-06-24 00:35:44 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_documents_replace() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with an id for only one of them.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let documents = &r#"[
|
2021-08-26 19:27:32 +08:00
|
|
|
{ "id": 2, "title": "Pride and Prejudice", "author": "Jane Austin", "genre": "romance", "price": 3.5, "_geo": { "lat": 12, "lng": 42 } },
|
2021-06-24 00:35:44 +08:00
|
|
|
{ "id": 456, "title": "Le Petit Prince", "author": "Antoine de Saint-Exupéry", "genre": "adventure" , "price": 10.0 },
|
|
|
|
{ "id": 1, "title": "Alice In Wonderland", "author": "Lewis Carroll", "genre": "fantasy", "price": 25.99 },
|
|
|
|
{ "id": 1344, "title": "The Hobbit", "author": "J. R. R. Tolkien", "genre": "fantasy" },
|
|
|
|
{ "id": 4, "title": "Harry Potter and the Half-Blood Prince", "author": "J. K. Rowling", "genre": "fantasy" },
|
2021-08-26 19:27:32 +08:00
|
|
|
{ "id": 42, "title": "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "_geo": { "lat": 35, "lng": 23 } }
|
2021-06-24 00:35:44 +08:00
|
|
|
]"#[..];
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
|
|
|
builder.index_documents_method(IndexDocumentsMethod::ReplaceDocuments);
|
|
|
|
builder.execute(Cursor::new(documents), |_, _| ()).unwrap();
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
|
|
|
builder.index_documents_method(IndexDocumentsMethod::UpdateDocuments);
|
|
|
|
let documents = &r#"[
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"author": "J. Austen",
|
|
|
|
"date": "1813"
|
|
|
|
}
|
|
|
|
]"#[..];
|
|
|
|
|
|
|
|
builder.execute(Cursor::new(documents), |_, _| ()).unwrap();
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-06-30 17:23:29 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_documents_then_insert() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = &br#"[
|
|
|
|
{ "objectId": 123, "title": "Pride and Prejudice", "comment": "A great book" },
|
|
|
|
{ "objectId": 456, "title": "Le Petit Prince", "comment": "A french book" },
|
|
|
|
{ "objectId": 1, "title": "Alice In Wonderland", "comment": "A weird book" },
|
2021-08-26 19:27:32 +08:00
|
|
|
{ "objectId": 30, "title": "Hamlet", "_geo": { "lat": 12, "lng": 89 } }
|
2021-06-30 17:23:29 +08:00
|
|
|
]"#[..];
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(index.primary_key(&wtxn).unwrap(), Some("objectId"));
|
|
|
|
|
|
|
|
// Delete not all of the documents but some of them.
|
|
|
|
let mut builder = DeleteDocuments::new(&mut wtxn, &index, 1).unwrap();
|
|
|
|
builder.delete_external_id("30");
|
|
|
|
builder.execute().unwrap();
|
|
|
|
|
|
|
|
let external_documents_ids = index.external_documents_ids(&wtxn).unwrap();
|
|
|
|
assert!(external_documents_ids.get("30").is_none());
|
|
|
|
|
|
|
|
let content = &br#"[
|
2021-08-26 19:27:32 +08:00
|
|
|
{ "objectId": 30, "title": "Hamlet", "_geo": { "lat": 12, "lng": 89 } }
|
2021-06-30 17:23:29 +08:00
|
|
|
]"#[..];
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
|
|
|
|
|
|
|
let external_documents_ids = index.external_documents_ids(&wtxn).unwrap();
|
|
|
|
assert!(external_documents_ids.get("30").is_some());
|
|
|
|
|
|
|
|
let content = &br#"[
|
2021-08-26 19:27:32 +08:00
|
|
|
{ "objectId": 30, "title": "Hamlet", "_geo": { "lat": 12, "lng": 89 } }
|
2021-06-30 17:23:29 +08:00
|
|
|
]"#[..];
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-07-06 17:40:45 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_more_than_256_fields() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
|
|
|
|
let mut big_object = HashMap::new();
|
|
|
|
big_object.insert(S("id"), "wow");
|
|
|
|
for i in 0..1000 {
|
|
|
|
let key = i.to_string();
|
|
|
|
big_object.insert(key, "I am a text!");
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = vec![big_object];
|
|
|
|
let content = serde_json::to_string(&content).unwrap();
|
|
|
|
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
|
|
|
builder.execute(Cursor::new(content), |_, _| ()).unwrap();
|
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-07-22 23:14:44 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_documents_with_zeroes() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let content = r#"#id,title,au{hor,genre,price$
|
|
|
|
2,"Prideand Prejudice","Jane Austin","romance",3.5$
|
|
|
|
456,"Le Petit Prince","Antoine de Saint-Exupéry","adventure",10.0$
|
|
|
|
1,Wonderland","Lewis Carroll","fantasy",25.99$
|
|
|
|
4,"Harry Potter ing","fantasy\0lood Prince","J. K. Rowling","fantasy\0,
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
|
|
|
builder.update_format(UpdateFormat::Csv);
|
|
|
|
builder.execute(content.as_bytes(), |_, _| ()).unwrap();
|
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2020-10-30 19:14:25 +08:00
|
|
|
}
|