diff --git a/lazy-supplements-core/src/global/config.rs b/lazy-supplements-core/src/global/config.rs index 4b68e9b..e27c402 100644 --- a/lazy-supplements-core/src/global/config.rs +++ b/lazy-supplements-core/src/global/config.rs @@ -1,18 +1,6 @@ -use crate::{config::StorageConfig, error::Error}; +use crate::{config::StorageConfig, error::Error, global::SimpleGlobal}; use tokio::sync::OnceCell; +use uuid::fmt::Simple; -static STORAGE_CONFIG: OnceCell = OnceCell::const_new(); +pub static STORAGE_CONFIG: SimpleGlobal = SimpleGlobal::const_new(); -pub async fn get_or_init_storage_config(config: StorageConfig) -> &'static StorageConfig { - STORAGE_CONFIG.get_or_init(|| async { - config - }).await -} - -pub fn get_storage_config() -> Option<&'static StorageConfig> { - STORAGE_CONFIG.get() -} - -pub fn get_and_unwrap_storage_config() -> &'static StorageConfig { - STORAGE_CONFIG.get().expect("global storage config is uninitialized!") -} diff --git a/lazy-supplements-core/src/global/database_connection.rs b/lazy-supplements-core/src/global/database_connection.rs index 4eb8f8d..902bf41 100644 --- a/lazy-supplements-core/src/global/database_connection.rs +++ b/lazy-supplements-core/src/global/database_connection.rs @@ -5,43 +5,35 @@ use sea_orm_migration::MigratorTrait; use crate::error::Error; use tokio::sync::OnceCell; -static DATA_DATABASE_CONNECTION: OnceCell = OnceCell::const_new(); -static CACHE_DATABASE_CONNECTION: OnceCell = OnceCell::const_new(); +static DATA_DATABASE_CONNECTION: GlobalDatabaseConnection = GlobalDatabaseConnection::const_new(); +static CACHE_DATABASE_CONNECTION: GlobalDatabaseConnection = GlobalDatabaseConnection::const_new(); -pub fn get_data_database_connection() -> Option<&'static DatabaseConnection> { - DATA_DATABASE_CONNECTION.get() -} -pub fn get_and_unwrap_data_database_connection() -> &'static DatabaseConnection { - get_data_database_connection().expect("global data database connection uninitialized!") -} -pub async fn get_or_try_init_data_database_connection(path: T, _: U) -> Result<&'static DatabaseConnection, Error> -where - T: AsRef, - U: MigratorTrait -{ - let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc"; - Ok(DATA_DATABASE_CONNECTION.get_or_try_init(|| async { - let db = Database::connect(&url).await?; - U::up(&db, None).await?; - Ok::(db) - }).await?) +struct GlobalDatabaseConnection { + inner: OnceCell } -pub fn get_cache_database_connection() -> Option<&'static DatabaseConnection> { - CACHE_DATABASE_CONNECTION.get() +impl GlobalDatabaseConnection { + pub const fn const_new() -> Self { + Self { + inner: OnceCell::const_new() + } + } + pub fn get(&'static self) -> Option<&'static DatabaseConnection> { + self.inner.get() + } + pub fn get_and_unwrap(&'static self) -> &'static DatabaseConnection { + self.get().expect(&format!("{} is uninitialized!", &stringify!(self))) + } + pub async fn get_or_try_init(&'static self, path: T, _: U) -> Result<&'static DatabaseConnection, Error> + where + T: AsRef, + U: MigratorTrait + { + let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc"; + Ok(self.inner.get_or_try_init(|| async { + let db = Database::connect(&url).await?; + U::up(&db, None).await?; + Ok::(db) + }).await?) + } } -pub fn get_and_unwrap_cache_database_connection() -> &'static DatabaseConnection { - CACHE_DATABASE_CONNECTION.get().expect("global data database connection uninitialized!") -} -pub async fn get_or_try_init_cache_database_connection(path: T, _: U) -> Result<&'static DatabaseConnection, Error> -where - T: AsRef, - U: MigratorTrait -{ - let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc"; - Ok(DATA_DATABASE_CONNECTION.get_or_try_init(|| async { - let db = Database::connect(&url).await?; - U::up(&db, None).await?; - Ok::(db) - }).await?) -} \ No newline at end of file diff --git a/lazy-supplements-core/src/global/mod.rs b/lazy-supplements-core/src/global/mod.rs index 72b7152..7fb5c3c 100644 --- a/lazy-supplements-core/src/global/mod.rs +++ b/lazy-supplements-core/src/global/mod.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, net::{IpAddr, Ipv4Addr}, path::{Path, PathBuf}, sync::LazyLock}; +use std::{any::type_name, collections::HashMap, net::{IpAddr, Ipv4Addr}, path::{Path, PathBuf}, sync::LazyLock}; use crate::{config::{P2pConfig, PartialP2pConfig, StorageConfig}, error::Error}; use futures::StreamExt; @@ -10,7 +10,7 @@ use tokio::sync::{OnceCell, RwLock}; mod peers; pub use peers::GlobalPeers; mod config; -pub use config::*; +pub use config::STORAGE_CONFIG; mod database_connection; pub use database_connection::*; use uuid::{ContextV7, Timestamp, Uuid}; @@ -33,6 +33,47 @@ pub static DEFAULT_CONFIG_FILE_NAME: LazyLock = LazyLock::new(|| { pub static DEFAULT_DATABASE_FILE_NAME: LazyLock = LazyLock::new(|| { PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".sqlite") }); +fn uninitialized_message(var: T) -> String { + format!("{} is uninitialized!", &stringify!(var)) +} + +struct SimpleGlobal { + inner: OnceCell +} + +impl SimpleGlobal { + pub const fn const_new() -> Self { + Self{inner: OnceCell::const_new()} + } + pub async fn get_or_init(&'static self, source: T) -> &'static T { + self.inner.get_or_init(|| async { + source + }).await + } + pub fn get(&'static self) -> Option<&'static T> { + self.inner.get() + } + pub fn get_and_unwrap(&'static self) -> &'static T { + self.get().expect(&format!("{} is uninitialized!", &stringify!(self))) + } +} + +struct GlobalRwLock { + inner: OnceCell> +} + +impl GlobalRwLock { + pub const fn const_new() -> Self { + Self{inner: OnceCell::const_new()} + } + async fn write(&'static self) -> tokio::sync::RwLockWriteGuard<'_ ,T> { + self.get_peers_once_cell().get().expect(UNINITIALIZED_MESSAGE).write().await + } + async fn read(&'static self) -> RwLockReadGuard<'_, T> { + self.get_peers_once_cell().get().expect(UNINITIALIZED_MESSAGE).read().await + + } +} #[cfg(test)] pub struct TestGlobal {