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-10-04 18:50:18 +02:00
|
|
|
use std::sync::{Arc, RwLock};
|
2022-10-04 18:19:18 +02:00
|
|
|
use std::{fs, thread};
|
2022-09-14 12:35:33 +02:00
|
|
|
|
2022-10-04 18:19:18 +02:00
|
|
|
use log::error;
|
2022-10-11 17:42:43 +02:00
|
|
|
use meilisearch_types::heed::types::{SerdeBincode, Str};
|
|
|
|
use meilisearch_types::heed::{Database, Env, EnvOpenOptions, RoTxn, RwTxn};
|
|
|
|
use meilisearch_types::milli::update::IndexerConfig;
|
|
|
|
use meilisearch_types::milli::Index;
|
2022-10-03 16:15:10 +02:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-10-04 18:19:18 +02:00
|
|
|
use self::IndexStatus::{Available, BeingDeleted};
|
2022-10-03 16:15:10 +02:00
|
|
|
use crate::{Error, Result};
|
|
|
|
|
|
|
|
const INDEX_MAPPING: &str = "index-mapping";
|
2022-09-14 12:35:33 +02:00
|
|
|
|
2022-10-20 10:25:34 +02:00
|
|
|
/// Structure managing meilisearch's indexes.
|
|
|
|
///
|
|
|
|
/// It is responsible for:
|
|
|
|
/// 1. Creating new indexes
|
|
|
|
/// 2. Opening indexes and storing references to these opened indexes
|
|
|
|
/// 3. Accessing indexes through their uuid
|
|
|
|
/// 4. Mapping a user-defined name to each index uuid.
|
2022-09-14 12:49:26 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct IndexMapper {
|
2022-10-20 10:25:34 +02:00
|
|
|
/// Keep track of the opened indexes. Used mainly by the index resolver.
|
2022-10-04 18:19:18 +02:00
|
|
|
index_map: Arc<RwLock<HashMap<Uuid, IndexStatus>>>,
|
2022-09-14 12:49:26 +02:00
|
|
|
|
2022-10-04 18:19:18 +02:00
|
|
|
// TODO create a UUID Codec that uses the 16 bytes representation
|
2022-10-20 10:25:34 +02:00
|
|
|
/// Map an index name with an index uuid currently available on disk.
|
2022-09-14 12:49:26 +02:00
|
|
|
index_mapping: Database<Str, SerdeBincode<Uuid>>,
|
|
|
|
|
2022-10-20 10:25:34 +02:00
|
|
|
/// Path to the folder where the LMDB environments of each index are.
|
2022-09-14 12:49:26 +02:00
|
|
|
base_path: PathBuf,
|
|
|
|
index_size: usize,
|
2022-10-16 01:39:01 +02:00
|
|
|
pub indexer_config: Arc<IndexerConfig>,
|
2022-09-14 12:49:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 10:25:34 +02:00
|
|
|
/// Whether the index is available for use or is forbidden to be inserted back in the index map
|
2022-10-04 18:19:18 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum IndexStatus {
|
|
|
|
/// Do not insert it back in the index map as it is currently being deleted.
|
|
|
|
BeingDeleted,
|
|
|
|
/// You can use the index without worrying about anything.
|
|
|
|
Available(Index),
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:49:26 +02:00
|
|
|
impl IndexMapper {
|
2022-09-14 16:16:53 +02: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 16:15:10 +02:00
|
|
|
index_mapping: env.create_database(Some(INDEX_MAPPING))?,
|
2022-09-14 16:16:53 +02:00
|
|
|
base_path,
|
|
|
|
index_size,
|
|
|
|
indexer_config: Arc::new(indexer_config),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:49:26 +02:00
|
|
|
/// Get or create the index.
|
2022-09-14 16:16:53 +02:00
|
|
|
pub fn create_index(&self, wtxn: &mut RwTxn, name: &str) -> Result<Index> {
|
2022-10-04 18:19:18 +02:00
|
|
|
match self.index(wtxn, name) {
|
|
|
|
Ok(index) => Ok(index),
|
2022-09-14 12:35:33 +02:00
|
|
|
Err(Error::IndexNotFound(_)) => {
|
|
|
|
let uuid = Uuid::new_v4();
|
2022-09-21 12:01:46 +02:00
|
|
|
self.index_mapping.put(wtxn, name, &uuid)?;
|
2022-10-03 16:53:07 +02: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);
|
2022-10-11 17:42:43 +02:00
|
|
|
Ok(Index::new(options, &index_path)?)
|
2022-09-14 12:35:33 +02:00
|
|
|
}
|
2022-10-04 18:19:18 +02:00
|
|
|
error => error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the index from the mapping table and the in-memory index map
|
|
|
|
/// but keeps the associated tasks.
|
|
|
|
pub fn delete_index(&self, mut wtxn: RwTxn, name: &str) -> Result<()> {
|
|
|
|
let uuid = self
|
|
|
|
.index_mapping
|
|
|
|
.get(&wtxn, name)?
|
|
|
|
.ok_or_else(|| Error::IndexNotFound(name.to_string()))?;
|
|
|
|
|
|
|
|
// Once we retrieved the UUID of the index we remove it from the mapping table.
|
|
|
|
assert!(self.index_mapping.delete(&mut wtxn, name)?);
|
|
|
|
|
|
|
|
wtxn.commit()?;
|
|
|
|
|
|
|
|
// We remove the index from the in-memory index map.
|
|
|
|
let mut lock = self.index_map.write().unwrap();
|
|
|
|
let closing_event = match lock.insert(uuid, BeingDeleted) {
|
|
|
|
Some(Available(index)) => Some(index.prepare_for_closing()),
|
|
|
|
_ => None,
|
2022-09-14 12:35:33 +02:00
|
|
|
};
|
|
|
|
|
2022-10-04 18:19:18 +02:00
|
|
|
drop(lock);
|
|
|
|
|
|
|
|
let index_map = self.index_map.clone();
|
|
|
|
let index_path = self.base_path.join(uuid.to_string());
|
|
|
|
let index_name = name.to_string();
|
|
|
|
thread::spawn(move || {
|
|
|
|
// We first wait to be sure that the previously opened index is effectively closed.
|
|
|
|
// This can take a lot of time, this is why we do that in a seperate thread.
|
|
|
|
if let Some(closing_event) = closing_event {
|
|
|
|
closing_event.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we remove the content from disk.
|
|
|
|
if let Err(e) = fs::remove_dir_all(&index_path) {
|
|
|
|
error!(
|
|
|
|
"An error happened when deleting the index {} ({}): {}",
|
|
|
|
index_name, uuid, e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally we remove the entry from the index map.
|
|
|
|
assert!(matches!(
|
|
|
|
index_map.write().unwrap().remove(&uuid),
|
|
|
|
Some(BeingDeleted)
|
|
|
|
));
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-14 12:35:33 +02:00
|
|
|
}
|
|
|
|
|
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
|
2022-10-03 15:29:37 +02:00
|
|
|
.get(rtxn, name)?
|
|
|
|
.ok_or_else(|| Error::IndexNotFound(name.to_string()))?;
|
2022-09-14 12:35:33 +02: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 {
|
2022-10-04 18:19:18 +02:00
|
|
|
Some(Available(index)) => index,
|
|
|
|
Some(BeingDeleted) => return Err(Error::IndexNotFound(name.to_string())),
|
2022-09-14 12:35:33 +02:00
|
|
|
// 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 16:53:07 +02: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);
|
2022-10-11 17:42:43 +02:00
|
|
|
let index = Index::new(options, &index_path)?;
|
2022-10-04 18:19:18 +02:00
|
|
|
entry.insert(Available(index.clone()));
|
2022-09-14 12:35:33 +02:00
|
|
|
index
|
|
|
|
}
|
2022-10-04 18:19:18 +02:00
|
|
|
Entry::Occupied(entry) => match entry.get() {
|
|
|
|
Available(index) => index.clone(),
|
|
|
|
BeingDeleted => return Err(Error::IndexNotFound(name.to_string())),
|
|
|
|
},
|
2022-09-14 12:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
2022-09-14 12:58:48 +02:00
|
|
|
|
2022-10-20 10:25:34 +02:00
|
|
|
/// Return all indexes, may open them if they weren't already opened.
|
2022-10-04 11:06:48 +02:00
|
|
|
pub fn indexes(&self, rtxn: &RoTxn) -> Result<Vec<(String, Index)>> {
|
2022-09-21 17:13:09 +02:00
|
|
|
self.index_mapping
|
2022-10-03 15:29:37 +02:00
|
|
|
.iter(rtxn)?
|
2022-09-21 17:13:09 +02:00
|
|
|
.map(|ret| {
|
2022-10-04 11:06:48 +02:00
|
|
|
ret.map_err(Error::from).and_then(|(name, _)| {
|
|
|
|
self.index(rtxn, name)
|
|
|
|
.map(|index| (name.to_string(), index))
|
|
|
|
})
|
2022-09-21 17:13:09 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:58:48 +02: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 15:29:37 +02:00
|
|
|
.ok_or_else(|| Error::IndexNotFound(lhs.to_string()))?;
|
2022-09-14 12:58:48 +02:00
|
|
|
let rhs_uuid = self
|
|
|
|
.index_mapping
|
|
|
|
.get(wtxn, rhs)?
|
2022-10-03 15:29:37 +02:00
|
|
|
.ok_or_else(|| Error::IndexNotFound(rhs.to_string()))?;
|
2022-09-14 12:58:48 +02:00
|
|
|
|
|
|
|
self.index_mapping.put(wtxn, lhs, &rhs_uuid)?;
|
|
|
|
self.index_mapping.put(wtxn, rhs, &lhs_uuid)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-29 18:15:50 +02:00
|
|
|
|
|
|
|
pub fn indexer_config(&self) -> &IndexerConfig {
|
|
|
|
&self.indexer_config
|
|
|
|
}
|
2022-09-14 12:35:33 +02:00
|
|
|
}
|