meilisearch/meilisearch-lib/src/index_resolver/index_store.rs

109 lines
3.4 KiB
Rust
Raw Normal View History

2021-09-24 17:53:11 +08:00
use std::collections::HashMap;
2022-01-19 18:21:19 +08:00
use std::convert::TryFrom;
2021-09-24 17:53:11 +08:00
use std::path::{Path, PathBuf};
use std::sync::Arc;
2022-01-19 18:21:19 +08:00
use milli::update::IndexerConfig;
2021-09-24 17:53:11 +08:00
use tokio::fs;
use tokio::sync::RwLock;
use tokio::task::spawn_blocking;
use uuid::Uuid;
use super::error::{IndexResolverError, Result};
2021-09-29 04:22:59 +08:00
use crate::index::Index;
2021-09-24 17:53:11 +08:00
use crate::options::IndexerOpts;
type AsyncMap<K, V> = Arc<RwLock<HashMap<K, V>>>;
#[async_trait::async_trait]
2021-10-04 18:15:21 +08:00
#[cfg_attr(test, mockall::automock)]
2021-09-24 17:53:11 +08:00
pub trait IndexStore {
async fn create(&self, uuid: Uuid) -> Result<Index>;
2021-09-24 17:53:11 +08:00
async fn get(&self, uuid: Uuid) -> Result<Option<Index>>;
async fn delete(&self, uuid: Uuid) -> Result<Option<Index>>;
}
pub struct MapIndexStore {
index_store: AsyncMap<Uuid, Index>,
path: PathBuf,
index_size: usize,
2022-01-19 18:21:19 +08:00
indexer_config: Arc<IndexerConfig>,
2021-09-24 17:53:11 +08:00
}
impl MapIndexStore {
2021-09-29 04:22:59 +08:00
pub fn new(
path: impl AsRef<Path>,
index_size: usize,
indexer_opts: &IndexerOpts,
) -> anyhow::Result<Self> {
2022-01-19 18:21:19 +08:00
let indexer_config = Arc::new(IndexerConfig::try_from(indexer_opts)?);
2021-09-24 17:53:11 +08:00
let path = path.as_ref().join("indexes/");
let index_store = Arc::new(RwLock::new(HashMap::new()));
Ok(Self {
index_store,
path,
index_size,
2022-01-19 18:21:19 +08:00
indexer_config,
2021-09-24 17:53:11 +08:00
})
}
}
#[async_trait::async_trait]
impl IndexStore for MapIndexStore {
async fn create(&self, uuid: Uuid) -> Result<Index> {
2021-09-24 17:53:11 +08:00
// We need to keep the lock until we are sure the db file has been opened correclty, to
// ensure that another db is not created at the same time.
let mut lock = self.index_store.write().await;
if let Some(index) = lock.get(&uuid) {
return Ok(index.clone());
}
2021-09-27 22:48:03 +08:00
let path = self.path.join(format!("{}", uuid));
2021-09-24 17:53:11 +08:00
if path.exists() {
2021-10-28 21:42:42 +08:00
return Err(IndexResolverError::UuidAlreadyExists(uuid));
2021-09-24 17:53:11 +08:00
}
let index_size = self.index_size;
2022-01-19 18:21:19 +08:00
let update_handler = self.indexer_config.clone();
2021-09-24 17:53:11 +08:00
let index = spawn_blocking(move || -> Result<Index> {
let index = Index::open(path, index_size, uuid, update_handler)?;
2021-09-24 17:53:11 +08:00
Ok(index)
})
.await??;
lock.insert(uuid, index.clone());
Ok(index)
}
async fn get(&self, uuid: Uuid) -> Result<Option<Index>> {
let guard = self.index_store.read().await;
match guard.get(&uuid) {
Some(index) => Ok(Some(index.clone())),
None => {
// drop the guard here so we can perform the write after without deadlocking;
drop(guard);
2021-09-27 22:48:03 +08:00
let path = self.path.join(format!("{}", uuid));
2021-09-24 17:53:11 +08:00
if !path.exists() {
return Ok(None);
}
let index_size = self.index_size;
2022-01-19 18:21:19 +08:00
let update_handler = self.indexer_config.clone();
let index =
spawn_blocking(move || Index::open(path, index_size, uuid, update_handler))
.await??;
2021-09-24 17:53:11 +08:00
self.index_store.write().await.insert(uuid, index.clone());
Ok(Some(index))
}
}
}
async fn delete(&self, uuid: Uuid) -> Result<Option<Index>> {
2021-09-27 22:48:03 +08:00
let db_path = self.path.join(format!("{}", uuid));
2021-09-24 17:53:11 +08:00
fs::remove_dir_all(db_path).await?;
let index = self.index_store.write().await.remove(&uuid);
Ok(index)
}
}