MeiliSearch/milli/src/search/new/db_cache.rs

175 lines
5.9 KiB
Rust
Raw Normal View History

2023-04-04 15:01:42 +02:00
use std::borrow::Cow;
2023-03-08 09:55:53 +01:00
use std::collections::hash_map::Entry;
2023-03-08 13:26:29 +01:00
use std::hash::Hash;
2023-03-08 09:55:53 +01:00
use fxhash::FxHashMap;
2023-02-21 13:57:34 +01:00
use heed::types::ByteSlice;
2023-03-08 13:26:29 +01:00
use heed::{BytesEncode, Database, RoTxn};
2023-03-08 09:55:53 +01:00
2023-03-30 11:08:17 +02:00
use super::interner::Interned;
use crate::{Result, SearchContext};
2023-03-08 13:26:29 +01:00
/// A cache storing pointers to values in the LMDB databases.
///
/// Used for performance reasons only. By using this cache, we avoid performing a
/// database lookup and instead get a direct reference to the value using a fast
/// local HashMap lookup.
#[derive(Default)]
2023-03-13 14:03:48 +01:00
pub struct DatabaseCache<'ctx> {
pub word_pair_proximity_docids:
2023-03-13 14:03:48 +01:00
FxHashMap<(u8, Interned<String>, Interned<String>), Option<&'ctx [u8]>>,
pub word_prefix_pair_proximity_docids:
2023-03-13 14:03:48 +01:00
FxHashMap<(u8, Interned<String>, Interned<String>), Option<&'ctx [u8]>>,
pub prefix_word_pair_proximity_docids:
2023-03-13 14:03:48 +01:00
FxHashMap<(u8, Interned<String>, Interned<String>), Option<&'ctx [u8]>>,
pub word_docids: FxHashMap<Interned<String>, Option<&'ctx [u8]>>,
pub exact_word_docids: FxHashMap<Interned<String>, Option<&'ctx [u8]>>,
pub word_prefix_docids: FxHashMap<Interned<String>, Option<&'ctx [u8]>>,
2023-04-04 15:01:42 +02:00
pub words_fst: Option<fst::Set<Cow<'ctx, [u8]>>>,
2023-04-04 17:04:46 +02:00
pub word_position_docids: FxHashMap<(Interned<String>, u16), Option<&'ctx [u8]>>,
pub word_fid_docids: FxHashMap<(Interned<String>, u16), Option<&'ctx [u8]>>,
}
2023-03-13 14:03:48 +01:00
impl<'ctx> DatabaseCache<'ctx> {
2023-03-08 13:26:29 +01:00
fn get_value<'v, K1, KC>(
2023-03-13 14:03:48 +01:00
txn: &'ctx RoTxn,
2023-03-08 13:26:29 +01:00
cache_key: K1,
db_key: &'v KC::EItem,
2023-03-13 14:03:48 +01:00
cache: &mut FxHashMap<K1, Option<&'ctx [u8]>>,
2023-03-08 13:26:29 +01:00
db: Database<KC, ByteSlice>,
2023-03-13 14:03:48 +01:00
) -> Result<Option<&'ctx [u8]>>
2023-03-08 13:26:29 +01:00
where
K1: Copy + Eq + Hash,
KC: BytesEncode<'v>,
{
let bitmap_ptr = match cache.entry(cache_key) {
Entry::Occupied(bitmap_ptr) => *bitmap_ptr.get(),
Entry::Vacant(entry) => {
2023-03-08 13:26:29 +01:00
let bitmap_ptr = db.get(txn, db_key)?;
entry.insert(bitmap_ptr);
bitmap_ptr
}
};
Ok(bitmap_ptr)
}
2023-03-30 11:08:17 +02:00
}
impl<'ctx> SearchContext<'ctx> {
2023-04-04 15:01:42 +02:00
pub fn get_words_fst(&mut self) -> Result<fst::Set<Cow<'ctx, [u8]>>> {
if let Some(fst) = self.db_cache.words_fst.clone() {
Ok(fst)
} else {
let fst = self.index.words_fst(self.txn)?;
self.db_cache.words_fst = Some(fst.clone());
Ok(fst)
}
}
2023-03-08 13:26:29 +01:00
/// Retrieve or insert the given value in the `word_docids` database.
2023-03-30 11:08:17 +02:00
pub fn get_db_word_docids(&mut self, word: Interned<String>) -> Result<Option<&'ctx [u8]>> {
DatabaseCache::get_value(
self.txn,
2023-03-08 13:26:29 +01:00
word,
2023-03-30 11:08:17 +02:00
self.word_interner.get(word).as_str(),
&mut self.db_cache.word_docids,
self.index.word_docids.remap_data_type::<ByteSlice>(),
2023-03-08 13:26:29 +01:00
)
}
/// Retrieve or insert the given value in the `word_prefix_docids` database.
2023-03-30 11:08:17 +02:00
pub fn get_db_word_prefix_docids(
2023-03-08 13:26:29 +01:00
&mut self,
prefix: Interned<String>,
2023-03-13 14:03:48 +01:00
) -> Result<Option<&'ctx [u8]>> {
2023-03-30 11:08:17 +02:00
DatabaseCache::get_value(
self.txn,
2023-03-08 13:26:29 +01:00
prefix,
2023-03-30 11:08:17 +02:00
self.word_interner.get(prefix).as_str(),
&mut self.db_cache.word_prefix_docids,
self.index.word_prefix_docids.remap_data_type::<ByteSlice>(),
2023-03-08 13:26:29 +01:00
)
}
2023-03-30 11:08:17 +02:00
pub fn get_db_word_pair_proximity_docids(
&mut self,
word1: Interned<String>,
word2: Interned<String>,
proximity: u8,
2023-03-13 14:03:48 +01:00
) -> Result<Option<&'ctx [u8]>> {
2023-03-30 11:08:17 +02:00
DatabaseCache::get_value(
self.txn,
2023-03-08 13:26:29 +01:00
(proximity, word1, word2),
2023-03-30 11:08:17 +02:00
&(
proximity,
self.word_interner.get(word1).as_str(),
self.word_interner.get(word2).as_str(),
),
&mut self.db_cache.word_pair_proximity_docids,
self.index.word_pair_proximity_docids.remap_data_type::<ByteSlice>(),
2023-03-08 13:26:29 +01:00
)
}
2023-03-30 11:08:17 +02:00
pub fn get_db_word_prefix_pair_proximity_docids(
&mut self,
word1: Interned<String>,
prefix2: Interned<String>,
proximity: u8,
2023-03-13 14:03:48 +01:00
) -> Result<Option<&'ctx [u8]>> {
2023-03-30 11:08:17 +02:00
DatabaseCache::get_value(
self.txn,
2023-03-08 13:26:29 +01:00
(proximity, word1, prefix2),
2023-03-30 11:08:17 +02:00
&(
proximity,
self.word_interner.get(word1).as_str(),
self.word_interner.get(prefix2).as_str(),
),
&mut self.db_cache.word_prefix_pair_proximity_docids,
self.index.word_prefix_pair_proximity_docids.remap_data_type::<ByteSlice>(),
2023-03-08 13:26:29 +01:00
)
}
2023-03-30 11:08:17 +02:00
pub fn get_db_prefix_word_pair_proximity_docids(
&mut self,
left_prefix: Interned<String>,
right: Interned<String>,
proximity: u8,
2023-03-13 14:03:48 +01:00
) -> Result<Option<&'ctx [u8]>> {
2023-03-30 11:08:17 +02:00
DatabaseCache::get_value(
self.txn,
2023-03-08 13:26:29 +01:00
(proximity, left_prefix, right),
&(
proximity,
2023-03-30 11:08:17 +02:00
self.word_interner.get(left_prefix).as_str(),
self.word_interner.get(right).as_str(),
2023-03-08 13:26:29 +01:00
),
2023-03-30 11:08:17 +02:00
&mut self.db_cache.prefix_word_pair_proximity_docids,
self.index.prefix_word_pair_proximity_docids.remap_data_type::<ByteSlice>(),
2023-03-08 13:26:29 +01:00
)
}
2023-04-04 17:04:46 +02:00
pub fn get_db_word_position_docids(
&mut self,
word: Interned<String>,
position: u16,
) -> Result<Option<&'ctx [u8]>> {
DatabaseCache::get_value(
self.txn,
(word, position),
&(self.word_interner.get(word).as_str(), position),
&mut self.db_cache.word_position_docids,
self.index.word_position_docids.remap_data_type::<ByteSlice>(),
)
}
pub fn get_db_word_fid_docids(
&mut self,
word: Interned<String>,
fid: u16,
) -> Result<Option<&'ctx [u8]>> {
DatabaseCache::get_value(
self.txn,
(word, fid),
&(self.word_interner.get(word).as_str(), fid),
&mut self.db_cache.word_fid_docids,
self.index.word_fid_docids.remap_data_type::<ByteSlice>(),
)
}
}