change index name to uid

This commit is contained in:
mpostma 2021-03-11 22:47:29 +01:00
parent 8617bcf8bd
commit 271c8ba991
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
3 changed files with 40 additions and 40 deletions

View File

@ -90,7 +90,7 @@ impl Data {
pub async fn create_index(&self, name: impl AsRef<str>, primary_key: Option<impl AsRef<str>>) -> anyhow::Result<IndexMetadata> { pub async fn create_index(&self, name: impl AsRef<str>, primary_key: Option<impl AsRef<str>>) -> anyhow::Result<IndexMetadata> {
let settings = IndexSettings { let settings = IndexSettings {
name: Some(name.as_ref().to_string()), uid: Some(name.as_ref().to_string()),
primary_key: primary_key.map(|s| s.as_ref().to_string()), primary_key: primary_key.map(|s| s.as_ref().to_string()),
}; };

View File

@ -26,7 +26,7 @@ pub type UpdateStatus = updates::UpdateStatus<UpdateMeta, UpdateResult, String>;
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct IndexMetadata { pub struct IndexMetadata {
name: String, uid: String,
#[serde(flatten)] #[serde(flatten)]
meta: index_actor::IndexMeta, meta: index_actor::IndexMeta,
} }
@ -47,7 +47,7 @@ pub enum UpdateMeta {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct IndexSettings { pub struct IndexSettings {
pub name: Option<String>, pub uid: Option<String>,
pub primary_key: Option<String>, pub primary_key: Option<String>,
} }
@ -77,13 +77,13 @@ impl IndexController {
pub async fn add_documents( pub async fn add_documents(
&self, &self,
index: String, uid: String,
method: milli::update::IndexDocumentsMethod, method: milli::update::IndexDocumentsMethod,
format: milli::update::UpdateFormat, format: milli::update::UpdateFormat,
mut payload: Payload, mut payload: Payload,
primary_key: Option<String>, primary_key: Option<String>,
) -> anyhow::Result<UpdateStatus> { ) -> anyhow::Result<UpdateStatus> {
let uuid = self.uuid_resolver.get_or_create(index).await?; let uuid = self.uuid_resolver.get_or_create(uid).await?;
let meta = UpdateMeta::DocumentsAddition { method, format, primary_key }; let meta = UpdateMeta::DocumentsAddition { method, format, primary_key };
let (sender, receiver) = mpsc::channel(10); let (sender, receiver) = mpsc::channel(10);
@ -106,16 +106,16 @@ impl IndexController {
Ok(status) Ok(status)
} }
pub async fn clear_documents(&self, index: String) -> anyhow::Result<UpdateStatus> { pub async fn clear_documents(&self, uid: String) -> anyhow::Result<UpdateStatus> {
let uuid = self.uuid_resolver.resolve(index).await?; let uuid = self.uuid_resolver.resolve(uid).await?;
let meta = UpdateMeta::ClearDocuments; let meta = UpdateMeta::ClearDocuments;
let (_, receiver) = mpsc::channel(1); let (_, receiver) = mpsc::channel(1);
let status = self.update_handle.update(meta, receiver, uuid).await?; let status = self.update_handle.update(meta, receiver, uuid).await?;
Ok(status) Ok(status)
} }
pub async fn delete_documents(&self, index: String, document_ids: Vec<String>) -> anyhow::Result<UpdateStatus> { pub async fn delete_documents(&self, uid: String, document_ids: Vec<String>) -> anyhow::Result<UpdateStatus> {
let uuid = self.uuid_resolver.resolve(index).await?; let uuid = self.uuid_resolver.resolve(uid).await?;
let meta = UpdateMeta::DeleteDocuments; let meta = UpdateMeta::DeleteDocuments;
let (sender, receiver) = mpsc::channel(10); let (sender, receiver) = mpsc::channel(10);
@ -129,11 +129,11 @@ impl IndexController {
Ok(status) Ok(status)
} }
pub async fn update_settings(&self, index_uid: String, settings: Settings, create: bool) -> anyhow::Result<UpdateStatus> { pub async fn update_settings(&self, uid: String, settings: Settings, create: bool) -> anyhow::Result<UpdateStatus> {
let uuid = if create { let uuid = if create {
self.uuid_resolver.get_or_create(index_uid).await? self.uuid_resolver.get_or_create(uid).await?
} else { } else {
self.uuid_resolver.resolve(index_uid).await? self.uuid_resolver.resolve(uid).await?
}; };
let meta = UpdateMeta::Settings(settings); let meta = UpdateMeta::Settings(settings);
// Nothing so send, drop the sender right away, as not to block the update actor. // Nothing so send, drop the sender right away, as not to block the update actor.
@ -144,36 +144,36 @@ impl IndexController {
} }
pub async fn create_index(&self, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> { pub async fn create_index(&self, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> {
let IndexSettings { name, primary_key } = index_settings; let IndexSettings { uid: name, primary_key } = index_settings;
let name = name.unwrap(); let uid = name.unwrap();
let uuid = self.uuid_resolver.create(name.clone()).await?; let uuid = self.uuid_resolver.create(uid.clone()).await?;
let meta = self.index_handle.create_index(uuid, primary_key).await?; let meta = self.index_handle.create_index(uuid, primary_key).await?;
let _ = self.update_handle.create(uuid).await?; let _ = self.update_handle.create(uuid).await?;
let meta = IndexMetadata { name, meta }; let meta = IndexMetadata { uid, meta };
Ok(meta) Ok(meta)
} }
pub async fn delete_index(&self, index_uid: String) -> anyhow::Result<()> { pub async fn delete_index(&self, uid: String) -> anyhow::Result<()> {
let uuid = self.uuid_resolver let uuid = self.uuid_resolver
.delete(index_uid) .delete(uid)
.await?; .await?;
self.update_handle.delete(uuid.clone()).await?; self.update_handle.delete(uuid.clone()).await?;
self.index_handle.delete(uuid).await?; self.index_handle.delete(uuid).await?;
Ok(()) Ok(())
} }
pub async fn update_status(&self, index: String, id: u64) -> anyhow::Result<Option<UpdateStatus>> { pub async fn update_status(&self, uid: String, id: u64) -> anyhow::Result<Option<UpdateStatus>> {
let uuid = self.uuid_resolver let uuid = self.uuid_resolver
.resolve(index) .resolve(uid)
.await?; .await?;
let result = self.update_handle.update_status(uuid, id).await?; let result = self.update_handle.update_status(uuid, id).await?;
Ok(result) Ok(result)
} }
pub async fn all_update_status(&self, index: String) -> anyhow::Result<Vec<UpdateStatus>> { pub async fn all_update_status(&self, uid: String) -> anyhow::Result<Vec<UpdateStatus>> {
let uuid = self.uuid_resolver let uuid = self.uuid_resolver
.resolve(index).await?; .resolve(uid).await?;
let result = self.update_handle.get_all_updates_status(uuid).await?; let result = self.update_handle.get_all_updates_status(uuid).await?;
Ok(result) Ok(result)
} }
@ -183,9 +183,9 @@ impl IndexController {
let mut ret = Vec::new(); let mut ret = Vec::new();
for (name, uuid) in uuids { for (uid, uuid) in uuids {
if let Some(meta) = self.index_handle.get_index_meta(uuid).await? { if let Some(meta) = self.index_handle.get_index_meta(uuid).await? {
let meta = IndexMetadata { name, meta }; let meta = IndexMetadata { uid, meta };
ret.push(meta); ret.push(meta);
} }
} }
@ -193,9 +193,9 @@ impl IndexController {
Ok(ret) Ok(ret)
} }
pub async fn settings(&self, index: String) -> anyhow::Result<Settings> { pub async fn settings(&self, uid: String) -> anyhow::Result<Settings> {
let uuid = self.uuid_resolver let uuid = self.uuid_resolver
.resolve(index.clone()) .resolve(uid.clone())
.await?; .await?;
let settings = self.index_handle.settings(uuid).await?; let settings = self.index_handle.settings(uuid).await?;
Ok(settings) Ok(settings)
@ -203,13 +203,13 @@ impl IndexController {
pub async fn documents( pub async fn documents(
&self, &self,
index: String, uid: String,
offset: usize, offset: usize,
limit: usize, limit: usize,
attributes_to_retrieve: Option<Vec<String>>, attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Vec<Document>> { ) -> anyhow::Result<Vec<Document>> {
let uuid = self.uuid_resolver let uuid = self.uuid_resolver
.resolve(index.clone()) .resolve(uid.clone())
.await?; .await?;
let documents = self.index_handle.documents(uuid, offset, limit, attributes_to_retrieve).await?; let documents = self.index_handle.documents(uuid, offset, limit, attributes_to_retrieve).await?;
Ok(documents) Ok(documents)
@ -217,33 +217,33 @@ impl IndexController {
pub async fn document( pub async fn document(
&self, &self,
index: String, uid: String,
doc_id: String, doc_id: String,
attributes_to_retrieve: Option<Vec<String>>, attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Document> { ) -> anyhow::Result<Document> {
let uuid = self.uuid_resolver let uuid = self.uuid_resolver
.resolve(index.clone()) .resolve(uid.clone())
.await?; .await?;
let document = self.index_handle.document(uuid, doc_id, attributes_to_retrieve).await?; let document = self.index_handle.document(uuid, doc_id, attributes_to_retrieve).await?;
Ok(document) Ok(document)
} }
fn update_index(&self, name: String, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> { fn update_index(&self, uid: String, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> {
todo!() todo!()
} }
pub async fn search(&self, name: String, query: SearchQuery) -> anyhow::Result<SearchResult> { pub async fn search(&self, uid: String, query: SearchQuery) -> anyhow::Result<SearchResult> {
let uuid = self.uuid_resolver.resolve(name).await?; let uuid = self.uuid_resolver.resolve(uid).await?;
let result = self.index_handle.search(uuid, query).await?; let result = self.index_handle.search(uuid, query).await?;
Ok(result) Ok(result)
} }
pub async fn get_index(&self, name: String) -> anyhow::Result<Option<IndexMetadata>> { pub async fn get_index(&self, uid: String) -> anyhow::Result<Option<IndexMetadata>> {
let uuid = self.uuid_resolver.resolve(name.clone()).await?; let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
let result = self.index_handle let result = self.index_handle
.get_index_meta(uuid) .get_index_meta(uuid)
.await? .await?
.map(|meta| IndexMetadata { name, meta }); .map(|meta| IndexMetadata { uid, meta });
Ok(result) Ok(result)
} }
} }

View File

@ -7,14 +7,15 @@ async fn create_index_no_primary_key() {
let index = server.index("test"); let index = server.index("test");
let (response, code) = index.create(None).await; let (response, code) = index.create(None).await;
println!("response: {}", response);
assert_eq!(code, 200); assert_eq!(code, 200);
assert_eq!(response["uid"], "test"); assert_eq!(response["uid"], "test");
assert!(response.get("uuid").is_some());
assert!(response.get("createdAt").is_some()); assert!(response.get("createdAt").is_some());
assert!(response.get("updatedAt").is_some()); assert!(response.get("updatedAt").is_some());
assert_eq!(response["createdAt"], response["updatedAt"]); assert_eq!(response["createdAt"], response["updatedAt"]);
assert_eq!(response["primaryKey"], Value::Null); assert_eq!(response["primaryKey"], Value::Null);
assert_eq!(response.as_object().unwrap().len(), 5); assert_eq!(response.as_object().unwrap().len(), 4);
} }
#[actix_rt::test] #[actix_rt::test]
@ -25,12 +26,11 @@ async fn create_index_with_primary_key() {
assert_eq!(code, 200); assert_eq!(code, 200);
assert_eq!(response["uid"], "test"); assert_eq!(response["uid"], "test");
assert!(response.get("uuid").is_some());
assert!(response.get("createdAt").is_some()); assert!(response.get("createdAt").is_some());
assert!(response.get("updatedAt").is_some()); assert!(response.get("updatedAt").is_some());
assert_eq!(response["createdAt"], response["updatedAt"]); assert_eq!(response["createdAt"], response["updatedAt"]);
assert_eq!(response["primaryKey"], "primary"); assert_eq!(response["primaryKey"], "primary");
assert_eq!(response.as_object().unwrap().len(), 5); assert_eq!(response.as_object().unwrap().len(), 4);
} }
// TODO: partial test since we are testing error, amd error is not yet fully implemented in // TODO: partial test since we are testing error, amd error is not yet fully implemented in