From ae5581d37c6d8e1ed025fc3935179f49de7e8dfc Mon Sep 17 00:00:00 2001 From: mpostma Date: Thu, 4 Mar 2021 15:59:18 +0100 Subject: [PATCH] implement delete documents --- src/data/updates.rs | 10 ++++------ src/index_controller/mod.rs | 18 ++++++++++++++---- src/routes/document.rs | 27 +++++++++++++-------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/data/updates.rs b/src/data/updates.rs index 92bba693c..775734ac1 100644 --- a/src/data/updates.rs +++ b/src/data/updates.rs @@ -44,13 +44,11 @@ impl Data { pub async fn delete_documents( &self, - _index: impl AsRef + Sync + Send + 'static, - _document_ids: Vec, + index: impl AsRef + Sync + Send + 'static, + document_ids: Vec, ) -> anyhow::Result { - todo!() - //let index_controller = self.index_controller.clone(); - //let update = tokio::task::spawn_blocking(move || index_controller.delete_documents(index, document_ids)).await??; - //Ok(update.into()) + let update = self.index_controller.delete_documents(index.as_ref().to_string(), document_ids).await?; + Ok(update.into()) } pub async fn delete_index( diff --git a/src/index_controller/mod.rs b/src/index_controller/mod.rs index dabf1b936..5f00c2540 100644 --- a/src/index_controller/mod.rs +++ b/src/index_controller/mod.rs @@ -7,8 +7,7 @@ mod update_handler; use std::path::Path; -use actix_web::web::Bytes; -use actix_web::web::Payload; +use actix_web::web::{Bytes, Payload}; use anyhow::Context; use chrono::{DateTime, Utc}; use futures::stream::StreamExt; @@ -111,8 +110,19 @@ impl IndexController { todo!() } - fn delete_documents(&self, index: String, document_ids: Vec) -> anyhow::Result { - todo!() + pub async fn delete_documents(&self, index: String, document_ids: Vec) -> anyhow::Result { + let uuid = self.uuid_resolver.resolve(index).await.unwrap().unwrap(); + let meta = UpdateMeta::DeleteDocuments; + let (sender, receiver) = mpsc::channel(10); + + tokio::task::spawn(async move { + let json = serde_json::to_vec(&document_ids).unwrap(); + let bytes = Bytes::from(json); + let _ = sender.send(Ok(bytes)).await; + }); + + let status = self.update_handle.update(meta, receiver, uuid).await?; + Ok(status) } pub async fn update_settings(&self, index_uid: String, settings: Settings) -> anyhow::Result { diff --git a/src/routes/document.rs b/src/routes/document.rs index 965b669d9..a848ddcc4 100644 --- a/src/routes/document.rs +++ b/src/routes/document.rs @@ -229,21 +229,20 @@ async fn delete_documents( path: web::Path, body: web::Json>, ) -> Result { - todo!() - //let ids = body - //.iter() - //.map(|v| v.as_str().map(String::from).unwrap_or_else(|| v.to_string())) - //.collect(); + let ids = body + .iter() + .map(|v| v.as_str().map(String::from).unwrap_or_else(|| v.to_string())) + .collect(); - //match data.delete_documents(path.index_uid.clone(), ids).await { - //Ok(result) => { - //let json = serde_json::to_string(&result).unwrap(); - //Ok(HttpResponse::Ok().body(json)) - //} - //Err(e) => { - //Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) - //} - //} + match data.delete_documents(path.index_uid.clone(), ids).await { + Ok(result) => { + let json = serde_json::to_string(&result).unwrap(); + Ok(HttpResponse::Ok().body(json)) + } + Err(e) => { + Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + } + } } #[delete("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]