#include "collection_registry.hpp" #include "db_errors.hpp" #include "errors.hpp" #include #include namespace svapi { CollectionMeta CollectionMeta::fromJson(const nlohmann::json& j) { CollectionMeta m; m.name = j.value("name", ""); m.kind = j.value("kind", "json"); m.vectorDimension = j.value("vector_dimension", 0u); m.embeddingModel = j.value("embedding_model", ""); m.createdAt = j.value("created_at", 0ull); return m; } nlohmann::json CollectionMeta::toJson() const { return {{"name", name}, {"kind", kind}, {"vector_dimension", vectorDimension}, {"embedding_model", embeddingModel}, {"created_at", createdAt}}; } CollectionRegistry::CollectionRegistry(DbGateway& db) : db_(db) {} void CollectionRegistry::ensure(const std::string& project) { const std::string c = coll(project); if (!db_.client().getCollectionInfo(c).has_value()) db_.client().createCollection(c, 0, false, 0, 0); } void CollectionRegistry::bootstrap(const std::vector& projects) { for (const auto& p : projects) ensure(p); } bool CollectionRegistry::add(const std::string& project, const CollectionMeta& meta) { CollectionMeta m = meta; if (m.createdAt == 0) m.createdAt = static_cast(std::time(nullptr)); auto [id, isNew] = db_.client().upsert(coll(project), m.toJson(), m.name); return !id.empty(); } std::optional CollectionRegistry::get(const std::string& project, const std::string& name) { // Since database client 2.13.0 this is simply honest: a read whose "nothing // there" value is also a valid answer THROWS on a failed call, and returns // nullopt only for a genuine absence. So nullopt means absent, and that is // the whole of it. // // What used to be here, and why it is gone: `Client::get` returned nullopt // BOTH for "no such document" and for a transport failure, which it only // logged. Left alone that reached a caller as 404 "no such collection" for // a collection that exists - and a 404 is unretryable BY DESIGN for a // careful client, because adding it to a retry set would retry every // genuine absence. A consumer lost a 6-minute ingest of an 8 GB mailbox to // exactly that, with the collection present before and after. We worked // around it by reading twice and then asking healthCheck() before daring to // report absence. The database took the report and fixed it at the source, // so the workaround is deleted rather than kept "just in case": a retry // that cannot distinguish anything is just a slower wrong answer, and // keeping it would hide the real behaviour from the next reader. try { if (auto doc = db_.client().get(coll(project), name)) return CollectionMeta::fromJson(*doc); return std::nullopt; } catch (const smartbotic::database::Client::Error& e) { // Keep the message that says what we could not determine. The global // handler would also map this to 503 now, but generically: "cannot // determine whether collection 'x' exists" tells the caller which // question went unanswered, and that is worth preserving on the one // path where a wrong answer is an unretryable 404. ApiError mapped = apiErrorFor(e); if (mapped.code == ErrCode::Unavailable) throw ApiError(ErrCode::Unavailable, "db_error", "cannot determine whether collection '" + name + "' exists: the database is not reachable"); throw mapped; } } std::vector CollectionRegistry::list(const std::string& project) { std::vector out; smartbotic::database::Client::QueryOptions opts; opts.limit = 100000; for (const auto& doc : db_.client().find(coll(project), opts)) out.push_back(CollectionMeta::fromJson(doc)); return out; } bool CollectionRegistry::remove(const std::string& project, const std::string& name) { return db_.client().remove(coll(project), name); } } // namespace svapi