2019-10-18 13:05:28 +02:00
|
|
|
use std::collections::hash_map::{Entry, HashMap};
|
2019-10-18 12:52:45 +02:00
|
|
|
use std::fs::File;
|
2019-10-04 16:49:17 +02:00
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::{Arc, RwLock};
|
2019-10-07 16:16:04 +02:00
|
|
|
use std::{fs, thread};
|
|
|
|
|
|
|
|
use crossbeam_channel::Receiver;
|
2019-10-21 12:05:53 +02:00
|
|
|
use heed::types::{Str, Unit};
|
|
|
|
use heed::{CompactionOption, Result as ZResult};
|
2019-10-08 14:53:35 +02:00
|
|
|
use log::{debug, error};
|
2019-10-07 16:16:04 +02:00
|
|
|
|
|
|
|
use crate::{store, update, Index, MResult};
|
2019-10-04 16:49:17 +02:00
|
|
|
|
2019-10-09 11:45:19 +02:00
|
|
|
pub type BoxUpdateFn = Box<dyn Fn(update::UpdateResult) + Send + Sync + 'static>;
|
|
|
|
type ArcSwapFn = arc_swap::ArcSwapOption<BoxUpdateFn>;
|
|
|
|
|
2019-10-04 16:49:17 +02:00
|
|
|
pub struct Database {
|
2019-10-21 12:05:53 +02:00
|
|
|
pub env: heed::Env,
|
|
|
|
common_store: heed::PolyDatabase,
|
|
|
|
indexes_store: heed::Database<Str, Unit>,
|
2019-10-09 11:45:19 +02:00
|
|
|
indexes: RwLock<HashMap<String, (Index, Arc<ArcSwapFn>, thread::JoinHandle<()>)>>,
|
2019-10-07 16:16:04 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:05:53 +02:00
|
|
|
fn update_awaiter(receiver: Receiver<()>, env: heed::Env, update_fn: Arc<ArcSwapFn>, index: Index) {
|
2019-10-07 16:16:04 +02:00
|
|
|
for () in receiver {
|
|
|
|
// consume all updates in order (oldest first)
|
|
|
|
loop {
|
2019-10-16 17:05:24 +02:00
|
|
|
let mut writer = match env.write_txn() {
|
2019-10-07 16:16:04 +02:00
|
|
|
Ok(writer) => writer,
|
2019-10-18 13:05:28 +02:00
|
|
|
Err(e) => {
|
|
|
|
error!("LMDB writer transaction begin failed: {}", e);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-07 16:16:04 +02:00
|
|
|
};
|
|
|
|
|
2019-10-09 17:23:48 +02:00
|
|
|
match update::update_task(&mut writer, index.clone()) {
|
|
|
|
Ok(Some(status)) => {
|
2019-10-23 15:52:40 +02:00
|
|
|
if status.result.is_ok() {
|
|
|
|
if let Err(e) = writer.commit() {
|
|
|
|
error!("update transaction failed: {}", e)
|
|
|
|
}
|
2019-10-18 13:05:28 +02:00
|
|
|
}
|
2019-10-09 17:23:48 +02:00
|
|
|
|
|
|
|
if let Some(ref callback) = *update_fn.load() {
|
|
|
|
(callback)(status);
|
|
|
|
}
|
2019-10-18 13:05:28 +02:00
|
|
|
}
|
2019-10-07 16:16:04 +02:00
|
|
|
// no more updates to handle for now
|
2019-10-18 13:05:28 +02:00
|
|
|
Ok(None) => {
|
|
|
|
debug!("no more updates");
|
|
|
|
writer.abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("update task failed: {}", e);
|
|
|
|
writer.abort()
|
|
|
|
}
|
2019-10-07 16:16:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 16:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
pub fn open_or_create(path: impl AsRef<Path>) -> MResult<Database> {
|
2019-10-07 16:16:04 +02:00
|
|
|
fs::create_dir_all(path.as_ref())?;
|
|
|
|
|
2019-10-21 12:05:53 +02:00
|
|
|
let env = heed::EnvOpenOptions::new()
|
2019-10-16 17:05:24 +02:00
|
|
|
.map_size(10 * 1024 * 1024 * 1024) // 10GB
|
|
|
|
.max_dbs(3000)
|
|
|
|
.open(path)?;
|
2019-10-04 16:49:17 +02:00
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
let common_store = env.create_dyn_database(Some("common"))?;
|
|
|
|
let indexes_store = env.create_database::<Str, Unit>(Some("indexes"))?;
|
2019-10-04 16:49:17 +02:00
|
|
|
|
|
|
|
// list all indexes that needs to be opened
|
|
|
|
let mut must_open = Vec::new();
|
2019-10-16 17:05:24 +02:00
|
|
|
let reader = env.read_txn()?;
|
|
|
|
for result in indexes_store.iter(&reader)? {
|
|
|
|
let (index_name, _) = result?;
|
|
|
|
must_open.push(index_name.to_owned());
|
2019-10-04 16:49:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
reader.abort();
|
2019-10-04 16:49:17 +02:00
|
|
|
|
|
|
|
// open the previously aggregated indexes
|
|
|
|
let mut indexes = HashMap::new();
|
|
|
|
for index_name in must_open {
|
2019-10-07 16:16:04 +02:00
|
|
|
let (sender, receiver) = crossbeam_channel::bounded(100);
|
2019-10-16 17:05:24 +02:00
|
|
|
let index = match store::open(&env, &index_name, sender.clone())? {
|
|
|
|
Some(index) => index,
|
|
|
|
None => {
|
2019-10-18 13:05:28 +02:00
|
|
|
log::warn!(
|
|
|
|
"the index {} doesn't exist or has not all the databases",
|
|
|
|
index_name
|
|
|
|
);
|
2019-10-16 17:05:24 +02:00
|
|
|
continue;
|
2019-10-18 13:05:28 +02:00
|
|
|
}
|
2019-10-16 17:05:24 +02:00
|
|
|
};
|
2019-10-09 11:45:19 +02:00
|
|
|
let update_fn = Arc::new(ArcSwapFn::empty());
|
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
let env_clone = env.clone();
|
2019-10-07 16:16:04 +02:00
|
|
|
let index_clone = index.clone();
|
2019-10-09 11:45:19 +02:00
|
|
|
let update_fn_clone = update_fn.clone();
|
|
|
|
|
|
|
|
let handle = thread::spawn(move || {
|
2019-10-16 17:05:24 +02:00
|
|
|
update_awaiter(receiver, env_clone, update_fn_clone, index_clone)
|
2019-10-09 11:45:19 +02:00
|
|
|
});
|
2019-10-07 16:16:04 +02:00
|
|
|
|
|
|
|
// send an update notification to make sure that
|
2019-10-16 17:05:24 +02:00
|
|
|
// possible pre-boot updates are consumed
|
2019-10-07 16:16:04 +02:00
|
|
|
sender.send(()).unwrap();
|
|
|
|
|
2019-10-09 11:45:19 +02:00
|
|
|
let result = indexes.insert(index_name, (index, update_fn, handle));
|
2019-10-18 13:05:28 +02:00
|
|
|
assert!(
|
|
|
|
result.is_none(),
|
|
|
|
"The index should not have been already open"
|
|
|
|
);
|
2019-10-04 16:49:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
Ok(Database {
|
|
|
|
env,
|
|
|
|
common_store,
|
|
|
|
indexes_store,
|
|
|
|
indexes: RwLock::new(indexes),
|
|
|
|
})
|
2019-10-04 16:49:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-10 13:38:58 +02:00
|
|
|
pub fn open_index(&self, name: impl AsRef<str>) -> Option<Index> {
|
2019-10-04 16:49:17 +02:00
|
|
|
let indexes_lock = self.indexes.read().unwrap();
|
2019-10-10 13:38:58 +02:00
|
|
|
match indexes_lock.get(name.as_ref()) {
|
|
|
|
Some((index, ..)) => Some(index.clone()),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 16:49:17 +02:00
|
|
|
|
2019-10-10 13:38:58 +02:00
|
|
|
pub fn create_index(&self, name: impl AsRef<str>) -> MResult<Index> {
|
|
|
|
let name = name.as_ref();
|
|
|
|
let mut indexes_lock = self.indexes.write().unwrap();
|
2019-10-04 16:49:17 +02:00
|
|
|
|
2019-10-10 13:38:58 +02:00
|
|
|
match indexes_lock.entry(name.to_owned()) {
|
|
|
|
Entry::Occupied(_) => Err(crate::Error::IndexAlreadyExists),
|
|
|
|
Entry::Vacant(entry) => {
|
2019-10-07 16:16:04 +02:00
|
|
|
let (sender, receiver) = crossbeam_channel::bounded(100);
|
2019-10-16 17:05:24 +02:00
|
|
|
let index = store::create(&self.env, name, sender)?;
|
2019-10-04 16:49:17 +02:00
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
let mut writer = self.env.write_txn()?;
|
|
|
|
self.indexes_store.put(&mut writer, name, &())?;
|
2019-10-09 11:45:19 +02:00
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
let env_clone = self.env.clone();
|
2019-10-10 13:38:58 +02:00
|
|
|
let index_clone = index.clone();
|
2019-10-09 11:45:19 +02:00
|
|
|
|
2019-10-10 13:38:58 +02:00
|
|
|
let no_update_fn = Arc::new(ArcSwapFn::empty());
|
|
|
|
let no_update_fn_clone = no_update_fn.clone();
|
2019-10-09 11:45:19 +02:00
|
|
|
|
2019-10-10 13:38:58 +02:00
|
|
|
let handle = thread::spawn(move || {
|
2019-10-16 17:05:24 +02:00
|
|
|
update_awaiter(receiver, env_clone, no_update_fn_clone, index_clone)
|
2019-10-10 13:38:58 +02:00
|
|
|
});
|
2019-10-04 16:49:17 +02:00
|
|
|
|
|
|
|
writer.commit()?;
|
2019-10-10 13:38:58 +02:00
|
|
|
entry.insert((index.clone(), no_update_fn, handle));
|
2019-10-04 16:49:17 +02:00
|
|
|
|
|
|
|
Ok(index)
|
2019-10-10 13:38:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_update_callback(&self, name: impl AsRef<str>, update_fn: BoxUpdateFn) -> bool {
|
|
|
|
let indexes_lock = self.indexes.read().unwrap();
|
|
|
|
match indexes_lock.get(name.as_ref()) {
|
|
|
|
Some((_, current_update_fn, _)) => {
|
|
|
|
let update_fn = Some(Arc::new(update_fn));
|
|
|
|
current_update_fn.swap(update_fn);
|
|
|
|
true
|
2019-10-18 13:05:28 +02:00
|
|
|
}
|
2019-10-10 13:38:58 +02:00
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unset_update_callback(&self, name: impl AsRef<str>) -> bool {
|
|
|
|
let indexes_lock = self.indexes.read().unwrap();
|
|
|
|
match indexes_lock.get(name.as_ref()) {
|
2019-10-18 13:05:28 +02:00
|
|
|
Some((_, current_update_fn, _)) => {
|
|
|
|
current_update_fn.swap(None);
|
|
|
|
true
|
|
|
|
}
|
2019-10-10 13:38:58 +02:00
|
|
|
None => false,
|
2019-10-04 16:49:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:52:45 +02:00
|
|
|
pub fn copy_and_compact_to_path<P: AsRef<Path>>(&self, path: P) -> ZResult<File> {
|
|
|
|
self.env.copy_to_path(path, CompactionOption::Enabled)
|
|
|
|
}
|
|
|
|
|
2019-10-04 16:49:17 +02:00
|
|
|
pub fn indexes_names(&self) -> MResult<Vec<String>> {
|
|
|
|
let indexes = self.indexes.read().unwrap();
|
|
|
|
Ok(indexes.keys().cloned().collect())
|
|
|
|
}
|
2019-10-07 16:16:04 +02:00
|
|
|
|
2019-10-21 12:05:53 +02:00
|
|
|
pub fn common_store(&self) -> heed::PolyDatabase {
|
2019-10-10 15:14:32 +02:00
|
|
|
self.common_store
|
2019-10-07 16:16:04 +02:00
|
|
|
}
|
2019-10-04 16:49:17 +02:00
|
|
|
}
|