From 6a1f73a30410374b2ed99e0e0a1c1d210892c669 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Fri, 10 Apr 2020 19:05:05 +0200 Subject: [PATCH] clippy + fmt --- meilisearch-http/src/data.rs | 2 +- meilisearch-http/src/error.rs | 5 +- meilisearch-http/src/main.rs | 18 +- meilisearch-http/src/routes/document.rs | 134 +++++--- meilisearch-http/src/routes/health.rs | 40 +-- meilisearch-http/src/routes/index.rs | 210 +++++++++---- meilisearch-http/src/routes/key.rs | 9 +- meilisearch-http/src/routes/mod.rs | 12 +- meilisearch-http/src/routes/search.rs | 21 +- meilisearch-http/src/routes/setting.rs | 291 +++++++++++++----- meilisearch-http/src/routes/stats.rs | 77 +++-- meilisearch-http/src/routes/stop_words.rs | 58 ++-- meilisearch-http/src/routes/synonym.rs | 64 ++-- meilisearch-http/tests/documents_add.rs | 14 +- meilisearch-http/tests/index.rs | 8 +- .../tests/settings_accept_new_fields.rs | 3 +- .../tests/settings_ranking_rules.rs | 6 +- 17 files changed, 633 insertions(+), 339 deletions(-) diff --git a/meilisearch-http/src/data.rs b/meilisearch-http/src/data.rs index d6836258a..23f7efb69 100644 --- a/meilisearch-http/src/data.rs +++ b/meilisearch-http/src/data.rs @@ -135,7 +135,7 @@ impl Data { let db = Arc::new(Database::open_or_create(opt.db_path).unwrap()); let mut api_keys = ApiKeys { - master: opt.master_key.clone(), + master: opt.master_key, private: None, public: None, }; diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index 57a996e84..6c3210e1b 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -1,8 +1,9 @@ use std::fmt; -use serde_json::json; -use actix_web as aweb; + use actix_http::ResponseBuilder; +use actix_web as aweb; use actix_web::http::StatusCode; +use serde_json::json; #[derive(Debug)] pub enum ResponseError { diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index edddb37d6..e0e19448b 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -1,14 +1,14 @@ use std::{env, thread}; +use actix_web::middleware::Logger; +use actix_web::{web, App, HttpServer}; use log::info; use main_error::MainError; -use structopt::StructOpt; -use actix_web::middleware::Logger; -use actix_web::{web, HttpServer, App}; use meilisearch_http::data::Data; use meilisearch_http::option::Opt; use meilisearch_http::routes; use meilisearch_http::routes::index_update_callback; +use structopt::StructOpt; mod analytics; @@ -30,7 +30,7 @@ async fn main() -> Result<(), MainError> { .into(), ); } - }, + } "development" => { env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init(); } @@ -50,7 +50,7 @@ async fn main() -> Result<(), MainError> { print_launch_resume(&opt, &data); - HttpServer::new(move || + HttpServer::new(move || { App::new() .wrap(Logger::default()) .app_data(web::Data::new(data.clone())) @@ -103,10 +103,10 @@ async fn main() -> Result<(), MainError> { .service(routes::stats::get_sys_info_pretty) .service(routes::health::get_health) .service(routes::health::change_healthyness) - ) - .bind(opt.http_addr)? - .run() - .await?; + }) + .bind(opt.http_addr)? + .run() + .await?; Ok(()) } diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 99e40e481..763402fd2 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -1,20 +1,21 @@ use std::collections::BTreeSet; + +use actix_web as aweb; +use actix_web::{delete, get, post, put, web, HttpResponse}; use indexmap::IndexMap; use serde::Deserialize; use serde_json::Value; -use actix_web::{web, get, post, put, delete, HttpResponse}; -use actix_web as aweb; use crate::error::ResponseError; +use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::Data; -use crate::routes::{IndexUpdateResponse, IndexParam}; type Document = IndexMap; #[derive(Default, Deserialize)] pub struct DocumentParam { index_uid: String, - document_id: String + document_id: String, } #[get("/indexes/{index_uid}/documents/{document_id}")] @@ -22,14 +23,19 @@ pub async fn get_document( data: web::Data, path: web::Path, ) -> aweb::Result> { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let document_id = meilisearch_core::serde::compute_document_id(path.document_id.clone()); - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let response = index.document::(&reader, None, document_id) + let response = index + .document::(&reader, None, document_id) .map_err(|_| ResponseError::DocumentNotFound(path.document_id.clone()))? .ok_or(ResponseError::DocumentNotFound(path.document_id.clone()))?; @@ -41,26 +47,31 @@ pub async fn delete_document( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let document_id = meilisearch_core::serde::compute_document_id(path.document_id.clone()); - let mut update_writer = data.db.update_write_txn() + let mut update_writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut documents_deletion = index.documents_deletion(); documents_deletion.delete_document_by_id(document_id); - let update_id = documents_deletion.finalize(&mut update_writer) + let update_id = documents_deletion + .finalize(&mut update_writer) .map_err(|err| ResponseError::Internal(err.to_string()))?; - update_writer.commit() + update_writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } - #[derive(Default, Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct BrowseQuery { @@ -75,14 +86,17 @@ pub async fn get_all_documents( path: web::Path, params: web::Query, ) -> aweb::Result>> { - - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let offset = params.offset.unwrap_or(0); let limit = params.limit.unwrap_or(20); - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let documents_ids: Result, _> = index @@ -93,10 +107,11 @@ pub async fn get_all_documents( .take(limit) .collect(); - let documents_ids = documents_ids - .map_err(|err| ResponseError::Internal(err.to_string()))?; + let documents_ids = documents_ids.map_err(|err| ResponseError::Internal(err.to_string()))?; - let attributes = params.attributes_to_retrieve.clone() + let attributes = params + .attributes_to_retrieve + .clone() .map(|a| a.split(',').map(|a| a.to_string()).collect()); let mut response_body = Vec::::new(); @@ -129,39 +144,48 @@ async fn update_multiple_documents( path: web::Path, params: web::Query, body: web::Json>, - is_partial: bool + is_partial: bool, ) -> aweb::Result { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut schema = index .main .schema(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))? - .ok_or(ResponseError::Internal("Impossible to retrieve the schema".to_string()))?; + .ok_or(ResponseError::Internal( + "Impossible to retrieve the schema".to_string(), + ))?; if schema.primary_key().is_none() { let id = match params.primary_key.clone() { Some(id) => id, - None => { - body.first() - .and_then(|docs| find_primary_key(docs)) - .ok_or(ResponseError::BadRequest("Impossible to infer the primary key".to_string()))? - } + None => body.first().and_then(|docs| find_primary_key(docs)).ok_or( + ResponseError::BadRequest("Impossible to infer the primary key".to_string()), + )?, }; - let mut writer = data.db.main_write_txn() + let mut writer = data + .db + .main_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - schema.set_primary_key(&id) + schema + .set_primary_key(&id) .map_err(|e| ResponseError::Internal(e.to_string()))?; - index.main.put_schema(&mut writer, &schema) + index + .main + .put_schema(&mut writer, &schema) .map_err(|e| ResponseError::Internal(e.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; } @@ -175,11 +199,15 @@ async fn update_multiple_documents( document_addition.update_document(document); } - let mut update_writer = data.db.update_write_txn() + let mut update_writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = document_addition.finalize(&mut update_writer) + let update_id = document_addition + .finalize(&mut update_writer) .map_err(|e| ResponseError::Internal(e.to_string()))?; - update_writer.commit() + update_writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -190,7 +218,7 @@ pub async fn add_documents( data: web::Data, path: web::Path, params: web::Query, - body: web::Json> + body: web::Json>, ) -> aweb::Result { update_multiple_documents(data, path, params, body, false).await } @@ -200,7 +228,7 @@ pub async fn update_documents( data: web::Data, path: web::Path, params: web::Query, - body: web::Json> + body: web::Json>, ) -> aweb::Result { update_multiple_documents(data, path, params, body, true).await } @@ -209,13 +237,16 @@ pub async fn update_documents( pub async fn delete_documents( data: web::Data, path: web::Path, - body: web::Json> + body: web::Json>, ) -> aweb::Result { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut documents_deletion = index.documents_deletion(); @@ -227,10 +258,12 @@ pub async fn delete_documents( } } - let update_id = documents_deletion.finalize(&mut writer) + let update_id = documents_deletion + .finalize(&mut writer) .map_err(|e| ResponseError::Internal(e.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -241,17 +274,22 @@ pub async fn clear_all_documents( data: web::Data, path: web::Path, ) -> aweb::Result { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.clear_all(&mut writer) + let update_id = index + .clear_all(&mut writer) .map_err(|e| ResponseError::Internal(e.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) diff --git a/meilisearch-http/src/routes/health.rs b/meilisearch-http/src/routes/health.rs index f1b92078d..8b4433303 100644 --- a/meilisearch-http/src/routes/health.rs +++ b/meilisearch-http/src/routes/health.rs @@ -1,17 +1,17 @@ use crate::error::ResponseError; -use actix_web::{web, get, put, HttpResponse}; -use actix_web as aweb; use crate::Data; +use actix_web as aweb; +use actix_web::{get, put, web, HttpResponse}; use heed::types::{Str, Unit}; use serde::Deserialize; const UNHEALTHY_KEY: &str = "_is_unhealthy"; #[get("/health")] -pub async fn get_health( - data: web::Data, -) -> aweb::Result { - let reader = data.db.main_read_txn() +pub async fn get_health(data: web::Data) -> aweb::Result { + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let common_store = data.db.common_store(); @@ -23,29 +23,33 @@ pub async fn get_health( Ok(HttpResponse::Ok().finish()) } -pub async fn set_healthy( - data: web::Data, -) -> aweb::Result { - let mut writer = data.db.main_write_txn() +pub async fn set_healthy(data: web::Data) -> aweb::Result { + let mut writer = data + .db + .main_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let common_store = data.db.common_store(); - common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY) + common_store + .delete::<_, Str>(&mut writer, UNHEALTHY_KEY) .map_err(|e| ResponseError::Internal(e.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Ok().finish()) } -pub async fn set_unhealthy( - data: web::Data, -) -> aweb::Result { - let mut writer = data.db.main_write_txn() +pub async fn set_unhealthy(data: web::Data) -> aweb::Result { + let mut writer = data + .db + .main_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let common_store = data.db.common_store(); - common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &()) + common_store + .put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &()) .map_err(|e| ResponseError::Internal(e.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Ok().finish()) diff --git a/meilisearch-http/src/routes/index.rs b/meilisearch-http/src/routes/index.rs index ec7f65315..f1153ad74 100644 --- a/meilisearch-http/src/routes/index.rs +++ b/meilisearch-http/src/routes/index.rs @@ -1,14 +1,14 @@ +use actix_web as aweb; +use actix_web::{delete, get, post, web, HttpResponse}; use chrono::{DateTime, Utc}; use log::error; +use meilisearch_core::UpdateStatus; use rand::seq::SliceRandom; use serde::{Deserialize, Serialize}; -use actix_web::{web, get, post, delete, HttpResponse}; -use actix_web as aweb; -use meilisearch_core::UpdateStatus; use crate::error::ResponseError; -use crate::Data; use crate::routes::IndexParam; +use crate::Data; fn generate_uid() -> String { let mut rng = rand::thread_rng(); @@ -30,11 +30,10 @@ pub struct IndexResponse { } #[get("/indexes")] -pub async fn list_indexes( - data: web::Data, -) -> aweb::Result>> { - - let reader = data.db.main_read_txn() +pub async fn list_indexes(data: web::Data) -> aweb::Result>> { + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut response_body = Vec::new(); @@ -44,15 +43,27 @@ pub async fn list_indexes( match index { Some(index) => { - let name = index.main.name(&reader) + let name = index + .main + .name(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the name of an index".to_string()))?; - let created_at = index.main.created_at(&reader) + .ok_or(ResponseError::Internal( + "Impossible to get the name of an index".to_string(), + ))?; + let created_at = index + .main + .created_at(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the create date of an index".to_string()))?; - let updated_at = index.main.updated_at(&reader) + .ok_or(ResponseError::Internal( + "Impossible to get the create date of an index".to_string(), + ))?; + let updated_at = index + .main + .updated_at(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the last update date of an index".to_string()))?; + .ok_or(ResponseError::Internal( + "Impossible to get the last update date of an index".to_string(), + ))?; let primary_key = match index.main.schema(&reader) { Ok(Some(schema)) => match schema.primary_key() { @@ -86,22 +97,37 @@ pub async fn get_index( data: web::Data, path: web::Path, ) -> aweb::Result> { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let name = index.main.name(&reader) + let name = index + .main + .name(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the name of an index".to_string()))?; - let created_at = index.main.created_at(&reader) + .ok_or(ResponseError::Internal( + "Impossible to get the name of an index".to_string(), + ))?; + let created_at = index + .main + .created_at(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the create date of an index".to_string()))?; - let updated_at = index.main.updated_at(&reader) + .ok_or(ResponseError::Internal( + "Impossible to get the create date of an index".to_string(), + ))?; + let updated_at = index + .main + .updated_at(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the last update date of an index".to_string()))?; + .ok_or(ResponseError::Internal( + "Impossible to get the last update date of an index".to_string(), + ))?; let primary_key = match index.main.schema(&reader) { Ok(Some(schema)) => match schema.primary_key() { @@ -131,11 +157,12 @@ pub struct IndexCreateRequest { #[post("/indexes")] pub async fn create_index( data: web::Data, - body: web::Json + body: web::Json, ) -> aweb::Result> { - if let (None, None) = (body.name.clone(), body.uid.clone()) { - return Err(ResponseError::BadRequest("Index creation must have an uid".to_string()).into()); + return Err( + ResponseError::BadRequest("Index creation must have an uid".to_string()).into(), + ); } let uid = match body.uid.clone() { @@ -157,14 +184,20 @@ pub async fn create_index( }, }; - let created_index = data.db.create_index(&uid) + let created_index = data + .db + .create_index(&uid) .map_err(|e| ResponseError::CreateIndex(e.to_string()))?; - let mut writer = data.db.main_write_txn() + let mut writer = data + .db + .main_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let name = body.name.clone().unwrap_or(uid.clone()); - created_index.main.put_name(&mut writer, &name) + created_index + .main + .put_name(&mut writer, &name) .map_err(|e| ResponseError::Internal(e.to_string()))?; let created_at = created_index @@ -180,16 +213,23 @@ pub async fn create_index( .ok_or(ResponseError::Internal("".to_string()))?; if let Some(id) = body.primary_key.clone() { - if let Some(mut schema) = created_index.main.schema(&mut writer) - .map_err(|e| ResponseError::Internal(e.to_string()))? { - schema.set_primary_key(&id) + if let Some(mut schema) = created_index + .main + .schema(&writer) + .map_err(|e| ResponseError::Internal(e.to_string()))? + { + schema + .set_primary_key(&id) .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - created_index.main.put_schema(&mut writer, &schema) + created_index + .main + .put_schema(&mut writer, &schema) .map_err(|e| ResponseError::Internal(e.to_string()))?; } } - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(web::Json(IndexResponse { @@ -222,55 +262,85 @@ pub struct UpdateIndexResponse { pub async fn update_index( data: web::Data, path: web::Path, - body: web::Json + body: web::Json, ) -> aweb::Result> { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.main_write_txn() - .map_err(|err| ResponseError::Internal(err.to_string()))?; + let mut writer = data + .db + .main_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; if let Some(name) = body.name.clone() { - index.main.put_name(&mut writer, &name) + index + .main + .put_name(&mut writer, &name) .map_err(|e| ResponseError::Internal(e.to_string()))?; } if let Some(id) = body.primary_key.clone() { - if let Some(mut schema) = index.main.schema(&mut writer) - .map_err(|e| ResponseError::Internal(e.to_string()))? { + if let Some(mut schema) = index + .main + .schema(&writer) + .map_err(|e| ResponseError::Internal(e.to_string()))? + { match schema.primary_key() { Some(_) => { - return Err(ResponseError::BadRequest("The primary key cannot be updated".to_string()).into()); + return Err(ResponseError::BadRequest( + "The primary key cannot be updated".to_string(), + ) + .into()); } None => { schema .set_primary_key(&id) .map_err(|e| ResponseError::Internal(e.to_string()))?; - index.main.put_schema(&mut writer, &schema) + index + .main + .put_schema(&mut writer, &schema) .map_err(|e| ResponseError::Internal(e.to_string()))?; } } } } - index.main.put_updated_at(&mut writer) + index + .main + .put_updated_at(&mut writer) .map_err(|e| ResponseError::Internal(e.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let name = index.main.name(&reader) + let name = index + .main + .name(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the name of an index".to_string()))?; - let created_at = index.main.created_at(&reader) + .ok_or(ResponseError::Internal( + "Impossible to get the name of an index".to_string(), + ))?; + let created_at = index + .main + .created_at(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the create date of an index".to_string()))?; - let updated_at = index.main.updated_at(&reader) + .ok_or(ResponseError::Internal( + "Impossible to get the create date of an index".to_string(), + ))?; + let updated_at = index + .main + .updated_at(&reader) .map_err(|e| ResponseError::Internal(e.to_string()))? - .ok_or(ResponseError::Internal("Impossible to get the last update date of an index".to_string()))?; + .ok_or(ResponseError::Internal( + "Impossible to get the last update date of an index".to_string(), + ))?; let primary_key = match index.main.schema(&reader) { Ok(Some(schema)) => match schema.primary_key() { @@ -294,8 +364,8 @@ pub async fn delete_index( data: web::Data, path: web::Path, ) -> aweb::Result { - - data.db.delete_index(&path.index_uid) + data.db + .delete_index(&path.index_uid) .map_err(|e| ResponseError::Internal(e.to_string()))?; HttpResponse::NoContent().await @@ -304,7 +374,7 @@ pub async fn delete_index( #[derive(Default, Deserialize)] pub struct UpdateParam { index_uid: String, - update_id: u64 + update_id: u64, } #[get("/indexes/{index_uid}/updates/{update_id}")] @@ -312,19 +382,23 @@ pub async fn get_update_status( data: web::Data, path: web::Path, ) -> aweb::Result> { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.update_read_txn() + let reader = data + .db + .update_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let status = index.update_status(&reader, path.update_id) + let status = index + .update_status(&reader, path.update_id) .map_err(|e| ResponseError::Internal(e.to_string()))?; match status { Some(status) => Ok(web::Json(status)), - None => Err(ResponseError::NotFound(format!("Update {} not found", path.update_id)).into()) + None => Err(ResponseError::NotFound(format!("Update {} not found", path.update_id)).into()), } } @@ -333,14 +407,18 @@ pub async fn get_all_updates_status( data: web::Data, path: web::Path, ) -> aweb::Result>> { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.update_read_txn() + let reader = data + .db + .update_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let response = index.all_updates_status(&reader) + let response = index + .all_updates_status(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(web::Json(response)) diff --git a/meilisearch-http/src/routes/key.rs b/meilisearch-http/src/routes/key.rs index d807840b7..76ab3a116 100644 --- a/meilisearch-http/src/routes/key.rs +++ b/meilisearch-http/src/routes/key.rs @@ -1,8 +1,7 @@ use crate::Data; -use actix_web::{web, get}; +use actix_web::{get, web}; use serde::Serialize; - #[derive(Default, Serialize)] pub struct KeysResponse { private: Option, @@ -10,11 +9,9 @@ pub struct KeysResponse { } #[get("/keys")] -pub async fn list( - data: web::Data, -) -> web::Json { +pub async fn list(data: web::Data) -> web::Json { let api_keys = data.api_keys.clone(); - web::Json(KeysResponse{ + web::Json(KeysResponse { private: api_keys.private, public: api_keys.public, }) diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index c8401e905..66cabd52d 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -1,7 +1,7 @@ use actix_web::{get, HttpResponse}; -use serde::{Serialize, Deserialize}; use log::error; use meilisearch_core::ProcessedUpdateResult; +use serde::{Deserialize, Serialize}; use crate::Data; @@ -10,14 +10,14 @@ pub mod health; pub mod index; pub mod key; pub mod search; -pub mod stats; pub mod setting; +pub mod stats; pub mod stop_words; pub mod synonym; #[derive(Default, Deserialize)] pub struct IndexParam { - index_uid: String + index_uid: String, } #[derive(Default, Serialize)] @@ -28,9 +28,7 @@ pub struct IndexUpdateResponse { impl IndexUpdateResponse { pub fn with_id(update_id: u64) -> Self { - Self { - update_id, - } + Self { update_id } } } @@ -48,7 +46,6 @@ pub async fn load_css() -> HttpResponse { .body(include_str!("../../public/bulma.min.css").to_string()) } - pub fn index_update_callback(index_uid: &str, data: &Data, status: ProcessedUpdateResult) { if status.error.is_some() { return; @@ -82,7 +79,6 @@ pub fn index_update_callback(index_uid: &str, data: &Data, status: ProcessedUpda } } - // pub fn load_routes(app: &mut tide::Server) { // app.at("/").get(|_| async { // tide::Response::new(200) diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index afdc96fcb..cd88694ff 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -4,15 +4,15 @@ use std::time::Duration; use log::warn; use meilisearch_core::Index; +use actix_web as aweb; +use actix_web::{get, post, web}; use rayon::iter::{IntoParallelIterator, ParallelIterator}; use serde::{Deserialize, Serialize}; -use actix_web::{web, get, post}; -use actix_web as aweb; use crate::error::ResponseError; use crate::helpers::meilisearch::{Error, IndexSearchExt, SearchHit, SearchResult}; -use crate::Data; use crate::routes::IndexParam; +use crate::Data; #[derive(Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] @@ -35,18 +35,23 @@ pub async fn search_with_url_query( path: web::Path, params: web::Query, ) -> aweb::Result> { - - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let schema = index .main .schema(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))? - .ok_or(ResponseError::Internal("Impossible to retrieve the schema".to_string()))?; + .ok_or(ResponseError::Internal( + "Impossible to retrieve the schema".to_string(), + ))?; let mut search_builder = index.new_search(params.q.clone()); @@ -182,7 +187,6 @@ pub async fn search_multi_index( data: web::Data, body: web::Json, ) -> aweb::Result> { - let mut index_list = body.clone().indexes; for index in index_list.clone() { @@ -203,7 +207,6 @@ pub async fn search_multi_index( } } - let par_body = body.clone(); let responses_per_index: Vec<(String, SearchResult)> = index_list .into_par_iter() diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 603b13902..2883c3997 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -1,42 +1,58 @@ +use actix_web as aweb; +use actix_web::{delete, get, post, web, HttpResponse}; use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES}; use std::collections::{BTreeMap, BTreeSet, HashSet}; -use actix_web::{web, get, post, delete, HttpResponse}; -use actix_web as aweb; -use crate::error::{ResponseError}; +use crate::error::ResponseError; +use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::Data; -use crate::routes::{IndexUpdateResponse, IndexParam}; #[post("/indexes/{index_uid}/settings")] pub async fn update_all( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let stop_words_fst = index.main.stop_words_fst(&reader) + let stop_words_fst = index + .main + .stop_words_fst(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; - let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs() + let stop_words = stop_words_fst + .unwrap_or_default() + .stream() + .into_strs() .map_err(|err| ResponseError::Internal(err.to_string()))?; let stop_words: BTreeSet = stop_words.into_iter().collect(); - let synonyms_fst = index.main.synonyms_fst(&reader) + let synonyms_fst = index + .main + .synonyms_fst(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))? .unwrap_or_default(); - let synonyms_list = synonyms_fst.stream().into_strs() + let synonyms_list = synonyms_fst + .stream() + .into_strs() .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut synonyms = BTreeMap::new(); let index_synonyms = &index.synonyms; for synonym in synonyms_list { - let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes()) + let alternative_list = index_synonyms + .synonyms(&reader, synonym.as_bytes()) .map_err(|err| ResponseError::Internal(err.to_string()))?; if let Some(list) = alternative_list { - let list = list.stream().into_strs() + let list = list + .stream() + .into_strs() .map_err(|err| ResponseError::Internal(err.to_string()))?; synonyms.insert(synonym, list); } @@ -51,10 +67,14 @@ pub async fn update_all( .map(|r| r.to_string()) .collect(); - let distinct_attribute = index.main.distinct_attribute(&reader) + let distinct_attribute = index + .main + .distinct_attribute(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; - let schema = index.main.schema(&reader) + let schema = index + .main + .schema(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; let searchable_attributes = schema.clone().map(|s| { @@ -92,16 +112,24 @@ pub async fn get_all( path: web::Path, body: web::Json, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let settings = body.into_inner().into_update() + let settings = body + .into_inner() + .into_update() .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -112,9 +140,13 @@ pub async fn delete_all( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let settings = SettingsUpdate { @@ -128,9 +160,11 @@ pub async fn delete_all( accept_new_fields: UpdateState::Clear, }; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -141,10 +175,14 @@ pub async fn get_rules( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() - .map_err(|err| ResponseError::Internal(err.to_string()))?; + let reader = data + .db + .main_read_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; let ranking_rules = index .main @@ -164,7 +202,9 @@ pub async fn update_rules( path: web::Path, body: web::Json>>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = Settings { @@ -172,13 +212,18 @@ pub async fn update_rules( ..Settings::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let settings = settings.into_update() + let settings = settings + .into_update() .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -189,9 +234,13 @@ pub async fn delete_rules( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let settings = SettingsUpdate { @@ -199,10 +248,12 @@ pub async fn delete_rules( ..SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -213,11 +264,17 @@ pub async fn get_distinct( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let distinct_attribute = index.main.distinct_attribute(&reader) + let distinct_attribute = index + .main + .distinct_attribute(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Ok().json(distinct_attribute)) @@ -229,7 +286,9 @@ pub async fn update_distinct( path: web::Path, body: web::Json>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = Settings { @@ -237,13 +296,18 @@ pub async fn update_distinct( ..Settings::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let settings = settings.into_update() + let settings = settings + .into_update() .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -254,21 +318,27 @@ pub async fn delete_distinct( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let mut writer = data.db.update_write_txn() - .map_err(|err| ResponseError::Internal(err.to_string()))?; + let mut writer = data + .db + .update_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; let settings = SettingsUpdate { distinct_attribute: UpdateState::Clear, ..SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings) - .map_err(|err| ResponseError::Internal(err.to_string()))?; + let update_id = index + .settings_update(&mut writer, settings) + .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() - .map_err(|err| ResponseError::Internal(err.to_string()))?; + writer + .commit() + .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } @@ -278,11 +348,17 @@ pub async fn get_searchable( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let schema = index.main.schema(&reader) + let schema = index + .main + .schema(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; let searchable_attributes: Option> = schema.map(|s| s.indexed_name().iter().map(|i| (*i).to_string()).collect()); @@ -296,7 +372,9 @@ pub async fn update_searchable( path: web::Path, body: web::Json>>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = Settings { @@ -304,13 +382,18 @@ pub async fn update_searchable( ..Settings::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let settings = settings.into_update() + let settings = settings + .into_update() .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -321,7 +404,9 @@ pub async fn delete_searchable( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { @@ -329,11 +414,15 @@ pub async fn delete_searchable( ..SettingsUpdate::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -344,12 +433,18 @@ pub async fn get_displayed( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let schema = index.main.schema(&reader) + let schema = index + .main + .schema(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; let displayed_attributes: Option> = schema.map(|s| { @@ -368,7 +463,9 @@ pub async fn update_displayed( path: web::Path, body: web::Json>>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = Settings { @@ -376,13 +473,18 @@ pub async fn update_displayed( ..Settings::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let settings = settings.into_update() + let settings = settings + .into_update() .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -393,7 +495,9 @@ pub async fn delete_displayed( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { @@ -401,12 +505,16 @@ pub async fn delete_displayed( ..SettingsUpdate::default() }; - let mut writer = data.db.update_write_txn() - .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) - .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() - .map_err(|err| ResponseError::Internal(err.to_string()))?; + let mut writer = data + .db + .update_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let update_id = index + .settings_update(&mut writer, settings) + .map_err(|err| ResponseError::Internal(err.to_string()))?; + writer + .commit() + .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } @@ -416,12 +524,18 @@ pub async fn get_accept_new_fields( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let schema = index.main.schema(&reader) + let schema = index + .main + .schema(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; let accept_new_fields = schema.map(|s| s.accept_new_fields()); @@ -435,7 +549,9 @@ pub async fn update_accept_new_fields( path: web::Path, body: web::Json>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = Settings { @@ -443,13 +559,18 @@ pub async fn update_accept_new_fields( ..Settings::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let settings = settings.into_update() + let settings = settings + .into_update() .map_err(|e| ResponseError::BadRequest(e.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) diff --git a/meilisearch-http/src/routes/stats.rs b/meilisearch-http/src/routes/stats.rs index b2261a7dc..ee804457e 100644 --- a/meilisearch-http/src/routes/stats.rs +++ b/meilisearch-http/src/routes/stats.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use actix_web as aweb; -use actix_web::{web, get}; +use actix_web::{get, web}; use chrono::{DateTime, Utc}; use log::error; use pretty_bytes::converter::convert; @@ -9,9 +9,9 @@ use serde::Serialize; use sysinfo::{NetworkExt, ProcessExt, ProcessorExt, System, SystemExt}; use walkdir::WalkDir; -use crate::Data; use crate::error::ResponseError; use crate::routes::IndexParam; +use crate::Data; #[derive(Serialize)] #[serde(rename_all = "camelCase")] @@ -26,20 +26,30 @@ pub async fn index_stats( data: web::Data, path: web::Path, ) -> aweb::Result> { - let index = data.db.open_index(path.index_uid.clone()) + let index = data + .db + .open_index(path.index_uid.clone()) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let number_of_documents = index.main.number_of_documents(&reader) + let number_of_documents = index + .main + .number_of_documents(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; - let fields_frequency = index.main.fields_frequency(&reader) + let fields_frequency = index + .main + .fields_frequency(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))? .unwrap_or_default(); - let update_reader = data.db.update_read_txn() + let update_reader = data + .db + .update_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let is_indexing = data @@ -63,15 +73,16 @@ pub struct StatsResult { } #[get("/stats")] -pub async fn get_stats( - data: web::Data, -) -> aweb::Result> { - +pub async fn get_stats(data: web::Data) -> aweb::Result> { let mut index_list = HashMap::new(); - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_reader = data.db.update_read_txn() + let update_reader = data + .db + .update_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; let indexes_set = data.db.indexes_uids(); @@ -79,10 +90,14 @@ pub async fn get_stats( let index = data.db.open_index(&index_uid); match index { Some(index) => { - let number_of_documents = index.main.number_of_documents(&reader) + let number_of_documents = index + .main + .number_of_documents(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; - let fields_frequency = index.main.fields_frequency(&reader) + let fields_frequency = index + .main + .fields_frequency(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))? .unwrap_or_default(); @@ -112,7 +127,8 @@ pub async fn get_stats( .filter(|metadata| metadata.is_file()) .fold(0, |acc, m| acc + m.len()); - let last_update = data.last_update(&reader) + let last_update = data + .last_update(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(web::Json(StatsResult { @@ -200,9 +216,7 @@ impl SysInfo { } #[get("/sys-info")] -pub async fn get_sys_info( - data: web::Data, -) -> web::Json { +pub async fn get_sys_info(data: web::Data) -> web::Json { let mut sys = System::new(); let mut info = SysInfo::new(); @@ -216,11 +230,13 @@ pub async fn get_sys_info( info.global.used_memory = sys.get_used_memory(); info.global.total_swap = sys.get_total_swap(); info.global.used_swap = sys.get_used_swap(); - info.global.input_data = sys.get_networks() + info.global.input_data = sys + .get_networks() .into_iter() .map(|(_, n)| n.get_received()) .sum::(); - info.global.output_data = sys.get_networks() + info.global.output_data = sys + .get_networks() .into_iter() .map(|(_, n)| n.get_transmitted()) .sum::(); @@ -294,11 +310,8 @@ impl SysInfoPretty { } } - #[get("/sys-info/pretty")] -pub async fn get_sys_info_pretty( - data: web::Data, -) -> web::Json { +pub async fn get_sys_info_pretty(data: web::Data) -> web::Json { let mut sys = System::new(); let mut info = SysInfoPretty::new(); @@ -316,8 +329,18 @@ pub async fn get_sys_info_pretty( info.global.used_memory = convert(sys.get_used_memory() as f64 * 1024.0); info.global.total_swap = convert(sys.get_total_swap() as f64 * 1024.0); info.global.used_swap = convert(sys.get_used_swap() as f64 * 1024.0); - info.global.input_data = convert(sys.get_networks().into_iter().map(|(_, n)| n.get_received()).sum::() as f64); - info.global.output_data = convert(sys.get_networks().into_iter().map(|(_, n)| n.get_transmitted()).sum::() as f64); + info.global.input_data = convert( + sys.get_networks() + .into_iter() + .map(|(_, n)| n.get_received()) + .sum::() as f64, + ); + info.global.output_data = convert( + sys.get_networks() + .into_iter() + .map(|(_, n)| n.get_transmitted()) + .sum::() as f64, + ); if let Some(process) = sys.get_process(data.server_pid) { info.process.memory = convert(process.memory() as f64 * 1024.0); diff --git a/meilisearch-http/src/routes/stop_words.rs b/meilisearch-http/src/routes/stop_words.rs index 2234497bf..905f90150 100644 --- a/meilisearch-http/src/routes/stop_words.rs +++ b/meilisearch-http/src/routes/stop_words.rs @@ -1,25 +1,31 @@ use std::collections::BTreeSet; -use meilisearch_core::settings::{SettingsUpdate, UpdateState}; -use actix_web::{web, get, post, delete, HttpResponse}; use actix_web as aweb; +use actix_web::{delete, get, post, web, HttpResponse}; +use meilisearch_core::settings::{SettingsUpdate, UpdateState}; -use crate::error::{ResponseError}; +use crate::error::ResponseError; +use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::Data; -use crate::routes::{IndexUpdateResponse, IndexParam}; #[get("/indexes/{index_uid}/settings/stop-words")] -pub async fn get( - data: web::Data, - path: web::Path, -) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) +pub async fn get(data: web::Data, path: web::Path) -> aweb::Result { + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let stop_words_fst = index.main.stop_words_fst(&reader) + let stop_words_fst = index + .main + .stop_words_fst(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))?; - let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs() + let stop_words = stop_words_fst + .unwrap_or_default() + .stream() + .into_strs() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Ok().json(stop_words)) @@ -31,7 +37,9 @@ pub async fn update( path: web::Path, body: web::Json>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { @@ -39,11 +47,15 @@ pub async fn update( ..SettingsUpdate::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -54,7 +66,9 @@ pub async fn delete( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { @@ -62,11 +76,15 @@ pub async fn delete( ..SettingsUpdate::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) diff --git a/meilisearch-http/src/routes/synonym.rs b/meilisearch-http/src/routes/synonym.rs index 587582ee9..7892988a6 100644 --- a/meilisearch-http/src/routes/synonym.rs +++ b/meilisearch-http/src/routes/synonym.rs @@ -1,29 +1,34 @@ use std::collections::BTreeMap; +use actix_web as aweb; +use actix_web::{delete, get, post, web, HttpResponse}; use indexmap::IndexMap; use meilisearch_core::settings::{SettingsUpdate, UpdateState}; -use actix_web::{web, get, post, delete, HttpResponse}; -use actix_web as aweb; -use crate::error::{ResponseError}; +use crate::error::ResponseError; +use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::Data; -use crate::routes::{IndexUpdateResponse, IndexParam}; #[get("/indexes/{index_uid}/settings/synonyms")] -pub async fn get( - data: web::Data, - path: web::Path, -) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) +pub async fn get(data: web::Data, path: web::Path) -> aweb::Result { + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let reader = data.db.main_read_txn() + let reader = data + .db + .main_read_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let synonyms_fst = index.main.synonyms_fst(&reader) + let synonyms_fst = index + .main + .synonyms_fst(&reader) .map_err(|err| ResponseError::Internal(err.to_string()))? .unwrap_or_default(); - let synonyms_list = synonyms_fst.stream().into_strs() + let synonyms_list = synonyms_fst + .stream() + .into_strs() .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut synonyms = IndexMap::new(); @@ -31,11 +36,14 @@ pub async fn get( let index_synonyms = &index.synonyms; for synonym in synonyms_list { - let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes()) + let alternative_list = index_synonyms + .synonyms(&reader, synonym.as_bytes()) .map_err(|err| ResponseError::Internal(err.to_string()))?; if let Some(list) = alternative_list { - let list = list.stream().into_strs() + let list = list + .stream() + .into_strs() .map_err(|err| ResponseError::Internal(err.to_string()))?; synonyms.insert(synonym, list); } @@ -50,7 +58,9 @@ pub async fn update( path: web::Path, body: web::Json>>, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { @@ -58,11 +68,15 @@ pub async fn update( ..SettingsUpdate::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) @@ -73,7 +87,9 @@ pub async fn delete( data: web::Data, path: web::Path, ) -> aweb::Result { - let index = data.db.open_index(&path.index_uid) + let index = data + .db + .open_index(&path.index_uid) .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { @@ -81,12 +97,16 @@ pub async fn delete( ..SettingsUpdate::default() }; - let mut writer = data.db.update_write_txn() + let mut writer = data + .db + .update_write_txn() .map_err(|err| ResponseError::Internal(err.to_string()))?; - let update_id = index.settings_update(&mut writer, settings) + let update_id = index + .settings_update(&mut writer, settings) .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit() + writer + .commit() .map_err(|err| ResponseError::Internal(err.to_string()))?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) diff --git a/meilisearch-http/tests/documents_add.rs b/meilisearch-http/tests/documents_add.rs index a223c4757..ebb66e7a4 100644 --- a/meilisearch-http/tests/documents_add.rs +++ b/meilisearch-http/tests/documents_add.rs @@ -91,11 +91,11 @@ fn check_add_documents_with_nested_null() { // 2 - Add a document that contains a null in a nested object - let body = json!([{ - "id": 0, - "foo": { + let body = json!([{ + "id": 0, + "foo": { "bar": null - } + } }]); let url = "/indexes/tasks/documents"; @@ -126,9 +126,9 @@ fn check_add_documents_with_nested_sequence() { // 2 - Add a document that contains a seq in a nested object - let body = json!([{ - "id": 0, - "foo": { + let body = json!([{ + "id": 0, + "foo": { "bar": [123,456], "fez": [{ "id": 255, diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index e0b44b9b5..f825e336b 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -660,7 +660,7 @@ fn check_add_documents_without_primary_key() { } #[test] -fn check_first_update_should_bring_up_processed_status_after_first_docs_addition(){ +fn check_first_update_should_bring_up_processed_status_after_first_docs_addition() { let mut server = common::Server::with_uid("movies"); let body = json!({ @@ -678,11 +678,11 @@ fn check_first_update_should_bring_up_processed_status_after_first_docs_addition // 2. Index the documents from movies.json, present inside of assets directory server.add_or_replace_multiple_documents(body); - + // 3. Fetch the status of the indexing done above. let (response, status_code) = server.get_all_updates_status(); - + // 4. Verify the fetch is successful and indexing status is 'processed' assert_eq!(status_code, 200); - assert_eq!(response[0]["status"], "processed"); + assert_eq!(response[0]["status"], "processed"); } diff --git a/meilisearch-http/tests/settings_accept_new_fields.rs b/meilisearch-http/tests/settings_accept_new_fields.rs index 807aa1cd2..7293badd4 100644 --- a/meilisearch-http/tests/settings_accept_new_fields.rs +++ b/meilisearch-http/tests/settings_accept_new_fields.rs @@ -289,10 +289,9 @@ fn index_new_fields_false_then_true() { assert_json_eq!(response, expected); } - // Fix issue https://github.com/meilisearch/MeiliSearch/issues/518 #[test] -fn accept_new_fields_does_not_take_into_account_the_primary_key () { +fn accept_new_fields_does_not_take_into_account_the_primary_key() { let mut server = common::Server::with_uid("movies"); // 1 - Create an index with no primary-key diff --git a/meilisearch-http/tests/settings_ranking_rules.rs b/meilisearch-http/tests/settings_ranking_rules.rs index f203216ff..9c5a82f59 100644 --- a/meilisearch-http/tests/settings_ranking_rules.rs +++ b/meilisearch-http/tests/settings_ranking_rules.rs @@ -148,10 +148,7 @@ fn write_custom_ranking_and_index_documents() { // 1 - Add ranking rules with one custom ranking on a string - let body = json!([ - "asc(title)", - "typo" - ]); + let body = json!(["asc(title)", "typo"]); server.update_ranking_rules(body); @@ -184,5 +181,4 @@ fn write_custom_ranking_and_index_documents() { assert_eq!(status_code, 200); assert_json_eq!(response, expected, ordered: false); - }