collection_registry.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "collection_registry.hpp"
  2. #include "db_errors.hpp"
  3. #include "errors.hpp"
  4. #include <spdlog/spdlog.h>
  5. #include <ctime>
  6. namespace svapi {
  7. CollectionMeta CollectionMeta::fromJson(const nlohmann::json& j) {
  8. CollectionMeta m;
  9. m.name = j.value("name", "");
  10. m.kind = j.value("kind", "json");
  11. m.vectorDimension = j.value("vector_dimension", 0u);
  12. m.embeddingModel = j.value("embedding_model", "");
  13. m.createdAt = j.value("created_at", 0ull);
  14. return m;
  15. }
  16. nlohmann::json CollectionMeta::toJson() const {
  17. return {{"name", name}, {"kind", kind}, {"vector_dimension", vectorDimension},
  18. {"embedding_model", embeddingModel}, {"created_at", createdAt}};
  19. }
  20. CollectionRegistry::CollectionRegistry(DbGateway& db) : db_(db) {}
  21. void CollectionRegistry::ensure(const std::string& project) {
  22. const std::string c = coll(project);
  23. if (!db_.client().getCollectionInfo(c).has_value())
  24. db_.client().createCollection(c, 0, false, 0, 0);
  25. }
  26. void CollectionRegistry::bootstrap(const std::vector<std::string>& projects) {
  27. for (const auto& p : projects) ensure(p);
  28. }
  29. bool CollectionRegistry::add(const std::string& project, const CollectionMeta& meta) {
  30. CollectionMeta m = meta;
  31. if (m.createdAt == 0) m.createdAt = static_cast<uint64_t>(std::time(nullptr));
  32. auto [id, isNew] = db_.client().upsert(coll(project), m.toJson(), m.name);
  33. return !id.empty();
  34. }
  35. std::optional<CollectionMeta> CollectionRegistry::get(const std::string& project,
  36. const std::string& name) {
  37. // Since database client 2.13.0 this is simply honest: a read whose "nothing
  38. // there" value is also a valid answer THROWS on a failed call, and returns
  39. // nullopt only for a genuine absence. So nullopt means absent, and that is
  40. // the whole of it.
  41. //
  42. // What used to be here, and why it is gone: `Client::get` returned nullopt
  43. // BOTH for "no such document" and for a transport failure, which it only
  44. // logged. Left alone that reached a caller as 404 "no such collection" for
  45. // a collection that exists - and a 404 is unretryable BY DESIGN for a
  46. // careful client, because adding it to a retry set would retry every
  47. // genuine absence. A consumer lost a 6-minute ingest of an 8 GB mailbox to
  48. // exactly that, with the collection present before and after. We worked
  49. // around it by reading twice and then asking healthCheck() before daring to
  50. // report absence. The database took the report and fixed it at the source,
  51. // so the workaround is deleted rather than kept "just in case": a retry
  52. // that cannot distinguish anything is just a slower wrong answer, and
  53. // keeping it would hide the real behaviour from the next reader.
  54. try {
  55. if (auto doc = db_.client().get(coll(project), name)) return CollectionMeta::fromJson(*doc);
  56. return std::nullopt;
  57. } catch (const smartbotic::database::Client::Error& e) {
  58. // Keep the message that says what we could not determine. The global
  59. // handler would also map this to 503 now, but generically: "cannot
  60. // determine whether collection 'x' exists" tells the caller which
  61. // question went unanswered, and that is worth preserving on the one
  62. // path where a wrong answer is an unretryable 404.
  63. ApiError mapped = apiErrorFor(e);
  64. if (mapped.code == ErrCode::Unavailable)
  65. throw ApiError(ErrCode::Unavailable, "db_error",
  66. "cannot determine whether collection '" + name +
  67. "' exists: the database is not reachable");
  68. throw mapped;
  69. }
  70. }
  71. std::vector<CollectionMeta> CollectionRegistry::list(const std::string& project) {
  72. std::vector<CollectionMeta> out;
  73. smartbotic::database::Client::QueryOptions opts;
  74. opts.limit = 100000;
  75. for (const auto& doc : db_.client().find(coll(project), opts))
  76. out.push_back(CollectionMeta::fromJson(doc));
  77. return out;
  78. }
  79. bool CollectionRegistry::remove(const std::string& project, const std::string& name) {
  80. return db_.client().remove(coll(project), name);
  81. }
  82. } // namespace svapi