requested changes

This commit is contained in:
mpostma 2020-05-28 16:12:24 +02:00
parent 2a32ad39a0
commit 8a2e60dc09
10 changed files with 46 additions and 114 deletions

View File

@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
use structopt::StructOpt; use structopt::StructOpt;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use meilisearch_core::{Database, DatabaseOptions, Highlight, ProcessedUpdateResult, Error as MError}; use meilisearch_core::{Database, DatabaseOptions, Highlight, ProcessedUpdateResult};
use meilisearch_core::settings::Settings; use meilisearch_core::settings::Settings;
use meilisearch_schema::FieldId; use meilisearch_schema::FieldId;
@ -126,10 +126,7 @@ fn index_command(command: IndexCommand, database: Database) -> Result<(), Box<dy
settings.into_update().unwrap() settings.into_update().unwrap()
}; };
db.update_write::<_, _, MError>(|writer| { db.update_write(|w| index.settings_update(w, settings))?;
index.settings_update(writer, settings)?;
Ok(())
})?;
let mut rdr = if command.csv_data_path.as_os_str() == "-" { let mut rdr = if command.csv_data_path.as_os_str() == "-" {
csv::Reader::from_reader(Box::new(io::stdin()) as Box<dyn Read>) csv::Reader::from_reader(Box::new(io::stdin()) as Box<dyn Read>)
@ -176,10 +173,7 @@ fn index_command(command: IndexCommand, database: Database) -> Result<(), Box<dy
println!(); println!();
let update_id = db.update_write::<_, _, MError>(|writer| { let update_id = db.update_write(|w| additions.finalize(w))?;
let update_id = additions.finalize(writer)?;
Ok(update_id)
})?;
println!("committing update..."); println!("committing update...");
max_update_id = max_update_id.max(update_id); max_update_id = max_update_id.max(update_id);

View File

@ -363,11 +363,13 @@ impl Database {
/// provides a context with a reader to the main database. experimental. /// provides a context with a reader to the main database. experimental.
pub fn main_read<F, R, E>(&self, f: F) -> Result<R, E> pub fn main_read<F, R, E>(&self, f: F) -> Result<R, E>
where where
F: Fn(&MainReader) -> Result<R, E>, F: FnOnce(&MainReader) -> Result<R, E>,
E: From<Error>, E: From<Error>,
{ {
let reader = self.main_read_txn()?; let reader = self.main_read_txn()?;
f(&reader) let result = f(&reader)?;
reader.abort().map_err(Error::Heed)?;
Ok(result)
} }
pub fn update_read_txn(&self) -> MResult<UpdateReader> { pub fn update_read_txn(&self) -> MResult<UpdateReader> {
@ -394,11 +396,13 @@ impl Database {
/// provides a context with a reader to the update database. experimental. /// provides a context with a reader to the update database. experimental.
pub fn update_read<F, R, E>(&self, f: F) -> Result<R, E> pub fn update_read<F, R, E>(&self, f: F) -> Result<R, E>
where where
F: Fn(&UpdateReader) -> Result<R, E>, F: FnOnce(&UpdateReader) -> Result<R, E>,
E: From<Error>, E: From<Error>,
{ {
let reader = self.update_read_txn()?; let reader = self.update_read_txn()?;
f(&reader) let result = f(&reader)?;
reader.abort().map_err(Error::Heed)?;
Ok(result)
} }
pub fn copy_and_compact_to_path<P: AsRef<Path>>(&self, path: P) -> MResult<(File, File)> { pub fn copy_and_compact_to_path<P: AsRef<Path>>(&self, path: P) -> MResult<(File, File)> {

View File

@ -22,7 +22,7 @@ pub trait ErrorCode: std::error::Error {
/// return the error type /// return the error type
fn error_type(&self) -> String { fn error_type(&self) -> String {
self.error_code().r#type() self.error_code().type_()
} }
} }
@ -130,13 +130,13 @@ impl Code {
} }
/// return the error type /// return the error type
fn r#type(&self) -> String { fn type_(&self) -> String {
self.err_code().error_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
fn url(&self) -> String { fn url(&self) -> String {
format!("docs.meilisearch.com/error/{}", self.name()) format!("https://docs.meilisearch.com/error/{}", self.name())
} }
} }
@ -148,26 +148,26 @@ struct ErrCode {
} }
impl ErrCode { impl ErrCode {
fn authentication(err_name: &'static str, status_code: StatusCode) -> ErrCode { fn authentication(error_name: &'static str, status_code: StatusCode) -> ErrCode {
ErrCode { ErrCode {
status_code, status_code,
error_name: err_name, error_name,
error_type: ErrorType::Authentication, error_type: ErrorType::Authentication,
} }
} }
fn internal(err_name: &'static str, status_code: StatusCode) -> ErrCode { fn internal(error_name: &'static str, status_code: StatusCode) -> ErrCode {
ErrCode { ErrCode {
status_code, status_code,
error_name: err_name, error_name,
error_type: ErrorType::InternalError, error_type: ErrorType::InternalError,
} }
} }
fn invalid(err_name: &'static str, status_code: StatusCode) -> ErrCode { fn invalid(error_name: &'static str, status_code: StatusCode) -> ErrCode {
ErrCode { ErrCode {
status_code, status_code,
error_name: err_name, error_name,
error_type: ErrorType::InvalidRequest, error_type: ErrorType::InvalidRequest,
} }
} }

View File

@ -72,10 +72,7 @@ async fn delete_document(
let mut documents_deletion = index.documents_deletion(); let mut documents_deletion = index.documents_deletion();
documents_deletion.delete_document_by_external_docid(path.document_id.clone()); documents_deletion.delete_document_by_external_docid(path.document_id.clone());
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| documents_deletion.finalize(w))?;
let update_id = documents_deletion.finalize(writer)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -174,10 +171,7 @@ async fn update_multiple_documents(
.set_primary_key(&id) .set_primary_key(&id)
.map_err(Error::bad_request)?; .map_err(Error::bad_request)?;
data.db.main_write::<_, _, ResponseError>(|mut writer| { data.db.main_write(|w| index.main.put_schema(w, &schema))?;
index.main.put_schema(&mut writer, &schema)?;
Ok(())
})?;
} }
let mut document_addition = if is_partial { let mut document_addition = if is_partial {
@ -190,12 +184,7 @@ async fn update_multiple_documents(
document_addition.update_document(document); document_addition.update_document(document);
} }
let update_id = data let update_id = data.db.update_write(|w| document_addition.finalize(w))?;
.db
.update_write::<_, _, ResponseError>(|writer| {
let update_id = document_addition.finalize(writer)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -242,7 +231,7 @@ async fn delete_documents(
documents_deletion.delete_document_by_external_docid(document_id); documents_deletion.delete_document_by_external_docid(document_id);
} }
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| Ok(documents_deletion.finalize(writer)?))?; let update_id = data.db.update_write(|w| documents_deletion.finalize(w))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -257,7 +246,7 @@ async fn clear_all_documents(
.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 update_id = data.db.update_write::<_, _, ResponseError>(|writer| Ok(index.clear_all(writer)?))?; let update_id = data.db.update_write(|w| index.clear_all(w))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }

View File

@ -20,20 +20,12 @@ async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError
} }
async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
data.db.main_write::<_, _, ResponseError>(|writer| { data.db.main_write(|w| data.db.set_healthy(w))?;
data.db.set_healthy(writer)?;
Ok(())
})?;
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
data.db.main_write::<_, _, ResponseError>(|writer| { data.db.main_write(|w| data.db.set_unhealthy(w))?;
data.db.set_unhealthy(writer)?;
Ok(())
})?;
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }

View File

@ -243,13 +243,13 @@ async fn update_index(
.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))?;
data.db.main_write::<_, _, ResponseError>(|mut writer| { data.db.main_write::<_, _, ResponseError>(|writer| {
if let Some(name) = &body.name { if let Some(name) = &body.name {
index.main.put_name(&mut writer, name)?; index.main.put_name(writer, name)?;
} }
if let Some(id) = body.primary_key.clone() { if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = index.main.schema(&writer)? { if let Some(mut schema) = index.main.schema(writer)? {
match schema.primary_key() { match schema.primary_key() {
Some(_) => { Some(_) => {
return Err(Error::bad_request( return Err(Error::bad_request(
@ -258,12 +258,12 @@ async fn update_index(
} }
None => { None => {
schema.set_primary_key(&id)?; schema.set_primary_key(&id)?;
index.main.put_schema(&mut writer, &schema)?; index.main.put_schema(writer, &schema)?;
} }
} }
} }
} }
index.main.put_updated_at(&mut writer)?; index.main.put_updated_at(writer)?;
Ok(()) Ok(())
})?; })?;

View File

@ -153,10 +153,7 @@ async fn delete_all(
attributes_for_faceting: UpdateState::Clear, attributes_for_faceting: UpdateState::Clear,
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -206,11 +203,7 @@ async fn update_rules(
}; };
let settings = settings.into_update().map_err(Error::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| {
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -233,10 +226,7 @@ async fn delete_rules(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -279,11 +269,7 @@ async fn update_distinct(
}; };
let settings = settings.into_update().map_err(Error::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| {
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -306,10 +292,7 @@ async fn delete_distinct(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -355,10 +338,7 @@ async fn update_searchable(
let settings = settings.into_update().map_err(Error::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -381,10 +361,7 @@ async fn delete_searchable(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -431,11 +408,7 @@ async fn update_displayed(
}; };
let settings = settings.into_update().map_err(Error::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| {
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -458,10 +431,7 @@ async fn delete_displayed(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -507,11 +477,7 @@ async fn update_accept_new_fields(
}; };
let settings = settings.into_update().map_err(Error::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| {
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }

View File

@ -49,10 +49,7 @@ async fn update(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -75,10 +72,7 @@ async fn delete(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }

View File

@ -60,10 +60,7 @@ async fn update(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }
@ -86,10 +83,7 @@ async fn delete(
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let update_id = data.db.update_write::<_, _, ResponseError>(|writer| { let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
let update_id = index.settings_update(writer, settings)?;
Ok(update_id)
})?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
} }

View File

@ -21,7 +21,6 @@ async fn delete_batch() {
server.populate_movies().await; server.populate_movies().await;
let (_response, status_code) = server.get_document(419704).await; let (_response, status_code) = server.get_document(419704).await;
println!("{:?}", _response);
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
let body = serde_json::json!([419704, 512200, 181812]); let body = serde_json::json!([419704, 512200, 181812]);