Files
Library/include/Void/Sqlite/AsyncPool.hpp

180 lines
5.3 KiB
C++

#pragma once
#include <Void/Async/Detail/Types.hpp>
#include <Void/Async/Executor.hpp>
#include <Void/Async/Operation.hpp>
#include <Void/Async/Task.hpp>
#include <Void/Sqlite/Sqlite.hpp>
#include <Void/Sqlite/Types.hpp>
#include <Void/Time/Clock.hpp>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
namespace Void::Sqlite
{
struct AsyncPoolOptions {
std::filesystem::path path{};
OpenOptions open_options{};
/// Default 4 reader connections alongside 1 dedicated writer lane.
std::size_t reader_connections{4};
std::size_t max_queued_jobs{1024};
std::size_t max_pending_input_bytes{64ULL * 1024ULL * 1024ULL};
int progress_handler_steps{64};
const Void::Time::MonotonicClock* clock{nullptr};
};
struct AsyncPoolStats {
std::size_t reader_connections{0};
std::size_t queued_read_jobs{0};
std::size_t queued_write_jobs{0};
std::size_t active_read_jobs{0};
std::size_t active_write_jobs{0};
std::size_t pending_input_bytes{0};
std::uint64_t completed_jobs{0};
std::uint64_t rejected_jobs{0};
std::uint64_t cancelled_jobs{0};
};
/// Bounded blocking SQLite connection pool (1 writer lane + N reader lanes).
/// Leases one connection exclusively for the entire callback/transaction
/// without ever running SQLite calls on an IO event-loop thread or leaking
/// interrupts across subsequent borrowers.
class AsyncPool
{
public:
~AsyncPool();
AsyncPool(AsyncPool&&) noexcept;
AsyncPool& operator=(AsyncPool&&) noexcept;
AsyncPool(const AsyncPool&) = delete;
AsyncPool& operator=(const AsyncPool&) = delete;
[[nodiscard]] static Result<AsyncPool> open(
AsyncPoolOptions options,
Void::Async::Executor* completion_executor = nullptr);
/// Execute a read-only unit of work on a leased reader connection.
template <typename T>
[[nodiscard]] Void::Coro::Task<Result<T>> run_read(
std::function<Result<T>(Database&)> fn,
Void::Async::OperationOptions op = {},
std::size_t input_bytes = 0)
{
auto box = std::make_shared<std::optional<Result<T>>>();
auto r = co_await submit_erased(
false,
[box, f = std::move(fn)](Database& db) -> Result<void> {
*box = f(db);
return {};
},
std::move(op),
input_bytes);
if (!r) {
co_return Void::Result::fail(std::move(r).error());
}
if (!box->has_value()) {
co_return Void::Result::fail(make_sqlite_error(
SqliteErrorCode::sqlite_error,
"AsyncPool read callback produced no result"));
}
co_return std::move(**box);
}
/// Execute a write unit of work on the serialized writer lane.
template <typename T>
[[nodiscard]] Void::Coro::Task<Result<T>> run_write(
std::function<Result<T>(Database&)> fn,
Void::Async::OperationOptions op = {},
std::size_t input_bytes = 0)
{
auto box = std::make_shared<std::optional<Result<T>>>();
auto r = co_await submit_erased(
true,
[box, f = std::move(fn)](Database& db) -> Result<void> {
*box = f(db);
return {};
},
std::move(op),
input_bytes);
if (!r) {
co_return Void::Result::fail(std::move(r).error());
}
if (!box->has_value()) {
co_return Void::Result::fail(make_sqlite_error(
SqliteErrorCode::sqlite_error,
"AsyncPool write callback produced no result"));
}
co_return std::move(**box);
}
/// Execute a full transaction on the leased writer connection.
/// Commits if `fn` returns ok; rolls back on error. Never releases
/// the writer lease mid-transaction.
template <typename T>
[[nodiscard]] Void::Coro::Task<Result<T>> run_write_transaction(
BeginMode mode,
std::function<Result<T>(Database&, Transaction&)> fn,
Void::Async::OperationOptions op = {},
std::size_t input_bytes = 0)
{
return run_write<T>(
[mode, f = std::move(fn)](Database& db) -> Result<T> {
auto tx = db.begin(mode);
if (!tx) {
return Void::Result::fail(std::move(tx).error());
}
auto res = f(db, *tx);
if (!res) {
(void)tx->rollback_checked();
return res;
}
if (tx->is_active()) {
auto c = tx->commit();
if (!c) {
if (tx->is_active()) {
(void)tx->rollback_checked();
}
return Void::Result::fail(std::move(c).error());
}
}
return res;
},
std::move(op),
input_bytes);
}
[[nodiscard]] Void::Coro::Task<Result<void>> run_read_void(
std::function<Result<void>(Database&)> fn,
Void::Async::OperationOptions op = {},
std::size_t input_bytes = 0);
[[nodiscard]] Void::Coro::Task<Result<void>> run_write_void(
std::function<Result<void>(Database&)> fn,
Void::Async::OperationOptions op = {},
std::size_t input_bytes = 0);
[[nodiscard]] AsyncPoolStats stats() const noexcept;
/// Stop new admission, drain all admitted jobs, join worker threads,
/// and close all pooled SQLite connections.
[[nodiscard]] Result<void> close_and_join();
private:
struct Impl;
explicit AsyncPool(std::shared_ptr<Impl> impl) noexcept;
[[nodiscard]] Void::Coro::Task<Result<void>> submit_erased(
bool is_write,
std::function<Result<void>(Database&)> fn,
Void::Async::OperationOptions op,
std::size_t input_bytes);
std::shared_ptr<Impl> m_impl{};
};
} // namespace Void::Sqlite