Format code with cargo fmt

This commit is contained in:
Quentin de Quelen 2019-11-20 11:24:08 +01:00
parent d01a3944c1
commit 45ded0498b
6 changed files with 69 additions and 47 deletions

View File

@ -1,9 +1,9 @@
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use crate::RankedMap; use crate::RankedMap;
use heed::Result as ZResult; use chrono::{DateTime, Utc};
use heed::types::{ByteSlice, OwnedType, SerdeBincode, Str}; use heed::types::{ByteSlice, OwnedType, SerdeBincode, Str};
use heed::Result as ZResult;
use meilidb_schema::Schema; use meilidb_schema::Schema;
use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
const CREATED_AT: &str = "created-at"; const CREATED_AT: &str = "created-at";
@ -32,30 +32,35 @@ impl Main {
self.main.clear(writer) self.main.clear(writer)
} }
pub fn name(self, reader: &heed::RoTxn) -> ZResult<Option<String>> {
Ok(self.main.get::<Str, Str>(reader, NAME)?.map(|name| name.to_owned()))
}
pub fn put_name(self, writer: &mut heed::RwTxn, name: &str) -> ZResult<()> { pub fn put_name(self, writer: &mut heed::RwTxn, name: &str) -> ZResult<()> {
self.main.put::<Str, Str>(writer, NAME, name) self.main.put::<Str, Str>(writer, NAME, name)
} }
pub fn name(self, reader: &heed::RoTxn) -> ZResult<Option<String>> {
Ok(self
.main
.get::<Str, Str>(reader, NAME)?
.map(|name| name.to_owned()))
}
pub fn put_created_at(self, writer: &mut heed::RwTxn) -> ZResult<()> {
self.main
.put::<Str, SerdeDatetime>(writer, CREATED_AT, &Utc::now())
}
pub fn created_at(self, reader: &heed::RoTxn) -> ZResult<Option<DateTime<Utc>>> { pub fn created_at(self, reader: &heed::RoTxn) -> ZResult<Option<DateTime<Utc>>> {
self.main.get::<Str, SerdeDatetime>(reader, CREATED_AT) self.main.get::<Str, SerdeDatetime>(reader, CREATED_AT)
} }
pub fn put_created_at(self, writer: &mut heed::RwTxn) -> ZResult<()> { pub fn put_updated_at(self, writer: &mut heed::RwTxn) -> ZResult<()> {
self.main.put::<Str, SerdeDatetime>(writer, CREATED_AT, &Utc::now()) self.main
.put::<Str, SerdeDatetime>(writer, UPDATED_AT, &Utc::now())
} }
pub fn updated_at(self, reader: &heed::RoTxn) -> ZResult<Option<DateTime<Utc>>> { pub fn updated_at(self, reader: &heed::RoTxn) -> ZResult<Option<DateTime<Utc>>> {
self.main.get::<Str, SerdeDatetime>(reader, UPDATED_AT) self.main.get::<Str, SerdeDatetime>(reader, UPDATED_AT)
} }
pub fn put_updated_at(self, writer: &mut heed::RwTxn) -> ZResult<()> {
self.main.put::<Str, SerdeDatetime>(writer, UPDATED_AT, &Utc::now())
}
pub fn put_words_fst(self, writer: &mut heed::RwTxn, fst: &fst::Set) -> ZResult<()> { pub fn put_words_fst(self, writer: &mut heed::RwTxn, fst: &fst::Set) -> ZResult<()> {
let bytes = fst.as_fst().as_bytes(); let bytes = fst.as_fst().as_bytes();
self.main.put::<Str, ByteSlice>(writer, WORDS_KEY, bytes) self.main.put::<Str, ByteSlice>(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 self.main
.put::<Str, SerdeFreqsMap>(writer, FIELDS_FREQUENCY, fields_frequency) .put::<Str, SerdeFreqsMap>(writer, FIELDS_FREQUENCY, fields_frequency)
} }

View File

@ -5,7 +5,7 @@ use std::sync::Arc;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use heed::types::{SerdeBincode, Str}; use heed::types::{SerdeBincode, Str};
use log::*; use log::*;
use meilidb_core::{Database, MResult, Error as MError}; use meilidb_core::{Database, Error as MError, MResult};
use sysinfo::Pid; use sysinfo::Pid;
use crate::option::Opt; use crate::option::Opt;
@ -91,7 +91,10 @@ impl DataInner {
.map(|(a, c)| (schema.attribute_name(a).to_owned(), c)) .map(|(a, c)| (schema.attribute_name(a).to_owned(), c))
.collect(); .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)
} }
} }

View File

@ -29,10 +29,7 @@ fn generate_uid() -> String {
pub async fn list_indexes(ctx: Context<Data>) -> SResult<Response> { pub async fn list_indexes(ctx: Context<Data>) -> SResult<Response> {
ctx.is_allowed(IndexesRead)?; ctx.is_allowed(IndexesRead)?;
let indexes_uids = ctx let indexes_uids = ctx.state().db.indexes_uids();
.state()
.db
.indexes_uids();
let env = &ctx.state().db.env; let env = &ctx.state().db.env;
let mut reader = env.read_txn().map_err(ResponseError::internal)?; let mut reader = env.read_txn().map_err(ResponseError::internal)?;
@ -45,13 +42,19 @@ pub async fn list_indexes(ctx: Context<Data>) -> SResult<Response> {
.db .db
.open_index(&index_uid) .open_index(&index_uid)
.ok_or(ResponseError::internal(&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)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Name not found"))?; .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)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Created date not found"))?; .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)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Updated date not found"))?; .ok_or(ResponseError::internal("Updated date not found"))?;
@ -85,13 +88,19 @@ pub async fn get_index(ctx: Context<Data>) -> SResult<Response> {
let mut reader = env.read_txn().map_err(ResponseError::internal)?; let mut reader = env.read_txn().map_err(ResponseError::internal)?;
let uid = ctx.url_param("index")?.to_string(); 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)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Name not found"))?; .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)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Created date not found"))?; .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)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Updated date not found"))?; .ok_or(ResponseError::internal("Updated date not found"))?;
@ -165,7 +174,10 @@ struct IndexCreateResponse {
pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> { pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
ctx.is_allowed(IndexesWrite)?; ctx.is_allowed(IndexesWrite)?;
let body = ctx.body_json::<IndexCreateRequest>().await.map_err(ResponseError::bad_request)?; let body = ctx
.body_json::<IndexCreateRequest>()
.await
.map_err(ResponseError::bad_request)?;
let generated_uid = generate_uid(); let generated_uid = generate_uid();
@ -179,13 +191,16 @@ pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
let env = &db.env; let env = &db.env;
let mut writer = env.write_txn().map_err(ResponseError::internal)?; let mut writer = env.write_txn().map_err(ResponseError::internal)?;
created_index.main created_index
.main
.put_name(&mut writer, &body.name) .put_name(&mut writer, &body.name)
.map_err(ResponseError::internal)?; .map_err(ResponseError::internal)?;
created_index.main created_index
.main
.put_created_at(&mut writer) .put_created_at(&mut writer)
.map_err(ResponseError::internal)?; .map_err(ResponseError::internal)?;
created_index.main created_index
.main
.put_updated_at(&mut writer) .put_updated_at(&mut writer)
.map_err(ResponseError::internal)?; .map_err(ResponseError::internal)?;
@ -193,8 +208,8 @@ pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
let mut response_update_id = None; let mut response_update_id = None;
if let Some(schema) = schema { if let Some(schema) = schema {
let update_id = created_index let update_id = created_index
.schema_update(&mut writer, schema.clone()) .schema_update(&mut writer, schema.clone())
.map_err(ResponseError::internal)?; .map_err(ResponseError::internal)?;
response_update_id = Some(update_id) response_update_id = Some(update_id)
} }
@ -210,8 +225,8 @@ pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
}; };
Ok(tide::response::json(response_body) Ok(tide::response::json(response_body)
.with_status(StatusCode::CREATED) .with_status(StatusCode::CREATED)
.into_response()) .into_response())
} }
pub async fn update_schema(mut ctx: Context<Data>) -> SResult<Response> { pub async fn update_schema(mut ctx: Context<Data>) -> SResult<Response> {

View File

@ -13,7 +13,8 @@ pub mod synonym;
pub fn load_routes(app: &mut tide::App<Data>) { pub fn load_routes(app: &mut tide::App<Data>) {
app.at("").nest(|router| { app.at("").nest(|router| {
router.at("/indexes").nest(|router| { router.at("/indexes").nest(|router| {
router.at("/") router
.at("/")
.get(index::list_indexes) .get(index::list_indexes)
.post(index::create_index); .post(index::create_index);

View File

@ -155,12 +155,7 @@ pub async fn search_multi_index(mut ctx: Context<Data>) -> SResult<Response> {
for index in index_list.clone() { for index in index_list.clone() {
if index == "*" { if index == "*" {
index_list = ctx index_list = ctx.state().db.indexes_uids().into_iter().collect();
.state()
.db
.indexes_uids()
.into_iter()
.collect();
} }
} }

View File

@ -65,7 +65,6 @@ pub async fn get_stats(ctx: Context<Data>) -> SResult<Response> {
let mut index_list = HashMap::new(); let mut index_list = HashMap::new();
let db = &ctx.state().db; let db = &ctx.state().db;
let env = &db.env; let env = &db.env;
let reader = env.read_txn().map_err(ResponseError::internal)?; let reader = env.read_txn().map_err(ResponseError::internal)?;