#pragma once // DatabaseBridge maps each FilePerTenant tenant to its own SQLite file under // a root directory and keeps the stores isolated. A transaction binds to // exactly one database: opening one across two files throws CrossFileError. // Each open database counts two files (data plus journal) toward the file cap. #include #include #include #include #include #include #include #include #include "tenancy/caps.h" #include "tenancy/tenant_id.h" namespace molo::tenancy { class DatabaseBridge { public: class CrossFileError : public std::runtime_error { public: explicit CrossFileError(const std::string& msg) : std::runtime_error(msg) {} }; class Txn { public: Txn() : bridge_(nullptr) {} Txn(DatabaseBridge* bridge, std::string slug) : bridge_(bridge), slug_(std::move(slug)), active_(true) {} Txn(Txn&&) = default; Txn& operator=(Txn&&) = default; Txn(const Txn&) = delete; Txn& operator=(const Txn&) = delete; bool active() const { return active_; } const std::string& slug() const { return slug_; } bool put(const std::string& key, const std::string& value) { if (!active_ || bridge_ == nullptr) return false; staged_[key] = value; return true; } std::optional get(const std::string& key) { if (!active_ || bridge_ == nullptr) return std::nullopt; auto it = staged_.find(key); if (it != staged_.end()) return it->second; return bridge_->get(slug_, key); } bool commit() { if (!active_ || bridge_ == nullptr) return false; bool ok = bridge_->ApplyStaged(slug_, staged_); active_ = false; staged_.clear(); return ok; } void rollback() { active_ = false; staged_.clear(); } private: DatabaseBridge* bridge_; std::string slug_; bool active_ = false; std::map staged_; }; explicit DatabaseBridge(std::string root) : root_(std::move(root)) {} std::string file_for(const std::string& slug) const { return root_ + "/" + slug + ".db"; } bool open(const std::string& slug) { if (!TenantId::TryParse(slug).has_value()) return false; std::lock_guard lock(mu_); if (dbs_.find(slug) != dbs_.end()) return true; if (dbs_.size() >= Caps::kBridgeDbsMax) return false; if ((dbs_.size() + 1) * 2 > Caps::kBridgeFilesMax) return false; dbs_.emplace(slug, std::map{}); return true; } bool is_open(const std::string& slug) const { std::lock_guard lock(mu_); return dbs_.find(slug) != dbs_.end(); } bool put(const std::string& slug, const std::string& key, const std::string& value) { std::lock_guard lock(mu_); auto it = dbs_.find(slug); if (it == dbs_.end()) return false; it->second[key] = value; return true; } std::optional get(const std::string& slug, const std::string& key) const { std::lock_guard lock(mu_); auto it = dbs_.find(slug); if (it == dbs_.end()) return std::nullopt; auto jt = it->second.find(key); if (jt == it->second.end()) return std::nullopt; return jt->second; } Txn begin(const std::string& slug) { std::lock_guard lock(mu_); if (dbs_.find(slug) == dbs_.end()) return Txn{}; return Txn(this, slug); } Txn begin2(const std::string& a, const std::string& b) { if (a != b) throw CrossFileError("cross-file transaction refused: " + a + " vs " + b); return begin(a); } std::size_t db_count() const { std::lock_guard lock(mu_); return dbs_.size(); } std::size_t file_count() const { std::lock_guard lock(mu_); return dbs_.size() * 2; } private: bool ApplyStaged(const std::string& slug, const std::map& staged) { std::lock_guard lock(mu_); auto it = dbs_.find(slug); if (it == dbs_.end()) return false; for (const auto& [k, v] : staged) it->second[k] = v; return true; } std::string root_; mutable std::mutex mu_; std::unordered_map> dbs_; }; } // namespace molo::tenancy