SharedStorage/core/DatabaseManager.cpp

62 lines
1.8 KiB
C++

//
// Created by Kirill Zhukov on 20.04.2025.
//
#include "DatabaseManager.h"
namespace usub::core
{
DatabaseManager::DatabaseManager(const std::string& config_path)
{
auto config = toml::parse_file(config_path);
if (!config.contains("database"))
throw std::runtime_error("No 'database' section found in config");
auto& databases = *config["database"].as_array();
for (auto& db_config : databases)
{
std::string db_name;
if (auto* table = db_config.as_table())
{
if (auto it = table->find("name"); it != table->end() && it->second.is_string())
{
db_name = it->second.value<std::string>().value_or("");
}
else
{
throw std::runtime_error("Database name must be a string");
}
}
else
{
throw std::runtime_error("Database entry must be a table");
}
utils::load_global_config(config);
auto& database_settings = utils::database_settings_map.at(db_name);
bool create_new = !SharedMemoryManager::exists("shm_" + db_name);
auto udb = std::make_unique<UDB>(db_name, "shm_" + db_name, database_settings, create_new);
this->databases_.push_back(DatabaseInstance{std::move(udb), {}});
}
}
void DatabaseManager::run_all()
{
for (auto& db : this->databases_)
{
db.worker = std::thread([&db]()
{
db.udb->run();
});
}
for (auto& db : this->databases_)
{
if (db.worker.joinable())
db.worker.join();
}
}
} // core
// usub