// 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 #include #include #include #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 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 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 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 ReadRange(std::uint64_t from_seq, std::size_t limit) override { std::lock_guard g(mu_); if (limit > kQueryPageMax) limit = kQueryPageMax; std::vector 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 g(mu_); seals_.push_back(s); } std::vector Seals() override { std::lock_guard g(mu_); return seals_; } std::uint64_t LatestSeq() override { std::lock_guard g(mu_); return recs_.size(); } std::string Tip() override { std::lock_guard g(mu_); return tip_; } private: std::mutex mu_; std::vector recs_; std::vector seals_; std::string tip_ = GenesisHash(); }; } // namespace molo::audit::log