make database config a sum type

This way we don't need to construct the entire configuration to load a
database or database engine.

The other advantage is that it allows having options that are unique to
each database backend.

The one thing I don't like about this is `DatabaseConfig::path`, whose
existence implies all databases will have a file path, which is not
true for out-of-process databases. The only thing this is really used
for is creating the media directory. I think we should restructure the
configuration in the future to resolve this.
This commit is contained in:
Charles Hall 2024-10-08 19:20:58 -07:00
parent f5ba3e3062
commit 153e3e4c93
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
6 changed files with 88 additions and 83 deletions

View file

@ -216,35 +216,53 @@ impl Default for TurnConfig {
}
}
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum DatabaseBackend {
#[cfg(feature = "rocksdb")]
Rocksdb,
#[cfg(feature = "sqlite")]
Sqlite,
#[cfg(feature = "rocksdb")]
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct RocksdbConfig {
pub(crate) path: PathBuf,
#[serde(default = "default_rocksdb_max_open_files")]
pub(crate) max_open_files: i32,
#[serde(default = "default_rocksdb_cache_capacity_bytes")]
pub(crate) cache_capacity_bytes: usize,
}
impl Display for DatabaseBackend {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
#[cfg(feature = "sqlite")]
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct SqliteConfig {
pub(crate) path: PathBuf,
#[serde(default = "default_sqlite_cache_capacity_kilobytes")]
pub(crate) cache_capacity_kilobytes: u32,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "lowercase", tag = "backend")]
pub(crate) enum DatabaseConfig {
#[cfg(feature = "rocksdb")]
Rocksdb(RocksdbConfig),
#[cfg(feature = "sqlite")]
Sqlite(SqliteConfig),
}
impl DatabaseConfig {
pub(crate) fn path(&self) -> &Path {
match self {
#[cfg(feature = "rocksdb")]
DatabaseBackend::Rocksdb => write!(f, "RocksDB"),
DatabaseConfig::Rocksdb(x) => &x.path,
#[cfg(feature = "sqlite")]
DatabaseBackend::Sqlite => write!(f, "SQLite"),
DatabaseConfig::Sqlite(x) => &x.path,
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct DatabaseConfig {
pub(crate) backend: DatabaseBackend,
pub(crate) path: String,
#[serde(default = "default_db_cache_capacity_mb")]
pub(crate) cache_capacity_mb: f64,
#[cfg(feature = "rocksdb")]
#[serde(default = "default_rocksdb_max_open_files")]
pub(crate) rocksdb_max_open_files: i32,
impl Display for DatabaseConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
#[cfg(feature = "rocksdb")]
DatabaseConfig::Rocksdb(_) => write!(f, "RocksDB"),
#[cfg(feature = "sqlite")]
DatabaseConfig::Sqlite(_) => write!(f, "SQLite"),
}
}
}
#[derive(Clone, Debug, Default, Deserialize)]
@ -377,10 +395,6 @@ fn default_port() -> u16 {
6167
}
fn default_db_cache_capacity_mb() -> f64 {
300.0
}
fn default_cache_capacity_modifier() -> f64 {
1.0
}
@ -390,6 +404,16 @@ fn default_rocksdb_max_open_files() -> i32 {
1000
}
#[cfg(feature = "rocksdb")]
fn default_rocksdb_cache_capacity_bytes() -> usize {
300 * 1024 * 1024
}
#[cfg(feature = "sqlite")]
fn default_sqlite_cache_capacity_kilobytes() -> u32 {
300 * 1024
}
fn default_pdu_cache_capacity() -> usize {
150_000
}