2020-10-26 20:18:10 +01:00
|
|
|
use std::borrow::Cow;
|
2020-11-03 13:42:29 +01:00
|
|
|
use std::collections::HashSet;
|
2020-10-26 20:18:10 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Seek, SeekFrom};
|
2020-11-17 21:19:25 +01:00
|
|
|
use std::num::NonZeroUsize;
|
2020-10-26 20:18:10 +01:00
|
|
|
use std::sync::mpsc::sync_channel;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
use bstr::ByteSlice as _;
|
2021-03-11 18:42:21 +01:00
|
|
|
use chrono::Utc;
|
2021-02-17 11:09:42 +01:00
|
|
|
use grenad::{MergerIter, Writer, Sorter, Merger, Reader, FileFuse, CompressionType};
|
2020-10-26 20:18:10 +01:00
|
|
|
use heed::types::ByteSlice;
|
|
|
|
use log::{debug, info, error};
|
2020-11-03 13:20:11 +01:00
|
|
|
use memmap::Mmap;
|
2020-11-02 19:11:22 +01:00
|
|
|
use rayon::ThreadPool;
|
2020-12-30 18:43:50 +01:00
|
|
|
use rayon::prelude::*;
|
|
|
|
use serde::{Serialize, Deserialize};
|
2020-11-03 13:20:11 +01:00
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
use crate::index::Index;
|
2021-03-11 17:23:46 +01:00
|
|
|
use crate::update::{Facets, WordsLevelPositions, WordsPrefixes, UpdateIndexingStep};
|
2020-11-13 14:49:48 +01:00
|
|
|
use self::store::{Store, Readers};
|
2021-02-17 11:12:38 +01:00
|
|
|
pub use self::merge_function::{
|
2020-10-26 20:18:10 +01:00
|
|
|
main_merge, word_docids_merge, words_pairs_proximities_docids_merge,
|
2021-03-11 17:23:46 +01:00
|
|
|
docid_word_positions_merge, documents_merge,
|
|
|
|
word_level_position_docids_merge, facet_field_value_docids_merge,
|
2020-12-02 18:31:41 +01:00
|
|
|
field_id_docid_facet_values_merge,
|
2020-10-26 20:18:10 +01:00
|
|
|
};
|
|
|
|
pub use self::transform::{Transform, TransformOutput};
|
|
|
|
|
2020-10-31 16:10:15 +01:00
|
|
|
use crate::MergeFn;
|
2020-10-26 20:18:10 +01:00
|
|
|
use super::UpdateBuilder;
|
|
|
|
|
|
|
|
mod merge_function;
|
|
|
|
mod store;
|
|
|
|
mod transform;
|
|
|
|
|
2020-12-30 18:43:50 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct DocumentAdditionResult {
|
|
|
|
nb_documents: usize,
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-11-17 21:19:25 +01:00
|
|
|
pub enum WriteMethod {
|
2020-10-26 20:18:10 +01:00
|
|
|
Append,
|
|
|
|
GetMergePut,
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:19:25 +01:00
|
|
|
pub fn create_writer(typ: CompressionType, level: Option<u32>, file: File) -> io::Result<Writer<File>> {
|
2020-10-26 20:18:10 +01:00
|
|
|
let mut builder = Writer::builder();
|
|
|
|
builder.compression_type(typ);
|
|
|
|
if let Some(level) = level {
|
|
|
|
builder.compression_level(level);
|
|
|
|
}
|
|
|
|
builder.build(file)
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:19:25 +01:00
|
|
|
pub fn create_sorter(
|
2020-10-26 20:18:10 +01:00
|
|
|
merge: MergeFn,
|
|
|
|
chunk_compression_type: CompressionType,
|
|
|
|
chunk_compression_level: Option<u32>,
|
|
|
|
chunk_fusing_shrink_size: Option<u64>,
|
|
|
|
max_nb_chunks: Option<usize>,
|
|
|
|
max_memory: Option<usize>,
|
|
|
|
) -> Sorter<MergeFn>
|
|
|
|
{
|
|
|
|
let mut builder = Sorter::builder(merge);
|
|
|
|
if let Some(shrink_size) = chunk_fusing_shrink_size {
|
|
|
|
builder.file_fusing_shrink_size(shrink_size);
|
|
|
|
}
|
|
|
|
builder.chunk_compression_type(chunk_compression_type);
|
|
|
|
if let Some(level) = chunk_compression_level {
|
|
|
|
builder.chunk_compression_level(level);
|
|
|
|
}
|
|
|
|
if let Some(nb_chunks) = max_nb_chunks {
|
|
|
|
builder.max_nb_chunks(nb_chunks);
|
|
|
|
}
|
|
|
|
if let Some(memory) = max_memory {
|
|
|
|
builder.max_memory(memory);
|
|
|
|
}
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:19:25 +01:00
|
|
|
pub fn writer_into_reader(writer: Writer<File>, shrink_size: Option<u64>) -> anyhow::Result<Reader<FileFuse>> {
|
2020-10-26 20:18:10 +01:00
|
|
|
let mut file = writer.into_inner()?;
|
|
|
|
file.seek(SeekFrom::Start(0))?;
|
|
|
|
let file = if let Some(shrink_size) = shrink_size {
|
|
|
|
FileFuse::builder().shrink_size(shrink_size).build(file)
|
|
|
|
} else {
|
|
|
|
FileFuse::new(file)
|
|
|
|
};
|
|
|
|
Reader::new(file).map_err(Into::into)
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:19:25 +01:00
|
|
|
pub fn merge_readers(sources: Vec<Reader<FileFuse>>, merge: MergeFn) -> Merger<FileFuse, MergeFn> {
|
2020-10-26 20:18:10 +01:00
|
|
|
let mut builder = Merger::builder(merge);
|
|
|
|
builder.extend(sources);
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:19:25 +01:00
|
|
|
pub fn merge_into_lmdb_database(
|
2020-10-26 20:18:10 +01:00
|
|
|
wtxn: &mut heed::RwTxn,
|
|
|
|
database: heed::PolyDatabase,
|
|
|
|
sources: Vec<Reader<FileFuse>>,
|
|
|
|
merge: MergeFn,
|
|
|
|
method: WriteMethod,
|
2021-02-17 11:09:42 +01:00
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
2020-10-26 20:18:10 +01:00
|
|
|
debug!("Merging {} MTBL stores...", sources.len());
|
|
|
|
let before = Instant::now();
|
|
|
|
|
|
|
|
let merger = merge_readers(sources, merge);
|
2021-02-17 11:09:42 +01:00
|
|
|
merger_iter_into_lmdb_database(
|
|
|
|
wtxn,
|
|
|
|
database,
|
|
|
|
merger.into_merge_iter()?,
|
|
|
|
merge,
|
|
|
|
method,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
debug!("MTBL stores merged in {:.02?}!", before.elapsed());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_into_lmdb_database(
|
|
|
|
wtxn: &mut heed::RwTxn,
|
|
|
|
database: heed::PolyDatabase,
|
|
|
|
mut reader: Reader<FileFuse>,
|
|
|
|
merge: MergeFn,
|
|
|
|
method: WriteMethod,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
|
|
|
debug!("Writing MTBL stores...");
|
|
|
|
let before = Instant::now();
|
2020-10-26 20:18:10 +01:00
|
|
|
|
|
|
|
match method {
|
|
|
|
WriteMethod::Append => {
|
|
|
|
let mut out_iter = database.iter_mut::<_, ByteSlice, ByteSlice>(wtxn)?;
|
2021-02-17 11:09:42 +01:00
|
|
|
while let Some((k, v)) = reader.next()? {
|
2020-10-29 13:52:00 +01:00
|
|
|
out_iter.append(k, v).with_context(|| {
|
|
|
|
format!("writing {:?} into LMDB", k.as_bstr())
|
|
|
|
})?;
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
WriteMethod::GetMergePut => {
|
2021-02-17 11:09:42 +01:00
|
|
|
while let Some((k, v)) = reader.next()? {
|
2020-10-30 11:13:45 +01:00
|
|
|
let mut iter = database.prefix_iter_mut::<_, ByteSlice, ByteSlice>(wtxn, k)?;
|
|
|
|
match iter.next().transpose()? {
|
|
|
|
Some((key, old_val)) if key == k => {
|
2020-10-26 20:18:10 +01:00
|
|
|
let vals = vec![Cow::Borrowed(old_val), Cow::Borrowed(v)];
|
2021-02-17 11:09:42 +01:00
|
|
|
let val = merge(k, &vals)?;
|
2020-10-30 11:13:45 +01:00
|
|
|
iter.put_current(k, &val)?;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
drop(iter);
|
|
|
|
database.put::<_, ByteSlice, ByteSlice>(wtxn, k, v)?;
|
2020-10-26 20:18:10 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 11:09:42 +01:00
|
|
|
}
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("MTBL stores merged in {:.02?}!", before.elapsed());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-17 11:09:42 +01:00
|
|
|
pub fn sorter_into_lmdb_database(
|
2020-10-26 20:18:10 +01:00
|
|
|
wtxn: &mut heed::RwTxn,
|
|
|
|
database: heed::PolyDatabase,
|
2021-02-17 11:09:42 +01:00
|
|
|
sorter: Sorter<MergeFn>,
|
2020-10-26 20:18:10 +01:00
|
|
|
merge: MergeFn,
|
|
|
|
method: WriteMethod,
|
2021-02-17 11:09:42 +01:00
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
|
|
|
debug!("Writing MTBL sorter...");
|
2020-10-26 20:18:10 +01:00
|
|
|
let before = Instant::now();
|
|
|
|
|
2021-02-17 11:09:42 +01:00
|
|
|
merger_iter_into_lmdb_database(
|
|
|
|
wtxn,
|
|
|
|
database,
|
|
|
|
sorter.into_iter()?,
|
|
|
|
merge,
|
|
|
|
method,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
debug!("MTBL sorter writen in {:.02?}!", before.elapsed());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merger_iter_into_lmdb_database<R: io::Read>(
|
|
|
|
wtxn: &mut heed::RwTxn,
|
|
|
|
database: heed::PolyDatabase,
|
|
|
|
mut sorter: MergerIter<R, MergeFn>,
|
|
|
|
merge: MergeFn,
|
|
|
|
method: WriteMethod,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
2020-10-26 20:18:10 +01:00
|
|
|
match method {
|
|
|
|
WriteMethod::Append => {
|
|
|
|
let mut out_iter = database.iter_mut::<_, ByteSlice, ByteSlice>(wtxn)?;
|
2021-02-17 11:09:42 +01:00
|
|
|
while let Some((k, v)) = sorter.next()? {
|
2020-10-29 13:52:00 +01:00
|
|
|
out_iter.append(k, v).with_context(|| {
|
|
|
|
format!("writing {:?} into LMDB", k.as_bstr())
|
|
|
|
})?;
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
WriteMethod::GetMergePut => {
|
2021-02-17 11:09:42 +01:00
|
|
|
while let Some((k, v)) = sorter.next()? {
|
2020-10-30 11:13:45 +01:00
|
|
|
let mut iter = database.prefix_iter_mut::<_, ByteSlice, ByteSlice>(wtxn, k)?;
|
|
|
|
match iter.next().transpose()? {
|
|
|
|
Some((key, old_val)) if key == k => {
|
2020-10-26 20:18:10 +01:00
|
|
|
let vals = vec![Cow::Borrowed(old_val), Cow::Borrowed(v)];
|
2021-02-17 11:09:42 +01:00
|
|
|
let val = merge(k, &vals).expect("merge failed");
|
2020-10-30 11:13:45 +01:00
|
|
|
iter.put_current(k, &val)?;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
drop(iter);
|
|
|
|
database.put::<_, ByteSlice, ByteSlice>(wtxn, k, v)?;
|
2020-10-26 20:18:10 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 11:09:42 +01:00
|
|
|
},
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-26 11:02:44 +01:00
|
|
|
|
2020-12-22 18:17:35 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
2020-10-31 16:10:15 +01:00
|
|
|
#[non_exhaustive]
|
2020-10-26 11:02:44 +01: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,
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:17:35 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
2020-10-31 16:10:15 +01: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 11:50:10 +01:00
|
|
|
/// The given update is a JSON stream with a document on each line.
|
|
|
|
JsonStream,
|
2020-10-31 16:10:15 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 19:11:22 +01:00
|
|
|
pub struct IndexDocuments<'t, 'u, 'i, 'a> {
|
2020-10-30 11:42:00 +01:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
2020-10-26 11:02:44 +01:00
|
|
|
index: &'i Index,
|
2020-11-01 14:55:06 +01:00
|
|
|
pub(crate) log_every_n: Option<usize>,
|
|
|
|
pub(crate) max_nb_chunks: Option<usize>,
|
|
|
|
pub(crate) max_memory: Option<usize>,
|
|
|
|
pub(crate) linked_hash_map_size: Option<usize>,
|
|
|
|
pub(crate) chunk_compression_type: CompressionType,
|
|
|
|
pub(crate) chunk_compression_level: Option<u32>,
|
|
|
|
pub(crate) chunk_fusing_shrink_size: Option<u64>,
|
2020-11-02 19:11:22 +01:00
|
|
|
pub(crate) thread_pool: Option<&'a ThreadPool>,
|
2020-11-28 12:43:43 +01:00
|
|
|
facet_level_group_size: Option<NonZeroUsize>,
|
|
|
|
facet_min_level_size: Option<NonZeroUsize>,
|
2021-02-10 11:53:13 +01:00
|
|
|
words_prefix_threshold: Option<f64>,
|
|
|
|
max_prefix_length: Option<usize>,
|
2021-03-17 13:55:24 +01:00
|
|
|
words_positions_level_group_size: Option<NonZeroUsize>,
|
|
|
|
words_positions_min_level_size: Option<NonZeroUsize>,
|
2020-10-26 11:02:44 +01:00
|
|
|
update_method: IndexDocumentsMethod,
|
2020-10-31 16:10:15 +01:00
|
|
|
update_format: UpdateFormat,
|
2020-10-31 21:46:55 +01:00
|
|
|
autogenerate_docids: bool,
|
2020-12-22 16:21:07 +01:00
|
|
|
update_id: u64,
|
2020-10-26 11:02:44 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 19:11:22 +01:00
|
|
|
impl<'t, 'u, 'i, 'a> IndexDocuments<'t, 'u, 'i, 'a> {
|
2020-12-22 16:21:07 +01:00
|
|
|
pub fn new(
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
|
|
|
update_id: u64,
|
|
|
|
) -> IndexDocuments<'t, 'u, 'i, 'a> {
|
2020-10-26 20:18:10 +01:00
|
|
|
IndexDocuments {
|
|
|
|
wtxn,
|
|
|
|
index,
|
|
|
|
log_every_n: None,
|
|
|
|
max_nb_chunks: None,
|
|
|
|
max_memory: None,
|
|
|
|
linked_hash_map_size: None,
|
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
|
|
|
chunk_fusing_shrink_size: None,
|
2020-11-02 19:11:22 +01:00
|
|
|
thread_pool: None,
|
2020-11-28 12:43:43 +01:00
|
|
|
facet_level_group_size: None,
|
|
|
|
facet_min_level_size: None,
|
2021-02-10 11:53:13 +01:00
|
|
|
words_prefix_threshold: None,
|
|
|
|
max_prefix_length: None,
|
2021-03-17 13:55:24 +01:00
|
|
|
words_positions_level_group_size: None,
|
|
|
|
words_positions_min_level_size: None,
|
2020-10-31 16:10:15 +01:00
|
|
|
update_method: IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
update_format: UpdateFormat::Json,
|
2020-10-31 21:46:55 +01:00
|
|
|
autogenerate_docids: true,
|
2020-12-22 16:21:07 +01:00
|
|
|
update_id,
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:55:06 +01:00
|
|
|
pub fn index_documents_method(&mut self, method: IndexDocumentsMethod) {
|
2020-10-26 11:02:44 +01:00
|
|
|
self.update_method = method;
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:55:06 +01:00
|
|
|
pub fn update_format(&mut self, format: UpdateFormat) {
|
2020-10-31 16:10:15 +01:00
|
|
|
self.update_format = format;
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:55:06 +01:00
|
|
|
pub fn enable_autogenerate_docids(&mut self) {
|
2020-10-31 21:46:55 +01:00
|
|
|
self.autogenerate_docids = true;
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:55:06 +01:00
|
|
|
pub fn disable_autogenerate_docids(&mut self) {
|
2020-10-31 21:46:55 +01:00
|
|
|
self.autogenerate_docids = false;
|
|
|
|
}
|
|
|
|
|
2020-12-30 18:43:50 +01:00
|
|
|
pub fn execute<R, F>(self, reader: R, progress_callback: F) -> anyhow::Result<DocumentAdditionResult>
|
2020-10-26 20:18:10 +01:00
|
|
|
where
|
|
|
|
R: io::Read,
|
2020-12-22 16:21:07 +01:00
|
|
|
F: Fn(UpdateIndexingStep, u64) + Sync,
|
2020-10-26 20:18:10 +01:00
|
|
|
{
|
2021-03-11 18:42:21 +01:00
|
|
|
self.index.set_updated_at(self.wtxn, &Utc::now())?;
|
2020-11-03 13:20:11 +01:00
|
|
|
let before_transform = Instant::now();
|
2020-12-22 16:21:07 +01:00
|
|
|
let update_id = self.update_id;
|
|
|
|
let progress_callback = |step| progress_callback(step, update_id);
|
2020-10-26 20:18:10 +01:00
|
|
|
|
|
|
|
let transform = Transform {
|
|
|
|
rtxn: &self.wtxn,
|
|
|
|
index: self.index,
|
2020-11-11 12:39:09 +01:00
|
|
|
log_every_n: self.log_every_n,
|
2020-10-26 20:18:10 +01:00
|
|
|
chunk_compression_type: self.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.chunk_compression_level,
|
|
|
|
chunk_fusing_shrink_size: self.chunk_fusing_shrink_size,
|
|
|
|
max_nb_chunks: self.max_nb_chunks,
|
|
|
|
max_memory: self.max_memory,
|
|
|
|
index_documents_method: self.update_method,
|
2020-10-31 21:46:55 +01:00
|
|
|
autogenerate_docids: self.autogenerate_docids,
|
2020-10-26 20:18:10 +01:00
|
|
|
};
|
|
|
|
|
2020-10-31 16:10:15 +01:00
|
|
|
let output = match self.update_format {
|
2020-12-01 14:25:17 +01:00
|
|
|
UpdateFormat::Csv => transform.output_from_csv(reader, &progress_callback)?,
|
|
|
|
UpdateFormat::Json => transform.output_from_json(reader, &progress_callback)?,
|
|
|
|
UpdateFormat::JsonStream => transform.output_from_json_stream(reader, &progress_callback)?,
|
2020-10-31 16:10:15 +01:00
|
|
|
};
|
|
|
|
|
2020-12-30 18:43:50 +01:00
|
|
|
let nb_documents = output.documents_count;
|
|
|
|
|
2020-11-03 13:20:11 +01:00
|
|
|
info!("Update transformed in {:.02?}", before_transform.elapsed());
|
|
|
|
|
2020-12-30 18:43:50 +01:00
|
|
|
self.execute_raw(output, progress_callback)?;
|
|
|
|
Ok(DocumentAdditionResult { nb_documents })
|
2020-11-03 13:20:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute_raw<F>(self, output: TransformOutput, progress_callback: F) -> anyhow::Result<()>
|
|
|
|
where
|
2020-11-11 12:39:09 +01:00
|
|
|
F: Fn(UpdateIndexingStep) + Sync
|
2020-11-03 13:20:11 +01:00
|
|
|
{
|
|
|
|
let before_indexing = Instant::now();
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
let TransformOutput {
|
2020-10-31 12:54:43 +01:00
|
|
|
primary_key,
|
2020-10-26 20:18:10 +01:00
|
|
|
fields_ids_map,
|
2021-03-31 17:14:23 +02:00
|
|
|
fields_distribution,
|
2020-11-22 11:54:04 +01:00
|
|
|
external_documents_ids,
|
2020-10-26 20:18:10 +01:00
|
|
|
new_documents_ids,
|
|
|
|
replaced_documents_ids,
|
|
|
|
documents_count,
|
|
|
|
documents_file,
|
2020-10-31 16:10:15 +01:00
|
|
|
} = output;
|
2020-10-26 20:18:10 +01: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,
|
|
|
|
linked_hash_map_size: self.linked_hash_map_size,
|
|
|
|
chunk_compression_type: self.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.chunk_compression_level,
|
|
|
|
chunk_fusing_shrink_size: self.chunk_fusing_shrink_size,
|
2020-11-02 19:11:22 +01:00
|
|
|
thread_pool: self.thread_pool,
|
2020-12-22 16:21:07 +01:00
|
|
|
update_id: self.update_id,
|
2020-10-26 20:18:10 +01:00
|
|
|
};
|
|
|
|
let mut deletion_builder = update_builder.delete_documents(self.wtxn, self.index)?;
|
2020-11-19 11:18:52 +01:00
|
|
|
debug!("documents to delete {:?}", replaced_documents_ids);
|
2020-10-26 20:18:10 +01:00
|
|
|
deletion_builder.delete_documents(&replaced_documents_ids);
|
2020-11-19 11:18:52 +01:00
|
|
|
let deleted_documents_count = deletion_builder.execute()?;
|
|
|
|
debug!("{} documents actually deleted", deleted_documents_count);
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 13:20:11 +01:00
|
|
|
let mmap;
|
|
|
|
let bytes = if documents_count == 0 {
|
|
|
|
&[][..]
|
2020-10-31 16:10:15 +01:00
|
|
|
} else {
|
2020-11-03 13:20:11 +01:00
|
|
|
mmap = unsafe { Mmap::map(&documents_file).context("mmaping the transform documents file")? };
|
|
|
|
&mmap
|
2020-10-26 20:18:10 +01:00
|
|
|
};
|
2020-10-31 16:10:15 +01:00
|
|
|
|
|
|
|
let documents = grenad::Reader::new(bytes).unwrap();
|
2020-10-26 20:18:10 +01:00
|
|
|
|
|
|
|
// The enum which indicates the type of the readers
|
|
|
|
// merges that are potentially done on different threads.
|
|
|
|
enum DatabaseType {
|
|
|
|
Main,
|
|
|
|
WordDocids,
|
2021-03-11 17:23:46 +01:00
|
|
|
WordLevel0PositionDocids,
|
2020-11-18 16:29:07 +01:00
|
|
|
FacetLevel0ValuesDocids,
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 17:27:43 +01:00
|
|
|
let faceted_fields = self.index.faceted_fields_ids(self.wtxn)?;
|
|
|
|
let searchable_fields: HashSet<_> = match self.index.searchable_fields_ids(self.wtxn)? {
|
2020-11-03 13:42:29 +01:00
|
|
|
Some(fields) => fields.iter().copied().collect(),
|
|
|
|
None => fields_ids_map.iter().map(|(id, _name)| id).collect(),
|
|
|
|
};
|
|
|
|
|
2021-03-29 19:15:47 +02:00
|
|
|
let stop_words = self.index.stop_words(self.wtxn)?;
|
|
|
|
let stop_words = stop_words.as_ref();
|
2020-10-26 20:18:10 +01:00
|
|
|
let linked_hash_map_size = self.linked_hash_map_size;
|
|
|
|
let max_nb_chunks = self.max_nb_chunks;
|
|
|
|
let max_memory = self.max_memory;
|
|
|
|
let chunk_compression_type = self.chunk_compression_type;
|
|
|
|
let chunk_compression_level = self.chunk_compression_level;
|
|
|
|
let log_every_n = self.log_every_n;
|
|
|
|
let chunk_fusing_shrink_size = self.chunk_fusing_shrink_size;
|
|
|
|
|
2020-11-02 19:11:22 +01:00
|
|
|
let backup_pool;
|
|
|
|
let pool = match self.thread_pool {
|
|
|
|
Some(pool) => pool,
|
|
|
|
None => {
|
|
|
|
// We initialize a bakcup pool with the default
|
|
|
|
// settings if none have already been set.
|
|
|
|
backup_pool = rayon::ThreadPoolBuilder::new().build()?;
|
|
|
|
&backup_pool
|
|
|
|
},
|
|
|
|
};
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2020-11-11 11:25:31 +01:00
|
|
|
let readers = pool.install(|| {
|
2020-10-26 20:18:10 +01:00
|
|
|
let num_threads = rayon::current_num_threads();
|
|
|
|
let max_memory_by_job = max_memory.map(|mm| mm / num_threads);
|
|
|
|
|
|
|
|
let readers = rayon::iter::repeatn(documents, num_threads)
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, documents)| {
|
|
|
|
let store = Store::new(
|
2021-04-07 14:52:51 +02:00
|
|
|
primary_key.clone(),
|
|
|
|
fields_ids_map.clone(),
|
2020-11-03 13:42:29 +01:00
|
|
|
searchable_fields.clone(),
|
2020-11-11 17:33:05 +01:00
|
|
|
faceted_fields.clone(),
|
2020-10-26 20:18:10 +01:00
|
|
|
linked_hash_map_size,
|
|
|
|
max_nb_chunks,
|
|
|
|
max_memory_by_job,
|
|
|
|
chunk_compression_type,
|
|
|
|
chunk_compression_level,
|
|
|
|
chunk_fusing_shrink_size,
|
2021-03-29 19:15:47 +02:00
|
|
|
stop_words,
|
2020-10-26 20:18:10 +01:00
|
|
|
)?;
|
|
|
|
store.index(
|
|
|
|
documents,
|
|
|
|
documents_count,
|
|
|
|
i,
|
|
|
|
num_threads,
|
|
|
|
log_every_n,
|
|
|
|
&progress_callback,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
|
|
|
let mut main_readers = Vec::with_capacity(readers.len());
|
|
|
|
let mut word_docids_readers = Vec::with_capacity(readers.len());
|
|
|
|
let mut docid_word_positions_readers = Vec::with_capacity(readers.len());
|
|
|
|
let mut words_pairs_proximities_docids_readers = Vec::with_capacity(readers.len());
|
2021-03-11 17:23:46 +01:00
|
|
|
let mut word_level_position_docids_readers = Vec::with_capacity(readers.len());
|
2020-11-13 14:49:48 +01:00
|
|
|
let mut facet_field_value_docids_readers = Vec::with_capacity(readers.len());
|
2020-12-02 18:31:41 +01:00
|
|
|
let mut field_id_docid_facet_values_readers = Vec::with_capacity(readers.len());
|
2020-10-26 20:18:10 +01:00
|
|
|
let mut documents_readers = Vec::with_capacity(readers.len());
|
|
|
|
readers.into_iter().for_each(|readers| {
|
2020-11-13 14:49:48 +01:00
|
|
|
let Readers {
|
|
|
|
main,
|
|
|
|
word_docids,
|
|
|
|
docid_word_positions,
|
|
|
|
words_pairs_proximities_docids,
|
2021-03-11 17:23:46 +01:00
|
|
|
word_level_position_docids,
|
2020-11-13 14:49:48 +01:00
|
|
|
facet_field_value_docids,
|
2020-12-02 18:31:41 +01:00
|
|
|
field_id_docid_facet_values,
|
2020-11-13 14:49:48 +01:00
|
|
|
documents
|
|
|
|
} = readers;
|
|
|
|
main_readers.push(main);
|
|
|
|
word_docids_readers.push(word_docids);
|
|
|
|
docid_word_positions_readers.push(docid_word_positions);
|
|
|
|
words_pairs_proximities_docids_readers.push(words_pairs_proximities_docids);
|
2021-03-11 17:23:46 +01:00
|
|
|
word_level_position_docids_readers.push(word_level_position_docids);
|
2020-11-13 14:49:48 +01:00
|
|
|
facet_field_value_docids_readers.push(facet_field_value_docids);
|
2020-12-02 18:31:41 +01:00
|
|
|
field_id_docid_facet_values_readers.push(field_id_docid_facet_values);
|
2020-11-13 14:49:48 +01:00
|
|
|
documents_readers.push(documents);
|
2020-10-26 20:18:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// This is the function that merge the readers
|
|
|
|
// by using the given merge function.
|
|
|
|
let merge_readers = move |readers, merge| {
|
|
|
|
let mut writer = tempfile::tempfile().and_then(|f| {
|
|
|
|
create_writer(chunk_compression_type, chunk_compression_level, f)
|
|
|
|
})?;
|
|
|
|
let merger = merge_readers(readers, merge);
|
|
|
|
merger.write_into(&mut writer)?;
|
|
|
|
writer_into_reader(writer, chunk_fusing_shrink_size)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The enum and the channel which is used to transfert
|
|
|
|
// the readers merges potentially done on another thread.
|
2020-11-11 11:25:31 +01:00
|
|
|
let (sender, receiver) = sync_channel(2);
|
2020-10-26 20:18:10 +01:00
|
|
|
|
|
|
|
debug!("Merging the main, word docids and words pairs proximity docids in parallel...");
|
|
|
|
rayon::spawn(move || {
|
|
|
|
vec![
|
|
|
|
(DatabaseType::Main, main_readers, main_merge as MergeFn),
|
|
|
|
(DatabaseType::WordDocids, word_docids_readers, word_docids_merge),
|
2020-11-13 14:49:48 +01:00
|
|
|
(
|
2020-11-18 16:29:07 +01:00
|
|
|
DatabaseType::FacetLevel0ValuesDocids,
|
2020-11-13 14:49:48 +01:00
|
|
|
facet_field_value_docids_readers,
|
|
|
|
facet_field_value_docids_merge,
|
|
|
|
),
|
2021-03-11 17:23:46 +01:00
|
|
|
(
|
|
|
|
DatabaseType::WordLevel0PositionDocids,
|
|
|
|
word_level_position_docids_readers,
|
|
|
|
word_level_position_docids_merge,
|
|
|
|
),
|
2020-10-26 20:18:10 +01:00
|
|
|
]
|
|
|
|
.into_par_iter()
|
|
|
|
.for_each(|(dbtype, readers, merge)| {
|
|
|
|
let result = merge_readers(readers, merge);
|
|
|
|
if let Err(e) = sender.send((dbtype, result)) {
|
|
|
|
error!("sender error: {}", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-11 11:25:31 +01:00
|
|
|
Ok((
|
|
|
|
receiver,
|
|
|
|
docid_word_positions_readers,
|
|
|
|
documents_readers,
|
|
|
|
words_pairs_proximities_docids_readers,
|
2020-12-02 18:31:41 +01:00
|
|
|
field_id_docid_facet_values_readers,
|
2020-11-11 11:25:31 +01:00
|
|
|
)) as anyhow::Result<_>
|
2020-10-26 20:18:10 +01:00
|
|
|
})?;
|
|
|
|
|
2020-11-11 11:25:31 +01:00
|
|
|
let (
|
|
|
|
receiver,
|
|
|
|
docid_word_positions_readers,
|
|
|
|
documents_readers,
|
|
|
|
words_pairs_proximities_docids_readers,
|
2020-12-02 18:31:41 +01:00
|
|
|
field_id_docid_facet_values_readers,
|
2020-11-11 11:25:31 +01:00
|
|
|
) = readers;
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
let mut documents_ids = self.index.documents_ids(self.wtxn)?;
|
|
|
|
let contains_documents = !documents_ids.is_empty();
|
|
|
|
let write_method = if contains_documents {
|
|
|
|
WriteMethod::GetMergePut
|
|
|
|
} else {
|
|
|
|
WriteMethod::Append
|
|
|
|
};
|
|
|
|
|
2020-10-28 11:17:36 +01:00
|
|
|
debug!("Writing using the write method: {:?}", write_method);
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
// We write the fields ids map into the main database
|
|
|
|
self.index.put_fields_ids_map(self.wtxn, &fields_ids_map)?;
|
|
|
|
|
2021-03-31 17:14:23 +02:00
|
|
|
// We write the fields distribution into the main database
|
|
|
|
self.index.put_fields_distribution(self.wtxn, &fields_distribution)?;
|
|
|
|
|
2020-10-31 12:54:43 +01:00
|
|
|
// We write the primary key field id into the main database
|
2021-01-20 17:27:43 +01:00
|
|
|
self.index.put_primary_key(self.wtxn, &primary_key)?;
|
2020-10-31 12:54:43 +01:00
|
|
|
|
2020-11-22 11:54:04 +01:00
|
|
|
// We write the external documents ids into the main database.
|
|
|
|
self.index.put_external_documents_ids(self.wtxn, &external_documents_ids)?;
|
2020-10-26 20:18:10 +01:00
|
|
|
|
|
|
|
// We merge the new documents ids with the existing ones.
|
|
|
|
documents_ids.union_with(&new_documents_ids);
|
2020-10-29 13:52:00 +01:00
|
|
|
documents_ids.union_with(&replaced_documents_ids);
|
2020-10-26 20:18:10 +01:00
|
|
|
self.index.put_documents_ids(self.wtxn, &documents_ids)?;
|
|
|
|
|
2020-11-11 12:39:09 +01:00
|
|
|
let mut database_count = 0;
|
2021-03-11 17:23:46 +01:00
|
|
|
let total_databases = 8;
|
2020-11-13 14:49:48 +01:00
|
|
|
|
2020-11-11 12:39:09 +01:00
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: 0,
|
2020-11-13 14:49:48 +01:00
|
|
|
total_databases,
|
2020-11-11 12:39:09 +01:00
|
|
|
});
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
debug!("Writing the docid word positions into LMDB on disk...");
|
|
|
|
merge_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.docid_word_positions.as_polymorph(),
|
|
|
|
docid_word_positions_readers,
|
|
|
|
docid_word_positions_merge,
|
|
|
|
write_method
|
|
|
|
)?;
|
|
|
|
|
2020-11-11 12:39:09 +01:00
|
|
|
database_count += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: database_count,
|
2020-11-13 14:49:48 +01:00
|
|
|
total_databases,
|
2020-11-11 12:39:09 +01:00
|
|
|
});
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
debug!("Writing the documents into LMDB on disk...");
|
|
|
|
merge_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.documents.as_polymorph(),
|
|
|
|
documents_readers,
|
|
|
|
documents_merge,
|
|
|
|
write_method
|
|
|
|
)?;
|
|
|
|
|
2020-11-11 12:39:09 +01:00
|
|
|
database_count += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: database_count,
|
2020-11-13 14:49:48 +01:00
|
|
|
total_databases,
|
2020-11-11 12:39:09 +01:00
|
|
|
});
|
|
|
|
|
2020-12-02 18:31:41 +01:00
|
|
|
debug!("Writing the field id docid facet values into LMDB on disk...");
|
|
|
|
merge_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.field_id_docid_facet_values.as_polymorph(),
|
|
|
|
field_id_docid_facet_values_readers,
|
|
|
|
field_id_docid_facet_values_merge,
|
|
|
|
write_method,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
database_count += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: database_count,
|
|
|
|
total_databases,
|
|
|
|
});
|
|
|
|
|
2020-11-11 11:25:31 +01:00
|
|
|
debug!("Writing the words pairs proximities docids into LMDB on disk...");
|
|
|
|
merge_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.word_pair_proximity_docids.as_polymorph(),
|
|
|
|
words_pairs_proximities_docids_readers,
|
|
|
|
words_pairs_proximities_docids_merge,
|
|
|
|
write_method,
|
|
|
|
)?;
|
|
|
|
|
2020-11-11 12:39:09 +01:00
|
|
|
database_count += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: database_count,
|
2020-11-13 14:49:48 +01:00
|
|
|
total_databases,
|
2020-11-11 12:39:09 +01:00
|
|
|
});
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
for (db_type, result) in receiver {
|
|
|
|
let content = result?;
|
|
|
|
match db_type {
|
|
|
|
DatabaseType::Main => {
|
|
|
|
debug!("Writing the main elements into LMDB on disk...");
|
|
|
|
write_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
self.index.main,
|
|
|
|
content,
|
|
|
|
main_merge,
|
2020-10-29 13:52:00 +01:00
|
|
|
WriteMethod::GetMergePut,
|
2020-10-26 20:18:10 +01:00
|
|
|
)?;
|
|
|
|
},
|
|
|
|
DatabaseType::WordDocids => {
|
|
|
|
debug!("Writing the words docids into LMDB on disk...");
|
|
|
|
let db = *self.index.word_docids.as_polymorph();
|
|
|
|
write_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
db,
|
|
|
|
content,
|
|
|
|
word_docids_merge,
|
|
|
|
write_method,
|
|
|
|
)?;
|
|
|
|
},
|
2020-11-18 16:29:07 +01:00
|
|
|
DatabaseType::FacetLevel0ValuesDocids => {
|
2021-03-11 17:23:46 +01:00
|
|
|
debug!("Writing the facet level 0 values docids into LMDB on disk...");
|
2020-11-13 14:49:48 +01:00
|
|
|
let db = *self.index.facet_field_id_value_docids.as_polymorph();
|
|
|
|
write_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
db,
|
|
|
|
content,
|
|
|
|
facet_field_value_docids_merge,
|
|
|
|
write_method,
|
|
|
|
)?;
|
|
|
|
},
|
2021-03-11 17:23:46 +01:00
|
|
|
DatabaseType::WordLevel0PositionDocids => {
|
|
|
|
debug!("Writing the word level 0 positions docids into LMDB on disk...");
|
|
|
|
let db = *self.index.word_level_position_docids.as_polymorph();
|
|
|
|
write_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
db,
|
|
|
|
content,
|
|
|
|
word_level_position_docids_merge,
|
|
|
|
write_method,
|
|
|
|
)?;
|
|
|
|
}
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
2020-11-11 12:39:09 +01:00
|
|
|
|
|
|
|
database_count += 1;
|
|
|
|
progress_callback(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen: database_count,
|
2020-11-13 14:49:48 +01:00
|
|
|
total_databases,
|
2020-11-11 12:39:09 +01:00
|
|
|
});
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 11:53:13 +01:00
|
|
|
// Run the facets update operation.
|
2020-12-22 16:21:07 +01:00
|
|
|
let mut builder = Facets::new(self.wtxn, self.index, self.update_id);
|
2020-11-17 21:19:25 +01:00
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
|
|
|
builder.chunk_fusing_shrink_size = self.chunk_fusing_shrink_size;
|
2020-11-28 12:43:43 +01:00
|
|
|
if let Some(value) = self.facet_level_group_size {
|
|
|
|
builder.level_group_size(value);
|
2020-11-17 21:19:25 +01:00
|
|
|
}
|
2020-11-28 12:43:43 +01:00
|
|
|
if let Some(value) = self.facet_min_level_size {
|
|
|
|
builder.min_level_size(value);
|
2020-11-18 16:29:07 +01:00
|
|
|
}
|
2020-11-17 21:19:25 +01:00
|
|
|
builder.execute()?;
|
2021-03-11 17:23:46 +01:00
|
|
|
|
2021-02-10 11:53:13 +01:00
|
|
|
// Run the words prefixes update operation.
|
|
|
|
let mut builder = WordsPrefixes::new(self.wtxn, self.index, self.update_id);
|
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
|
|
|
builder.chunk_fusing_shrink_size = self.chunk_fusing_shrink_size;
|
|
|
|
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-03-17 13:55:24 +01:00
|
|
|
// Run the words level positions update operation.
|
|
|
|
let mut builder = WordsLevelPositions::new(self.wtxn, self.index, self.update_id);
|
|
|
|
builder.chunk_compression_type = self.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.chunk_compression_level;
|
|
|
|
builder.chunk_fusing_shrink_size = self.chunk_fusing_shrink_size;
|
|
|
|
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()?;
|
|
|
|
|
2020-11-13 14:49:48 +01:00
|
|
|
debug_assert_eq!(database_count, total_databases);
|
2020-11-11 12:39:09 +01:00
|
|
|
|
2020-11-03 13:20:11 +01:00
|
|
|
info!("Transform output indexed in {:.02?}", before_indexing.elapsed());
|
2020-10-26 20:18:10 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-10-26 11:02:44 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 12:14:25 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use heed::EnvOpenOptions;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-30 13:46:56 +01:00
|
|
|
fn simple_document_replacement() {
|
2020-10-30 12:14:25 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 12:14:25 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 12:14:25 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2020-10-30 13:46:56 +01:00
|
|
|
// Check that there is **always** 3 documents.
|
2020-10-30 12:14:25 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 2);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 12:14:25 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2020-10-30 13:46:56 +01:00
|
|
|
// Check that there is **always** 3 documents.
|
2020-10-30 12:14:25 +01:00
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-30 13:46:56 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-10-30 13:46:56 +01:00
|
|
|
builder.index_documents_method(IndexDocumentsMethod::UpdateDocuments);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 13:46:56 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-10-30 13:46:56 +01:00
|
|
|
builder.index_documents_method(IndexDocumentsMethod::UpdateDocuments);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-30 13:46:56 +01: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 12:54:43 +01:00
|
|
|
|
2020-10-31 21:46:55 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 21:46:55 +01:00
|
|
|
builder.disable_autogenerate_docids();
|
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
assert!(builder.execute(content, |_, _| ()).is_err());
|
2020-10-31 21:46:55 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 21:46:55 +01:00
|
|
|
builder.disable_autogenerate_docids();
|
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 16:21:07 +01:00
|
|
|
assert!(builder.execute(content, |_, _| ()).is_err());
|
2020-10-31 21:46:55 +01: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 12:54:43 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 12:54:43 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content.as_bytes(), |_, _| ()).unwrap();
|
2020-10-31 12:54:43 +01: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 12:14:44 +01:00
|
|
|
|
|
|
|
let docs = index.documents(&rtxn, vec![0, 1, 2]).unwrap();
|
2020-11-01 16:43:12 +01:00
|
|
|
let (kevin_id, _) = docs.iter().find(|(_, d)| {
|
|
|
|
d.get(0).unwrap() == br#""updated kevin""#
|
|
|
|
}).unwrap();
|
2020-11-01 12:14:44 +01: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 16:43:12 +01:00
|
|
|
assert_eq!(doc.get(0), Some(&br#""updated kevin""#[..]));
|
2020-11-01 12:14:44 +01:00
|
|
|
// This is an UUID, it must be 36 bytes long plus the 2 surrounding string quotes (").
|
2021-03-31 17:14:23 +02:00
|
|
|
assert_eq!(doc.get(1).unwrap().len(), 36 + 2);
|
2020-10-31 12:54:43 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 12:54:43 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 12:54:43 +01: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 16:10:15 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 16:10:15 +01: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-10-31 17:25:07 +01:00
|
|
|
{ "name": "kevina", "id": 21 },
|
2020-10-31 16:10:15 +01:00
|
|
|
{ "name": "benoit" }
|
|
|
|
]"#[..];
|
2020-12-22 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 16:10:15 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-10-31 16:10:15 +01:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-10-31 16:10:15 +01: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 11:50:10 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-01 11:50:10 +01:00
|
|
|
builder.update_format(UpdateFormat::JsonStream);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-11-01 11:50:10 +01: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 16:43:12 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-01 16:43:12 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
assert!(builder.execute(content, |_, _| ()).is_err());
|
2020-11-01 16:43:12 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 1);
|
2020-11-01 16:43:12 +01:00
|
|
|
builder.update_format(UpdateFormat::Csv);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-11-01 16:43:12 +01: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 16:15:07 +01: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 16:21:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, 0);
|
2020-11-06 16:15:07 +01:00
|
|
|
builder.update_format(UpdateFormat::Json);
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(content, |_, _| ()).unwrap();
|
2020-11-06 16:15:07 +01: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);
|
|
|
|
}
|
2020-10-30 12:14:25 +01:00
|
|
|
}
|