NetworkedApplications: SQLite-backed KV profile
This commit is contained in:
1 parent
61582ea461
commit
c21dbf5c93
4 files changed
+651
-2
No files matched your search
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "networked_kv/domain.hpp"
|
||||
|
||||
#include <Void/Sqlite/Sqlite.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string_view>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
/// Durable KV profile backed by Void::Sqlite.
|
||||
///
|
||||
/// Business rows and idempotency records are written in one explicit
|
||||
/// transaction, so a request either commits both or neither. Versions are
|
||||
/// checked inside that transaction.
|
||||
class SqliteKvStore final : public KvStore
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<std::unique_ptr<SqliteKvStore>>
|
||||
open(const std::filesystem::path& path, Limits limits = {});
|
||||
|
||||
~SqliteKvStore() override = default;
|
||||
SqliteKvStore(const SqliteKvStore&) = delete;
|
||||
SqliteKvStore& operator=(const SqliteKvStore&) = delete;
|
||||
SqliteKvStore(SqliteKvStore&&) noexcept = default;
|
||||
SqliteKvStore& operator=(SqliteKvStore&&) noexcept = default;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::optional<Entry>>
|
||||
get(std::string_view key) const override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WriteResult>
|
||||
put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request) override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WriteResult>
|
||||
erase(std::string_view key, Precondition precondition,
|
||||
RequestId request) override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ScanPage>
|
||||
scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit = 0) const override;
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept
|
||||
{
|
||||
return m_limits;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
SqliteKvStore(Void::Sqlite::Database database, Limits limits);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> ensure_schema();
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
// prepare() is non-const, but the read-only KvStore methods are const.
|
||||
mutable Void::Sqlite::Database m_db;
|
||||
Limits m_limits;
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user