2020-10-23 20:11:00 +08:00
|
|
|
use std::borrow::Cow;
|
2021-06-17 23:05:34 +08:00
|
|
|
use std::collections::btree_map::Entry;
|
2020-10-23 20:11:00 +08:00
|
|
|
use std::fs::File;
|
2020-10-24 20:02:29 +08:00
|
|
|
use std::io::{Read, Seek, SeekFrom};
|
2020-11-01 18:50:10 +08:00
|
|
|
use std::iter::Peekable;
|
2021-06-14 22:46:19 +08:00
|
|
|
use std::result::Result as StdResult;
|
2020-11-22 18:28:35 +08:00
|
|
|
use std::time::Instant;
|
2020-10-23 20:11:00 +08:00
|
|
|
|
2020-10-24 22:23:08 +08:00
|
|
|
use grenad::CompressionType;
|
2020-11-22 18:28:35 +08:00
|
|
|
use log::info;
|
2020-10-23 20:11:00 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2020-10-31 23:10:15 +08:00
|
|
|
use serde_json::{Map, Value};
|
2020-10-23 20:11:00 +08:00
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
use super::merge_function::merge_two_obkvs;
|
|
|
|
use super::{create_sorter, create_writer, IndexDocumentsMethod};
|
|
|
|
use crate::error::{Error, InternalError, UserError};
|
2021-06-15 17:06:42 +08:00
|
|
|
use crate::index::db_name;
|
2021-06-17 00:33:33 +08:00
|
|
|
use crate::update::index_documents::merge_function::{keep_latest_obkv, merge_obkvs};
|
2020-11-11 19:39:09 +08:00
|
|
|
use crate::update::{AvailableDocumentsIds, UpdateIndexingStep};
|
2021-06-17 00:33:33 +08:00
|
|
|
use crate::{
|
2021-06-22 17:22:33 +08:00
|
|
|
ExternalDocumentsIds, FieldDistribution, FieldId, FieldsIdsMap, Index, MergeFn, Result, BEU32,
|
2021-06-17 00:33:33 +08:00
|
|
|
};
|
2020-10-24 22:23:08 +08:00
|
|
|
|
2021-01-21 00:27:43 +08:00
|
|
|
const DEFAULT_PRIMARY_KEY_NAME: &str = "id";
|
|
|
|
|
2020-10-23 20:11:00 +08:00
|
|
|
pub struct TransformOutput {
|
2021-01-21 00:27:43 +08:00
|
|
|
pub primary_key: String,
|
2020-10-23 20:11:00 +08:00
|
|
|
pub fields_ids_map: FieldsIdsMap,
|
2021-06-21 21:57:41 +08:00
|
|
|
pub field_distribution: FieldDistribution,
|
2020-11-23 00:53:33 +08:00
|
|
|
pub external_documents_ids: ExternalDocumentsIds<'static>,
|
2020-10-23 20:11:00 +08:00
|
|
|
pub new_documents_ids: RoaringBitmap,
|
|
|
|
pub replaced_documents_ids: RoaringBitmap,
|
|
|
|
pub documents_count: usize,
|
|
|
|
pub documents_file: File,
|
|
|
|
}
|
|
|
|
|
2020-11-22 18:54:04 +08:00
|
|
|
/// Extract the external ids, deduplicate and compute the new internal documents ids
|
2020-11-01 18:50:10 +08:00
|
|
|
/// and fields ids, writing all the documents under their internal ids into a final file.
|
|
|
|
///
|
|
|
|
/// Outputs the new `FieldsIdsMap`, the new `UsersIdsDocumentsIds` map, the new documents ids,
|
|
|
|
/// the replaced documents ids, the number of documents in this update and the file
|
|
|
|
/// containing all those documents.
|
2020-10-27 03:18:10 +08:00
|
|
|
pub struct Transform<'t, 'i> {
|
2020-10-31 19:54:43 +08:00
|
|
|
pub rtxn: &'t heed::RoTxn<'i>,
|
2020-10-27 03:18:10 +08:00
|
|
|
pub index: &'i Index,
|
2020-11-11 19:39:09 +08:00
|
|
|
pub log_every_n: Option<usize>,
|
2020-10-24 22:23:08 +08:00
|
|
|
pub chunk_compression_type: CompressionType,
|
|
|
|
pub chunk_compression_level: Option<u32>,
|
|
|
|
pub chunk_fusing_shrink_size: Option<u64>,
|
|
|
|
pub max_nb_chunks: Option<usize>,
|
|
|
|
pub max_memory: Option<usize>,
|
2020-10-27 03:18:10 +08:00
|
|
|
pub index_documents_method: IndexDocumentsMethod,
|
2020-11-01 04:46:55 +08:00
|
|
|
pub autogenerate_docids: bool,
|
2020-10-23 20:11:00 +08:00
|
|
|
}
|
|
|
|
|
2021-05-07 03:16:40 +08:00
|
|
|
fn is_primary_key(field: impl AsRef<str>) -> bool {
|
|
|
|
field.as_ref().to_lowercase().contains(DEFAULT_PRIMARY_KEY_NAME)
|
|
|
|
}
|
|
|
|
|
2020-10-27 03:18:10 +08:00
|
|
|
impl Transform<'_, '_> {
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn output_from_json<R, F>(self, reader: R, progress_callback: F) -> Result<TransformOutput>
|
2020-11-11 19:39:09 +08:00
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2020-12-01 21:25:17 +08:00
|
|
|
self.output_from_generic_json(reader, false, progress_callback)
|
2020-11-01 18:50:10 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
pub fn output_from_json_stream<R, F>(
|
|
|
|
self,
|
|
|
|
reader: R,
|
|
|
|
progress_callback: F,
|
|
|
|
) -> Result<TransformOutput>
|
2020-11-11 19:39:09 +08:00
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2020-12-01 21:25:17 +08:00
|
|
|
self.output_from_generic_json(reader, true, progress_callback)
|
2020-11-01 18:50:10 +08:00
|
|
|
}
|
|
|
|
|
2020-12-01 21:25:17 +08:00
|
|
|
fn output_from_generic_json<R, F>(
|
2020-11-11 19:39:09 +08:00
|
|
|
self,
|
|
|
|
reader: R,
|
|
|
|
is_stream: bool,
|
|
|
|
progress_callback: F,
|
2021-06-14 22:46:19 +08:00
|
|
|
) -> Result<TransformOutput>
|
2020-11-11 19:39:09 +08:00
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2020-10-31 23:10:15 +08:00
|
|
|
let mut fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
2020-11-22 18:54:04 +08:00
|
|
|
let external_documents_ids = self.index.external_documents_ids(self.rtxn).unwrap();
|
2020-10-31 23:10:15 +08:00
|
|
|
|
|
|
|
// Deserialize the whole batch of documents in memory.
|
2021-06-17 00:33:33 +08:00
|
|
|
let mut documents: Peekable<
|
|
|
|
Box<dyn Iterator<Item = serde_json::Result<Map<String, Value>>>>,
|
|
|
|
> = if is_stream {
|
2020-11-01 18:50:10 +08:00
|
|
|
let iter = serde_json::Deserializer::from_reader(reader).into_iter();
|
2021-06-17 00:33:33 +08:00
|
|
|
let iter = Box::new(iter) as Box<dyn Iterator<Item = _>>;
|
2020-11-01 18:50:10 +08:00
|
|
|
iter.peekable()
|
|
|
|
} else {
|
2021-06-14 22:46:19 +08:00
|
|
|
let vec: Vec<_> = serde_json::from_reader(reader).map_err(UserError::SerdeJson)?;
|
2020-11-01 18:50:10 +08:00
|
|
|
let iter = vec.into_iter().map(Ok);
|
2021-06-17 00:33:33 +08:00
|
|
|
let iter = Box::new(iter) as Box<dyn Iterator<Item = _>>;
|
2020-11-01 18:50:10 +08:00
|
|
|
iter.peekable()
|
|
|
|
};
|
2020-10-31 23:10:15 +08:00
|
|
|
|
|
|
|
// We extract the primary key from the first document in
|
2021-01-21 00:27:43 +08:00
|
|
|
// the batch if it hasn't already been defined in the index
|
2021-06-14 22:46:19 +08:00
|
|
|
let first = match documents.peek().map(StdResult::as_ref).transpose() {
|
2021-06-03 01:05:12 +08:00
|
|
|
Ok(first) => first,
|
2021-06-14 22:46:19 +08:00
|
|
|
Err(_) => {
|
|
|
|
let error = documents.next().unwrap().unwrap_err();
|
|
|
|
return Err(UserError::SerdeJson(error).into());
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2021-06-03 01:05:12 +08:00
|
|
|
};
|
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
let alternative_name =
|
|
|
|
first.and_then(|doc| doc.keys().find(|f| is_primary_key(f)).cloned());
|
2021-01-21 00:27:43 +08:00
|
|
|
let (primary_key_id, primary_key) = compute_primary_key_pair(
|
|
|
|
self.index.primary_key(self.rtxn)?,
|
|
|
|
&mut fields_ids_map,
|
|
|
|
alternative_name,
|
2021-06-17 00:33:33 +08:00
|
|
|
self.autogenerate_docids,
|
2021-01-21 00:27:43 +08:00
|
|
|
)?;
|
2020-10-31 23:10:15 +08:00
|
|
|
|
2020-11-01 18:50:10 +08:00
|
|
|
if documents.peek().is_none() {
|
2020-10-31 23:10:15 +08:00
|
|
|
return Ok(TransformOutput {
|
|
|
|
primary_key,
|
|
|
|
fields_ids_map,
|
2021-06-17 21:16:20 +08:00
|
|
|
field_distribution: self.index.field_distribution(self.rtxn)?,
|
2020-11-23 00:53:33 +08:00
|
|
|
external_documents_ids: ExternalDocumentsIds::default(),
|
2020-10-31 23:10:15 +08:00
|
|
|
new_documents_ids: RoaringBitmap::new(),
|
|
|
|
replaced_documents_ids: RoaringBitmap::new(),
|
|
|
|
documents_count: 0,
|
|
|
|
documents_file: tempfile::tempfile()?,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must choose the appropriate merge function for when two or more documents
|
|
|
|
// with the same user id must be merged or fully replaced in the same batch.
|
|
|
|
let merge_function = match self.index_documents_method {
|
|
|
|
IndexDocumentsMethod::ReplaceDocuments => keep_latest_obkv,
|
|
|
|
IndexDocumentsMethod::UpdateDocuments => merge_obkvs,
|
|
|
|
};
|
|
|
|
|
|
|
|
// We initialize the sorter with the user indexing settings.
|
|
|
|
let mut sorter = create_sorter(
|
|
|
|
merge_function,
|
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
self.chunk_fusing_shrink_size,
|
|
|
|
self.max_nb_chunks,
|
|
|
|
self.max_memory,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut json_buffer = Vec::new();
|
|
|
|
let mut obkv_buffer = Vec::new();
|
|
|
|
let mut uuid_buffer = [0; uuid::adapter::Hyphenated::LENGTH];
|
2020-11-11 19:39:09 +08:00
|
|
|
let mut documents_count = 0;
|
2020-10-31 23:10:15 +08:00
|
|
|
|
2020-11-01 18:50:10 +08:00
|
|
|
for result in documents {
|
2021-06-14 22:46:19 +08:00
|
|
|
let document = result.map_err(UserError::SerdeJson)?;
|
2020-11-01 18:50:10 +08:00
|
|
|
|
2020-11-11 19:39:09 +08:00
|
|
|
if self.log_every_n.map_or(false, |len| documents_count % len == 0) {
|
|
|
|
progress_callback(UpdateIndexingStep::TransformFromUserIntoGenericFormat {
|
|
|
|
documents_seen: documents_count,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:10:15 +08:00
|
|
|
obkv_buffer.clear();
|
|
|
|
let mut writer = obkv::KvWriter::new(&mut obkv_buffer);
|
|
|
|
|
|
|
|
// We prepare the fields ids map with the documents keys.
|
|
|
|
for (key, _value) in &document {
|
2021-06-14 22:46:19 +08:00
|
|
|
fields_ids_map.insert(&key).ok_or(UserError::AttributeLimitReached)?;
|
2020-10-31 23:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We retrieve the user id from the document based on the primary key name,
|
|
|
|
// if the document id isn't present we generate a uuid.
|
2021-01-21 00:27:43 +08:00
|
|
|
let external_id = match document.get(&primary_key) {
|
2020-10-31 23:10:15 +08:00
|
|
|
Some(value) => match value {
|
2020-11-01 19:14:44 +08:00
|
|
|
Value::String(string) => Cow::Borrowed(string.as_str()),
|
2020-10-31 23:10:15 +08:00
|
|
|
Value::Number(number) => Cow::Owned(number.to_string()),
|
2021-06-17 00:33:33 +08:00
|
|
|
content => {
|
|
|
|
return Err(
|
|
|
|
UserError::InvalidDocumentId { document_id: content.clone() }.into()
|
|
|
|
)
|
|
|
|
}
|
2020-10-31 23:10:15 +08:00
|
|
|
},
|
|
|
|
None => {
|
2020-11-01 04:46:55 +08:00
|
|
|
if !self.autogenerate_docids {
|
2021-06-22 17:22:33 +08:00
|
|
|
return Err(UserError::MissingDocumentId { document }.into());
|
2020-11-01 04:46:55 +08:00
|
|
|
}
|
2020-10-31 23:10:15 +08:00
|
|
|
let uuid = uuid::Uuid::new_v4().to_hyphenated().encode_lower(&mut uuid_buffer);
|
|
|
|
Cow::Borrowed(uuid)
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2020-10-31 23:10:15 +08:00
|
|
|
};
|
|
|
|
|
2020-11-01 19:14:44 +08:00
|
|
|
// We iterate in the fields ids ordered.
|
|
|
|
for (field_id, name) in fields_ids_map.iter() {
|
|
|
|
json_buffer.clear();
|
|
|
|
|
|
|
|
// We try to extract the value from the document and if we don't find anything
|
|
|
|
// and this should be the document id we return the one we generated.
|
|
|
|
if let Some(value) = document.get(name) {
|
|
|
|
// We serialize the attribute values.
|
2021-06-17 00:33:33 +08:00
|
|
|
serde_json::to_writer(&mut json_buffer, value)
|
|
|
|
.map_err(InternalError::SerdeJson)?;
|
2020-11-01 19:14:44 +08:00
|
|
|
writer.insert(field_id, &json_buffer)?;
|
|
|
|
}
|
2021-02-13 21:16:27 +08:00
|
|
|
|
|
|
|
// We validate the document id [a-zA-Z0-9\-_].
|
|
|
|
if field_id == primary_key_id && validate_document_id(&external_id).is_none() {
|
2021-06-14 22:46:19 +08:00
|
|
|
return Err(UserError::InvalidDocumentId {
|
|
|
|
document_id: Value::from(external_id),
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
|
|
|
.into());
|
2020-11-01 19:14:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:10:15 +08:00
|
|
|
// We use the extracted/generated user id as the key for this document.
|
2020-11-22 18:54:04 +08:00
|
|
|
sorter.insert(external_id.as_bytes(), &obkv_buffer)?;
|
2020-11-11 19:39:09 +08:00
|
|
|
documents_count += 1;
|
2020-10-31 23:10:15 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 19:39:09 +08:00
|
|
|
progress_callback(UpdateIndexingStep::TransformFromUserIntoGenericFormat {
|
|
|
|
documents_seen: documents_count,
|
|
|
|
});
|
|
|
|
|
2020-10-31 23:10:15 +08:00
|
|
|
// Now that we have a valid sorter that contains the user id and the obkv we
|
|
|
|
// give it to the last transforming function which returns the TransformOutput.
|
2020-12-01 21:25:17 +08:00
|
|
|
self.output_from_sorter(
|
2020-11-11 19:39:09 +08:00
|
|
|
sorter,
|
|
|
|
primary_key,
|
|
|
|
fields_ids_map,
|
|
|
|
documents_count,
|
2020-11-22 18:54:04 +08:00
|
|
|
external_documents_ids,
|
2020-11-11 19:39:09 +08:00
|
|
|
progress_callback,
|
|
|
|
)
|
2020-10-31 23:10:15 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn output_from_csv<R, F>(self, reader: R, progress_callback: F) -> Result<TransformOutput>
|
2020-11-11 19:39:09 +08:00
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2020-10-27 03:18:10 +08:00
|
|
|
let mut fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
2020-11-22 18:54:04 +08:00
|
|
|
let external_documents_ids = self.index.external_documents_ids(self.rtxn).unwrap();
|
2020-10-27 03:18:10 +08:00
|
|
|
|
2020-10-23 20:11:00 +08:00
|
|
|
let mut csv = csv::Reader::from_reader(reader);
|
2021-06-14 22:46:19 +08:00
|
|
|
let headers = csv.headers().map_err(UserError::Csv)?;
|
2020-10-23 20:11:00 +08:00
|
|
|
|
|
|
|
let mut fields_ids = Vec::new();
|
2021-01-21 00:27:43 +08:00
|
|
|
// Generate the new fields ids based on the current fields ids and this CSV headers.
|
2020-10-31 19:54:43 +08:00
|
|
|
for (i, header) in headers.iter().enumerate() {
|
2021-06-14 22:46:19 +08:00
|
|
|
let id = fields_ids_map.insert(header).ok_or(UserError::AttributeLimitReached)?;
|
2020-10-31 19:54:43 +08:00
|
|
|
fields_ids.push((id, i));
|
2020-10-23 20:11:00 +08:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:54:43 +08:00
|
|
|
// Extract the position of the primary key in the current headers, None if not found.
|
2021-01-21 00:27:43 +08:00
|
|
|
let primary_key_pos = match self.index.primary_key(self.rtxn)? {
|
2020-10-31 19:54:43 +08:00
|
|
|
Some(primary_key) => {
|
2021-06-17 00:33:33 +08:00
|
|
|
// The primary key is known so we must find the position in the CSV headers.
|
|
|
|
headers.iter().position(|h| h == primary_key)
|
|
|
|
}
|
2021-06-03 01:05:12 +08:00
|
|
|
None => headers.iter().position(is_primary_key),
|
2020-10-31 19:54:43 +08:00
|
|
|
};
|
|
|
|
|
2021-01-21 00:27:43 +08:00
|
|
|
// Returns the field id in the fields ids map, create an "id" field
|
2020-10-31 19:54:43 +08:00
|
|
|
// in case it is not in the current headers.
|
2021-01-21 00:27:43 +08:00
|
|
|
let alternative_name = primary_key_pos.map(|pos| headers[pos].to_string());
|
2021-06-09 16:57:32 +08:00
|
|
|
let (primary_key_id, primary_key_name) = compute_primary_key_pair(
|
2021-01-21 00:27:43 +08:00
|
|
|
self.index.primary_key(self.rtxn)?,
|
|
|
|
&mut fields_ids_map,
|
|
|
|
alternative_name,
|
2021-06-17 00:33:33 +08:00
|
|
|
self.autogenerate_docids,
|
2021-01-21 00:27:43 +08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// The primary key field is not present in the header, so we need to create it.
|
|
|
|
if primary_key_pos.is_none() {
|
|
|
|
fields_ids.push((primary_key_id, usize::max_value()));
|
|
|
|
}
|
2020-10-31 19:54:43 +08:00
|
|
|
|
|
|
|
// We sort the fields ids by the fields ids map id, this way we are sure to iterate over
|
|
|
|
// the records fields in the fields ids map order and correctly generate the obkv.
|
|
|
|
fields_ids.sort_unstable_by_key(|(field_id, _)| *field_id);
|
|
|
|
|
2020-10-23 20:11:00 +08:00
|
|
|
// We initialize the sorter with the user indexing settings.
|
2020-10-24 22:23:08 +08:00
|
|
|
let mut sorter = create_sorter(
|
2020-10-30 20:46:56 +08:00
|
|
|
keep_latest_obkv,
|
2020-10-24 22:23:08 +08:00
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
self.chunk_fusing_shrink_size,
|
|
|
|
self.max_nb_chunks,
|
|
|
|
self.max_memory,
|
|
|
|
);
|
2020-10-23 20:11:00 +08:00
|
|
|
|
|
|
|
// We write into the sorter to merge and deduplicate the documents
|
2020-11-22 18:54:04 +08:00
|
|
|
// based on the external ids.
|
2020-10-24 20:02:29 +08:00
|
|
|
let mut json_buffer = Vec::new();
|
|
|
|
let mut obkv_buffer = Vec::new();
|
2020-10-31 19:54:43 +08:00
|
|
|
let mut uuid_buffer = [0; uuid::adapter::Hyphenated::LENGTH];
|
2020-11-11 19:39:09 +08:00
|
|
|
let mut documents_count = 0;
|
2020-10-31 23:10:15 +08:00
|
|
|
|
2020-10-23 20:11:00 +08:00
|
|
|
let mut record = csv::StringRecord::new();
|
2021-06-14 22:46:19 +08:00
|
|
|
while csv.read_record(&mut record).map_err(UserError::Csv)? {
|
2020-10-24 20:02:29 +08:00
|
|
|
obkv_buffer.clear();
|
|
|
|
let mut writer = obkv::KvWriter::new(&mut obkv_buffer);
|
2020-10-23 20:11:00 +08:00
|
|
|
|
2020-11-11 19:39:09 +08:00
|
|
|
if self.log_every_n.map_or(false, |len| documents_count % len == 0) {
|
|
|
|
progress_callback(UpdateIndexingStep::TransformFromUserIntoGenericFormat {
|
|
|
|
documents_seen: documents_count,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-31 19:54:43 +08:00
|
|
|
// We extract the user id if we know where it is or generate an UUID V4 otherwise.
|
2021-01-21 00:27:43 +08:00
|
|
|
let external_id = match primary_key_pos {
|
2020-11-01 23:43:12 +08:00
|
|
|
Some(pos) => {
|
2020-11-22 18:54:04 +08:00
|
|
|
let external_id = &record[pos];
|
2020-11-01 23:43:12 +08:00
|
|
|
// We validate the document id [a-zA-Z0-9\-_].
|
2020-11-22 18:54:04 +08:00
|
|
|
match validate_document_id(&external_id) {
|
2020-11-01 23:43:12 +08:00
|
|
|
Some(valid) => valid,
|
2021-06-17 00:33:33 +08:00
|
|
|
None => {
|
|
|
|
return Err(UserError::InvalidDocumentId {
|
|
|
|
document_id: Value::from(external_id),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
2020-11-01 23:43:12 +08:00
|
|
|
}
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2020-10-31 19:54:43 +08:00
|
|
|
None => uuid::Uuid::new_v4().to_hyphenated().encode_lower(&mut uuid_buffer),
|
|
|
|
};
|
|
|
|
|
|
|
|
// When the primary_key_field_id is found in the fields ids list
|
|
|
|
// we return the generated document id instead of the record field.
|
2021-06-17 00:33:33 +08:00
|
|
|
let iter = fields_ids.iter().map(|(fi, i)| {
|
|
|
|
let field = if *fi == primary_key_id { external_id } else { &record[*i] };
|
|
|
|
(fi, field)
|
|
|
|
});
|
2020-10-31 19:54:43 +08:00
|
|
|
|
|
|
|
// We retrieve the field id based on the fields ids map fields ids order.
|
|
|
|
for (field_id, field) in iter {
|
2020-10-24 20:02:29 +08:00
|
|
|
// We serialize the attribute values as JSON strings.
|
|
|
|
json_buffer.clear();
|
2021-06-17 00:33:33 +08:00
|
|
|
serde_json::to_writer(&mut json_buffer, &field)
|
|
|
|
.map_err(InternalError::SerdeJson)?;
|
2020-10-31 19:54:43 +08:00
|
|
|
writer.insert(*field_id, &json_buffer)?;
|
2020-10-23 20:11:00 +08:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:54:43 +08:00
|
|
|
// We use the extracted/generated user id as the key for this document.
|
2020-11-22 18:54:04 +08:00
|
|
|
sorter.insert(external_id, &obkv_buffer)?;
|
2020-11-11 19:39:09 +08:00
|
|
|
documents_count += 1;
|
2020-10-23 20:11:00 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 19:39:09 +08:00
|
|
|
progress_callback(UpdateIndexingStep::TransformFromUserIntoGenericFormat {
|
|
|
|
documents_seen: documents_count,
|
|
|
|
});
|
|
|
|
|
2020-10-31 23:10:15 +08:00
|
|
|
// Now that we have a valid sorter that contains the user id and the obkv we
|
|
|
|
// give it to the last transforming function which returns the TransformOutput.
|
2020-12-01 21:25:17 +08:00
|
|
|
self.output_from_sorter(
|
2020-11-11 19:39:09 +08:00
|
|
|
sorter,
|
2021-01-21 00:27:43 +08:00
|
|
|
primary_key_name,
|
2020-11-11 19:39:09 +08:00
|
|
|
fields_ids_map,
|
|
|
|
documents_count,
|
2020-11-22 18:54:04 +08:00
|
|
|
external_documents_ids,
|
2020-11-11 19:39:09 +08:00
|
|
|
progress_callback,
|
|
|
|
)
|
2020-10-31 23:10:15 +08:00
|
|
|
}
|
|
|
|
|
2020-11-01 18:50:10 +08:00
|
|
|
/// Generate the `TransformOutput` based on the given sorter that can be generated from any
|
|
|
|
/// format like CSV, JSON or JSON stream. This sorter must contain a key that is the document
|
2020-10-31 23:10:15 +08:00
|
|
|
/// id for the user side and the value must be an obkv where keys are valid fields ids.
|
2021-06-14 22:46:19 +08:00
|
|
|
fn output_from_sorter<F, E>(
|
2020-10-31 23:10:15 +08:00
|
|
|
self,
|
2021-06-14 22:46:19 +08:00
|
|
|
sorter: grenad::Sorter<MergeFn<E>>,
|
2021-01-21 00:27:43 +08:00
|
|
|
primary_key: String,
|
2020-10-31 23:10:15 +08:00
|
|
|
fields_ids_map: FieldsIdsMap,
|
2020-11-11 19:39:09 +08:00
|
|
|
approximate_number_of_documents: usize,
|
2020-11-23 00:53:33 +08:00
|
|
|
mut external_documents_ids: ExternalDocumentsIds<'_>,
|
2020-11-11 19:39:09 +08:00
|
|
|
progress_callback: F,
|
2021-06-14 22:46:19 +08:00
|
|
|
) -> Result<TransformOutput>
|
2020-11-11 19:39:09 +08:00
|
|
|
where
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
2021-06-14 22:46:19 +08:00
|
|
|
Error: From<E>,
|
2020-10-31 23:10:15 +08:00
|
|
|
{
|
|
|
|
let documents_ids = self.index.documents_ids(self.rtxn)?;
|
2021-06-17 21:16:20 +08:00
|
|
|
let mut field_distribution = self.index.field_distribution(self.rtxn)?;
|
2020-10-31 23:10:15 +08:00
|
|
|
let mut available_documents_ids = AvailableDocumentsIds::from_documents_ids(&documents_ids);
|
|
|
|
|
2020-10-23 20:11:00 +08:00
|
|
|
// Once we have sort and deduplicated the documents we write them into a final file.
|
2020-10-29 21:20:03 +08:00
|
|
|
let mut final_sorter = create_sorter(
|
2021-06-15 19:45:20 +08:00
|
|
|
|_id, _obkvs| Err(InternalError::IndexingMergingKeys { process: "documents" }),
|
2020-10-29 21:20:03 +08:00
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
self.chunk_fusing_shrink_size,
|
|
|
|
self.max_nb_chunks,
|
|
|
|
self.max_memory,
|
|
|
|
);
|
2020-11-22 18:54:04 +08:00
|
|
|
let mut new_external_documents_ids_builder = fst::MapBuilder::memory();
|
2020-10-23 20:11:00 +08:00
|
|
|
let mut replaced_documents_ids = RoaringBitmap::new();
|
|
|
|
let mut new_documents_ids = RoaringBitmap::new();
|
2020-10-31 23:10:15 +08:00
|
|
|
let mut obkv_buffer = Vec::new();
|
2020-10-23 20:11:00 +08:00
|
|
|
|
|
|
|
// While we write into final file we get or generate the internal documents ids.
|
|
|
|
let mut documents_count = 0;
|
|
|
|
let mut iter = sorter.into_iter()?;
|
2020-11-22 18:54:04 +08:00
|
|
|
while let Some((external_id, update_obkv)) = iter.next()? {
|
2020-11-11 19:39:09 +08:00
|
|
|
if self.log_every_n.map_or(false, |len| documents_count % len == 0) {
|
|
|
|
progress_callback(UpdateIndexingStep::ComputeIdsAndMergeDocuments {
|
|
|
|
documents_seen: documents_count,
|
|
|
|
total_documents: approximate_number_of_documents,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-22 18:54:04 +08:00
|
|
|
let (docid, obkv) = match external_documents_ids.get(external_id) {
|
2020-10-23 20:11:00 +08:00
|
|
|
Some(docid) => {
|
2020-11-22 18:54:04 +08:00
|
|
|
// If we find the user id in the current external documents ids map
|
2020-10-23 20:11:00 +08:00
|
|
|
// we use it and insert it in the list of replaced documents.
|
|
|
|
replaced_documents_ids.insert(docid);
|
2020-10-26 17:55:07 +08:00
|
|
|
|
2021-06-17 23:05:34 +08:00
|
|
|
let key = BEU32::new(docid);
|
|
|
|
let base_obkv = self.index.documents.get(&self.rtxn, &key)?.ok_or(
|
|
|
|
InternalError::DatabaseMissingEntry {
|
|
|
|
db_name: db_name::DOCUMENTS,
|
|
|
|
key: None,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// we remove all the fields that were already counted
|
|
|
|
for (field_id, _) in base_obkv.iter() {
|
|
|
|
let field_name = fields_ids_map.name(field_id).unwrap();
|
|
|
|
if let Entry::Occupied(mut entry) =
|
|
|
|
field_distribution.entry(field_name.to_string())
|
|
|
|
{
|
|
|
|
match entry.get().checked_sub(1) {
|
|
|
|
Some(0) | None => entry.remove(),
|
|
|
|
Some(count) => entry.insert(count),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 17:55:07 +08:00
|
|
|
// Depending on the update indexing method we will merge
|
|
|
|
// the document update with the current document or not.
|
2020-10-27 03:18:10 +08:00
|
|
|
match self.index_documents_method {
|
|
|
|
IndexDocumentsMethod::ReplaceDocuments => (docid, update_obkv),
|
|
|
|
IndexDocumentsMethod::UpdateDocuments => {
|
|
|
|
let update_obkv = obkv::KvReader::new(update_obkv);
|
2020-10-30 20:46:56 +08:00
|
|
|
merge_two_obkvs(base_obkv, update_obkv, &mut obkv_buffer);
|
2020-10-27 03:18:10 +08:00
|
|
|
(docid, obkv_buffer.as_slice())
|
|
|
|
}
|
2020-10-26 17:55:07 +08:00
|
|
|
}
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2020-10-23 20:11:00 +08:00
|
|
|
None => {
|
2020-11-22 18:54:04 +08:00
|
|
|
// If this user id is new we add it to the external documents ids map
|
2020-10-23 20:11:00 +08:00
|
|
|
// for new ids and into the list of new documents.
|
2021-06-17 00:33:33 +08:00
|
|
|
let new_docid =
|
|
|
|
available_documents_ids.next().ok_or(UserError::DocumentLimitReached)?;
|
2020-11-22 18:54:04 +08:00
|
|
|
new_external_documents_ids_builder.insert(external_id, new_docid as u64)?;
|
2020-10-23 20:11:00 +08:00
|
|
|
new_documents_ids.insert(new_docid);
|
2020-10-26 17:55:07 +08:00
|
|
|
(new_docid, update_obkv)
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2020-10-23 20:11:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// We insert the document under the documents ids map into the final file.
|
2020-10-29 21:20:03 +08:00
|
|
|
final_sorter.insert(docid.to_be_bytes(), obkv)?;
|
2020-10-23 20:11:00 +08:00
|
|
|
documents_count += 1;
|
2021-05-05 03:01:11 +08:00
|
|
|
|
|
|
|
let reader = obkv::KvReader::new(obkv);
|
|
|
|
for (field_id, _) in reader.iter() {
|
|
|
|
let field_name = fields_ids_map.name(field_id).unwrap();
|
2021-06-17 21:16:20 +08:00
|
|
|
*field_distribution.entry(field_name.to_string()).or_default() += 1;
|
2021-05-05 03:01:11 +08:00
|
|
|
}
|
2020-10-23 20:11:00 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 19:39:09 +08:00
|
|
|
progress_callback(UpdateIndexingStep::ComputeIdsAndMergeDocuments {
|
|
|
|
documents_seen: documents_count,
|
|
|
|
total_documents: documents_count,
|
|
|
|
});
|
|
|
|
|
2020-10-29 21:20:03 +08:00
|
|
|
// We create a final writer to write the new documents in order from the sorter.
|
|
|
|
let file = tempfile::tempfile()?;
|
2021-06-17 00:33:33 +08:00
|
|
|
let mut writer =
|
|
|
|
create_writer(self.chunk_compression_type, self.chunk_compression_level, file)?;
|
2020-10-29 21:20:03 +08:00
|
|
|
|
|
|
|
// Once we have written all the documents into the final sorter, we write the documents
|
|
|
|
// into this writer, extract the file and reset the seek to be able to read it again.
|
|
|
|
final_sorter.write_into(&mut writer)?;
|
2020-10-23 20:11:00 +08:00
|
|
|
let mut documents_file = writer.into_inner()?;
|
|
|
|
documents_file.seek(SeekFrom::Start(0))?;
|
|
|
|
|
2020-11-22 18:28:35 +08:00
|
|
|
let before_docids_merging = Instant::now();
|
2020-11-23 00:53:33 +08:00
|
|
|
// We merge the new external ids with existing external documents ids.
|
|
|
|
let new_external_documents_ids = new_external_documents_ids_builder.into_map();
|
|
|
|
external_documents_ids.insert_ids(&new_external_documents_ids)?;
|
2020-10-23 20:11:00 +08:00
|
|
|
|
2020-11-22 18:54:04 +08:00
|
|
|
info!("Documents external merging took {:.02?}", before_docids_merging.elapsed());
|
2020-11-22 18:28:35 +08:00
|
|
|
|
2020-10-23 20:11:00 +08:00
|
|
|
Ok(TransformOutput {
|
2020-10-31 23:10:15 +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-23 00:53:33 +08:00
|
|
|
external_documents_ids: external_documents_ids.into_static(),
|
2020-10-23 20:11:00 +08:00
|
|
|
new_documents_ids,
|
|
|
|
replaced_documents_ids,
|
|
|
|
documents_count,
|
|
|
|
documents_file,
|
2020-11-03 20:20:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `TransformOutput` with a file that contains the documents of the index
|
|
|
|
/// with the attributes reordered accordingly to the `FieldsIdsMap` given as argument.
|
|
|
|
// TODO this can be done in parallel by using the rayon `ThreadPool`.
|
|
|
|
pub fn remap_index_documents(
|
|
|
|
self,
|
2021-01-21 00:27:43 +08:00
|
|
|
primary_key: String,
|
|
|
|
old_fields_ids_map: FieldsIdsMap,
|
|
|
|
new_fields_ids_map: FieldsIdsMap,
|
2021-06-17 00:33:33 +08:00
|
|
|
) -> Result<TransformOutput> {
|
2021-06-17 21:16:20 +08:00
|
|
|
let field_distribution = self.index.field_distribution(self.rtxn)?;
|
2020-11-22 18:54:04 +08:00
|
|
|
let external_documents_ids = self.index.external_documents_ids(self.rtxn)?;
|
2020-11-03 20:20:11 +08:00
|
|
|
let documents_ids = self.index.documents_ids(self.rtxn)?;
|
|
|
|
let documents_count = documents_ids.len() as usize;
|
|
|
|
|
|
|
|
// We create a final writer to write the new documents in order from the sorter.
|
|
|
|
let file = tempfile::tempfile()?;
|
2021-06-17 00:33:33 +08:00
|
|
|
let mut writer =
|
|
|
|
create_writer(self.chunk_compression_type, self.chunk_compression_level, file)?;
|
2020-11-03 20:20:11 +08:00
|
|
|
|
|
|
|
let mut obkv_buffer = Vec::new();
|
|
|
|
for result in self.index.documents.iter(self.rtxn)? {
|
|
|
|
let (docid, obkv) = result?;
|
|
|
|
let docid = docid.get();
|
|
|
|
|
|
|
|
obkv_buffer.clear();
|
|
|
|
let mut obkv_writer = obkv::KvWriter::new(&mut obkv_buffer);
|
|
|
|
|
|
|
|
// We iterate over the new `FieldsIdsMap` ids in order and construct the new obkv.
|
2021-01-21 00:27:43 +08:00
|
|
|
for (id, name) in new_fields_ids_map.iter() {
|
|
|
|
if let Some(val) = old_fields_ids_map.id(name).and_then(|id| obkv.get(id)) {
|
2020-11-03 20:20:11 +08:00
|
|
|
obkv_writer.insert(id, val)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = obkv_writer.into_inner()?;
|
|
|
|
writer.insert(docid.to_be_bytes(), buffer)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we have written all the documents, we extract
|
|
|
|
// the file and reset the seek to be able to read it again.
|
|
|
|
let mut documents_file = writer.into_inner()?;
|
|
|
|
documents_file.seek(SeekFrom::Start(0))?;
|
|
|
|
|
|
|
|
Ok(TransformOutput {
|
|
|
|
primary_key,
|
2021-01-21 00:27:43 +08:00
|
|
|
fields_ids_map: new_fields_ids_map,
|
2021-06-17 21:16:20 +08:00
|
|
|
field_distribution,
|
2020-11-23 00:53:33 +08:00
|
|
|
external_documents_ids: external_documents_ids.into_static(),
|
2020-11-03 20:20:11 +08:00
|
|
|
new_documents_ids: documents_ids,
|
|
|
|
replaced_documents_ids: RoaringBitmap::default(),
|
|
|
|
documents_count,
|
|
|
|
documents_file,
|
2020-10-23 20:11:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-31 23:10:15 +08:00
|
|
|
|
2021-01-21 00:27:43 +08:00
|
|
|
/// Given an optional primary key and an optional alternative name, returns the (field_id, attr_name)
|
|
|
|
/// for the primary key according to the following rules:
|
|
|
|
/// - if primary_key is `Some`, returns the id and the name, else
|
|
|
|
/// - if alternative_name is Some, adds alternative to the fields_ids_map, and returns the pair, else
|
|
|
|
/// - if autogenerate_docids is true, insert the default id value in the field ids map ("id") and
|
|
|
|
/// returns the pair, else
|
|
|
|
/// - returns an error.
|
|
|
|
fn compute_primary_key_pair(
|
|
|
|
primary_key: Option<&str>,
|
|
|
|
fields_ids_map: &mut FieldsIdsMap,
|
|
|
|
alternative_name: Option<String>,
|
|
|
|
autogenerate_docids: bool,
|
2021-06-14 22:46:19 +08:00
|
|
|
) -> Result<(FieldId, String)> {
|
2021-01-21 00:27:43 +08:00
|
|
|
match primary_key {
|
|
|
|
Some(primary_key) => {
|
2021-06-14 22:46:19 +08:00
|
|
|
let id = fields_ids_map.insert(primary_key).ok_or(UserError::AttributeLimitReached)?;
|
2021-01-21 00:27:43 +08:00
|
|
|
Ok((id, primary_key.to_string()))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let name = match alternative_name {
|
|
|
|
Some(key) => key,
|
|
|
|
None => {
|
|
|
|
if !autogenerate_docids {
|
|
|
|
// If there is no primary key in the current document batch, we must
|
|
|
|
// return an error and not automatically generate any document id.
|
2021-06-14 22:46:19 +08:00
|
|
|
return Err(UserError::MissingPrimaryKey.into());
|
2021-01-21 00:27:43 +08:00
|
|
|
}
|
|
|
|
DEFAULT_PRIMARY_KEY_NAME.to_string()
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2021-01-21 00:27:43 +08:00
|
|
|
};
|
2021-06-14 22:46:19 +08:00
|
|
|
let id = fields_ids_map.insert(&name).ok_or(UserError::AttributeLimitReached)?;
|
2021-01-21 00:27:43 +08:00
|
|
|
Ok((id, name))
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2021-01-21 00:27:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:43:12 +08:00
|
|
|
fn validate_document_id(document_id: &str) -> Option<&str> {
|
|
|
|
let document_id = document_id.trim();
|
|
|
|
Some(document_id).filter(|id| {
|
2021-06-17 00:33:33 +08:00
|
|
|
!id.is_empty()
|
|
|
|
&& id.chars().all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_'))
|
2020-11-01 23:43:12 +08:00
|
|
|
})
|
|
|
|
}
|
2021-01-21 00:27:43 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
mod compute_primary_key {
|
2021-06-17 00:33:33 +08:00
|
|
|
use super::{compute_primary_key_pair, FieldsIdsMap};
|
2021-01-21 00:27:43 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_primary_key_if_is_some() {
|
|
|
|
let mut fields_map = FieldsIdsMap::new();
|
|
|
|
fields_map.insert("toto").unwrap();
|
|
|
|
let result = compute_primary_key_pair(
|
|
|
|
Some("toto"),
|
|
|
|
&mut fields_map,
|
|
|
|
Some("tata".to_string()),
|
2021-06-17 00:33:33 +08:00
|
|
|
false,
|
|
|
|
);
|
2021-07-06 17:31:24 +08:00
|
|
|
assert_eq!(result.unwrap(), (0, "toto".to_string()));
|
2021-01-21 00:27:43 +08:00
|
|
|
assert_eq!(fields_map.len(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_alternative_if_primary_is_none() {
|
|
|
|
let mut fields_map = FieldsIdsMap::new();
|
2021-06-17 00:33:33 +08:00
|
|
|
let result =
|
|
|
|
compute_primary_key_pair(None, &mut fields_map, Some("tata".to_string()), false);
|
2021-07-06 17:31:24 +08:00
|
|
|
assert_eq!(result.unwrap(), (0, "tata".to_string()));
|
2021-01-21 00:27:43 +08:00
|
|
|
assert_eq!(fields_map.len(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_default_if_both_are_none() {
|
|
|
|
let mut fields_map = FieldsIdsMap::new();
|
2021-06-17 00:33:33 +08:00
|
|
|
let result = compute_primary_key_pair(None, &mut fields_map, None, true);
|
2021-07-06 17:31:24 +08:00
|
|
|
assert_eq!(result.unwrap(), (0, "id".to_string()));
|
2021-01-21 00:27:43 +08:00
|
|
|
assert_eq!(fields_map.len(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-17 00:33:33 +08:00
|
|
|
fn should_return_err_if_both_are_none_and_recompute_is_false() {
|
2021-01-21 00:27:43 +08:00
|
|
|
let mut fields_map = FieldsIdsMap::new();
|
2021-06-17 00:33:33 +08:00
|
|
|
let result = compute_primary_key_pair(None, &mut fields_map, None, false);
|
2021-01-21 00:27:43 +08:00
|
|
|
assert!(result.is_err());
|
|
|
|
assert_eq!(fields_map.len(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|