Introduce the MResult type

This commit is contained in:
Clément Renault 2019-10-03 17:33:15 +02:00
parent 00c70d3cb5
commit 38e474deaf
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
9 changed files with 34 additions and 30 deletions

View File

@ -1,6 +1,8 @@
use std::{error, fmt}; use std::{error, fmt};
use crate::serde::SerializerError; use crate::serde::SerializerError;
pub type MResult<T> = Result<T, Error>;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
SchemaDiffer, SchemaDiffer,

View File

@ -13,7 +13,7 @@ pub mod store;
pub use self::query_builder::QueryBuilder; pub use self::query_builder::QueryBuilder;
pub use self::raw_document::RawDocument; pub use self::raw_document::RawDocument;
pub use self::error::Error; pub use self::error::{Error, MResult};
use self::number::{Number, ParseNumberError}; use self::number::{Number, ParseNumberError};
use self::ranked_map::RankedMap; use self::ranked_map::RankedMap;

View File

@ -9,7 +9,7 @@ use slice_group_by::{GroupBy, GroupByMut};
use crate::automaton::{Automaton, AutomatonProducer, QueryEnhancer}; use crate::automaton::{Automaton, AutomatonProducer, QueryEnhancer};
use crate::raw_document::{RawDocument, raw_documents_from}; use crate::raw_document::{RawDocument, raw_documents_from};
use crate::{Document, DocumentId, Highlight, TmpMatch, criterion::Criteria}; 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> { pub struct QueryBuilder<'a> {
criteria: Criteria<'a>, criteria: Criteria<'a>,
@ -125,7 +125,7 @@ fn fetch_raw_documents(
searchables: Option<&ReorderedAttrs>, searchables: Option<&ReorderedAttrs>,
main_store: &store::Main, main_store: &store::Main,
postings_lists_store: &store::PostingsLists, postings_lists_store: &store::PostingsLists,
) -> Result<Vec<RawDocument>, rkv::StoreError> ) -> MResult<Vec<RawDocument>>
{ {
let mut matches = Vec::new(); let mut matches = Vec::new();
let mut highlights = Vec::new(); let mut highlights = Vec::new();
@ -206,7 +206,7 @@ impl<'a> QueryBuilder<'a> {
reader: &rkv::Reader, reader: &rkv::Reader,
query: &str, query: &str,
range: Range<usize>, range: Range<usize>,
) -> Result<Vec<Document>, rkv::StoreError> ) -> MResult<Vec<Document>>
{ {
let start_processing = Instant::now(); let start_processing = Instant::now();
let mut raw_documents_processed = Vec::new(); let mut raw_documents_processed = Vec::new();

View File

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use rkv::Value; use rkv::Value;
use crate::DocumentId; use crate::{DocumentId, MResult};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct DocsWords { pub struct DocsWords {
@ -34,14 +34,14 @@ impl DocsWords {
&self, &self,
reader: &T, reader: &T,
document_id: DocumentId, document_id: DocumentId,
) -> Result<Option<fst::Set>, rkv::StoreError> ) -> MResult<Option<fst::Set>>
{ {
let document_id_bytes = document_id.0.to_be_bytes(); let document_id_bytes = document_id.0.to_be_bytes();
match self.docs_words.get(reader, document_id_bytes)? { match self.docs_words.get(reader, document_id_bytes)? {
Some(Value::Blob(bytes)) => { Some(Value::Blob(bytes)) => {
let len = bytes.len(); let len = bytes.len();
let bytes = Arc::from(bytes); 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))) Ok(Some(fst::Set::from(fst)))
}, },
Some(value) => panic!("invalid type {:?}", value), Some(value) => panic!("invalid type {:?}", value),

View File

@ -103,8 +103,8 @@ impl<'r, T: rkv::Readable + 'r> Iterator for DocumentFieldsIter<'r, T> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() { match self.iter.next() {
Some(Ok((key, Some(rkv::Value::Blob(bytes))))) => { Some(Ok((key, Some(rkv::Value::Blob(bytes))))) => {
let bytes = key.get(8..8+2).unwrap(); let key_bytes = key.get(8..8+2).unwrap();
let array = TryFrom::try_from(bytes).unwrap(); let array = TryFrom::try_from(key_bytes).unwrap();
let attr = u16::from_be_bytes(array); let attr = u16::from_be_bytes(array);
let attr = SchemaAttr::new(attr); let attr = SchemaAttr::new(attr);
Some(Ok((attr, bytes))) Some(Ok((attr, bytes)))

View File

@ -2,7 +2,7 @@ use std::sync::Arc;
use std::convert::TryInto; use std::convert::TryInto;
use rkv::Value; use rkv::Value;
use crate::RankedMap; use crate::{RankedMap, MResult};
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents"; const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
const RANKED_MAP_KEY: &str = "ranked-map"; const RANKED_MAP_KEY: &str = "ranked-map";
@ -29,13 +29,13 @@ impl Main {
pub fn words_fst<T: rkv::Readable>( pub fn words_fst<T: rkv::Readable>(
&self, &self,
reader: &T, reader: &T,
) -> Result<Option<fst::Set>, rkv::StoreError> ) -> MResult<Option<fst::Set>>
{ {
match self.main.get(reader, WORDS_KEY)? { match self.main.get(reader, WORDS_KEY)? {
Some(Value::Blob(bytes)) => { Some(Value::Blob(bytes)) => {
let len = bytes.len(); let len = bytes.len();
let bytes = Arc::from(bytes); 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))) Ok(Some(fst::Set::from(fst)))
}, },
Some(value) => panic!("invalid type {:?}", value), Some(value) => panic!("invalid type {:?}", value),
@ -47,22 +47,23 @@ impl Main {
&self, &self,
writer: &mut rkv::Writer, writer: &mut rkv::Writer,
ranked_map: &RankedMap, ranked_map: &RankedMap,
) -> Result<(), rkv::StoreError> ) -> MResult<()>
{ {
let mut bytes = Vec::new(); 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[..]); 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<T: rkv::Readable>( pub fn ranked_map<T: rkv::Readable>(
&self, &self,
reader: &T, reader: &T,
) -> Result<Option<RankedMap>, rkv::StoreError> ) -> MResult<Option<RankedMap>>
{ {
match self.main.get(reader, RANKED_MAP_KEY)? { match self.main.get(reader, RANKED_MAP_KEY)? {
Some(Value::Blob(bytes)) => { 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)) Ok(Some(ranked_map))
}, },
Some(value) => panic!("invalid type {:?}", value), Some(value) => panic!("invalid type {:?}", value),

View File

@ -1,6 +1,6 @@
use std::convert::TryInto; use std::convert::TryInto;
use rkv::Value; use rkv::Value;
use crate::update::Update; use crate::{update::Update, MResult};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Updates { pub struct Updates {
@ -47,13 +47,13 @@ impl Updates {
&self, &self,
writer: &mut rkv::Writer, writer: &mut rkv::Writer,
update: &Update, update: &Update,
) -> Result<u64, rkv::StoreError> ) -> MResult<u64>
{ {
let last_update_id = self.last_update_id(writer)?; 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 = last_update_id.map_or(0, |(n, _)| n + 1);
let last_update_id_bytes = last_update_id.to_be_bytes(); 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); let blob = Value::Blob(&update);
self.updates.put(writer, last_update_id_bytes, &blob)?; self.updates.put(writer, last_update_id_bytes, &blob)?;
@ -63,7 +63,7 @@ impl Updates {
pub fn pop_back( pub fn pop_back(
&self, &self,
writer: &mut rkv::Writer, writer: &mut rkv::Writer,
) -> Result<Option<(u64, Update)>, rkv::StoreError> ) -> MResult<Option<(u64, Update)>>
{ {
let (last_id, last_data) = match self.last_update_id(writer)? { let (last_id, last_data) = match self.last_update_id(writer)? {
Some(entry) => entry, Some(entry) => entry,
@ -72,7 +72,7 @@ impl Updates {
match last_data { match last_data {
Some(Value::Blob(bytes)) => { 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))) Ok(Some((last_id, update)))
}, },
Some(value) => panic!("invalid type {:?}", value), Some(value) => panic!("invalid type {:?}", value),

View File

@ -1,5 +1,5 @@
use rkv::Value; use rkv::Value;
use crate::update::UpdateResult; use crate::{update::UpdateResult, MResult};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct UpdatesResults { pub struct UpdatesResults {
@ -12,25 +12,26 @@ impl UpdatesResults {
writer: &mut rkv::Writer, writer: &mut rkv::Writer,
update_id: u64, update_id: u64,
update_result: &UpdateResult, update_result: &UpdateResult,
) -> Result<(), rkv::StoreError> ) -> MResult<()>
{ {
let update_id_bytes = update_id.to_be_bytes(); 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); 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<T: rkv::Readable>( pub fn update_result<T: rkv::Readable>(
&self, &self,
reader: &T, reader: &T,
update_id: u64, update_id: u64,
) -> Result<Option<UpdateResult>, rkv::StoreError> ) -> MResult<Option<UpdateResult>>
{ {
let update_id_bytes = update_id.to_be_bytes(); let update_id_bytes = update_id.to_be_bytes();
match self.updates_results.get(reader, update_id_bytes)? { match self.updates_results.get(reader, update_id_bytes)? {
Some(Value::Blob(bytes)) => { Some(Value::Blob(bytes)) => {
let update_result = bincode::deserialize(&bytes).unwrap(); let update_result = bincode::deserialize(&bytes)?;
Ok(Some(update_result)) Ok(Some(update_result))
}, },
Some(value) => panic!("invalid type {:?}", value), Some(value) => panic!("invalid type {:?}", value),

View File

@ -7,7 +7,7 @@ pub use self::documents_deletion::{DocumentsDeletion, apply_documents_deletion};
use std::time::Duration; use std::time::Duration;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::{store, Error, DocumentId}; use crate::{store, Error, MResult, DocumentId};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub enum Update { pub enum Update {
@ -50,7 +50,7 @@ pub fn update_status<T: rkv::Readable>(
updates_store: store::Updates, updates_store: store::Updates,
updates_results_store: store::UpdatesResults, updates_results_store: store::UpdatesResults,
update_id: u64, update_id: u64,
) -> Result<UpdateStatus, rkv::StoreError> ) -> MResult<UpdateStatus>
{ {
match updates_results_store.update_result(reader, update_id)? { match updates_results_store.update_result(reader, update_id)? {
Some(result) => Ok(UpdateStatus::Processed(result)), Some(result) => Ok(UpdateStatus::Processed(result)),