From adacc7977d34e6494c3a315d4650d678e258a46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 25 Oct 2020 14:49:04 +0100 Subject: [PATCH] Make the Index return default values when value don't exist --- src/index.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/index.rs b/src/index.rs index f00f35ec8..f62a14ec8 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use anyhow::Context; use heed::types::*; use heed::{PolyDatabase, Database}; @@ -47,8 +49,8 @@ impl Index { } /// Returns the internal documents ids. - pub fn documents_ids(&self, rtxn: &heed::RoTxn) -> heed::Result> { - self.main.get::<_, Str, RoaringBitmapCodec>(rtxn, DOCUMENTS_IDS_KEY) + pub fn documents_ids(&self, rtxn: &heed::RoTxn) -> heed::Result { + Ok(self.main.get::<_, Str, RoaringBitmapCodec>(rtxn, DOCUMENTS_IDS_KEY)?.unwrap_or_default()) } /// Writes the users ids documents ids, a user id is a byte slice (i.e. `[u8]`) @@ -59,10 +61,10 @@ impl Index { /// Returns the user ids documents ids map which associate the user ids (i.e. `[u8]`) /// with the internal ids (i.e. `u32`). - pub fn users_ids_documents_ids<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result>> { + pub fn users_ids_documents_ids<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result>> { match self.main.get::<_, Str, ByteSlice>(rtxn, USERS_IDS_DOCUMENTS_IDS_KEY)? { - Some(bytes) => Ok(Some(fst::Map::new(bytes)?)), - None => Ok(None), + Some(bytes) => Ok(fst::Map::new(bytes)?.map_data(Cow::Borrowed)?), + None => Ok(fst::Map::default().map_data(Cow::Owned)?), } } @@ -74,20 +76,20 @@ impl Index { /// Returns the fields ids map which associate the documents keys with an internal field id /// (i.e. `u8`), this field id is used to identify fields in the obkv documents. - pub fn fields_ids_map(&self, rtxn: &heed::RoTxn) -> heed::Result> { - self.main.get::<_, Str, SerdeJson>(rtxn, FIELDS_IDS_MAP_KEY) + pub fn fields_ids_map(&self, rtxn: &heed::RoTxn) -> heed::Result { + Ok(self.main.get::<_, Str, SerdeJson>(rtxn, FIELDS_IDS_MAP_KEY)?.unwrap_or_default()) } /// Writes the FST which is the words dictionnary of the engine. - pub fn put_fst>(&self, wtxn: &mut heed::RwTxn, fst: &fst::Set) -> heed::Result<()> { + pub fn put_words_fst>(&self, wtxn: &mut heed::RwTxn, fst: &fst::Set) -> heed::Result<()> { self.main.put::<_, Str, ByteSlice>(wtxn, WORDS_FST_KEY, fst.as_fst().as_bytes()) } /// Returns the FST which is the words dictionnary of the engine. - pub fn fst<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result>> { + pub fn words_fst<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result>> { match self.main.get::<_, Str, ByteSlice>(rtxn, WORDS_FST_KEY)? { - Some(bytes) => Ok(Some(fst::Set::new(bytes)?)), - None => Ok(None), + Some(bytes) => Ok(fst::Set::new(bytes)?.map_data(Cow::Borrowed)?), + None => Ok(fst::Set::default().map_data(Cow::Owned)?), } } @@ -111,10 +113,7 @@ impl Index { /// Returns the number of documents indexed in the database. pub fn number_of_documents(&self, rtxn: &heed::RoTxn) -> anyhow::Result { - match self.documents_ids(rtxn)? { - Some(docids) => Ok(docids.len() as usize), - None => Ok(0), - } + Ok(self.documents_ids(rtxn).map(|docids| docids.len() as usize)?) } pub fn search<'a>(&'a self, rtxn: &'a heed::RoTxn) -> Search<'a> {