add lazy create index on settings handlers

This commit is contained in:
qdequele 2020-09-08 19:23:09 +02:00
parent bfe3bb0eeb
commit 17f71a1a55
3 changed files with 74 additions and 87 deletions

View File

@ -192,20 +192,17 @@ async fn update_rules(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>, body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = Settings {
.open_index(&path.index_uid) ranking_rules: Some(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..Settings::default()
};
let settings = Settings { let settings = settings.to_update().map_err(Error::bad_request)?;
ranking_rules: Some(body.into_inner()), Ok(data
..Settings::default() .db
}; .update_write(|w| index.settings_update(w, settings))?)
})?;
let settings = settings.to_update().map_err(Error::bad_request)?;
let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -267,20 +264,17 @@ async fn update_distinct(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<String>>, body: web::Json<Option<String>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = Settings {
.open_index(&path.index_uid) distinct_attribute: Some(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..Settings::default()
};
let settings = Settings { let settings = settings.to_update().map_err(Error::bad_request)?;
distinct_attribute: Some(body.into_inner()), Ok(data
..Settings::default() .db
}; .update_write(|w| index.settings_update(w, settings))?)
})?;
let settings = settings.to_update().map_err(Error::bad_request)?;
let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -338,21 +332,18 @@ async fn update_searchable(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>, body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = Settings {
.open_index(&path.index_uid) searchable_attributes: Some(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..Settings::default()
};
let settings = Settings { let settings = settings.to_update().map_err(Error::bad_request)?;
searchable_attributes: Some(body.into_inner()),
..Settings::default()
};
let settings = settings.to_update().map_err(Error::bad_request)?; Ok(data
.db
let update_id = data .update_write(|w| index.settings_update(w, settings))?)
.db })?;
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -412,20 +403,17 @@ async fn update_displayed(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<HashSet<String>>>, body: web::Json<Option<HashSet<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = Settings {
.open_index(&path.index_uid) displayed_attributes: Some(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..Settings::default()
};
let settings = Settings { let settings = settings.to_update().map_err(Error::bad_request)?;
displayed_attributes: Some(body.into_inner()), Ok(data
..Settings::default() .db
}; .update_write(|w| index.settings_update(w, settings))?)
})?;
let settings = settings.to_update().map_err(Error::bad_request)?;
let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -494,20 +482,17 @@ async fn update_attributes_for_faceting(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>, body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = Settings {
.open_index(&path.index_uid) attributes_for_faceting: Some(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..Settings::default()
};
let settings = Settings { let settings = settings.to_update().map_err(Error::bad_request)?;
attributes_for_faceting: Some(body.into_inner()), Ok(data
..Settings::default() .db
}; .update_write(|w| index.settings_update(w, settings))?)
})?;
let settings = settings.to_update().map_err(Error::bad_request)?;
let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }

View File

@ -39,17 +39,16 @@ async fn update(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<BTreeSet<String>>, body: web::Json<BTreeSet<String>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = SettingsUpdate {
.open_index(&path.index_uid) stop_words: UpdateState::Update(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..SettingsUpdate::default()
};
let settings = SettingsUpdate { Ok(data
stop_words: UpdateState::Update(body.into_inner()), .db
..SettingsUpdate::default() .update_write(|w| index.settings_update(w, settings))?)
}; })?;
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -72,7 +71,9 @@ async fn delete(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?; let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }

View File

@ -50,17 +50,16 @@ async fn update(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<BTreeMap<String, Vec<String>>>, body: web::Json<BTreeMap<String, Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = data let update_id = data.get_or_create_index(&path.index_uid, |index| {
.db let settings = SettingsUpdate {
.open_index(&path.index_uid) synonyms: UpdateState::Update(body.into_inner()),
.ok_or(Error::index_not_found(&path.index_uid))?; ..SettingsUpdate::default()
};
let settings = SettingsUpdate { Ok(data
synonyms: UpdateState::Update(body.into_inner()), .db
..SettingsUpdate::default() .update_write(|w| index.settings_update(w, settings))?)
}; })?;
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -83,7 +82,9 @@ async fn delete(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?; let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }