clippy + fmt

This commit is contained in:
Quentin de Quelen 2020-04-10 19:05:05 +02:00 committed by qdequele
parent 22fbff98d4
commit 6a1f73a304
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
17 changed files with 633 additions and 339 deletions

View file

@ -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<Data>,
) -> aweb::Result<web::Json<Vec<IndexResponse>>> {
let reader = data.db.main_read_txn()
pub async fn list_indexes(data: web::Data<Data>) -> aweb::Result<web::Json<Vec<IndexResponse>>> {
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<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<web::Json<IndexResponse>> {
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<Data>,
body: web::Json<IndexCreateRequest>
body: web::Json<IndexCreateRequest>,
) -> aweb::Result<web::Json<IndexResponse>> {
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<Data>,
path: web::Path<IndexParam>,
body: web::Json<IndexCreateRequest>
body: web::Json<IndexCreateRequest>,
) -> aweb::Result<web::Json<IndexResponse>> {
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<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<HttpResponse> {
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<Data>,
path: web::Path<UpdateParam>,
) -> aweb::Result<web::Json<UpdateStatus>> {
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<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<web::Json<Vec<UpdateStatus>>> {
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))