From 47e526f5ea762e1ed6b850380e375fda569a7809 Mon Sep 17 00:00:00 2001 From: karribalu Date: Mon, 8 Jul 2024 22:27:10 +0100 Subject: [PATCH 1/4] Add index exists function in index_scheduler --- index-scheduler/src/lib.rs | 16 ++++++++++------ meilisearch/src/analytics/mod.rs | 2 +- meilisearch/src/routes/indexes/documents.rs | 12 ++++++++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index 0b98cc22a..1861665d2 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -662,7 +662,11 @@ impl IndexScheduler { let rtxn = self.env.read_txn()?; self.index_mapper.index(&rtxn, name) } - + /// Return the boolean referring if index exists. + pub fn index_exists(&self, name: &str) -> Result { + let rtxn = self.env.read_txn()?; + self.index_mapper.index_exists(&rtxn, name) + } /// Return the name of all indexes without opening them. pub fn index_names(&self) -> Result> { let rtxn = self.env.read_txn()?; @@ -3787,15 +3791,15 @@ mod tests { ]); snapshot!(snapshot_index_scheduler(&index_scheduler), name: "after_processing_the_10_tasks"); - // The index should not exists. - snapshot!(format!("{}", index_scheduler.index("doggos").map(|_| ()).unwrap_err()), @"Index `doggos` not found."); + // The index should not exist. + snapshot!(matches!(index_scheduler.index_exists("doggos"), Ok(true)), @"false"); } #[test] fn test_document_addition_cant_create_index_without_index_without_autobatching() { // We're going to execute multiple document addition that don't have // the right to create an index while there is no index currently. - // Since the autobatching is disabled, every tasks should be processed + // Since the auto-batching is disabled, every task should be processed // sequentially and throw an IndexDoesNotExists. let (index_scheduler, mut handle) = IndexScheduler::test(false, vec![]); @@ -3837,8 +3841,8 @@ mod tests { handle.advance_n_failed_batches(5); snapshot!(snapshot_index_scheduler(&index_scheduler), name: "all_tasks_processed"); - // The index should not exists. - snapshot!(format!("{}", index_scheduler.index("doggos").map(|_| ()).unwrap_err()), @"Index `doggos` not found."); + // The index should not exist. + snapshot!(matches!(index_scheduler.index_exists("doggos"), Ok(true)), @"false"); } #[test] diff --git a/meilisearch/src/analytics/mod.rs b/meilisearch/src/analytics/mod.rs index 6863dc57b..98cc292c6 100644 --- a/meilisearch/src/analytics/mod.rs +++ b/meilisearch/src/analytics/mod.rs @@ -102,7 +102,7 @@ pub trait Analytics: Sync + Send { /// This method should be called to aggregate post facet values searches fn post_facet_search(&self, aggregate: FacetSearchAggregator); - // this method should be called to aggregate a add documents request + // this method should be called to aggregate an add documents request fn add_documents( &self, documents_query: &UpdateDocumentsQuery, diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index 1f413ec7d..bf2042d81 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -304,7 +304,11 @@ pub async fn replace_documents( debug!(parameters = ?params, "Replace documents"); let params = params.into_inner(); - analytics.add_documents(¶ms, index_scheduler.index(&index_uid).is_err(), &req); + analytics.add_documents( + ¶ms, + !matches!(index_scheduler.index_exists(&index_uid), Ok(true)), + &req, + ); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; @@ -341,7 +345,11 @@ pub async fn update_documents( let params = params.into_inner(); debug!(parameters = ?params, "Update documents"); - analytics.update_documents(¶ms, index_scheduler.index(&index_uid).is_err(), &req); + analytics.update_documents( + ¶ms, + !matches!(index_scheduler.index_exists(&index_uid), Ok(true)), + &req, + ); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; From ea21b948b17ac12f75e02f8bc7ae751b461e2917 Mon Sep 17 00:00:00 2001 From: karribalu Date: Tue, 9 Jul 2024 09:18:57 +0100 Subject: [PATCH 2/4] Address PR review changes --- meilisearch/src/routes/indexes/documents.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index bf2042d81..12c79d20e 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -304,11 +304,7 @@ pub async fn replace_documents( debug!(parameters = ?params, "Replace documents"); let params = params.into_inner(); - analytics.add_documents( - ¶ms, - !matches!(index_scheduler.index_exists(&index_uid), Ok(true)), - &req, - ); + analytics.add_documents(¶ms, index_scheduler.index_exists(&index_uid) == Ok(true), &req); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; @@ -345,11 +341,7 @@ pub async fn update_documents( let params = params.into_inner(); debug!(parameters = ?params, "Update documents"); - analytics.update_documents( - ¶ms, - !matches!(index_scheduler.index_exists(&index_uid), Ok(true)), - &req, - ); + analytics.update_documents(¶ms, index_scheduler.index_exists(&index_uid) == Ok(true), &req); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; From 23e102ca71ec002bce98888e1427add27e7e29a2 Mon Sep 17 00:00:00 2001 From: karribalu Date: Wed, 10 Jul 2024 19:33:16 +0100 Subject: [PATCH 3/4] Address PR review changes --- meilisearch/src/routes/indexes/documents.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index 12c79d20e..bc560123b 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -304,7 +304,7 @@ pub async fn replace_documents( debug!(parameters = ?params, "Replace documents"); let params = params.into_inner(); - analytics.add_documents(¶ms, index_scheduler.index_exists(&index_uid) == Ok(true), &req); + analytics.add_documents(¶ms, index_scheduler.index_exists(&index_uid) != Ok(true), &req); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; @@ -341,7 +341,7 @@ pub async fn update_documents( let params = params.into_inner(); debug!(parameters = ?params, "Update documents"); - analytics.update_documents(¶ms, index_scheduler.index_exists(&index_uid) == Ok(true), &req); + analytics.update_documents(¶ms, index_scheduler.index_exists(&index_uid) != Ok(true), &req); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; From 603676cb3b194b911c5f3a987abc50d3bbe4803a Mon Sep 17 00:00:00 2001 From: karribalu Date: Wed, 10 Jul 2024 19:42:16 +0100 Subject: [PATCH 4/4] Address PR review changes --- meilisearch/src/routes/indexes/documents.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index bc560123b..6661c828c 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -304,7 +304,11 @@ pub async fn replace_documents( debug!(parameters = ?params, "Replace documents"); let params = params.into_inner(); - analytics.add_documents(¶ms, index_scheduler.index_exists(&index_uid) != Ok(true), &req); + analytics.add_documents( + ¶ms, + index_scheduler.index_exists(&index_uid).map_or(true, |x| !x), + &req, + ); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?; @@ -341,7 +345,11 @@ pub async fn update_documents( let params = params.into_inner(); debug!(parameters = ?params, "Update documents"); - analytics.update_documents(¶ms, index_scheduler.index_exists(&index_uid) != Ok(true), &req); + analytics.add_documents( + ¶ms, + index_scheduler.index_exists(&index_uid).map_or(true, |x| !x), + &req, + ); let allow_index_creation = index_scheduler.filters().allow_index_creation(&index_uid); let uid = get_task_id(&req, &opt)?;