Files
moloapp/src/audit/log/store.h
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

81 lines
2.2 KiB
C++

// Store: the only persistence seam for the audit trail. Three operations:
// append, read_range, seal. MemoryStore is the standalone/test backend;
// SqlStore (sql_store.h) is the production backend over both SQL drivers.
#pragma once
#include <cstdint>
#include <mutex>
#include <string>
#include <vector>
#include "../caps.h"
#include "chain.h"
#include "record.h"
namespace molo::audit::log {
class Store {
public:
virtual ~Store() = default;
// Appends rec, assigns rec.seq, links the chain, returns the seq.
virtual std::uint64_t Append(AuditRecord rec) = 0;
// Reads at most limit records from from_seq (inclusive), ascending.
virtual std::vector<AuditRecord> ReadRange(std::uint64_t from_seq, std::size_t limit) = 0;
// Persists a chain seal checkpoint.
virtual void SaveSeal(const Seal& s) = 0;
virtual std::vector<Seal> Seals() = 0;
virtual std::uint64_t LatestSeq() = 0;
// Current chain tip, genesis when empty.
virtual std::string Tip() = 0;
};
class MemoryStore : public Store {
public:
std::uint64_t Append(AuditRecord rec) override {
std::lock_guard<std::mutex> g(mu_);
rec.seq = recs_.size() + 1;
Chain::Link(rec, tip_);
tip_ = rec.hash;
recs_.push_back(std::move(rec));
return recs_.size();
}
std::vector<AuditRecord> ReadRange(std::uint64_t from_seq, std::size_t limit) override {
std::lock_guard<std::mutex> g(mu_);
if (limit > kQueryPageMax) limit = kQueryPageMax;
std::vector<AuditRecord> out;
if (from_seq == 0 || from_seq > recs_.size()) return out;
for (std::size_t i = from_seq - 1; i < recs_.size() && out.size() < limit; ++i)
out.push_back(recs_[i]);
return out;
}
void SaveSeal(const Seal& s) override {
std::lock_guard<std::mutex> g(mu_);
seals_.push_back(s);
}
std::vector<Seal> Seals() override {
std::lock_guard<std::mutex> g(mu_);
return seals_;
}
std::uint64_t LatestSeq() override {
std::lock_guard<std::mutex> g(mu_);
return recs_.size();
}
std::string Tip() override {
std::lock_guard<std::mutex> g(mu_);
return tip_;
}
private:
std::mutex mu_;
std::vector<AuditRecord> recs_;
std::vector<Seal> seals_;
std::string tip_ = GenesisHash();
};
} // namespace molo::audit::log