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.
125 lines
3.8 KiB
C++
125 lines
3.8 KiB
C++
#pragma once
|
|
// Catalog facade over a CatalogStore with a bounded slug lookup cache
|
|
// (kSlugCacheMax, FIFO eviction). Owns lifecycle transitions: create,
|
|
// suspend, tombstone, and the gated purge.
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "tenancy/caps.h"
|
|
#include "tenancy/store/catalog_store.h"
|
|
#include "tenancy/strategy.h"
|
|
#include "tenancy/tenant.h"
|
|
#include "tenancy/tenant_id.h"
|
|
|
|
namespace molo::tenancy {
|
|
|
|
enum class PurgeResult { Ok, NotFound, NotTombstone, RetentionHold, BadConfirm };
|
|
|
|
class Catalog {
|
|
public:
|
|
explicit Catalog(std::unique_ptr<store::CatalogStore> backend)
|
|
: backend_(std::move(backend)) {}
|
|
|
|
std::optional<Tenant> create(const std::string& slug,
|
|
Strategy strategy,
|
|
std::int64_t now_unix) {
|
|
auto id = TenantId::TryParse(slug);
|
|
if (!id.has_value()) return std::nullopt;
|
|
if (find_by_slug(slug).has_value()) return std::nullopt;
|
|
Tenant tenant("ten-" + slug, slug, strategy, TenantStatus::Active, 0, now_unix, 0);
|
|
if (!backend_->upsert(tenant)) return std::nullopt;
|
|
CachePut(tenant);
|
|
return tenant;
|
|
}
|
|
|
|
std::optional<Tenant> find_by_slug(const std::string& slug) {
|
|
{
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
auto it = cache_.find(slug);
|
|
if (it != cache_.end()) return it->second;
|
|
}
|
|
auto found = backend_->get_by_slug(slug);
|
|
if (found.has_value()) CachePut(*found);
|
|
return found;
|
|
}
|
|
|
|
std::optional<Tenant> find_by_id(const std::string& id) {
|
|
return backend_->get_by_id(id);
|
|
}
|
|
|
|
bool suspend(const std::string& slug) {
|
|
auto found = backend_->get_by_slug(slug);
|
|
if (!found.has_value() || !found->is_active()) return false;
|
|
found->suspend();
|
|
if (!backend_->upsert(*found)) return false;
|
|
CachePut(*found);
|
|
return true;
|
|
}
|
|
|
|
bool tombstone(const std::string& slug, std::int64_t now_unix) {
|
|
auto found = backend_->get_by_slug(slug);
|
|
if (!found.has_value()) return false;
|
|
found->mark_tombstone(now_unix);
|
|
if (!backend_->upsert(*found)) return false;
|
|
Invalidate(slug);
|
|
return true;
|
|
}
|
|
|
|
PurgeResult purge(const std::string& slug,
|
|
const std::string& confirm,
|
|
std::int64_t now_unix,
|
|
std::int64_t retention_sec) {
|
|
auto found = backend_->get_by_slug(slug);
|
|
if (!found.has_value()) return PurgeResult::NotFound;
|
|
if (found->status() != TenantStatus::Tombstone) return PurgeResult::NotTombstone;
|
|
if (now_unix - found->tombstoned_unix() < retention_sec) return PurgeResult::RetentionHold;
|
|
if (confirm != "PURGE:" + found->id()) return PurgeResult::BadConfirm;
|
|
if (!backend_->remove(found->id())) return PurgeResult::NotFound;
|
|
Invalidate(slug);
|
|
return PurgeResult::Ok;
|
|
}
|
|
|
|
std::vector<Tenant> list(std::size_t limit) { return backend_->list(limit); }
|
|
|
|
void invalidate(const std::string& slug) { Invalidate(slug); }
|
|
|
|
std::size_t cache_size() const {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
return cache_.size();
|
|
}
|
|
|
|
private:
|
|
void CachePut(const Tenant& tenant) {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
if (cache_.find(tenant.slug()) == cache_.end()) {
|
|
order_.push_back(tenant.slug());
|
|
while (order_.size() > Caps::kSlugCacheMax) {
|
|
cache_.erase(order_.front());
|
|
order_.pop_front();
|
|
}
|
|
}
|
|
cache_.insert_or_assign(tenant.slug(), tenant);
|
|
}
|
|
|
|
void Invalidate(const std::string& slug) {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
cache_.erase(slug);
|
|
}
|
|
|
|
std::unique_ptr<store::CatalogStore> backend_;
|
|
mutable std::mutex mu_;
|
|
std::unordered_map<std::string, Tenant> cache_;
|
|
std::deque<std::string> order_;
|
|
};
|
|
|
|
} // namespace molo::tenancy
|