style fixes

This commit is contained in:
mpostma 2020-05-27 12:04:35 +02:00
parent d9e2e1a177
commit 05344043b2
13 changed files with 51 additions and 56 deletions

View File

@ -11,13 +11,20 @@ use heed::CompactionOption;
use log::{debug, error}; use log::{debug, error};
use meilisearch_schema::Schema; 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<dyn Fn(&str, update::ProcessedUpdateResult) + Send + Sync + 'static>; pub type BoxUpdateFn = Box<dyn Fn(&str, update::ProcessedUpdateResult) + Send + Sync + 'static>;
type ArcSwapFn = arc_swap::ArcSwapOption<BoxUpdateFn>; type ArcSwapFn = arc_swap::ArcSwapOption<BoxUpdateFn>;
type SerdeDatetime = SerdeBincode<DateTime<Utc>>; type SerdeDatetime = SerdeBincode<DateTime<Utc>>;
pub type MainWriter<'a> = heed::RwTxn<'a, MainT>;
pub type MainReader = heed::RoTxn<MainT>;
pub type UpdateWriter<'a> = heed::RwTxn<'a, UpdateT>;
pub type UpdateReader = heed::RoTxn<UpdateT>;
const UNHEALTHY_KEY: &str = "_is_unhealthy"; const UNHEALTHY_KEY: &str = "_is_unhealthy";
const LAST_UPDATE_KEY: &str = "last-update"; const LAST_UPDATE_KEY: &str = "last-update";
@ -427,8 +434,7 @@ impl Database {
pub fn last_update(&self, reader: &heed::RoTxn<MainT>) -> MResult<Option<DateTime<Utc>>> { pub fn last_update(&self, reader: &heed::RoTxn<MainT>) -> MResult<Option<DateTime<Utc>>> {
match self.common_store() match self.common_store()
.get::<_, Str, SerdeDatetime>(reader, LAST_UPDATE_KEY)? .get::<_, Str, SerdeDatetime>(reader, LAST_UPDATE_KEY)? {
{
Some(datetime) => Ok(Some(datetime)), Some(datetime) => Ok(Some(datetime)),
None => Ok(None), None => Ok(None),
} }
@ -492,7 +498,7 @@ impl Database {
index index
.main .main
.put_fields_frequency(writer, &frequency) .put_fields_distribution(writer, &frequency)
} }
} }
@ -1223,4 +1229,3 @@ mod tests {
assert_matches!(iter.next(), None); assert_matches!(iter.next(), None);
} }
} }

View File

@ -26,7 +26,7 @@ pub mod settings;
pub mod store; pub mod store;
pub mod update; 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::error::{Error, HeedError, FstError, MResult, pest_error, FacetError};
pub use self::filters::Filter; pub use self::filters::Filter;
pub use self::number::{Number, ParseNumberError}; pub use self::number::{Number, ParseNumberError};
@ -52,12 +52,6 @@ use crate::reordered_attrs::ReorderedAttrs;
type FstSetCow<'a> = fst::Set<Cow<'a, [u8]>>; type FstSetCow<'a> = fst::Set<Cow<'a, [u8]>>;
type FstMapCow<'a> = fst::Map<Cow<'a, [u8]>>; type FstMapCow<'a> = fst::Map<Cow<'a, [u8]>>;
pub type MainWriter<'a> = heed::RwTxn<'a, MainT>;
pub type MainReader = heed::RoTxn<MainT>;
pub type UpdateWriter<'a> = heed::RwTxn<'a, UpdateT>;
pub type UpdateReader = heed::RoTxn<UpdateT>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Document { pub struct Document {
pub id: DocumentId, pub id: DocumentId,

View File

@ -197,7 +197,7 @@ impl Main {
} }
} }
pub fn synonyms_list(self, reader: &heed::RoTxn<MainT>) -> MResult<Vec<String>> { pub fn synonyms(self, reader: &heed::RoTxn<MainT>) -> MResult<Vec<String>> {
let synonyms = self let synonyms = self
.synonyms_fst(&reader)? .synonyms_fst(&reader)?
.stream() .stream()
@ -217,7 +217,7 @@ impl Main {
} }
} }
pub fn stop_words_list(self, reader: &heed::RoTxn<MainT>) -> MResult<Vec<String>> { pub fn stop_words(self, reader: &heed::RoTxn<MainT>) -> MResult<Vec<String>> {
let stop_word_list = self let stop_word_list = self
.stop_words_fst(reader)? .stop_words_fst(reader)?
.stream() .stream()
@ -249,7 +249,7 @@ impl Main {
writer: &mut heed::RwTxn<MainT>, writer: &mut heed::RwTxn<MainT>,
fields_frequency: &FreqsMap, fields_frequency: &FreqsMap,
) -> MResult<()> { ) -> 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<MainT>) -> MResult<Option<FreqsMap>> { pub fn fields_distribution(&self, reader: &heed::RoTxn<MainT>) -> MResult<Option<FreqsMap>> {

View File

@ -27,7 +27,7 @@ impl Synonyms {
self.synonyms.clear(writer) self.synonyms.clear(writer)
} }
pub fn synonyms_fst<'txn>(self, reader: &'txn heed::RoTxn<MainT>, word: &[u8]) -> ZResult<FstSetCow<'txn>> { pub(crate) fn synonyms_fst<'txn>(self, reader: &'txn heed::RoTxn<MainT>, word: &[u8]) -> ZResult<FstSetCow<'txn>> {
match self.synonyms.get(reader, word)? { match self.synonyms.get(reader, word)? {
Some(bytes) => Ok(fst::Set::new(bytes).unwrap().map_data(Cow::Borrowed).unwrap()), Some(bytes) => Ok(fst::Set::new(bytes).unwrap().map_data(Cow::Borrowed).unwrap()),
None => Ok(fst::Set::default().map_data(Cow::Owned).unwrap()), None => Ok(fst::Set::default().map_data(Cow::Owned).unwrap()),
@ -42,4 +42,3 @@ impl Synonyms {
Ok(synonyms) Ok(synonyms)
} }
} }

View File

@ -1,10 +1,8 @@
[package] [package]
name = "meilisearch-error" name = "meilisearch-error"
version = "0.10.1" version = "0.10.1"
authors = ["mpostma <postma.marin@protonmail.com>"] authors = ["marin <postma.marin@protonmail.com>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-http = "1.0.1" actix-http = "1.0.1"

View File

@ -57,12 +57,11 @@ pub enum Code {
MissingPrimaryKey, MissingPrimaryKey,
PrimaryKeyAlreadyPresent, PrimaryKeyAlreadyPresent,
// invalid documents FIXME make one error code?
MissingDocumentId,
MaxFieldsLimitExceeded, MaxFieldsLimitExceeded,
MissingDocumentId,
Filter,
Facet, Facet,
Filter,
BadParameter, BadParameter,
BadRequest, BadRequest,
@ -73,9 +72,9 @@ pub enum Code {
MissingAuthorizationHeader, MissingAuthorizationHeader,
MissingHeader, MissingHeader,
NotFound, NotFound,
PayloadTooLarge,
RetrieveDocument, RetrieveDocument,
SearchDocuments, SearchDocuments,
PayloadTooLarge,
UnsupportedMediaType, UnsupportedMediaType,
Other, Other,
} }
@ -90,8 +89,7 @@ impl Code {
// index related errors // index related errors
CreateIndex => ErrCode::invalid("create_index", StatusCode::BAD_REQUEST), CreateIndex => ErrCode::invalid("create_index", StatusCode::BAD_REQUEST),
IndexAlreadyExists => ErrCode::invalid("existing_index", StatusCode::BAD_REQUEST), IndexAlreadyExists => ErrCode::invalid("existing_index", StatusCode::BAD_REQUEST),
IndexNotFound => ErrCode::invalid("index_not_found", StatusCode::NOT_FOUND), IndexNotFound => ErrCode::invalid("index_not_found", StatusCode::NOT_FOUND), InvalidIndexUid => ErrCode::invalid("invalid_index_uid", StatusCode::BAD_REQUEST),
InvalidIndexUid => ErrCode::invalid("invalid_index_uid", StatusCode::BAD_REQUEST),
OpenIndex => ErrCode::internal("open_index", StatusCode::INTERNAL_SERVER_ERROR), OpenIndex => ErrCode::internal("open_index", StatusCode::INTERNAL_SERVER_ERROR),
// invalid state error // invalid state error
@ -101,24 +99,24 @@ impl Code {
PrimaryKeyAlreadyPresent => ErrCode::internal("primary_key_already_present", StatusCode::INTERNAL_SERVER_ERROR), PrimaryKeyAlreadyPresent => ErrCode::internal("primary_key_already_present", StatusCode::INTERNAL_SERVER_ERROR),
// invalid document // invalid document
MissingDocumentId => ErrCode::invalid("MissingDocumentId", StatusCode::BAD_REQUEST),
MaxFieldsLimitExceeded => ErrCode::invalid("max_field_limit_exceeded", 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("invalid_facet", StatusCode::BAD_REQUEST),
Facet => ErrCode::invalid("facet", StatusCode::BAD_REQUEST), Filter => ErrCode::invalid("invalid_filter", StatusCode::BAD_REQUEST),
BadParameter => ErrCode::invalid("bad_parameter", StatusCode::BAD_REQUEST), BadParameter => ErrCode::invalid("bad_parameter", StatusCode::BAD_REQUEST),
BadRequest => ErrCode::invalid("bad_request", StatusCode::BAD_REQUEST), BadRequest => ErrCode::invalid("bad_request", StatusCode::BAD_REQUEST),
RetrieveDocument => ErrCode::invalid("retrieve_document", StatusCode::BAD_REQUEST), DocumentNotFound => ErrCode::internal("document_not_found", StatusCode::NOT_FOUND),
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),
Internal => ErrCode::internal("internal", StatusCode::INTERNAL_SERVER_ERROR), 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), 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), UnsupportedMediaType => ErrCode::invalid("unsupported_media_type", StatusCode::UNSUPPORTED_MEDIA_TYPE),
_ => ErrCode::invalid("other", StatusCode::BAD_REQUEST), _ => ErrCode::invalid("other", StatusCode::BAD_REQUEST),
} }
@ -131,12 +129,12 @@ impl Code {
/// return error name, used as error code /// return error name, used as error code
fn name(&self) -> String { fn name(&self) -> String {
self.err_code().err_name.to_string() self.err_code().error_name.to_string()
} }
/// return the error type /// return the error type
fn r#type(&self) -> String { 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 /// return the doc url ascociated with the error
@ -148,8 +146,8 @@ impl Code {
/// Internal structure providing a convenient way to create error codes /// Internal structure providing a convenient way to create error codes
struct ErrCode { struct ErrCode {
status_code: StatusCode, status_code: StatusCode,
err_type: ErrorType, error_type: ErrorType,
err_name: &'static str, error_name: &'static str,
} }
impl ErrCode { impl ErrCode {
@ -157,24 +155,24 @@ impl ErrCode {
fn authentication(err_name: &'static str, status_code: StatusCode) -> ErrCode { fn authentication(err_name: &'static str, status_code: StatusCode) -> ErrCode {
ErrCode { ErrCode {
status_code, status_code,
err_name, error_name: err_name,
err_type: ErrorType::Authentication, error_type: ErrorType::Authentication,
} }
} }
fn internal(err_name: &'static str, status_code: StatusCode) -> ErrCode { fn internal(err_name: &'static str, status_code: StatusCode) -> ErrCode {
ErrCode { ErrCode {
status_code, status_code,
err_name, error_name: err_name,
err_type: ErrorType::InternalError, error_type: ErrorType::InternalError,
} }
} }
fn invalid(err_name: &'static str, status_code: StatusCode) -> ErrCode { fn invalid(err_name: &'static str, status_code: StatusCode) -> ErrCode {
ErrCode { ErrCode {
status_code, status_code,
err_name, error_name: err_name,
err_type: ErrorType::InvalidRequest, error_type: ErrorType::InvalidRequest,
} }
} }
} }

View File

@ -96,4 +96,3 @@ impl Data {
data data
} }
} }

View File

@ -1,11 +1,11 @@
use std::fmt;
use std::error; use std::error;
use std::fmt;
use actix_http::ResponseBuilder; use actix_http::ResponseBuilder;
use actix_web as aweb; use actix_web as aweb;
use actix_web::error::JsonPayloadError;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use serde_json::json; use serde_json::json;
use actix_web::error::JsonPayloadError;
use meilisearch_error::{ErrorCode, Code}; use meilisearch_error::{ErrorCode, Code};

View File

@ -7,10 +7,10 @@ use meilisearch_core::update;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use crate::Data;
use crate::error::{Error, ResponseError}; use crate::error::{Error, ResponseError};
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::Data;
type Document = IndexMap<String, Value>; type Document = IndexMap<String, Value>;
@ -192,7 +192,7 @@ async fn update_multiple_documents(
let update_id = data let update_id = data
.db .db
.update_write::<_, _, ResponseError>(move |writer| { .update_write::<_, _, ResponseError>(|writer| {
let update_id = document_addition.finalize(writer)?; let update_id = document_addition.finalize(writer)?;
Ok(update_id) Ok(update_id)
})?; })?;

View File

@ -24,6 +24,7 @@ async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseErro
data.db.set_healthy(writer)?; data.db.set_healthy(writer)?;
Ok(()) Ok(())
})?; })?;
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }
@ -32,6 +33,7 @@ async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseEr
data.db.set_unhealthy(writer)?; data.db.set_unhealthy(writer)?;
Ok(()) Ok(())
})?; })?;
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }

View File

@ -65,11 +65,11 @@ async fn get_all(
let stop_words: BTreeSet<String> = index let stop_words: BTreeSet<String> = index
.main .main
.stop_words_list(&reader)? .stop_words(&reader)?
.into_iter() .into_iter()
.collect(); .collect();
let synonyms_list = index.main.synonyms_list(&reader)?; let synonyms_list = index.main.synonyms(&reader)?;
let mut synonyms = BTreeMap::new(); let mut synonyms = BTreeMap::new();
let index_synonyms = &index.synonyms; let index_synonyms = &index.synonyms;

View File

@ -25,7 +25,7 @@ async fn get(
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(Error::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; 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)) Ok(HttpResponse::Ok().json(stop_words))
} }

View File

@ -29,7 +29,7 @@ async fn get(
let reader = data.db.main_read_txn()?; 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 mut synonyms = IndexMap::new();
let index_synonyms = &index.synonyms; let index_synonyms = &index.synonyms;