From 45ded0498bd09e20e6143a5e7f957bbd8c76dbca Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Wed, 20 Nov 2019 11:24:08 +0100 Subject: [PATCH] Format code with cargo fmt --- meilidb-core/src/store/main.rs | 39 +++++++++++++--------- meilidb-http/src/data.rs | 7 ++-- meilidb-http/src/routes/index.rs | 55 ++++++++++++++++++++----------- meilidb-http/src/routes/mod.rs | 3 +- meilidb-http/src/routes/search.rs | 7 +--- meilidb-http/src/routes/stats.rs | 5 ++- 6 files changed, 69 insertions(+), 47 deletions(-) diff --git a/meilidb-core/src/store/main.rs b/meilidb-core/src/store/main.rs index c89cd23a1..bc5fe1d10 100644 --- a/meilidb-core/src/store/main.rs +++ b/meilidb-core/src/store/main.rs @@ -1,9 +1,9 @@ -use std::collections::HashMap; -use chrono::{DateTime, Utc}; use crate::RankedMap; -use heed::Result as ZResult; +use chrono::{DateTime, Utc}; use heed::types::{ByteSlice, OwnedType, SerdeBincode, Str}; +use heed::Result as ZResult; use meilidb_schema::Schema; +use std::collections::HashMap; use std::sync::Arc; const CREATED_AT: &str = "created-at"; @@ -32,30 +32,35 @@ impl Main { self.main.clear(writer) } - pub fn name(self, reader: &heed::RoTxn) -> ZResult> { - Ok(self.main.get::(reader, NAME)?.map(|name| name.to_owned())) - } - pub fn put_name(self, writer: &mut heed::RwTxn, name: &str) -> ZResult<()> { self.main.put::(writer, NAME, name) } + pub fn name(self, reader: &heed::RoTxn) -> ZResult> { + Ok(self + .main + .get::(reader, NAME)? + .map(|name| name.to_owned())) + } + + pub fn put_created_at(self, writer: &mut heed::RwTxn) -> ZResult<()> { + self.main + .put::(writer, CREATED_AT, &Utc::now()) + } + pub fn created_at(self, reader: &heed::RoTxn) -> ZResult>> { self.main.get::(reader, CREATED_AT) } - - pub fn put_created_at(self, writer: &mut heed::RwTxn) -> ZResult<()> { - self.main.put::(writer, CREATED_AT, &Utc::now()) + + pub fn put_updated_at(self, writer: &mut heed::RwTxn) -> ZResult<()> { + self.main + .put::(writer, UPDATED_AT, &Utc::now()) } pub fn updated_at(self, reader: &heed::RoTxn) -> ZResult>> { self.main.get::(reader, UPDATED_AT) } - pub fn put_updated_at(self, writer: &mut heed::RwTxn) -> ZResult<()> { - self.main.put::(writer, UPDATED_AT, &Utc::now()) - } - pub fn put_words_fst(self, writer: &mut heed::RwTxn, fst: &fst::Set) -> ZResult<()> { let bytes = fst.as_fst().as_bytes(); self.main.put::(writer, WORDS_KEY, bytes) @@ -148,7 +153,11 @@ impl Main { } } - pub fn put_fields_frequency(self, writer: &mut heed::RwTxn, fields_frequency: &FreqsMap) -> ZResult<()> { + pub fn put_fields_frequency( + self, + writer: &mut heed::RwTxn, + fields_frequency: &FreqsMap, + ) -> ZResult<()> { self.main .put::(writer, FIELDS_FREQUENCY, fields_frequency) } diff --git a/meilidb-http/src/data.rs b/meilidb-http/src/data.rs index 06016b9e7..f8af58187 100644 --- a/meilidb-http/src/data.rs +++ b/meilidb-http/src/data.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use chrono::{DateTime, Utc}; use heed::types::{SerdeBincode, Str}; use log::*; -use meilidb_core::{Database, MResult, Error as MError}; +use meilidb_core::{Database, Error as MError, MResult}; use sysinfo::Pid; use crate::option::Opt; @@ -91,7 +91,10 @@ impl DataInner { .map(|(a, c)| (schema.attribute_name(a).to_owned(), c)) .collect(); - index.main.put_fields_frequency(&mut writer, &frequency).map_err(MError::Zlmdb) + index + .main + .put_fields_frequency(&mut writer, &frequency) + .map_err(MError::Zlmdb) } } diff --git a/meilidb-http/src/routes/index.rs b/meilidb-http/src/routes/index.rs index f6fe999f2..563fb768c 100644 --- a/meilidb-http/src/routes/index.rs +++ b/meilidb-http/src/routes/index.rs @@ -28,11 +28,8 @@ fn generate_uid() -> String { pub async fn list_indexes(ctx: Context) -> SResult { ctx.is_allowed(IndexesRead)?; - - let indexes_uids = ctx - .state() - .db - .indexes_uids(); + + let indexes_uids = ctx.state().db.indexes_uids(); let env = &ctx.state().db.env; let mut reader = env.read_txn().map_err(ResponseError::internal)?; @@ -45,16 +42,22 @@ pub async fn list_indexes(ctx: Context) -> SResult { .db .open_index(&index_uid) .ok_or(ResponseError::internal(&index_uid))?; - let name = index.main.name(&mut reader) + let name = index + .main + .name(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Name not found"))?; - let created_at = index.main.created_at(&mut reader) + let created_at = index + .main + .created_at(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Created date not found"))?; - let updated_at = index.main.updated_at(&mut reader) + let updated_at = index + .main + .updated_at(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Updated date not found"))?; - + let index_reponse = IndexResponse { name, uid: index_uid, @@ -85,13 +88,19 @@ pub async fn get_index(ctx: Context) -> SResult { let mut reader = env.read_txn().map_err(ResponseError::internal)?; let uid = ctx.url_param("index")?.to_string(); - let name = index.main.name(&mut reader) + let name = index + .main + .name(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Name not found"))?; - let created_at = index.main.created_at(&mut reader) + let created_at = index + .main + .created_at(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Created date not found"))?; - let updated_at = index.main.updated_at(&mut reader) + let updated_at = index + .main + .updated_at(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Updated date not found"))?; @@ -165,7 +174,10 @@ struct IndexCreateResponse { pub async fn create_index(mut ctx: Context) -> SResult { ctx.is_allowed(IndexesWrite)?; - let body = ctx.body_json::().await.map_err(ResponseError::bad_request)?; + let body = ctx + .body_json::() + .await + .map_err(ResponseError::bad_request)?; let generated_uid = generate_uid(); @@ -179,13 +191,16 @@ pub async fn create_index(mut ctx: Context) -> SResult { let env = &db.env; let mut writer = env.write_txn().map_err(ResponseError::internal)?; - created_index.main + created_index + .main .put_name(&mut writer, &body.name) .map_err(ResponseError::internal)?; - created_index.main + created_index + .main .put_created_at(&mut writer) .map_err(ResponseError::internal)?; - created_index.main + created_index + .main .put_updated_at(&mut writer) .map_err(ResponseError::internal)?; @@ -193,8 +208,8 @@ pub async fn create_index(mut ctx: Context) -> SResult { let mut response_update_id = None; if let Some(schema) = schema { let update_id = created_index - .schema_update(&mut writer, schema.clone()) - .map_err(ResponseError::internal)?; + .schema_update(&mut writer, schema.clone()) + .map_err(ResponseError::internal)?; response_update_id = Some(update_id) } @@ -210,8 +225,8 @@ pub async fn create_index(mut ctx: Context) -> SResult { }; Ok(tide::response::json(response_body) - .with_status(StatusCode::CREATED) - .into_response()) + .with_status(StatusCode::CREATED) + .into_response()) } pub async fn update_schema(mut ctx: Context) -> SResult { diff --git a/meilidb-http/src/routes/mod.rs b/meilidb-http/src/routes/mod.rs index 1ba53f0fc..02cede3d4 100644 --- a/meilidb-http/src/routes/mod.rs +++ b/meilidb-http/src/routes/mod.rs @@ -13,7 +13,8 @@ pub mod synonym; pub fn load_routes(app: &mut tide::App) { app.at("").nest(|router| { router.at("/indexes").nest(|router| { - router.at("/") + router + .at("/") .get(index::list_indexes) .post(index::create_index); diff --git a/meilidb-http/src/routes/search.rs b/meilidb-http/src/routes/search.rs index 0551a278e..6e1b558eb 100644 --- a/meilidb-http/src/routes/search.rs +++ b/meilidb-http/src/routes/search.rs @@ -155,12 +155,7 @@ pub async fn search_multi_index(mut ctx: Context) -> SResult { for index in index_list.clone() { if index == "*" { - index_list = ctx - .state() - .db - .indexes_uids() - .into_iter() - .collect(); + index_list = ctx.state().db.indexes_uids().into_iter().collect(); } } diff --git a/meilidb-http/src/routes/stats.rs b/meilidb-http/src/routes/stats.rs index 488b10189..af2e3e49d 100644 --- a/meilidb-http/src/routes/stats.rs +++ b/meilidb-http/src/routes/stats.rs @@ -65,15 +65,14 @@ pub async fn get_stats(ctx: Context) -> SResult { let mut index_list = HashMap::new(); - let db = &ctx.state().db; let env = &db.env; - let reader = env.read_txn().map_err(ResponseError::internal)?; + let reader = env.read_txn().map_err(ResponseError::internal)?; let indexes_set = ctx.state().db.indexes_uids(); for index_uid in indexes_set { let index = db.open_index(&index_uid).unwrap(); - + let number_of_documents = index .main .number_of_documents(&reader)