From 13c95d25aa94abd33cfab9dedd06e59fe6637505 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Mon, 19 Dec 2022 15:59:22 +0100 Subject: [PATCH] Remove uses of UserError::MissingPrimaryKey not related to inference --- milli/src/update/index_documents/enrich.rs | 6 +++++- milli/src/update/index_documents/transform.rs | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/milli/src/update/index_documents/enrich.rs b/milli/src/update/index_documents/enrich.rs index 7eda5dca4..8874b836c 100644 --- a/milli/src/update/index_documents/enrich.rs +++ b/milli/src/update/index_documents/enrich.rs @@ -21,6 +21,10 @@ const DEFAULT_PRIMARY_KEY: &str = "id"; /// - all the documents id exist and are extracted, /// - the validity of them but also, /// - the validity of the `_geo` field depending on the settings. +/// +/// # Panics +/// +/// - if reader.is_empty(), this function may panic in some cases pub fn enrich_documents_batch( rtxn: &heed::RoTxn, index: &Index, @@ -49,7 +53,7 @@ pub fn enrich_documents_batch( primary_key: primary_key.to_string(), document: obkv_to_object(&first_document, &documents_batch_index)?, })), - None => Ok(Err(UserError::MissingPrimaryKey)), + None => unreachable!("Called with reader.is_empty()"), }; } }, diff --git a/milli/src/update/index_documents/transform.rs b/milli/src/update/index_documents/transform.rs index f414569b9..68ef2b7ee 100644 --- a/milli/src/update/index_documents/transform.rs +++ b/milli/src/update/index_documents/transform.rs @@ -16,7 +16,7 @@ use super::helpers::{create_sorter, create_writer, keep_latest_obkv, merge_obkvs use super::{IndexDocumentsMethod, IndexerConfig}; use crate::documents::{DocumentsBatchIndex, EnrichedDocument, EnrichedDocumentsBatchReader}; use crate::error::{Error, InternalError, UserError}; -use crate::index::db_name; +use crate::index::{db_name, main_key}; use crate::update::{AvailableDocumentsIds, ClearDocuments, UpdateIndexingStep}; use crate::{ ExternalDocumentsIds, FieldDistribution, FieldId, FieldIdMapMissingEntry, FieldsIdsMap, Index, @@ -459,7 +459,10 @@ impl<'a, 'i> Transform<'a, 'i> { let primary_key = self .index .primary_key(wtxn)? - .ok_or(Error::UserError(UserError::MissingPrimaryKey))? + .ok_or(Error::InternalError(InternalError::DatabaseMissingEntry { + db_name: db_name::MAIN, + key: Some(main_key::PRIMARY_KEY_KEY), + }))? .to_string(); let mut external_documents_ids = self.index.external_documents_ids(wtxn)?; @@ -557,8 +560,14 @@ impl<'a, 'i> Transform<'a, 'i> { mut new_fields_ids_map: FieldsIdsMap, ) -> Result { // There already has been a document addition, the primary key should be set by now. - let primary_key = - self.index.primary_key(wtxn)?.ok_or(UserError::MissingPrimaryKey)?.to_string(); + let primary_key = self + .index + .primary_key(wtxn)? + .ok_or(InternalError::DatabaseMissingEntry { + db_name: db_name::MAIN, + key: Some(main_key::PRIMARY_KEY_KEY), + })? + .to_string(); let field_distribution = self.index.field_distribution(wtxn)?; // Delete the soft deleted document ids from the maps inside the external_document_ids structure