From 05344043b238865f9d786d63ae8a10139f34e4ae Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 27 May 2020 12:04:35 +0200 Subject: [PATCH] style fixes --- meilisearch-core/src/database.rs | 15 ++++--- meilisearch-core/src/lib.rs | 8 +--- meilisearch-core/src/store/main.rs | 6 +-- meilisearch-core/src/store/synonyms.rs | 3 +- meilisearch-error/Cargo.toml | 4 +- meilisearch-error/src/lib.rs | 52 +++++++++++------------ meilisearch-http/src/data.rs | 1 - meilisearch-http/src/error.rs | 4 +- meilisearch-http/src/routes/document.rs | 4 +- meilisearch-http/src/routes/health.rs | 2 + meilisearch-http/src/routes/setting.rs | 4 +- meilisearch-http/src/routes/stop_words.rs | 2 +- meilisearch-http/src/routes/synonym.rs | 2 +- 13 files changed, 51 insertions(+), 56 deletions(-) diff --git a/meilisearch-core/src/database.rs b/meilisearch-core/src/database.rs index 22c7c7d56..0b0cd3149 100644 --- a/meilisearch-core/src/database.rs +++ b/meilisearch-core/src/database.rs @@ -11,13 +11,20 @@ use heed::CompactionOption; use log::{debug, error}; use meilisearch_schema::Schema; -use crate::{store, update, Index, MResult, Error, UpdateReader, UpdateWriter, MainReader, MainWriter}; +use crate::{store, update, Index, MResult, Error}; pub type BoxUpdateFn = Box; + type ArcSwapFn = arc_swap::ArcSwapOption; type SerdeDatetime = SerdeBincode>; +pub type MainWriter<'a> = heed::RwTxn<'a, MainT>; +pub type MainReader = heed::RoTxn; + +pub type UpdateWriter<'a> = heed::RwTxn<'a, UpdateT>; +pub type UpdateReader = heed::RoTxn; + const UNHEALTHY_KEY: &str = "_is_unhealthy"; const LAST_UPDATE_KEY: &str = "last-update"; @@ -427,8 +434,7 @@ impl Database { pub fn last_update(&self, reader: &heed::RoTxn) -> MResult>> { match self.common_store() - .get::<_, Str, SerdeDatetime>(reader, LAST_UPDATE_KEY)? - { + .get::<_, Str, SerdeDatetime>(reader, LAST_UPDATE_KEY)? { Some(datetime) => Ok(Some(datetime)), None => Ok(None), } @@ -492,7 +498,7 @@ impl Database { index .main - .put_fields_frequency(writer, &frequency) + .put_fields_distribution(writer, &frequency) } } @@ -1223,4 +1229,3 @@ mod tests { assert_matches!(iter.next(), None); } } - diff --git a/meilisearch-core/src/lib.rs b/meilisearch-core/src/lib.rs index d837bae62..e468a794e 100644 --- a/meilisearch-core/src/lib.rs +++ b/meilisearch-core/src/lib.rs @@ -26,7 +26,7 @@ pub mod settings; pub mod store; pub mod update; -pub use self::database::{BoxUpdateFn, Database, DatabaseOptions, MainT, UpdateT}; +pub use self::database::{BoxUpdateFn, Database, DatabaseOptions, MainT, UpdateT, MainWriter, MainReader, UpdateWriter, UpdateReader}; pub use self::error::{Error, HeedError, FstError, MResult, pest_error, FacetError}; pub use self::filters::Filter; pub use self::number::{Number, ParseNumberError}; @@ -52,12 +52,6 @@ use crate::reordered_attrs::ReorderedAttrs; type FstSetCow<'a> = fst::Set>; type FstMapCow<'a> = fst::Map>; -pub type MainWriter<'a> = heed::RwTxn<'a, MainT>; -pub type MainReader = heed::RoTxn; - -pub type UpdateWriter<'a> = heed::RwTxn<'a, UpdateT>; -pub type UpdateReader = heed::RoTxn; - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Document { pub id: DocumentId, diff --git a/meilisearch-core/src/store/main.rs b/meilisearch-core/src/store/main.rs index 21b506307..f1ef6fc5e 100644 --- a/meilisearch-core/src/store/main.rs +++ b/meilisearch-core/src/store/main.rs @@ -197,7 +197,7 @@ impl Main { } } - pub fn synonyms_list(self, reader: &heed::RoTxn) -> MResult> { + pub fn synonyms(self, reader: &heed::RoTxn) -> MResult> { let synonyms = self .synonyms_fst(&reader)? .stream() @@ -217,7 +217,7 @@ impl Main { } } - pub fn stop_words_list(self, reader: &heed::RoTxn) -> MResult> { + pub fn stop_words(self, reader: &heed::RoTxn) -> MResult> { let stop_word_list = self .stop_words_fst(reader)? .stream() @@ -249,7 +249,7 @@ impl Main { writer: &mut heed::RwTxn, fields_frequency: &FreqsMap, ) -> MResult<()> { - Ok(self.main.put::<_, Str, SerdeFreqsMap>(writer, FIELDS_FREQUENCY_KEY, fields_frequency)?) + Ok(self.main.put::<_, Str, SerdeFreqsMap>(writer, FIELDS_DISTRIBUTION_KEY, fields_frequency)?) } pub fn fields_distribution(&self, reader: &heed::RoTxn) -> MResult> { diff --git a/meilisearch-core/src/store/synonyms.rs b/meilisearch-core/src/store/synonyms.rs index 699667fcd..bf7472f96 100644 --- a/meilisearch-core/src/store/synonyms.rs +++ b/meilisearch-core/src/store/synonyms.rs @@ -27,7 +27,7 @@ impl Synonyms { self.synonyms.clear(writer) } - pub fn synonyms_fst<'txn>(self, reader: &'txn heed::RoTxn, word: &[u8]) -> ZResult> { + pub(crate) fn synonyms_fst<'txn>(self, reader: &'txn heed::RoTxn, word: &[u8]) -> ZResult> { match self.synonyms.get(reader, word)? { Some(bytes) => Ok(fst::Set::new(bytes).unwrap().map_data(Cow::Borrowed).unwrap()), None => Ok(fst::Set::default().map_data(Cow::Owned).unwrap()), @@ -42,4 +42,3 @@ impl Synonyms { Ok(synonyms) } } - diff --git a/meilisearch-error/Cargo.toml b/meilisearch-error/Cargo.toml index 1be97bdb8..ad77978b1 100644 --- a/meilisearch-error/Cargo.toml +++ b/meilisearch-error/Cargo.toml @@ -1,10 +1,8 @@ [package] name = "meilisearch-error" version = "0.10.1" -authors = ["mpostma "] +authors = ["marin "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] actix-http = "1.0.1" diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index 63f94f655..66838ed02 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -57,12 +57,11 @@ pub enum Code { MissingPrimaryKey, PrimaryKeyAlreadyPresent, - // invalid documents FIXME make one error code? - MissingDocumentId, MaxFieldsLimitExceeded, + MissingDocumentId, - Filter, Facet, + Filter, BadParameter, BadRequest, @@ -73,9 +72,9 @@ pub enum Code { MissingAuthorizationHeader, MissingHeader, NotFound, + PayloadTooLarge, RetrieveDocument, SearchDocuments, - PayloadTooLarge, UnsupportedMediaType, Other, } @@ -90,8 +89,7 @@ impl Code { // index related errors CreateIndex => ErrCode::invalid("create_index", StatusCode::BAD_REQUEST), IndexAlreadyExists => ErrCode::invalid("existing_index", StatusCode::BAD_REQUEST), - IndexNotFound => ErrCode::invalid("index_not_found", StatusCode::NOT_FOUND), - InvalidIndexUid => ErrCode::invalid("invalid_index_uid", StatusCode::BAD_REQUEST), + IndexNotFound => ErrCode::invalid("index_not_found", StatusCode::NOT_FOUND), InvalidIndexUid => ErrCode::invalid("invalid_index_uid", StatusCode::BAD_REQUEST), OpenIndex => ErrCode::internal("open_index", StatusCode::INTERNAL_SERVER_ERROR), // invalid state error @@ -101,24 +99,24 @@ impl Code { PrimaryKeyAlreadyPresent => ErrCode::internal("primary_key_already_present", StatusCode::INTERNAL_SERVER_ERROR), // invalid document - MissingDocumentId => ErrCode::invalid("MissingDocumentId", StatusCode::BAD_REQUEST), MaxFieldsLimitExceeded => ErrCode::invalid("max_field_limit_exceeded", StatusCode::BAD_REQUEST), + MissingDocumentId => ErrCode::invalid("missing_document_id", StatusCode::BAD_REQUEST), - Filter => ErrCode::invalid("fitler", StatusCode::BAD_REQUEST), - Facet => ErrCode::invalid("facet", StatusCode::BAD_REQUEST), + Facet => ErrCode::invalid("invalid_facet", StatusCode::BAD_REQUEST), + Filter => ErrCode::invalid("invalid_filter", StatusCode::BAD_REQUEST), BadParameter => ErrCode::invalid("bad_parameter", StatusCode::BAD_REQUEST), BadRequest => ErrCode::invalid("bad_request", StatusCode::BAD_REQUEST), - RetrieveDocument => ErrCode::invalid("retrieve_document", StatusCode::BAD_REQUEST), - SearchDocuments => ErrCode::invalid("search_document", StatusCode::BAD_REQUEST), - DocumentNotFound => ErrCode::invalid("document_not_found", StatusCode::NOT_FOUND), - NotFound => ErrCode::invalid("not_found", StatusCode::NOT_FOUND), - InvalidToken => ErrCode::authentication("invalid_token", StatusCode::UNAUTHORIZED), - MissingHeader => ErrCode::authentication("missing_header", StatusCode::UNAUTHORIZED), - MissingAuthorizationHeader => ErrCode::authentication("missing_authorization_header", StatusCode::FORBIDDEN), + DocumentNotFound => ErrCode::internal("document_not_found", StatusCode::NOT_FOUND), Internal => ErrCode::internal("internal", StatusCode::INTERNAL_SERVER_ERROR), - Maintenance => ErrCode::invalid("maintenance", StatusCode::SERVICE_UNAVAILABLE), + InvalidToken => ErrCode::authentication("invalid_token", StatusCode::UNAUTHORIZED), + Maintenance => ErrCode::internal("maintenance", StatusCode::SERVICE_UNAVAILABLE), + MissingAuthorizationHeader => ErrCode::authentication("missing_authorization_header", StatusCode::FORBIDDEN), + MissingHeader => ErrCode::authentication("missing_header", StatusCode::UNAUTHORIZED), + NotFound => ErrCode::invalid("not_found", StatusCode::NOT_FOUND), PayloadTooLarge => ErrCode::invalid("payload_too_large", StatusCode::PAYLOAD_TOO_LARGE), + RetrieveDocument => ErrCode::internal("retrieve_document", StatusCode::BAD_REQUEST), + SearchDocuments => ErrCode::internal("search_error", StatusCode::BAD_REQUEST), UnsupportedMediaType => ErrCode::invalid("unsupported_media_type", StatusCode::UNSUPPORTED_MEDIA_TYPE), _ => ErrCode::invalid("other", StatusCode::BAD_REQUEST), } @@ -131,12 +129,12 @@ impl Code { /// return error name, used as error code fn name(&self) -> String { - self.err_code().err_name.to_string() + self.err_code().error_name.to_string() } /// return the error type fn r#type(&self) -> String { - self.err_code().err_type.to_string() + self.err_code().error_type.to_string() } /// return the doc url ascociated with the error @@ -148,8 +146,8 @@ impl Code { /// Internal structure providing a convenient way to create error codes struct ErrCode { status_code: StatusCode, - err_type: ErrorType, - err_name: &'static str, + error_type: ErrorType, + error_name: &'static str, } impl ErrCode { @@ -157,24 +155,24 @@ impl ErrCode { fn authentication(err_name: &'static str, status_code: StatusCode) -> ErrCode { ErrCode { status_code, - err_name, - err_type: ErrorType::Authentication, + error_name: err_name, + error_type: ErrorType::Authentication, } } fn internal(err_name: &'static str, status_code: StatusCode) -> ErrCode { ErrCode { status_code, - err_name, - err_type: ErrorType::InternalError, + error_name: err_name, + error_type: ErrorType::InternalError, } } fn invalid(err_name: &'static str, status_code: StatusCode) -> ErrCode { ErrCode { status_code, - err_name, - err_type: ErrorType::InvalidRequest, + error_name: err_name, + error_type: ErrorType::InvalidRequest, } } } diff --git a/meilisearch-http/src/data.rs b/meilisearch-http/src/data.rs index 15c2ff1af..3692b0b69 100644 --- a/meilisearch-http/src/data.rs +++ b/meilisearch-http/src/data.rs @@ -96,4 +96,3 @@ impl Data { data } } - diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index 606b54b65..3f57e3a92 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -1,11 +1,11 @@ -use std::fmt; use std::error; +use std::fmt; use actix_http::ResponseBuilder; use actix_web as aweb; +use actix_web::error::JsonPayloadError; use actix_web::http::StatusCode; use serde_json::json; -use actix_web::error::JsonPayloadError; use meilisearch_error::{ErrorCode, Code}; diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index c97bc7b28..448d04562 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -7,10 +7,10 @@ use meilisearch_core::update; use serde::Deserialize; use serde_json::Value; +use crate::Data; use crate::error::{Error, ResponseError}; use crate::helpers::Authentication; use crate::routes::{IndexParam, IndexUpdateResponse}; -use crate::Data; type Document = IndexMap; @@ -192,7 +192,7 @@ async fn update_multiple_documents( let update_id = data .db - .update_write::<_, _, ResponseError>(move |writer| { + .update_write::<_, _, ResponseError>(|writer| { let update_id = document_addition.finalize(writer)?; Ok(update_id) })?; diff --git a/meilisearch-http/src/routes/health.rs b/meilisearch-http/src/routes/health.rs index d0114d8c5..c6f33b6fb 100644 --- a/meilisearch-http/src/routes/health.rs +++ b/meilisearch-http/src/routes/health.rs @@ -24,6 +24,7 @@ async fn set_healthy(data: web::Data) -> Result) -> Result = index .main - .stop_words_list(&reader)? + .stop_words(&reader)? .into_iter() .collect(); - let synonyms_list = index.main.synonyms_list(&reader)?; + let synonyms_list = index.main.synonyms(&reader)?; let mut synonyms = BTreeMap::new(); let index_synonyms = &index.synonyms; diff --git a/meilisearch-http/src/routes/stop_words.rs b/meilisearch-http/src/routes/stop_words.rs index 092ca9b2a..0c22cbd13 100644 --- a/meilisearch-http/src/routes/stop_words.rs +++ b/meilisearch-http/src/routes/stop_words.rs @@ -25,7 +25,7 @@ async fn get( .open_index(&path.index_uid) .ok_or(Error::index_not_found(&path.index_uid))?; let reader = data.db.main_read_txn()?; - let stop_words = index.main.stop_words_list(&reader)?; + let stop_words = index.main.stop_words(&reader)?; Ok(HttpResponse::Ok().json(stop_words)) } diff --git a/meilisearch-http/src/routes/synonym.rs b/meilisearch-http/src/routes/synonym.rs index 0b1867126..169230adb 100644 --- a/meilisearch-http/src/routes/synonym.rs +++ b/meilisearch-http/src/routes/synonym.rs @@ -29,7 +29,7 @@ async fn get( let reader = data.db.main_read_txn()?; - let synonyms_list = index.main.synonyms_list(&reader)?; + let synonyms_list = index.main.synonyms(&reader)?; let mut synonyms = IndexMap::new(); let index_synonyms = &index.synonyms;