From 38e474deafabd550de5437e95c0489dbac949ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 3 Oct 2019 17:33:15 +0200 Subject: [PATCH] Introduce the MResult type --- src/error.rs | 2 ++ src/lib.rs | 2 +- src/query_builder.rs | 6 +++--- src/store/docs_words.rs | 6 +++--- src/store/documents_fields.rs | 4 ++-- src/store/main.rs | 17 +++++++++-------- src/store/updates.rs | 10 +++++----- src/store/updates_results.rs | 13 +++++++------ src/update/mod.rs | 4 ++-- 9 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/error.rs b/src/error.rs index cf140c5e8..dd0200f30 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,8 @@ use std::{error, fmt}; use crate::serde::SerializerError; +pub type MResult = Result; + #[derive(Debug)] pub enum Error { SchemaDiffer, diff --git a/src/lib.rs b/src/lib.rs index d70a428d7..99f85dcb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ pub mod store; pub use self::query_builder::QueryBuilder; pub use self::raw_document::RawDocument; -pub use self::error::Error; +pub use self::error::{Error, MResult}; use self::number::{Number, ParseNumberError}; use self::ranked_map::RankedMap; diff --git a/src/query_builder.rs b/src/query_builder.rs index 5812ba60b..d91ec94f5 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -9,7 +9,7 @@ use slice_group_by::{GroupBy, GroupByMut}; use crate::automaton::{Automaton, AutomatonProducer, QueryEnhancer}; use crate::raw_document::{RawDocument, raw_documents_from}; use crate::{Document, DocumentId, Highlight, TmpMatch, criterion::Criteria}; -use crate::{store, reordered_attrs::ReorderedAttrs}; +use crate::{store, MResult, reordered_attrs::ReorderedAttrs}; pub struct QueryBuilder<'a> { criteria: Criteria<'a>, @@ -125,7 +125,7 @@ fn fetch_raw_documents( searchables: Option<&ReorderedAttrs>, main_store: &store::Main, postings_lists_store: &store::PostingsLists, -) -> Result, rkv::StoreError> +) -> MResult> { let mut matches = Vec::new(); let mut highlights = Vec::new(); @@ -206,7 +206,7 @@ impl<'a> QueryBuilder<'a> { reader: &rkv::Reader, query: &str, range: Range, - ) -> Result, rkv::StoreError> + ) -> MResult> { let start_processing = Instant::now(); let mut raw_documents_processed = Vec::new(); diff --git a/src/store/docs_words.rs b/src/store/docs_words.rs index 40a6d0a5c..554726952 100644 --- a/src/store/docs_words.rs +++ b/src/store/docs_words.rs @@ -1,6 +1,6 @@ use std::sync::Arc; use rkv::Value; -use crate::DocumentId; +use crate::{DocumentId, MResult}; #[derive(Copy, Clone)] pub struct DocsWords { @@ -34,14 +34,14 @@ impl DocsWords { &self, reader: &T, document_id: DocumentId, - ) -> Result, rkv::StoreError> + ) -> MResult> { let document_id_bytes = document_id.0.to_be_bytes(); match self.docs_words.get(reader, document_id_bytes)? { Some(Value::Blob(bytes)) => { let len = bytes.len(); let bytes = Arc::from(bytes); - let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap(); + let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len)?; Ok(Some(fst::Set::from(fst))) }, Some(value) => panic!("invalid type {:?}", value), diff --git a/src/store/documents_fields.rs b/src/store/documents_fields.rs index a5f41876e..6a80f7e0f 100644 --- a/src/store/documents_fields.rs +++ b/src/store/documents_fields.rs @@ -103,8 +103,8 @@ impl<'r, T: rkv::Readable + 'r> Iterator for DocumentFieldsIter<'r, T> { fn next(&mut self) -> Option { match self.iter.next() { Some(Ok((key, Some(rkv::Value::Blob(bytes))))) => { - let bytes = key.get(8..8+2).unwrap(); - let array = TryFrom::try_from(bytes).unwrap(); + let key_bytes = key.get(8..8+2).unwrap(); + let array = TryFrom::try_from(key_bytes).unwrap(); let attr = u16::from_be_bytes(array); let attr = SchemaAttr::new(attr); Some(Ok((attr, bytes))) diff --git a/src/store/main.rs b/src/store/main.rs index 870539f3e..a2cace7fe 100644 --- a/src/store/main.rs +++ b/src/store/main.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use std::convert::TryInto; use rkv::Value; -use crate::RankedMap; +use crate::{RankedMap, MResult}; const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents"; const RANKED_MAP_KEY: &str = "ranked-map"; @@ -29,13 +29,13 @@ impl Main { pub fn words_fst( &self, reader: &T, - ) -> Result, rkv::StoreError> + ) -> MResult> { match self.main.get(reader, WORDS_KEY)? { Some(Value::Blob(bytes)) => { let len = bytes.len(); let bytes = Arc::from(bytes); - let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap(); + let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len)?; Ok(Some(fst::Set::from(fst))) }, Some(value) => panic!("invalid type {:?}", value), @@ -47,22 +47,23 @@ impl Main { &self, writer: &mut rkv::Writer, ranked_map: &RankedMap, - ) -> Result<(), rkv::StoreError> + ) -> MResult<()> { let mut bytes = Vec::new(); - ranked_map.write_to_bin(&mut bytes).unwrap(); + ranked_map.write_to_bin(&mut bytes)?; let blob = Value::Blob(&bytes[..]); - self.main.put(writer, RANKED_MAP_KEY, &blob) + self.main.put(writer, RANKED_MAP_KEY, &blob)?; + Ok(()) } pub fn ranked_map( &self, reader: &T, - ) -> Result, rkv::StoreError> + ) -> MResult> { match self.main.get(reader, RANKED_MAP_KEY)? { Some(Value::Blob(bytes)) => { - let ranked_map = RankedMap::read_from_bin(bytes).unwrap(); + let ranked_map = RankedMap::read_from_bin(bytes)?; Ok(Some(ranked_map)) }, Some(value) => panic!("invalid type {:?}", value), diff --git a/src/store/updates.rs b/src/store/updates.rs index 22968738f..21264cc5e 100644 --- a/src/store/updates.rs +++ b/src/store/updates.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; use rkv::Value; -use crate::update::Update; +use crate::{update::Update, MResult}; #[derive(Copy, Clone)] pub struct Updates { @@ -47,13 +47,13 @@ impl Updates { &self, writer: &mut rkv::Writer, update: &Update, - ) -> Result + ) -> MResult { let last_update_id = self.last_update_id(writer)?; let last_update_id = last_update_id.map_or(0, |(n, _)| n + 1); let last_update_id_bytes = last_update_id.to_be_bytes(); - let update = rmp_serde::to_vec_named(&update).unwrap(); + let update = rmp_serde::to_vec_named(&update)?; let blob = Value::Blob(&update); self.updates.put(writer, last_update_id_bytes, &blob)?; @@ -63,7 +63,7 @@ impl Updates { pub fn pop_back( &self, writer: &mut rkv::Writer, - ) -> Result, rkv::StoreError> + ) -> MResult> { let (last_id, last_data) = match self.last_update_id(writer)? { Some(entry) => entry, @@ -72,7 +72,7 @@ impl Updates { match last_data { Some(Value::Blob(bytes)) => { - let update = rmp_serde::from_read_ref(&bytes).unwrap(); + let update = rmp_serde::from_read_ref(&bytes)?; Ok(Some((last_id, update))) }, Some(value) => panic!("invalid type {:?}", value), diff --git a/src/store/updates_results.rs b/src/store/updates_results.rs index 0a99c18df..527d7d376 100644 --- a/src/store/updates_results.rs +++ b/src/store/updates_results.rs @@ -1,5 +1,5 @@ use rkv::Value; -use crate::update::UpdateResult; +use crate::{update::UpdateResult, MResult}; #[derive(Copy, Clone)] pub struct UpdatesResults { @@ -12,25 +12,26 @@ impl UpdatesResults { writer: &mut rkv::Writer, update_id: u64, update_result: &UpdateResult, - ) -> Result<(), rkv::StoreError> + ) -> MResult<()> { let update_id_bytes = update_id.to_be_bytes(); - let update_result = bincode::serialize(&update_result).unwrap(); + let update_result = bincode::serialize(&update_result)?; let blob = Value::Blob(&update_result); - self.updates_results.put(writer, update_id_bytes, &blob) + self.updates_results.put(writer, update_id_bytes, &blob)?; + Ok(()) } pub fn update_result( &self, reader: &T, update_id: u64, - ) -> Result, rkv::StoreError> + ) -> MResult> { let update_id_bytes = update_id.to_be_bytes(); match self.updates_results.get(reader, update_id_bytes)? { Some(Value::Blob(bytes)) => { - let update_result = bincode::deserialize(&bytes).unwrap(); + let update_result = bincode::deserialize(&bytes)?; Ok(Some(update_result)) }, Some(value) => panic!("invalid type {:?}", value), diff --git a/src/update/mod.rs b/src/update/mod.rs index 742c716fe..013dd4981 100644 --- a/src/update/mod.rs +++ b/src/update/mod.rs @@ -7,7 +7,7 @@ pub use self::documents_deletion::{DocumentsDeletion, apply_documents_deletion}; use std::time::Duration; use std::collections::BTreeMap; use serde::{Serialize, Deserialize}; -use crate::{store, Error, DocumentId}; +use crate::{store, Error, MResult, DocumentId}; #[derive(Serialize, Deserialize)] pub enum Update { @@ -50,7 +50,7 @@ pub fn update_status( updates_store: store::Updates, updates_results_store: store::UpdatesResults, update_id: u64, -) -> Result +) -> MResult { match updates_results_store.update_result(reader, update_id)? { Some(result) => Ok(UpdateStatus::Processed(result)),