MeiliSearch/meilisearch-http/src/index_controller/local_index_controller/index_store.rs

608 lines
21 KiB
Rust
Raw Normal View History

2021-02-01 19:51:47 +01:00
use std::fs::{create_dir_all, remove_dir_all};
2021-01-28 14:12:34 +01:00
use std::path::{Path, PathBuf};
use std::sync::Arc;
2021-02-15 23:37:08 +01:00
use std::time::Duration;
2021-01-28 14:12:34 +01:00
2021-02-08 10:47:34 +01:00
use anyhow::{bail, Context};
2021-02-03 17:44:20 +01:00
use chrono::{DateTime, Utc};
2021-02-15 23:37:08 +01:00
use dashmap::{mapref::entry::Entry, DashMap};
use heed::{
types::{ByteSlice, SerdeJson, Str},
Database, Env, EnvOpenOptions, RoTxn, RwTxn,
};
2021-02-15 10:53:21 +01:00
use log::{error, info};
2021-01-28 14:12:34 +01:00
use milli::Index;
use rayon::ThreadPool;
2021-02-15 23:37:08 +01:00
use serde::{Deserialize, Serialize};
2021-02-01 19:51:47 +01:00
use uuid::Uuid;
2021-01-28 14:12:34 +01:00
2021-01-28 20:55:29 +01:00
use super::update_handler::UpdateHandler;
use super::{UpdateMeta, UpdateResult};
2021-02-15 23:37:08 +01:00
use crate::option::IndexerOpts;
2021-01-28 20:55:29 +01:00
type UpdateStore = super::update_store::UpdateStore<UpdateMeta, UpdateResult, String>;
2021-01-28 14:12:34 +01:00
2021-01-29 19:14:23 +01:00
#[derive(Serialize, Deserialize, Debug, PartialEq)]
2021-02-03 17:44:20 +01:00
pub struct IndexMeta {
2021-02-04 15:09:43 +01:00
update_store_size: u64,
index_store_size: u64,
2021-02-03 17:44:20 +01:00
pub uuid: Uuid,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
2021-01-28 14:12:34 +01:00
}
impl IndexMeta {
fn open(
&self,
path: impl AsRef<Path>,
thread_pool: Arc<ThreadPool>,
2021-02-01 19:51:47 +01:00
indexer_options: &IndexerOpts,
2021-01-28 14:12:34 +01:00
) -> anyhow::Result<(Arc<Index>, Arc<UpdateStore>)> {
2021-01-29 19:14:23 +01:00
let update_path = make_update_db_path(&path, &self.uuid);
let index_path = make_index_db_path(&path, &self.uuid);
2021-01-28 14:12:34 +01:00
2021-01-28 15:14:48 +01:00
create_dir_all(&update_path)?;
create_dir_all(&index_path)?;
2021-01-28 14:12:34 +01:00
let mut options = EnvOpenOptions::new();
2021-02-04 13:21:15 +01:00
options.map_size(self.index_store_size as usize);
2021-01-28 14:12:34 +01:00
let index = Arc::new(Index::new(options, index_path)?);
let mut options = EnvOpenOptions::new();
2021-02-04 13:21:15 +01:00
options.map_size(self.update_store_size as usize);
2021-02-01 19:51:47 +01:00
let handler = UpdateHandler::new(indexer_options, index.clone(), thread_pool)?;
2021-01-28 14:12:34 +01:00
let update_store = UpdateStore::open(options, update_path, handler)?;
2021-02-01 19:51:47 +01:00
2021-01-28 14:12:34 +01:00
Ok((index, update_store))
}
}
pub struct IndexStore {
env: Env,
2021-02-04 15:09:43 +01:00
name_to_uuid: Database<Str, ByteSlice>,
2021-02-01 19:51:47 +01:00
uuid_to_index: DashMap<Uuid, (Arc<Index>, Arc<UpdateStore>)>,
2021-02-04 15:09:43 +01:00
uuid_to_index_meta: Database<ByteSlice, SerdeJson<IndexMeta>>,
2021-01-28 14:12:34 +01:00
thread_pool: Arc<ThreadPool>,
2021-02-01 19:51:47 +01:00
indexer_options: IndexerOpts,
2021-01-28 14:12:34 +01:00
}
impl IndexStore {
2021-02-01 19:51:47 +01:00
pub fn new(path: impl AsRef<Path>, indexer_options: IndexerOpts) -> anyhow::Result<Self> {
2021-01-28 14:12:34 +01:00
let env = EnvOpenOptions::new()
.map_size(4096 * 100)
.max_dbs(2)
.open(path)?;
2021-02-04 15:09:43 +01:00
let uuid_to_index = DashMap::new();
let name_to_uuid = open_or_create_database(&env, Some("name_to_uid"))?;
let uuid_to_index_meta = open_or_create_database(&env, Some("uid_to_index_db"))?;
2021-01-28 14:12:34 +01:00
let thread_pool = rayon::ThreadPoolBuilder::new()
2021-02-01 19:51:47 +01:00
.num_threads(indexer_options.indexing_jobs.unwrap_or(0))
2021-01-28 14:12:34 +01:00
.build()?;
let thread_pool = Arc::new(thread_pool);
Ok(Self {
env,
2021-02-04 15:09:43 +01:00
name_to_uuid,
uuid_to_index,
uuid_to_index_meta,
2021-01-28 14:12:34 +01:00
thread_pool,
2021-02-01 19:51:47 +01:00
indexer_options,
2021-01-28 14:12:34 +01:00
})
}
2021-02-15 10:53:21 +01:00
pub fn delete(&self, index_uid: impl AsRef<str>) -> anyhow::Result<()> {
// we remove the references to the index from the index map so it is not accessible anymore
let mut txn = self.env.write_txn()?;
2021-02-15 23:37:08 +01:00
let uuid = self
.index_uuid(&txn, &index_uid)?
2021-02-15 10:53:21 +01:00
.with_context(|| format!("Index {:?} doesn't exist", index_uid.as_ref()))?;
self.name_to_uuid.delete(&mut txn, index_uid.as_ref())?;
self.uuid_to_index_meta.delete(&mut txn, uuid.as_bytes())?;
txn.commit()?;
2021-02-15 23:32:38 +01:00
// If the index was loaded (i.e it is present in the uuid_to_index map), then we need to
// close it. The process goes as follow:
2021-02-15 10:53:21 +01:00
//
2021-02-15 23:32:38 +01:00
// 1) We want to remove any pending updates from the store.
// 2) We try to get ownership on the update store so we can close it. It may take a
2021-02-15 10:53:21 +01:00
// couple of tries, but since the update store event loop only has a weak reference to
// itself, and we are the only other function holding a reference to it otherwise, we will
// get it eventually.
2021-02-15 23:32:38 +01:00
// 3) We request a closing of the update store.
// 4) We can take ownership on the index, and close it.
// 5) We remove all the files from the file system.
2021-02-15 10:53:21 +01:00
let index_uid = index_uid.as_ref().to_string();
2021-02-15 23:32:38 +01:00
let path = self.env.path().to_owned();
2021-02-15 10:53:21 +01:00
if let Some((_, (index, updates))) = self.uuid_to_index.remove(&uuid) {
std::thread::spawn(move || {
info!("Preparing for {:?} deletion.", index_uid);
// this error is non fatal, but may delay the deletion.
if let Err(e) = updates.abort_pendings() {
error!(
"error aborting pending updates when deleting index {:?}: {}",
2021-02-15 23:37:08 +01:00
index_uid, e
2021-02-15 10:53:21 +01:00
);
}
let updates = get_arc_ownership_blocking(updates);
let close_event = updates.prepare_for_closing();
close_event.wait();
info!("closed update store for {:?}", index_uid);
let index = get_arc_ownership_blocking(index);
let close_event = index.prepare_for_closing();
close_event.wait();
2021-02-15 23:32:38 +01:00
let update_path = make_update_db_path(&path, &uuid);
let index_path = make_index_db_path(&path, &uuid);
if let Err(e) = remove_dir_all(index_path) {
error!("error removing index {:?}: {}", index_uid, e);
}
if let Err(e) = remove_dir_all(update_path) {
error!("error removing index {:?}: {}", index_uid, e);
}
2021-02-15 10:53:21 +01:00
info!("index {:?} deleted.", index_uid);
});
}
Ok(())
}
2021-02-01 19:51:47 +01:00
fn index_uuid(&self, txn: &RoTxn, name: impl AsRef<str>) -> anyhow::Result<Option<Uuid>> {
2021-02-04 15:09:43 +01:00
match self.name_to_uuid.get(txn, name.as_ref())? {
2021-01-29 19:14:23 +01:00
Some(bytes) => {
let uuid = Uuid::from_slice(bytes)?;
Ok(Some(uuid))
2021-01-28 14:12:34 +01:00
}
2021-02-15 23:37:08 +01:00
None => Ok(None),
2021-01-28 14:12:34 +01:00
}
}
2021-02-15 23:37:08 +01:00
fn retrieve_index(
&self,
txn: &RoTxn,
uid: Uuid,
) -> anyhow::Result<Option<(Arc<Index>, Arc<UpdateStore>)>> {
2021-02-01 19:51:47 +01:00
match self.uuid_to_index.entry(uid.clone()) {
2021-02-15 23:37:08 +01:00
Entry::Vacant(entry) => match self.uuid_to_index_meta.get(txn, uid.as_bytes())? {
Some(meta) => {
let path = self.env.path();
let (index, updates) =
meta.open(path, self.thread_pool.clone(), &self.indexer_options)?;
entry.insert((index.clone(), updates.clone()));
Ok(Some((index, updates)))
2021-01-28 14:12:34 +01:00
}
2021-02-15 23:37:08 +01:00
None => Ok(None),
},
2021-01-28 14:12:34 +01:00
Entry::Occupied(entry) => {
let (index, updates) = entry.get();
Ok(Some((index.clone(), updates.clone())))
}
}
}
2021-02-15 23:37:08 +01:00
fn get_index_txn(
&self,
txn: &RoTxn,
name: impl AsRef<str>,
) -> anyhow::Result<Option<(Arc<Index>, Arc<UpdateStore>)>> {
2021-02-01 19:51:47 +01:00
match self.index_uuid(&txn, name)? {
2021-01-28 14:12:34 +01:00
Some(uid) => self.retrieve_index(&txn, uid),
None => Ok(None),
}
}
2021-02-15 23:37:08 +01:00
pub fn index(
&self,
name: impl AsRef<str>,
) -> anyhow::Result<Option<(Arc<Index>, Arc<UpdateStore>)>> {
2021-01-28 14:12:34 +01:00
let txn = self.env.read_txn()?;
2021-02-04 13:21:15 +01:00
self.get_index_txn(&txn, name)
2021-01-28 14:12:34 +01:00
}
/// Use this function to perform an update on an index.
2021-02-09 16:08:13 +01:00
/// This function also puts a lock on what index is allowed to perform an update.
pub fn update_index<F, T>(&self, name: impl AsRef<str>, f: F) -> anyhow::Result<(T, IndexMeta)>
where
F: FnOnce(&Index) -> anyhow::Result<T>,
{
let mut txn = self.env.write_txn()?;
2021-02-15 23:37:08 +01:00
let (index, _) = self
.get_index_txn(&txn, &name)?
.with_context(|| format!("Index {:?} doesn't exist", name.as_ref()))?;
let result = f(index.as_ref());
match result {
Ok(ret) => {
let meta = self.update_meta(&mut txn, name, |meta| meta.updated_at = Utc::now())?;
txn.commit()?;
Ok((ret, meta))
}
2021-02-15 23:37:08 +01:00
Err(e) => Err(e),
}
}
2021-02-15 23:37:08 +01:00
pub fn index_with_meta(
&self,
name: impl AsRef<str>,
) -> anyhow::Result<Option<(Arc<Index>, IndexMeta)>> {
let txn = self.env.read_txn()?;
let uuid = self.index_uuid(&txn, &name)?;
match uuid {
Some(uuid) => {
2021-02-15 23:37:08 +01:00
let meta = self
.uuid_to_index_meta
.get(&txn, uuid.as_bytes())?
.with_context(|| {
format!("unable to retrieve metadata for index {:?}", name.as_ref())
})?;
let (index, _) = self
.retrieve_index(&txn, uuid)?
.with_context(|| format!("unable to retrieve index {:?}", name.as_ref()))?;
Ok(Some((index, meta)))
}
None => Ok(None),
}
}
2021-02-15 23:37:08 +01:00
fn update_meta<F>(
&self,
txn: &mut RwTxn,
name: impl AsRef<str>,
f: F,
) -> anyhow::Result<IndexMeta>
2021-02-09 16:08:13 +01:00
where
2021-02-15 23:37:08 +01:00
F: FnOnce(&mut IndexMeta),
2021-02-09 16:08:13 +01:00
{
2021-02-15 23:37:08 +01:00
let uuid = self
.index_uuid(txn, &name)?
.with_context(|| format!("Index {:?} doesn't exist", name.as_ref()))?;
let mut meta = self
.uuid_to_index_meta
.get(txn, uuid.as_bytes())?
.with_context(|| format!("couldn't retrieve metadata for index {:?}", name.as_ref()))?;
f(&mut meta);
self.uuid_to_index_meta.put(txn, uuid.as_bytes(), &meta)?;
Ok(meta)
}
2021-01-28 14:12:34 +01:00
pub fn get_or_create_index(
2021-02-01 19:51:47 +01:00
&self,
name: impl AsRef<str>,
2021-01-28 15:14:48 +01:00
update_size: u64,
index_size: u64,
2021-02-01 19:51:47 +01:00
) -> anyhow::Result<(Arc<Index>, Arc<UpdateStore>)> {
2021-01-28 14:12:34 +01:00
let mut txn = self.env.write_txn()?;
2021-02-04 13:21:15 +01:00
match self.get_index_txn(&txn, name.as_ref())? {
2021-01-28 14:12:34 +01:00
Some(res) => Ok(res),
None => {
2021-02-01 19:51:47 +01:00
let uuid = Uuid::new_v4();
2021-02-15 23:37:08 +01:00
let (index, updates, _) =
self.create_index_txn(&mut txn, uuid, name, update_size, index_size)?;
2021-02-01 19:51:47 +01:00
// If we fail to commit the transaction, we must delete the database from the
// file-system.
if let Err(e) = txn.commit() {
self.clean_db(uuid);
return Err(e)?;
2021-01-28 15:14:48 +01:00
}
2021-02-08 10:47:34 +01:00
Ok((index, updates))
2021-02-15 23:37:08 +01:00
}
2021-01-28 14:12:34 +01:00
}
}
2021-02-01 19:51:47 +01:00
// Remove all the files and data associated with a db uuid.
fn clean_db(&self, uuid: Uuid) {
let update_db_path = make_update_db_path(self.env.path(), &uuid);
let index_db_path = make_index_db_path(self.env.path(), &uuid);
remove_dir_all(update_db_path).expect("Failed to clean database");
remove_dir_all(index_db_path).expect("Failed to clean database");
self.uuid_to_index.remove(&uuid);
2021-01-28 15:14:48 +01:00
}
2021-02-15 23:37:08 +01:00
fn create_index_txn(
&self,
2021-01-28 14:12:34 +01:00
txn: &mut RwTxn,
2021-02-01 19:51:47 +01:00
uuid: Uuid,
2021-01-28 14:12:34 +01:00
name: impl AsRef<str>,
2021-02-04 15:09:43 +01:00
update_store_size: u64,
index_store_size: u64,
2021-02-08 10:47:34 +01:00
) -> anyhow::Result<(Arc<Index>, Arc<UpdateStore>, IndexMeta)> {
2021-02-03 17:44:20 +01:00
let created_at = Utc::now();
let updated_at = created_at;
let meta = IndexMeta {
update_store_size,
index_store_size,
uuid: uuid.clone(),
created_at,
updated_at,
};
2021-01-28 14:12:34 +01:00
2021-02-04 15:09:43 +01:00
self.name_to_uuid.put(txn, name.as_ref(), uuid.as_bytes())?;
self.uuid_to_index_meta.put(txn, uuid.as_bytes(), &meta)?;
2021-01-28 14:12:34 +01:00
let path = self.env.path();
2021-02-15 23:37:08 +01:00
let (index, update_store) =
match meta.open(path, self.thread_pool.clone(), &self.indexer_options) {
Ok(res) => res,
Err(e) => {
self.clean_db(uuid);
return Err(e);
}
};
2021-01-28 14:12:34 +01:00
2021-02-15 23:37:08 +01:00
self.uuid_to_index
.insert(uuid, (index.clone(), update_store.clone()));
2021-01-28 14:12:34 +01:00
2021-02-08 10:47:34 +01:00
Ok((index, update_store, meta))
2021-01-28 14:12:34 +01:00
}
2021-02-03 17:44:20 +01:00
2021-02-04 15:28:52 +01:00
/// Same as `get_or_create`, but returns an error if the index already exists.
2021-02-03 20:12:48 +01:00
pub fn create_index(
&self,
name: impl AsRef<str>,
update_size: u64,
index_size: u64,
2021-02-08 10:47:34 +01:00
) -> anyhow::Result<(Arc<Index>, Arc<UpdateStore>, IndexMeta)> {
2021-02-03 20:12:48 +01:00
let uuid = Uuid::new_v4();
let mut txn = self.env.write_txn()?;
2021-02-04 15:09:43 +01:00
if self.name_to_uuid.get(&txn, name.as_ref())?.is_some() {
2021-02-09 13:25:20 +01:00
bail!("index {:?} already exists", name.as_ref())
2021-02-03 20:12:48 +01:00
}
let result = self.create_index_txn(&mut txn, uuid, name, update_size, index_size)?;
// If we fail to commit the transaction, we must delete the database from the
// file-system.
if let Err(e) = txn.commit() {
self.clean_db(uuid);
return Err(e)?;
}
Ok(result)
}
2021-02-08 10:47:34 +01:00
/// Returns each index associated with its metadata:
/// (index_name, IndexMeta, primary_key)
/// This method will force all the indexes to be loaded.
pub fn list_indexes(&self) -> anyhow::Result<Vec<(String, IndexMeta, Option<String>)>> {
2021-02-03 17:44:20 +01:00
let txn = self.env.read_txn()?;
2021-02-15 23:37:08 +01:00
let metas = self.name_to_uuid.iter(&txn)?.filter_map(|entry| {
entry
.map_err(|e| {
error!("error decoding entry while listing indexes: {}", e);
e
})
.ok()
});
2021-02-04 15:28:52 +01:00
let mut indexes = Vec::new();
for (name, uuid) in metas {
2021-02-08 10:47:34 +01:00
// get index to retrieve primary key
2021-02-15 23:37:08 +01:00
let (index, _) = self
.get_index_txn(&txn, name)?
2021-02-08 10:47:34 +01:00
.with_context(|| format!("could not load index {:?}", name))?;
2021-02-15 23:37:08 +01:00
let primary_key = index.primary_key(&index.read_txn()?)?.map(String::from);
2021-02-08 10:47:34 +01:00
// retieve meta
2021-02-15 23:37:08 +01:00
let meta = self
.uuid_to_index_meta
2021-02-04 15:28:52 +01:00
.get(&txn, &uuid)?
2021-02-08 10:47:34 +01:00
.with_context(|| format!("could not retieve meta for index {:?}", name))?;
indexes.push((name.to_owned(), meta, primary_key));
2021-02-04 15:28:52 +01:00
}
2021-02-03 17:44:20 +01:00
Ok(indexes)
}
2021-01-28 14:12:34 +01:00
}
2021-02-15 10:53:21 +01:00
// Loops on an arc to get ownership on the wrapped value. This method sleeps 100ms before retrying.
fn get_arc_ownership_blocking<T>(mut item: Arc<T>) -> T {
loop {
match Arc::try_unwrap(item) {
Ok(item) => return item,
Err(item_arc) => {
item = item_arc;
std::thread::sleep(Duration::from_millis(100));
continue;
}
}
}
}
2021-02-15 23:37:08 +01:00
fn open_or_create_database<K: 'static, V: 'static>(
env: &Env,
name: Option<&str>,
) -> anyhow::Result<Database<K, V>> {
2021-01-28 14:12:34 +01:00
match env.open_database::<K, V>(name)? {
Some(db) => Ok(db),
None => Ok(env.create_database::<K, V>(name)?),
}
}
2021-02-01 19:51:47 +01:00
fn make_update_db_path(path: impl AsRef<Path>, uuid: &Uuid) -> PathBuf {
2021-01-28 14:12:34 +01:00
let mut path = path.as_ref().to_path_buf();
2021-02-01 19:51:47 +01:00
path.push(format!("update{}", uuid));
2021-01-28 14:12:34 +01:00
path
}
2021-02-01 19:51:47 +01:00
fn make_index_db_path(path: impl AsRef<Path>, uuid: &Uuid) -> PathBuf {
2021-01-28 14:12:34 +01:00
let mut path = path.as_ref().to_path_buf();
2021-02-01 19:51:47 +01:00
path.push(format!("index{}", uuid));
2021-01-28 14:12:34 +01:00
path
}
2021-01-29 19:14:23 +01:00
#[cfg(test)]
mod test {
use super::*;
use std::path::PathBuf;
#[test]
fn test_make_update_db_path() {
let uuid = Uuid::new_v4();
assert_eq!(
make_update_db_path("/home", &uuid),
PathBuf::from(format!("/home/update{}", uuid))
);
}
#[test]
fn test_make_index_db_path() {
let uuid = Uuid::new_v4();
assert_eq!(
make_index_db_path("/home", &uuid),
PathBuf::from(format!("/home/index{}", uuid))
);
}
mod index_store {
use super::*;
#[test]
fn test_index_uuid() {
let temp = tempfile::tempdir().unwrap();
let store = IndexStore::new(temp, IndexerOpts::default()).unwrap();
let name = "foobar";
let txn = store.env.read_txn().unwrap();
// name is not found if the uuid in not present in the db
2021-02-01 19:51:47 +01:00
assert!(store.index_uuid(&txn, &name).unwrap().is_none());
2021-01-29 19:14:23 +01:00
drop(txn);
// insert an uuid in the the name_to_uuid_db:
let uuid = Uuid::new_v4();
let mut txn = store.env.write_txn().unwrap();
2021-02-15 23:37:08 +01:00
store
.name_to_uuid
.put(&mut txn, &name, uuid.as_bytes())
.unwrap();
2021-01-29 19:14:23 +01:00
txn.commit().unwrap();
// check that the uuid is there
let txn = store.env.read_txn().unwrap();
2021-02-01 19:51:47 +01:00
assert_eq!(store.index_uuid(&txn, &name).unwrap(), Some(uuid));
2021-01-29 19:14:23 +01:00
}
#[test]
fn test_retrieve_index() {
let temp = tempfile::tempdir().unwrap();
let store = IndexStore::new(temp, IndexerOpts::default()).unwrap();
let uuid = Uuid::new_v4();
let txn = store.env.read_txn().unwrap();
assert!(store.retrieve_index(&txn, uuid).unwrap().is_none());
let created_at = Utc::now();
let updated_at = created_at;
2021-02-03 17:44:20 +01:00
let meta = IndexMeta {
2021-02-04 15:09:43 +01:00
update_store_size: 4096 * 100,
index_store_size: 4096 * 100,
2021-02-03 17:44:20 +01:00
uuid: uuid.clone(),
created_at,
updated_at,
2021-02-03 17:44:20 +01:00
};
2021-01-29 19:14:23 +01:00
let mut txn = store.env.write_txn().unwrap();
2021-02-15 23:37:08 +01:00
store
.uuid_to_index_meta
.put(&mut txn, uuid.as_bytes(), &meta)
.unwrap();
2021-01-29 19:14:23 +01:00
txn.commit().unwrap();
// the index cache should be empty
2021-02-01 19:51:47 +01:00
assert!(store.uuid_to_index.is_empty());
2021-01-29 19:14:23 +01:00
let txn = store.env.read_txn().unwrap();
assert!(store.retrieve_index(&txn, uuid).unwrap().is_some());
2021-02-01 19:51:47 +01:00
assert_eq!(store.uuid_to_index.len(), 1);
2021-01-29 19:14:23 +01:00
}
#[test]
fn test_index() {
let temp = tempfile::tempdir().unwrap();
let store = IndexStore::new(temp, IndexerOpts::default()).unwrap();
let name = "foobar";
assert!(store.index(&name).unwrap().is_none());
let created_at = Utc::now();
let updated_at = created_at;
2021-01-29 19:14:23 +01:00
let uuid = Uuid::new_v4();
2021-02-03 17:44:20 +01:00
let meta = IndexMeta {
2021-02-04 15:09:43 +01:00
update_store_size: 4096 * 100,
index_store_size: 4096 * 100,
2021-02-03 17:44:20 +01:00
uuid: uuid.clone(),
created_at,
updated_at,
2021-02-03 17:44:20 +01:00
};
2021-01-29 19:14:23 +01:00
let mut txn = store.env.write_txn().unwrap();
2021-02-15 23:37:08 +01:00
store
.name_to_uuid
.put(&mut txn, &name, uuid.as_bytes())
.unwrap();
store
.uuid_to_index_meta
.put(&mut txn, uuid.as_bytes(), &meta)
.unwrap();
2021-01-29 19:14:23 +01:00
txn.commit().unwrap();
assert!(store.index(&name).unwrap().is_some());
}
#[test]
fn test_get_or_create_index() {
let temp = tempfile::tempdir().unwrap();
let store = IndexStore::new(temp, IndexerOpts::default()).unwrap();
let name = "foobar";
2021-02-04 15:09:43 +01:00
let update_store_size = 4096 * 100;
let index_store_size = 4096 * 100;
2021-02-15 23:37:08 +01:00
store
.get_or_create_index(&name, update_store_size, index_store_size)
.unwrap();
2021-01-29 19:14:23 +01:00
let txn = store.env.read_txn().unwrap();
2021-02-15 23:37:08 +01:00
let uuid = store.name_to_uuid.get(&txn, &name).unwrap();
2021-02-01 19:51:47 +01:00
assert_eq!(store.uuid_to_index.len(), 1);
2021-01-29 19:14:23 +01:00
assert!(uuid.is_some());
let uuid = Uuid::from_slice(uuid.unwrap()).unwrap();
2021-02-15 23:37:08 +01:00
let meta = store
.uuid_to_index_meta
.get(&txn, uuid.as_bytes())
.unwrap()
.unwrap();
2021-02-04 15:09:43 +01:00
assert_eq!(meta.update_store_size, update_store_size);
assert_eq!(meta.index_store_size, index_store_size);
2021-02-03 17:44:20 +01:00
assert_eq!(meta.uuid, uuid);
2021-01-29 19:14:23 +01:00
}
#[test]
fn test_create_index() {
let temp = tempfile::tempdir().unwrap();
let store = IndexStore::new(temp, IndexerOpts::default()).unwrap();
let name = "foobar";
2021-02-04 15:09:43 +01:00
let update_store_size = 4096 * 100;
let index_store_size = 4096 * 100;
2021-01-29 19:14:23 +01:00
let uuid = Uuid::new_v4();
let mut txn = store.env.write_txn().unwrap();
2021-02-15 23:37:08 +01:00
store
.create_index_txn(&mut txn, uuid, name, update_store_size, index_store_size)
.unwrap();
2021-02-03 20:12:48 +01:00
let uuid = store.name_to_uuid.get(&txn, &name).unwrap();
2021-02-01 19:51:47 +01:00
assert_eq!(store.uuid_to_index.len(), 1);
2021-01-29 19:14:23 +01:00
assert!(uuid.is_some());
let uuid = Uuid::from_slice(uuid.unwrap()).unwrap();
2021-02-15 23:37:08 +01:00
let meta = store
.uuid_to_index_meta
.get(&txn, uuid.as_bytes())
.unwrap()
.unwrap();
2021-02-04 15:09:43 +01:00
assert_eq!(meta.update_store_size, update_store_size);
assert_eq!(meta.index_store_size, index_store_size);
2021-02-03 17:44:20 +01:00
assert_eq!(meta.uuid, uuid);
2021-01-29 19:14:23 +01:00
}
}
}