2022-09-14 18:35:33 +08:00
|
|
|
use std::collections::hash_map::Entry;
|
2022-09-14 18:49:26 +08:00
|
|
|
use std::collections::HashMap;
|
2022-10-03 22:53:07 +08:00
|
|
|
use std::fs;
|
2022-09-14 18:49:26 +08:00
|
|
|
use std::path::PathBuf;
|
2022-10-03 22:53:07 +08:00
|
|
|
use std::sync::{Arc, RwLock};
|
2022-09-14 18:35:33 +08:00
|
|
|
|
2022-10-03 22:53:07 +08:00
|
|
|
use milli::Index;
|
2022-10-03 22:15:10 +08:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-10-03 22:53:07 +08:00
|
|
|
use milli::heed::types::{SerdeBincode, Str};
|
|
|
|
use milli::heed::{Database, Env, EnvOpenOptions, RoTxn, RwTxn};
|
2022-09-14 18:49:26 +08:00
|
|
|
use milli::update::IndexerConfig;
|
2022-09-14 18:35:33 +08:00
|
|
|
|
2022-10-03 22:15:10 +08:00
|
|
|
use crate::{Error, Result};
|
|
|
|
|
|
|
|
const INDEX_MAPPING: &str = "index-mapping";
|
2022-09-14 18:35:33 +08:00
|
|
|
|
2022-09-14 18:49:26 +08: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 {
|
2022-09-14 22:16:53 +08:00
|
|
|
pub fn new(
|
|
|
|
env: &Env,
|
|
|
|
base_path: PathBuf,
|
|
|
|
index_size: usize,
|
|
|
|
indexer_config: IndexerConfig,
|
|
|
|
) -> Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
index_map: Arc::default(),
|
2022-10-03 22:15:10 +08:00
|
|
|
index_mapping: env.create_database(Some(INDEX_MAPPING))?,
|
2022-09-14 22:16:53 +08:00
|
|
|
base_path,
|
|
|
|
index_size,
|
|
|
|
indexer_config: Arc::new(indexer_config),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-14 18:49:26 +08:00
|
|
|
/// Get or create the index.
|
2022-09-14 22:16:53 +08:00
|
|
|
pub fn create_index(&self, wtxn: &mut RwTxn, name: &str) -> Result<Index> {
|
|
|
|
let index = match self.index(wtxn, name) {
|
2022-09-14 18:35:33 +08:00
|
|
|
Ok(index) => index,
|
|
|
|
Err(Error::IndexNotFound(_)) => {
|
|
|
|
let uuid = Uuid::new_v4();
|
2022-09-21 18:01:46 +08:00
|
|
|
self.index_mapping.put(wtxn, name, &uuid)?;
|
2022-10-03 22:53:07 +08:00
|
|
|
|
|
|
|
let index_path = self.base_path.join(uuid.to_string());
|
|
|
|
fs::create_dir_all(&index_path)?;
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(self.index_size);
|
|
|
|
milli::Index::new(options, &index_path)?
|
2022-09-14 18:35:33 +08:00
|
|
|
}
|
|
|
|
error => return error,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
2022-09-14 18:49:26 +08: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 18:35:33 +08:00
|
|
|
let uuid = self
|
|
|
|
.index_mapping
|
2022-10-03 21:29:37 +08:00
|
|
|
.get(rtxn, name)?
|
|
|
|
.ok_or_else(|| Error::IndexNotFound(name.to_string()))?;
|
2022-09-14 18:35:33 +08:00
|
|
|
|
|
|
|
// 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) => {
|
2022-10-03 22:53:07 +08:00
|
|
|
let index_path = self.base_path.join(uuid.to_string());
|
|
|
|
fs::create_dir_all(&index_path)?;
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(self.index_size);
|
|
|
|
let index = milli::Index::new(options, &index_path)?;
|
|
|
|
|
2022-09-14 18:35:33 +08:00
|
|
|
entry.insert(index.clone());
|
|
|
|
index
|
|
|
|
}
|
|
|
|
Entry::Occupied(entry) => entry.get().clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
2022-09-14 18:58:48 +08:00
|
|
|
|
2022-10-04 17:06:48 +08:00
|
|
|
pub fn indexes(&self, rtxn: &RoTxn) -> Result<Vec<(String, Index)>> {
|
2022-09-21 23:13:09 +08:00
|
|
|
self.index_mapping
|
2022-10-03 21:29:37 +08:00
|
|
|
.iter(rtxn)?
|
2022-09-21 23:13:09 +08:00
|
|
|
.map(|ret| {
|
2022-10-04 17:06:48 +08:00
|
|
|
ret.map_err(Error::from).and_then(|(name, _)| {
|
|
|
|
self.index(rtxn, name)
|
|
|
|
.map(|index| (name.to_string(), index))
|
|
|
|
})
|
2022-09-21 23:13:09 +08:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-09-14 18:58:48 +08:00
|
|
|
/// Swap two index name.
|
|
|
|
pub fn swap(&self, wtxn: &mut RwTxn, lhs: &str, rhs: &str) -> Result<()> {
|
|
|
|
let lhs_uuid = self
|
|
|
|
.index_mapping
|
|
|
|
.get(wtxn, lhs)?
|
2022-10-03 21:29:37 +08:00
|
|
|
.ok_or_else(|| Error::IndexNotFound(lhs.to_string()))?;
|
2022-09-14 18:58:48 +08:00
|
|
|
let rhs_uuid = self
|
|
|
|
.index_mapping
|
|
|
|
.get(wtxn, rhs)?
|
2022-10-03 21:29:37 +08:00
|
|
|
.ok_or_else(|| Error::IndexNotFound(rhs.to_string()))?;
|
2022-09-14 18:58:48 +08:00
|
|
|
|
|
|
|
self.index_mapping.put(wtxn, lhs, &rhs_uuid)?;
|
|
|
|
self.index_mapping.put(wtxn, rhs, &lhs_uuid)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-30 00:15:50 +08:00
|
|
|
|
|
|
|
pub fn indexer_config(&self) -> &IndexerConfig {
|
|
|
|
&self.indexer_config
|
|
|
|
}
|
2022-09-14 18:35:33 +08:00
|
|
|
}
|