mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
config: convert database backend to enum
This reports a nice error when the config is loaded, rather than later when the database is initialized.
This commit is contained in:
parent
d26b87a2f2
commit
8a30817930
3 changed files with 40 additions and 11 deletions
|
|
@ -153,9 +153,29 @@ 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for DatabaseBackend {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
#[cfg(feature = "rocksdb")]
|
||||||
|
DatabaseBackend::Rocksdb => write!(f, "RocksDB"),
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
|
DatabaseBackend::Sqlite => write!(f, "SQLite"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub(crate) struct DatabaseConfig {
|
pub(crate) struct DatabaseConfig {
|
||||||
pub(crate) backend: String,
|
pub(crate) backend: DatabaseBackend,
|
||||||
pub(crate) path: String,
|
pub(crate) path: String,
|
||||||
#[serde(default = "default_db_cache_capacity_mb")]
|
#[serde(default = "default_db_cache_capacity_mb")]
|
||||||
pub(crate) cache_capacity_mb: f64,
|
pub(crate) cache_capacity_mb: f64,
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,8 @@ use ruma::{
|
||||||
use tracing::{debug, error, info, info_span, warn, Instrument};
|
use tracing::{debug, error, info, info_span, warn, Instrument};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
service::rooms::timeline::PduCount, services, utils, Config, Error,
|
config::DatabaseBackend, service::rooms::timeline::PduCount, services,
|
||||||
PduEvent, Result, Services, SERVICES,
|
utils, Config, Error, PduEvent, Result, Services, SERVICES,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) struct KeyValueDatabase {
|
pub(crate) struct KeyValueDatabase {
|
||||||
|
|
@ -279,14 +279,22 @@ impl KeyValueDatabase {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if sqlite_exists && config.database.backend != "sqlite" {
|
let (backend_is_rocksdb, backend_is_sqlite): (bool, bool) =
|
||||||
|
match config.database.backend {
|
||||||
|
#[cfg(feature = "rocksdb")]
|
||||||
|
DatabaseBackend::Rocksdb => (true, false),
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
|
DatabaseBackend::Sqlite => (false, true),
|
||||||
|
};
|
||||||
|
|
||||||
|
if sqlite_exists && !backend_is_sqlite {
|
||||||
return Err(Error::bad_config(
|
return Err(Error::bad_config(
|
||||||
"Found sqlite at database_path, but is not specified in \
|
"Found sqlite at database_path, but is not specified in \
|
||||||
config.",
|
config.",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if rocksdb_exists && config.database.backend != "rocksdb" {
|
if rocksdb_exists && !backend_is_rocksdb {
|
||||||
return Err(Error::bad_config(
|
return Err(Error::bad_config(
|
||||||
"Found rocksdb at database_path, but is not specified in \
|
"Found rocksdb at database_path, but is not specified in \
|
||||||
config.",
|
config.",
|
||||||
|
|
@ -319,21 +327,18 @@ impl KeyValueDatabase {
|
||||||
not(any(feature = "rocksdb", feature = "sqlite")),
|
not(any(feature = "rocksdb", feature = "sqlite")),
|
||||||
allow(unused_variables)
|
allow(unused_variables)
|
||||||
)]
|
)]
|
||||||
let builder: Arc<dyn KeyValueDatabaseEngine> = match &*config
|
let builder: Arc<dyn KeyValueDatabaseEngine> = match config
|
||||||
.database
|
.database
|
||||||
.backend
|
.backend
|
||||||
{
|
{
|
||||||
#[cfg(feature = "sqlite")]
|
#[cfg(feature = "sqlite")]
|
||||||
"sqlite" => {
|
DatabaseBackend::Sqlite => {
|
||||||
Arc::new(Arc::<abstraction::sqlite::Engine>::open(&config)?)
|
Arc::new(Arc::<abstraction::sqlite::Engine>::open(&config)?)
|
||||||
}
|
}
|
||||||
#[cfg(feature = "rocksdb")]
|
#[cfg(feature = "rocksdb")]
|
||||||
"rocksdb" => {
|
DatabaseBackend::Rocksdb => {
|
||||||
Arc::new(Arc::<abstraction::rocksdb::Engine>::open(&config)?)
|
Arc::new(Arc::<abstraction::rocksdb::Engine>::open(&config)?)
|
||||||
}
|
}
|
||||||
_ => {
|
|
||||||
return Err(Error::BadConfig("Database backend not found."));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if config.registration_token == Some(String::new()) {
|
if config.registration_token == Some(String::new()) {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Avoid spurious warnings with --no-default-features, which isn't expected to
|
||||||
|
// work anyway
|
||||||
|
#![cfg_attr(not(any(feature = "sqlite", feature = "rocksdb")), allow(unused))]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
future::Future,
|
future::Future,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue