|
|
@@ -0,0 +1,56 @@
|
|
|
+#include "collection_registry.hpp"
|
|
|
+#include <ctime>
|
|
|
+
|
|
|
+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);
|
|
|
+}
|
|
|
+
|
|
|
+bool CollectionRegistry::add(const std::string& project, const CollectionMeta& meta) {
|
|
|
+ CollectionMeta m = meta;
|
|
|
+ if (m.createdAt == 0) m.createdAt = static_cast<uint64_t>(std::time(nullptr));
|
|
|
+ auto [id, isNew] = db_.client().upsert(coll(project), m.toJson(), m.name);
|
|
|
+ return !id.empty();
|
|
|
+}
|
|
|
+
|
|
|
+std::optional<CollectionMeta> CollectionRegistry::get(const std::string& project,
|
|
|
+ const std::string& name) {
|
|
|
+ if (auto doc = db_.client().get(coll(project), name))
|
|
|
+ return CollectionMeta::fromJson(*doc);
|
|
|
+ return std::nullopt;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<CollectionMeta> CollectionRegistry::list(const std::string& project) {
|
|
|
+ std::vector<CollectionMeta> 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
|