diff --git a/src/index_controller/local_index_controller/index_store.rs b/src/index_controller/local_index_controller/index_store.rs index 96ef3e7cd..226915016 100644 --- a/src/index_controller/local_index_controller/index_store.rs +++ b/src/index_controller/local_index_controller/index_store.rs @@ -133,7 +133,7 @@ impl IndexStore { } /// Use this function to perform an update on an index. - /// This function also puts a lock on what index in allowed to perform an update. + /// This function also puts a lock on what index is allowed to perform an update. pub fn update_index(&self, name: impl AsRef, f: F) -> anyhow::Result<(T, IndexMeta)> where F: FnOnce(&Index) -> anyhow::Result, @@ -167,7 +167,10 @@ impl IndexStore { } } - fn update_meta(&self, txn: &mut RwTxn, name: impl AsRef, f: impl FnOnce(&mut IndexMeta)) -> anyhow::Result { + fn update_meta(&self, txn: &mut RwTxn, name: impl AsRef, f: F) -> anyhow::Result + where + F: FnOnce(&mut IndexMeta) + { let uuid = self.index_uuid(txn, &name)? .with_context(|| format!("Index {:?} doesn't exist", name.as_ref()))?; let mut meta = self.uuid_to_index_meta diff --git a/src/index_controller/mod.rs b/src/index_controller/mod.rs index a0f8476ff..77d91575a 100644 --- a/src/index_controller/mod.rs +++ b/src/index_controller/mod.rs @@ -10,7 +10,9 @@ use std::sync::Arc; use anyhow::Result; use chrono::{DateTime, Utc}; use milli::Index; -use milli::update::{IndexDocumentsMethod, UpdateFormat, DocumentAdditionResult}; use serde::{Serialize, Deserialize, de::Deserializer}; use uuid::Uuid; +use milli::update::{IndexDocumentsMethod, UpdateFormat, DocumentAdditionResult}; +use serde::{Serialize, Deserialize, de::Deserializer}; +use uuid::Uuid; pub use updates::{Processed, Processing, Failed}; @@ -95,6 +97,7 @@ pub enum UpdateResult { Other, } +#[derive(Clone, Debug)] pub struct IndexSettings { pub name: Option, pub primary_key: Option, @@ -183,10 +186,15 @@ pub(crate) mod test { fn test_create_index_with_no_name_is_error() { crate::index_controller::test::create_index_with_no_name_is_error($controller_buider); } + + #[test] + fn test_update_index() { + crate::index_controller::test::update_index($controller_buider); + } }; } - pub(crate) fn create_and_list_indexes(controller: S) { + pub(crate) fn create_and_list_indexes(controller: impl IndexController) { let settings1 = IndexSettings { name: Some(String::from("test_index")), primary_key: None, @@ -207,11 +215,54 @@ pub(crate) mod test { assert_eq!(indexes[1].primary_key.clone().unwrap(), "foo"); } - pub(crate) fn create_index_with_no_name_is_error(controller: S) { + pub(crate) fn create_index_with_no_name_is_error(controller: impl IndexController) { let settings = IndexSettings { name: None, primary_key: None, }; assert!(controller.create_index(settings).is_err()); } + + pub(crate) fn update_index(controller: impl IndexController) { + + let settings = IndexSettings { + name: Some(String::from("test")), + primary_key: None, + }; + + assert!(controller.create_index(settings).is_ok()); + + // perform empty update returns index meta unchanged + let settings = IndexSettings { + name: None, + primary_key: None, + }; + + let result = controller.update_index("test", settings).unwrap(); + assert_eq!(result.name, "test"); + assert_eq!(result.created_at, result.updated_at); + assert!(result.primary_key.is_none()); + + // Changing the name trigger an error + let settings = IndexSettings { + name: Some(String::from("bar")), + primary_key: None, + }; + + assert!(controller.update_index("test", settings).is_err()); + + // Update primary key + let settings = IndexSettings { + name: None, + primary_key: Some(String::from("foo")), + }; + + let result = controller.update_index("test", settings.clone()).unwrap(); + assert_eq!(result.name, "test"); + assert!(result.created_at < result.updated_at); + assert_eq!(result.primary_key.unwrap(), "foo"); + + // setting the primary key again is an error + assert!(controller.update_index("test", settings).is_err()); + } }