collection_registry.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include "db_gateway.hpp"
  3. #include <nlohmann/json.hpp>
  4. #include <optional>
  5. #include <string>
  6. #include <vector>
  7. namespace svapi {
  8. /// Metadata describing a managed collection.
  9. struct CollectionMeta {
  10. std::string name;
  11. std::string kind;
  12. uint32_t vectorDimension = 0;
  13. std::string embeddingModel;
  14. uint64_t createdAt = 0;
  15. static CollectionMeta fromJson(const nlohmann::json&);
  16. nlohmann::json toJson() const;
  17. };
  18. /// Project-scoped registry: one document per managed collection stored in
  19. /// qualify(project, "vectorapi_collections").
  20. class CollectionRegistry {
  21. public:
  22. explicit CollectionRegistry(DbGateway& db);
  23. /// Ensure the registry collection for `project` exists (idempotent).
  24. void ensure(const std::string& project);
  25. /// Re-open the registry collection for every given project. The DB only
  26. /// surfaces a collection through find()/list() once it has been opened in
  27. /// the current server process; without this, a restart leaves every
  28. /// per-project registry index invisible (collections appear empty / 404)
  29. /// even though the data survives. Call once at startup with all known
  30. /// projects — mirrors KeyStore::bootstrap re-opening vectorapi_keys. (issue #1)
  31. void bootstrap(const std::vector<std::string>& projects);
  32. /// Add (or overwrite) a collection entry. Returns true on success.
  33. bool add(const std::string& project, const CollectionMeta& meta);
  34. /// Look up a single collection entry by name.
  35. std::optional<CollectionMeta> get(const std::string& project, const std::string& name);
  36. /// List all collection entries for the project.
  37. std::vector<CollectionMeta> list(const std::string& project);
  38. /// Remove a collection entry. Returns true if the document was present.
  39. bool remove(const std::string& project, const std::string& name);
  40. private:
  41. DbGateway& db_;
  42. /// Qualified registry collection name for the given project.
  43. std::string coll(const std::string& project) const {
  44. return qualify(project, "vectorapi_collections");
  45. }
  46. };
  47. } // namespace svapi