|
@@ -2387,6 +2387,198 @@ TEST_F(ApiFixture, ARecycledCollectionNameInheritsNoConstraint) {
|
|
|
// the vector so a read-modify-write cannot put it back. Measured against the
|
|
// the vector so a read-modify-write cannot put it back. Measured against the
|
|
|
// database before fixing: a merge PATCH preserves the vector, an update drops
|
|
// database before fixing: a merge PATCH preserves the vector, an update drops
|
|
|
// it. On data that cost money to produce.
|
|
// it. On data that cost money to produce.
|
|
|
|
|
+TEST_F(ApiFixture, ExportingAVectorCollectionRoundTripsThroughOurOwnApi) {
|
|
|
|
|
+ // The property that matters, and the only one worth asserting: a collection
|
|
|
|
|
+ // exported and replayed through our OWN write route comes back searchable.
|
|
|
|
|
+ //
|
|
|
|
|
+ // This is the half of a backup that was impossible before database 2.13.0 -
|
|
|
|
|
+ // no read returned a stored vector, so an "export" built on the read routes
|
|
|
|
|
+ // produced documents that restore as rows no search can find. A consumer
|
|
|
|
|
+ // has had a documented hole in their backup where the embeddings should be.
|
|
|
|
|
+ //
|
|
|
|
|
+ // So the test restores and SEARCHES. Asserting that the export contained N
|
|
|
|
|
+ // lines would be asserting a mechanism; asserting the restored row is found
|
|
|
|
|
+ // by a similarity search is asserting the outcome.
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ const std::string ct = "application/json";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"name","expsrc"},{"kind","vector"},
|
|
|
|
|
+ {"vector_dimension",4}}.dump(), ct)->status, 201);
|
|
|
|
|
+ // Distinct vectors, so "is it its own nearest neighbour" is a meaningful
|
|
|
|
|
+ // question - identical vectors tie and the assertion would prove nothing.
|
|
|
|
|
+ const std::vector<std::vector<double>> vecs = {
|
|
|
|
|
+ {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0},
|
|
|
|
|
+ {0.0, 0.0, 1.0, 0.0}, {0.25, 0.5, 0.75, 1.0}};
|
|
|
|
|
+ std::vector<std::string> ids;
|
|
|
|
|
+ for (size_t i = 0; i < vecs.size(); ++i) {
|
|
|
|
|
+ auto r = c.Post((base + "/expsrc/vectors").c_str(),
|
|
|
|
|
+ nlohmann::json{{"vector", vecs[i]},
|
|
|
|
|
+ {"metadata", {{"tag", "t" + std::to_string(i)}}}}.dump(), ct);
|
|
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 201) << r->body;
|
|
|
|
|
+ ids.push_back(nlohmann::json::parse(r->body).value("id", ""));
|
|
|
|
|
+ ASSERT_FALSE(ids.back().empty());
|
|
|
|
|
+ }
|
|
|
|
|
+ // One row deliberately WITHOUT a vector, written through the documents
|
|
|
|
|
+ // route before 0.8.3 would have refused it - so instead, prove the export
|
|
|
|
|
+ // reports a json-collection row faithfully. Here we simply check the vector
|
|
|
|
|
+ // collection's own states.
|
|
|
|
|
+
|
|
|
|
|
+ // --- the export ------------------------------------------------------
|
|
|
|
|
+ auto exp = c.Get((base + "/expsrc/export").c_str());
|
|
|
|
|
+ ASSERT_TRUE(exp);
|
|
|
|
|
+ ASSERT_EQ(exp->status, 200) << exp->body;
|
|
|
|
|
+ EXPECT_NE(exp->get_header_value("Content-Type").find("ndjson"), std::string::npos);
|
|
|
|
|
+
|
|
|
|
|
+ // Parse NDJSON: one object per line, which is the unit of recovery.
|
|
|
|
|
+ std::vector<nlohmann::json> lines;
|
|
|
|
|
+ {
|
|
|
|
|
+ std::istringstream is(exp->body);
|
|
|
|
|
+ std::string line;
|
|
|
|
|
+ while (std::getline(is, line)) if (!line.empty()) lines.push_back(nlohmann::json::parse(line));
|
|
|
|
|
+ }
|
|
|
|
|
+ ASSERT_GE(lines.size(), vecs.size() + 2u) << "header + rows + trailer";
|
|
|
|
|
+ EXPECT_EQ(lines.front().value("type", ""), "header");
|
|
|
|
|
+ EXPECT_EQ(lines.front().value("vector_dimension", 0), 4);
|
|
|
|
|
+ EXPECT_EQ(lines.front().value("include_vectors", false), true);
|
|
|
|
|
+ EXPECT_EQ(lines.front().value("vector_format", ""), "array");
|
|
|
|
|
+ EXPECT_EQ(lines.back().value("type", ""), "trailer");
|
|
|
|
|
+ EXPECT_EQ(lines.back().value("complete", false), true)
|
|
|
|
|
+ << "an uninterrupted export must say so, or a half one is indistinguishable";
|
|
|
|
|
+ EXPECT_EQ(lines.back().value("rows_sent", 0u), vecs.size());
|
|
|
|
|
+
|
|
|
|
|
+ // Every row carries its id, its state, and a vector of the right width.
|
|
|
|
|
+ size_t present = 0;
|
|
|
|
|
+ std::map<std::string, std::vector<double>> exported;
|
|
|
|
|
+ for (const auto& l : lines) {
|
|
|
|
|
+ if (l.value("type", "") != "row") continue;
|
|
|
|
|
+ ASSERT_FALSE(l.value("id", "").empty()) << "a row without an id cannot be restored";
|
|
|
|
|
+ EXPECT_EQ(l.value("vector_state", ""), "present");
|
|
|
|
|
+ ASSERT_TRUE(l.contains("vector"));
|
|
|
|
|
+ ASSERT_EQ(l["vector"].size(), 4u);
|
|
|
|
|
+ exported[l["id"]] = l["vector"].get<std::vector<double>>();
|
|
|
|
|
+ ++present;
|
|
|
|
|
+ }
|
|
|
|
|
+ EXPECT_EQ(present, vecs.size());
|
|
|
|
|
+
|
|
|
|
|
+ // --- the restore, through our OWN document write ---------------------
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"name","expdst"},{"kind","vector"},
|
|
|
|
|
+ {"vector_dimension",4}}.dump(), ct)->status, 201);
|
|
|
|
|
+ for (const auto& l : lines) {
|
|
|
|
|
+ if (l.value("type", "") != "row") continue;
|
|
|
|
|
+ nlohmann::json doc = l["document"];
|
|
|
|
|
+ doc["_vector"] = l["vector"]; // exactly what the docs tell a caller to do
|
|
|
|
|
+ auto put = c.Put((base + "/expdst/documents/" + l.value("id", "")).c_str(), doc.dump(), ct);
|
|
|
|
|
+ ASSERT_TRUE(put);
|
|
|
|
|
+ EXPECT_EQ(put->status, 201) << put->body;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // --- the assertion that matters: searchable again --------------------
|
|
|
|
|
+ for (size_t i = 0; i < vecs.size(); ++i) {
|
|
|
|
|
+ auto r = c.Post((base + "/expdst/search").c_str(),
|
|
|
|
|
+ nlohmann::json{{"query_vector", vecs[i]},{"top_k",1}}.dump(), ct);
|
|
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 200) << r->body;
|
|
|
|
|
+ const auto hits = nlohmann::json::parse(r->body)["results"];
|
|
|
|
|
+ ASSERT_EQ(hits.size(), 1u) << "a restored row must be findable, which is the whole point";
|
|
|
|
|
+ EXPECT_EQ(hits[0].value("id", ""), ids[i])
|
|
|
|
|
+ << "the restored row must be its OWN nearest neighbour";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // And the vectors came back byte-for-byte, not merely close.
|
|
|
|
|
+ auto exp2 = c.Get((base + "/expdst/export").c_str());
|
|
|
|
|
+ ASSERT_TRUE(exp2); ASSERT_EQ(exp2->status, 200);
|
|
|
|
|
+ std::istringstream is2(exp2->body);
|
|
|
|
|
+ std::string l2;
|
|
|
|
|
+ size_t compared = 0;
|
|
|
|
|
+ while (std::getline(is2, l2)) {
|
|
|
|
|
+ if (l2.empty()) continue;
|
|
|
|
|
+ const auto j = nlohmann::json::parse(l2);
|
|
|
|
|
+ if (j.value("type", "") != "row") continue;
|
|
|
|
|
+ const auto it = exported.find(j.value("id", ""));
|
|
|
|
|
+ ASSERT_NE(it, exported.end());
|
|
|
|
|
+ EXPECT_EQ(j["vector"].get<std::vector<double>>(), it->second)
|
|
|
|
|
+ << "a restored vector must be identical, not approximate";
|
|
|
|
|
+ ++compared;
|
|
|
|
|
+ }
|
|
|
|
|
+ EXPECT_EQ(compared, vecs.size());
|
|
|
|
|
+
|
|
|
|
|
+ (void)c.Delete((base + "/expsrc").c_str());
|
|
|
|
|
+ (void)c.Delete((base + "/expdst").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, ExportRefusesVectorsFromAJsonCollectionAndSaysWhy) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ const std::string ct = "application/json";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"name","expjson"},{"kind","json"}}.dump(), ct)->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Post((base + "/expjson/documents").c_str(),
|
|
|
|
|
+ nlohmann::json{{"a",1}}.dump(), ct)->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ // Asking for vectors from a collection that cannot have them is a caller
|
|
|
|
|
+ // mistake worth naming, and it must be refused BEFORE the stream starts -
|
|
|
|
|
+ // once a 200 is on the wire a refusal can only look like truncation.
|
|
|
|
|
+ auto bad = c.Get((base + "/expjson/export?include_vectors=true").c_str());
|
|
|
|
|
+ ASSERT_TRUE(bad);
|
|
|
|
|
+ EXPECT_EQ(bad->status, 422) << bad->body;
|
|
|
|
|
+ EXPECT_NE(bad->body.find("not_vector"), std::string::npos) << bad->body;
|
|
|
|
|
+
|
|
|
|
|
+ // Without the flag it exports happily, with vectors off by default.
|
|
|
|
|
+ auto ok = c.Get((base + "/expjson/export").c_str());
|
|
|
|
|
+ ASSERT_TRUE(ok);
|
|
|
|
|
+ ASSERT_EQ(ok->status, 200) << ok->body;
|
|
|
|
|
+ std::istringstream is(ok->body);
|
|
|
|
|
+ std::string line;
|
|
|
|
|
+ ASSERT_TRUE(std::getline(is, line));
|
|
|
|
|
+ const auto header = nlohmann::json::parse(line);
|
|
|
|
|
+ EXPECT_EQ(header.value("include_vectors", true), false);
|
|
|
|
|
+ EXPECT_EQ(header.value("vector_dimension", -1), 0);
|
|
|
|
|
+
|
|
|
|
|
+ (void)c.Delete((base + "/expjson").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, ExportResumesFromACursorWithoutRepeatingARow) {
|
|
|
|
|
+ // A backup tool resumes; the trailer's last_id is how. Asserting it never
|
|
|
|
|
+ // repeats matters more than asserting it continues, because a duplicate on
|
|
|
|
|
+ // restore is silent where a gap is at least countable.
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ const std::string ct = "application/json";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"name","expres"},{"kind","json"}}.dump(), ct)->status, 201);
|
|
|
|
|
+ for (int i = 0; i < 12; ++i)
|
|
|
|
|
+ ASSERT_EQ(c.Post((base + "/expres/documents").c_str(),
|
|
|
|
|
+ nlohmann::json{{"id","row-" + std::to_string(i)},{"n",i}}.dump(),
|
|
|
|
|
+ ct)->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto idsOf = [&](const std::string& url) {
|
|
|
|
|
+ std::set<std::string> out;
|
|
|
|
|
+ auto r = c.Get(url.c_str());
|
|
|
|
|
+ EXPECT_TRUE(r); if (!r) return out;
|
|
|
|
|
+ EXPECT_EQ(r->status, 200) << r->body;
|
|
|
|
|
+ std::istringstream is(r->body);
|
|
|
|
|
+ std::string line;
|
|
|
|
|
+ while (std::getline(is, line)) {
|
|
|
|
|
+ if (line.empty()) continue;
|
|
|
|
|
+ const auto j = nlohmann::json::parse(line);
|
|
|
|
|
+ if (j.value("type", "") == "row") out.insert(j.value("id", ""));
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
|
|
+ };
|
|
|
|
|
+ const auto all = idsOf(base + "/expres/export");
|
|
|
|
|
+ ASSERT_EQ(all.size(), 12u);
|
|
|
|
|
+
|
|
|
|
|
+ // Resume from the sixth id: everything after it, and nothing at or before.
|
|
|
|
|
+ const auto after = idsOf(base + "/expres/export?after_id=row-5");
|
|
|
|
|
+ EXPECT_EQ(after.count("row-5"), 0u) << "the cursor row itself must not repeat";
|
|
|
|
|
+ EXPECT_FALSE(after.empty());
|
|
|
|
|
+ for (const auto& id : after) EXPECT_GT(id, "row-5") << "resumed ids must be strictly after";
|
|
|
|
|
+
|
|
|
|
|
+ (void)c.Delete((base + "/expres").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
TEST_F(ApiFixture, InsertingAVectorlessDocumentIntoAVectorCollectionIsRefused) {
|
|
TEST_F(ApiFixture, InsertingAVectorlessDocumentIntoAVectorCollectionIsRefused) {
|
|
|
// The sibling of the PUT refusal below, through a different door. Reported
|
|
// The sibling of the PUT refusal below, through a different door. Reported
|
|
|
// from production with the only symptom it has: a 12448-document collection
|
|
// from production with the only symptom it has: a 12448-document collection
|