Make the Index return default values when value don't exist

This commit is contained in:
Clément Renault 2020-10-25 14:49:04 +01:00
parent a7a4984175
commit adacc7977d
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use anyhow::Context; use anyhow::Context;
use heed::types::*; use heed::types::*;
use heed::{PolyDatabase, Database}; use heed::{PolyDatabase, Database};
@ -47,8 +49,8 @@ impl Index {
} }
/// Returns the internal documents ids. /// Returns the internal documents ids.
pub fn documents_ids(&self, rtxn: &heed::RoTxn) -> heed::Result<Option<RoaringBitmap>> { pub fn documents_ids(&self, rtxn: &heed::RoTxn) -> heed::Result<RoaringBitmap> {
self.main.get::<_, Str, RoaringBitmapCodec>(rtxn, DOCUMENTS_IDS_KEY) 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]`) /// 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]`) /// Returns the user ids documents ids map which associate the user ids (i.e. `[u8]`)
/// with the internal ids (i.e. `u32`). /// with the internal ids (i.e. `u32`).
pub fn users_ids_documents_ids<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result<Option<fst::Map<&'t [u8]>>> { pub fn users_ids_documents_ids<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result<fst::Map<Cow<'t, [u8]>>> {
match self.main.get::<_, Str, ByteSlice>(rtxn, USERS_IDS_DOCUMENTS_IDS_KEY)? { match self.main.get::<_, Str, ByteSlice>(rtxn, USERS_IDS_DOCUMENTS_IDS_KEY)? {
Some(bytes) => Ok(Some(fst::Map::new(bytes)?)), Some(bytes) => Ok(fst::Map::new(bytes)?.map_data(Cow::Borrowed)?),
None => Ok(None), 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 /// 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. /// (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<Option<FieldsIdsMap>> { pub fn fields_ids_map(&self, rtxn: &heed::RoTxn) -> heed::Result<FieldsIdsMap> {
self.main.get::<_, Str, SerdeJson<FieldsIdsMap>>(rtxn, FIELDS_IDS_MAP_KEY) Ok(self.main.get::<_, Str, SerdeJson<FieldsIdsMap>>(rtxn, FIELDS_IDS_MAP_KEY)?.unwrap_or_default())
} }
/// Writes the FST which is the words dictionnary of the engine. /// Writes the FST which is the words dictionnary of the engine.
pub fn put_fst<A: AsRef<[u8]>>(&self, wtxn: &mut heed::RwTxn, fst: &fst::Set<A>) -> heed::Result<()> { pub fn put_words_fst<A: AsRef<[u8]>>(&self, wtxn: &mut heed::RwTxn, fst: &fst::Set<A>) -> heed::Result<()> {
self.main.put::<_, Str, ByteSlice>(wtxn, WORDS_FST_KEY, fst.as_fst().as_bytes()) 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. /// Returns the FST which is the words dictionnary of the engine.
pub fn fst<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result<Option<fst::Set<&'t [u8]>>> { pub fn words_fst<'t>(&self, rtxn: &'t heed::RoTxn) -> anyhow::Result<fst::Set<Cow<'t, [u8]>>> {
match self.main.get::<_, Str, ByteSlice>(rtxn, WORDS_FST_KEY)? { match self.main.get::<_, Str, ByteSlice>(rtxn, WORDS_FST_KEY)? {
Some(bytes) => Ok(Some(fst::Set::new(bytes)?)), Some(bytes) => Ok(fst::Set::new(bytes)?.map_data(Cow::Borrowed)?),
None => Ok(None), None => Ok(fst::Set::default().map_data(Cow::Owned)?),
} }
} }
@ -111,10 +113,7 @@ impl Index {
/// Returns the number of documents indexed in the database. /// Returns the number of documents indexed in the database.
pub fn number_of_documents(&self, rtxn: &heed::RoTxn) -> anyhow::Result<usize> { pub fn number_of_documents(&self, rtxn: &heed::RoTxn) -> anyhow::Result<usize> {
match self.documents_ids(rtxn)? { Ok(self.documents_ids(rtxn).map(|docids| docids.len() as usize)?)
Some(docids) => Ok(docids.len() as usize),
None => Ok(0),
}
} }
pub fn search<'a>(&'a self, rtxn: &'a heed::RoTxn) -> Search<'a> { pub fn search<'a>(&'a self, rtxn: &'a heed::RoTxn) -> Search<'a> {