2022-09-14 12:35:33 +02:00
|
|
|
use std::collections::hash_map::Entry;
|
2022-09-14 12:49:26 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::PathBuf;
|
2022-09-14 12:35:33 +02:00
|
|
|
use std::sync::Arc;
|
2022-09-14 12:49:26 +02:00
|
|
|
use std::sync::RwLock;
|
2022-09-14 12:35:33 +02:00
|
|
|
|
|
|
|
use index::Index;
|
2022-09-14 12:49:26 +02:00
|
|
|
use milli::heed::types::SerdeBincode;
|
|
|
|
use milli::heed::types::Str;
|
|
|
|
use milli::heed::Database;
|
2022-09-14 12:35:33 +02:00
|
|
|
use milli::heed::RoTxn;
|
|
|
|
use milli::heed::RwTxn;
|
2022-09-14 12:49:26 +02:00
|
|
|
use milli::update::IndexerConfig;
|
2022-09-14 12:35:33 +02:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::Error;
|
|
|
|
use crate::Result;
|
|
|
|
|
2022-09-14 12:49:26 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct IndexMapper {
|
|
|
|
// Keep track of the opened indexes and is used
|
|
|
|
// mainly by the index resolver.
|
|
|
|
index_map: Arc<RwLock<HashMap<Uuid, Index>>>,
|
|
|
|
|
|
|
|
// Map an index name with an index uuid currentl available on disk.
|
|
|
|
index_mapping: Database<Str, SerdeBincode<Uuid>>,
|
|
|
|
|
|
|
|
base_path: PathBuf,
|
|
|
|
index_size: usize,
|
|
|
|
indexer_config: Arc<IndexerConfig>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMapper {
|
|
|
|
/// Get or create the index.
|
2022-09-14 12:35:33 +02:00
|
|
|
pub fn create_index(&self, rwtxn: &mut RwTxn, name: &str) -> Result<Index> {
|
2022-09-14 12:49:26 +02:00
|
|
|
let index = match self.index(rwtxn, name) {
|
2022-09-14 12:35:33 +02:00
|
|
|
Ok(index) => index,
|
|
|
|
Err(Error::IndexNotFound(_)) => {
|
|
|
|
let uuid = Uuid::new_v4();
|
2022-09-14 12:49:26 +02:00
|
|
|
Index::open(
|
|
|
|
self.base_path.join(uuid.to_string()),
|
|
|
|
name.to_string(),
|
|
|
|
self.index_size,
|
|
|
|
self.indexer_config.clone(),
|
|
|
|
)?
|
2022-09-14 12:35:33 +02:00
|
|
|
}
|
|
|
|
error => return error,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:49:26 +02:00
|
|
|
/// Return an index, may open it if it wasn't already opened.
|
|
|
|
pub fn index(&self, rtxn: &RoTxn, name: &str) -> Result<Index> {
|
2022-09-14 12:35:33 +02:00
|
|
|
let uuid = self
|
|
|
|
.index_mapping
|
|
|
|
.get(&rtxn, name)?
|
|
|
|
.ok_or(Error::IndexNotFound(name.to_string()))?;
|
|
|
|
|
|
|
|
// we clone here to drop the lock before entering the match
|
|
|
|
let index = self.index_map.read().unwrap().get(&uuid).cloned();
|
|
|
|
let index = match index {
|
|
|
|
Some(index) => index,
|
|
|
|
// since we're lazy, it's possible that the index has not been opened yet.
|
|
|
|
None => {
|
|
|
|
let mut index_map = self.index_map.write().unwrap();
|
|
|
|
// between the read lock and the write lock it's not impossible
|
|
|
|
// that someone already opened the index (eg if two search happens
|
|
|
|
// at the same time), thus before opening it we check a second time
|
|
|
|
// if it's not already there.
|
|
|
|
// Since there is a good chance it's not already there we can use
|
|
|
|
// the entry method.
|
|
|
|
match index_map.entry(uuid) {
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
let index = Index::open(
|
2022-09-14 12:49:26 +02:00
|
|
|
self.base_path.join(uuid.to_string()),
|
2022-09-14 12:35:33 +02:00
|
|
|
name.to_string(),
|
2022-09-14 12:49:26 +02:00
|
|
|
self.index_size,
|
|
|
|
self.indexer_config.clone(),
|
2022-09-14 12:35:33 +02:00
|
|
|
)?;
|
|
|
|
entry.insert(index.clone());
|
|
|
|
index
|
|
|
|
}
|
|
|
|
Entry::Occupied(entry) => entry.get().clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
}
|