Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
backend_registry.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#include "backend_registry.h"
6
7#include <algorithm>
8
9namespace database
10{
11namespace core
12{
13
19
20kcenon::common::VoidResult backend_registry::register_backend(const std::string& name,
21 backend_factory_fn factory)
22{
23 if (name.empty())
24 {
25 return kcenon::common::error_info{1, "Backend name cannot be empty", "backend_registry"};
26 }
27
28 if (!factory)
29 {
30 return kcenon::common::error_info{2, "Factory function cannot be null", "backend_registry"};
31 }
32
33 std::lock_guard<std::mutex> lock(mutex_);
34
35 // Check if backend already registered
36 if (factories_.find(name) != factories_.end())
37 {
38 return kcenon::common::error_info{3, "Backend '" + name + "' is already registered", "backend_registry"};
39 }
40
41 factories_[name] = factory;
42 return kcenon::common::VoidResult::ok(std::monostate{});
43}
44
45kcenon::common::VoidResult backend_registry::unregister_backend(const std::string& name)
46{
47 std::lock_guard<std::mutex> lock(mutex_);
48
49 auto it = factories_.find(name);
50 if (it == factories_.end())
51 {
52 return kcenon::common::error_info{4, "Backend '" + name + "' not found", "backend_registry"};
53 }
54
55 factories_.erase(it);
56 return kcenon::common::VoidResult::ok(std::monostate{});
57}
58
59std::unique_ptr<database_backend> backend_registry::create(const std::string& name) const
60{
61 std::lock_guard<std::mutex> lock(mutex_);
62
63 auto it = factories_.find(name);
64 if (it == factories_.end())
65 {
66 return nullptr;
67 }
68
69 try
70 {
71 return it->second();
72 }
73 catch (...)
74 {
75 // Factory function threw an exception
76 return nullptr;
77 }
78}
79
80bool backend_registry::has_backend(const std::string& name) const
81{
82 std::lock_guard<std::mutex> lock(mutex_);
83 return factories_.find(name) != factories_.end();
84}
85
86std::vector<std::string> backend_registry::available_backends() const
87{
88 std::lock_guard<std::mutex> lock(mutex_);
89
90 std::vector<std::string> names;
91 names.reserve(factories_.size());
92
93 for (const auto& pair : factories_)
94 {
95 names.push_back(pair.first);
96 }
97
98 // Sort for consistent output
99 std::sort(names.begin(), names.end());
100
101 return names;
102}
103
105{
106 std::lock_guard<std::mutex> lock(mutex_);
107 return factories_.size();
108}
109
111{
112 std::lock_guard<std::mutex> lock(mutex_);
113 factories_.clear();
114}
115
116} // namespace core
117} // namespace database
Registry for database backend plugins.
Singleton registry for database backend plugins.
std::vector< std::string > available_backends() const
Get list of all registered backend names.
void clear()
Clear all registered backends (for testing)
kcenon::common::VoidResult unregister_backend(const std::string &name)
Unregister a backend (for testing or dynamic unloading)
static backend_registry & instance()
Get the singleton instance.
bool has_backend(const std::string &name) const
Check if a backend is registered.
std::unique_ptr< database_backend > create(const std::string &name) const
Create a backend instance by name.
std::map< std::string, backend_factory_fn > factories_
kcenon::common::VoidResult register_backend(const std::string &name, backend_factory_fn factory)
Register a backend factory function.
size_t backend_count() const
Get number of registered backends.
std::unique_ptr< database_backend >(*)() backend_factory_fn
Factory function type for creating database backends.