remove database's dependency on entire Config

This makes it easier to ad-hoc construct databases, e.g. in a CLI tool
that operates on multiple database instances.
This commit is contained in:
Charles Hall 2024-09-26 17:36:29 -07:00
parent 14b0769a3e
commit b3d9cd5e9c
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
5 changed files with 89 additions and 54 deletions

View file

@ -12,7 +12,7 @@ use thread_local::ThreadLocal;
use tracing::debug;
use super::{watchers::Watchers, KeyValueDatabaseEngine, KvTree};
use crate::{database::Config, Result};
use crate::Result;
thread_local! {
static READ_CONNECTION: RefCell<Option<&'static Connection>> =
@ -66,10 +66,14 @@ pub(crate) struct Engine {
}
impl Engine {
pub(crate) fn open(config: &Config) -> Result<Self> {
let path = Path::new(&config.database.path).join(format!(
pub(crate) fn open(
path: &Path,
cache_capacity_mb: f64,
conduit_compat: bool,
) -> Result<Self> {
let path = path.join(format!(
"{}.db",
if config.conduit_compat {
if conduit_compat {
"conduit"
} else {
"grapevine"
@ -87,9 +91,9 @@ impl Engine {
clippy::cast_precision_loss,
clippy::cast_sign_loss
)]
let cache_size_per_thread =
((config.database.cache_capacity_mb * 1024.0)
/ ((num_cpus::get() as f64 * 2.0) + 1.0)) as u32;
let cache_size_per_thread = ((cache_capacity_mb * 1024.0)
/ ((num_cpus::get() as f64 * 2.0) + 1.0))
as u32;
let writer =
Mutex::new(Engine::prepare_conn(&path, cache_size_per_thread)?);