diff --git a/src/error.rs b/src/error.rs index ad5b87b..f1fedd7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -51,6 +51,10 @@ pub enum Error { #[error("JSON string escape surrogate (ancient style) is not supported")] JsonEscapeSurrogate, + // LMDB + #[error("LMDB: {0}")] + Lmdb(#[from] heed::Error), + // UTF-8 #[error("UTF-8: {0}")] Utf8(#[from] std::str::Utf8Error), diff --git a/src/store/mod.rs b/src/store/mod.rs index 80eb383..c3df13a 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -1,2 +1,54 @@ pub mod event_store; pub use event_store::EventStore; + +use crate::error::Error; +use crate::types::Id; +use heed::{Database, Env, EnvFlags, EnvOpenOptions}; +use std::fs; + +#[derive(Debug)] +pub struct Store { + events: EventStore, + env: Env, + ids: Database, +} + +impl Store { + pub fn new(data_directory: &str) -> Result { + let mut builder = EnvOpenOptions::new(); + unsafe { + builder.flags(EnvFlags::NO_TLS); + } + builder.max_dbs(32); + builder.map_size(1048576 * 1024 * 24); // 24 GB + + let dir = format!("{}/lmdb", data_directory); + fs::create_dir_all(&dir)?; + + let env = match builder.open(&dir) { + Ok(env) => env, + Err(e) => { + log::error!("Unable to open LMDB at {}", dir); + return Err(e.into()); + } + }; + + // Open/Create maps + let mut txn = env.write_txn()?; + let ids = env + .database_options() + .types::() + .create(&mut txn)?; + txn.commit()?; + + log::info!("Store is setup"); + + let event_map_file = format!("{}/event.map", data_directory); + + Ok(Store { + events: EventStore::new(event_map_file)?, + env, + ids, + }) + } +}