fix settings delete bug

This commit is contained in:
mpostma 2021-02-24 10:42:27 +01:00
parent 402203aa2a
commit d52e6fc21e
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
4 changed files with 18 additions and 11 deletions

View File

@ -55,10 +55,11 @@ impl Data {
pub async fn update_settings(
&self,
index: impl AsRef<str> + Send + Sync + 'static,
settings: Settings
settings: Settings,
create: bool,
) -> anyhow::Result<UpdateStatus> {
let index_controller = self.index_controller.clone();
let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings)).await??;
let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings, create)).await??;
Ok(update.into())
}

View File

@ -5,7 +5,7 @@ mod update_handler;
use std::path::Path;
use std::sync::Arc;
use anyhow::{bail, Context};
use anyhow::{bail, Context, anyhow};
use itertools::Itertools;
use milli::Index;
@ -51,9 +51,14 @@ impl IndexController for LocalIndexController {
fn update_settings<S: AsRef<str>>(
&self,
index: S,
settings: super::Settings
settings: super::Settings,
create: bool,
) -> anyhow::Result<UpdateStatus<UpdateMeta, UpdateResult, String>> {
let (_, update_store) = self.indexes.get_or_create_index(&index, self.update_db_size, self.index_db_size)?;
let (_, update_store) = if create {
self.indexes.get_or_create_index(&index, self.update_db_size, self.index_db_size)?
} else {
self.indexes.index(&index)?.ok_or_else(|| anyhow!("Index {:?} doesn't exist", index.as_ref()))?
};
let meta = UpdateMeta::Settings(settings);
let pending = update_store.register_update(meta, &[])?;
Ok(pending.into())

View File

@ -142,8 +142,9 @@ pub trait IndexController {
fn delete_documents(&self, index: impl AsRef<str>, document_ids: Vec<String>) -> anyhow::Result<UpdateStatus>;
/// Updates an index settings. If the index does not exist, it will be created when the update
/// is applied to the index.
fn update_settings<S: AsRef<str>>(&self, index_uid: S, settings: Settings) -> anyhow::Result<UpdateStatus>;
/// is applied to the index. `create` specifies whether an index should be created if not
/// existing.
fn update_settings<S: AsRef<str>>(&self, index_uid: S, settings: Settings, create: bool) -> anyhow::Result<UpdateStatus>;
/// Create an index with the given `index_uid`.
fn create_index(&self, index_settings: IndexSettings) -> Result<IndexMetadata>;

View File

@ -26,7 +26,7 @@ macro_rules! make_setting_route {
$attr: Some(None),
..Default::default()
};
match data.update_settings(index_uid.into_inner(), settings).await {
match data.update_settings(index_uid.into_inner(), settings, false).await {
Ok(update_status) => {
let json = serde_json::to_string(&update_status).unwrap();
Ok(HttpResponse::Ok().body(json))
@ -48,7 +48,7 @@ macro_rules! make_setting_route {
..Default::default()
};
match data.update_settings(index_uid.into_inner(), settings).await {
match data.update_settings(index_uid.into_inner(), settings, true).await {
Ok(update_status) => {
let json = serde_json::to_string(&update_status).unwrap();
Ok(HttpResponse::Ok().body(json))
@ -137,7 +137,7 @@ async fn update_all(
index_uid: web::Path<String>,
body: web::Json<Settings>,
) -> Result<HttpResponse, ResponseError> {
match data.update_settings(index_uid.into_inner(), body.into_inner()).await {
match data.update_settings(index_uid.into_inner(), body.into_inner(), true).await {
Ok(update_result) => {
let json = serde_json::to_string(&update_result).unwrap();
Ok(HttpResponse::Ok().body(json))
@ -170,7 +170,7 @@ async fn delete_all(
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let settings = Settings::cleared();
match data.update_settings(index_uid.into_inner(), settings).await {
match data.update_settings(index_uid.into_inner(), settings, false).await {
Ok(update_result) => {
let json = serde_json::to_string(&update_result).unwrap();
Ok(HttpResponse::Ok().body(json))