Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
thread_adapter.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 "thread_adapter.h"
9
10namespace database
11{
12namespace integrated
13{
14namespace adapters
15{
16
17// ═══════════════════════════════════════════════════════════════
18// Backend Factory
19// ═══════════════════════════════════════════════════════════════
20
21std::unique_ptr<backends::thread_backend> thread_adapter::create_backend(
22 const db_thread_config& config,
24{
25 switch (backend_type)
26 {
29 return std::make_unique<backends::fallback_thread_backend>(config);
30
32 return std::make_unique<backends::null_thread_backend>(config);
33
34 default:
35 return std::make_unique<backends::fallback_thread_backend>(config);
36 }
37}
38
39// ═══════════════════════════════════════════════════════════════
40// Public Interface Implementation
41// ═══════════════════════════════════════════════════════════════
42
44 const db_thread_config& config,
46 : config_(config)
47 , backend_(create_backend(config, backend_type))
48{
49}
50
52{
53 if (backend_ && backend_->is_initialized())
54 {
55 backend_->shutdown();
56 }
57}
58
60
62{
63 if (!backend_)
64 {
65 return common::VoidResult(
66 common::error_info{ -1, "Backend not created", "" });
67 }
68
69 return backend_->initialize();
70}
71
73{
74 if (!backend_)
75 {
76 return common::ok();
77 }
78
79 return backend_->shutdown();
80}
81
83{
84 return backend_ && backend_->is_initialized();
85}
86
87// ═══════════════════════════════════════════════════════════════
88// Task Execution
89// ═══════════════════════════════════════════════════════════════
90
92{
93 if (!backend_)
94 {
95 return common::VoidResult(
96 common::error_info{ -1, "Backend not initialized", "" });
97 }
98
99 return backend_->execute(std::move(task));
100}
101
102// ═══════════════════════════════════════════════════════════════
103// Work Completion
104// ═══════════════════════════════════════════════════════════════
105
107{
108 if (backend_)
109 {
110 backend_->wait_for_completion();
111 }
112}
113
115{
116 if (!backend_)
117 {
118 return true;
119 }
120
121 return backend_->wait_for_completion_timeout(timeout);
122}
123
124// ═══════════════════════════════════════════════════════════════
125// Pool Statistics
126// ═══════════════════════════════════════════════════════════════
127
129{
130 if (!backend_)
131 {
132 return 0;
133 }
134
135 return backend_->worker_count();
136}
137
138std::size_t thread_adapter::queue_size() const
139{
140 if (!backend_)
141 {
142 return 0;
143 }
144
145 return backend_->queue_size();
146}
147
149{
150 if (!backend_)
151 {
152 return true;
153 }
154
155 return backend_->is_idle();
156}
157
158} // namespace adapters
159} // namespace integrated
160} // namespace database
Thread pool adapter for async database operations.
common::VoidResult shutdown()
Shutdown thread pool gracefully.
bool wait_for_completion_timeout(std::chrono::milliseconds timeout)
Wait for tasks with timeout.
common::VoidResult initialize()
Initialize thread pool.
void wait_for_completion()
Wait for all pending tasks to complete.
std::unique_ptr< backends::thread_backend > backend_
Thread backend implementation.
~thread_adapter()
Destructor - ensures graceful shutdown.
std::size_t queue_size() const
Get current queue size.
thread_adapter(const db_thread_config &config, thread_backend_type backend_type=thread_backend_type::auto_select)
Construct thread adapter with configuration.
std::size_t worker_count() const
Get number of worker threads.
bool is_initialized() const
Check if thread pool is initialized.
common::VoidResult execute(std::function< void()> task)
Execute a task (fire-and-forget)
static std::unique_ptr< backends::thread_backend > create_backend(const db_thread_config &config, thread_backend_type backend_type)
Create appropriate backend based on type.
bool is_idle() const
Check if thread pool is idle.
Fallback thread backend using std::thread.
VoidResult ok()
Result< std::monostate > VoidResult
thread_backend_type
Thread backend type selection.
@ null
Synchronous execution (no threading)
@ auto_select
Automatically select best available backend.
static std::shared_ptr< core::database_backend > create_backend(backend_type type)
Create database backend instance.
backend_type
Database backend type enumeration.
kcenon::common::VoidResult VoidResult
Primary VoidResult type - use this for void operations.
Definition result.h:33
Null thread backend (synchronous execution)
Thread pool configuration for async operations.
Thread pool adapter with runtime backend selection.
Abstract interface for thread pool backends.