|
@@ -5,6 +5,10 @@
|
|
|
|
|
|
|
|
#include <spdlog/spdlog.h>
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
|
|
|
|
|
+#include <openssl/evp.h>
|
|
|
|
|
+
|
|
|
|
|
+#include <ctime>
|
|
|
|
|
+
|
|
|
/// Collection export: every document, and for a vector collection the stored
|
|
/// Collection export: every document, and for a vector collection the stored
|
|
|
/// embedding with it.
|
|
/// embedding with it.
|
|
|
///
|
|
///
|
|
@@ -110,6 +114,61 @@ std::string base64Floats(const std::vector<float>& v) {
|
|
|
return out;
|
|
return out;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// A rolling SHA-256 over the export's CONTENT, finished into the trailer.
|
|
|
|
|
+///
|
|
|
|
|
+/// Asked for by the consumer who will use this, who then argued against their
|
|
|
|
|
+/// own request - `rows_sent` already catches loss, and a corrupted NDJSON line
|
|
|
|
|
+/// fails to parse, so the only genuinely undetectable case is a vector whose
|
|
|
|
|
+/// floats were mangled but still parse as numbers. That is unlikely over TLS.
|
|
|
|
|
+///
|
|
|
|
|
+/// Added anyway because it is nearly free and it is the difference between
|
|
|
|
|
+/// "probably intact" and "provably intact". It hashes the id and the RAW
|
|
|
|
|
+/// VECTOR BYTES rather than the serialised line, so the digest is identical
|
|
|
|
|
+/// whichever `vector_format` was requested - which means a caller can export
|
|
|
|
|
+/// as base64, restore, re-export as an array, and still compare digests.
|
|
|
|
|
+/// Hashing the emitted text would have made the two formats incomparable, and
|
|
|
|
|
+/// the whole point of a digest is comparing things.
|
|
|
|
|
+class ContentDigest {
|
|
|
|
|
+public:
|
|
|
|
|
+ ContentDigest() : ctx_(EVP_MD_CTX_new()) {
|
|
|
|
|
+ if (ctx_ && EVP_DigestInit_ex(ctx_, EVP_sha256(), nullptr) != 1) reset();
|
|
|
|
|
+ }
|
|
|
|
|
+ ~ContentDigest() { reset(); }
|
|
|
|
|
+ ContentDigest(const ContentDigest&) = delete;
|
|
|
|
|
+ ContentDigest& operator=(const ContentDigest&) = delete;
|
|
|
|
|
+
|
|
|
|
|
+ void add(const std::string& id, const std::vector<float>& vec) {
|
|
|
|
|
+ if (!ctx_) return;
|
|
|
|
|
+ // The id, then a separator that cannot occur in it, then the vector's
|
|
|
|
|
+ // bytes. Without the separator "ab" + "c" and "a" + "bc" would hash
|
|
|
|
|
+ // alike, which is the classic way a digest stops distinguishing.
|
|
|
|
|
+ EVP_DigestUpdate(ctx_, id.data(), id.size());
|
|
|
|
|
+ EVP_DigestUpdate(ctx_, "\x1f", 1);
|
|
|
|
|
+ if (!vec.empty())
|
|
|
|
|
+ EVP_DigestUpdate(ctx_, vec.data(), vec.size() * sizeof(float));
|
|
|
|
|
+ EVP_DigestUpdate(ctx_, "\x1e", 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Hex, or empty when the digest could not be computed - never a wrong
|
|
|
|
|
+ /// value dressed as a right one.
|
|
|
|
|
+ std::string finish() {
|
|
|
|
|
+ if (!ctx_) return {};
|
|
|
|
|
+ unsigned char out[EVP_MAX_MD_SIZE];
|
|
|
|
|
+ unsigned int len = 0;
|
|
|
|
|
+ if (EVP_DigestFinal_ex(ctx_, out, &len) != 1) { reset(); return {}; }
|
|
|
|
|
+ static const char* hex = "0123456789abcdef";
|
|
|
|
|
+ std::string s;
|
|
|
|
|
+ s.reserve(len * 2);
|
|
|
|
|
+ for (unsigned i = 0; i < len; ++i) { s += hex[out[i] >> 4]; s += hex[out[i] & 0xF]; }
|
|
|
|
|
+ reset();
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+private:
|
|
|
|
|
+ void reset() { if (ctx_) { EVP_MD_CTX_free(ctx_); ctx_ = nullptr; } }
|
|
|
|
|
+ EVP_MD_CTX* ctx_ = nullptr;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
} // namespace
|
|
} // namespace
|
|
|
|
|
|
|
|
void registerExportRoutes(ApiServer& s) {
|
|
void registerExportRoutes(ApiServer& s) {
|
|
@@ -149,6 +208,7 @@ void registerExportRoutes(ApiServer& s) {
|
|
|
// stream cap the trailer says complete=false with a cursor.
|
|
// stream cap the trailer says complete=false with a cursor.
|
|
|
uint64_t written = 0;
|
|
uint64_t written = 0;
|
|
|
bool wroteHeader = false;
|
|
bool wroteHeader = false;
|
|
|
|
|
+ ContentDigest digest;
|
|
|
auto emit = [&sink](const nlohmann::json& j) {
|
|
auto emit = [&sink](const nlohmann::json& j) {
|
|
|
const std::string line = j.dump() + "\n";
|
|
const std::string line = j.dump() + "\n";
|
|
|
return sink.write(line.data(), line.size());
|
|
return sink.write(line.data(), line.size());
|
|
@@ -159,6 +219,16 @@ void registerExportRoutes(ApiServer& s) {
|
|
|
[&](const smartbotic::database::Client::ExportHeader& h) {
|
|
[&](const smartbotic::database::Client::ExportHeader& h) {
|
|
|
wroteHeader = emit({{"type", "header"},
|
|
wroteHeader = emit({{"type", "header"},
|
|
|
{"collection", name},
|
|
{"collection", name},
|
|
|
|
|
+ // "as of when" is the first
|
|
|
|
|
+ // question anyone asks of a
|
|
|
|
|
+ // backup, and the FILE should
|
|
|
|
|
+ // answer it - a filesystem
|
|
|
|
|
+ // mtime does not survive a
|
|
|
|
|
+ // copy, and only the server
|
|
|
|
|
+ // knows when the snapshot was
|
|
|
|
|
+ // actually taken.
|
|
|
|
|
+ {"snapshot_at",
|
|
|
|
|
+ static_cast<uint64_t>(std::time(nullptr))},
|
|
|
{"vector_dimension", h.vectorDimension},
|
|
{"vector_dimension", h.vectorDimension},
|
|
|
{"vector_dtype", h.vectorDtype},
|
|
{"vector_dtype", h.vectorDtype},
|
|
|
{"rows_at_snapshot", h.rowsAtSnapshot},
|
|
{"rows_at_snapshot", h.rowsAtSnapshot},
|
|
@@ -186,6 +256,10 @@ void registerExportRoutes(ApiServer& s) {
|
|
|
else line["vector"] = r.vector;
|
|
else line["vector"] = r.vector;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // Hashed for every row, including one with no
|
|
|
|
|
+ // vector: a row that exists is part of the content
|
|
|
|
|
+ // whether or not it has an embedding.
|
|
|
|
|
+ digest.add(r.id, r.vector);
|
|
|
++written;
|
|
++written;
|
|
|
// Returning false stops the export, which is how a
|
|
// Returning false stops the export, which is how a
|
|
|
// disconnected client stops the database read
|
|
// disconnected client stops the database read
|
|
@@ -193,10 +267,15 @@ void registerExportRoutes(ApiServer& s) {
|
|
|
return emit(line);
|
|
return emit(line);
|
|
|
},
|
|
},
|
|
|
q.batchRows);
|
|
q.batchRows);
|
|
|
- emit({{"type", "trailer"},
|
|
|
|
|
- {"rows_sent", summary.rowsSent},
|
|
|
|
|
- {"last_id", summary.lastId},
|
|
|
|
|
- {"complete", summary.complete}});
|
|
|
|
|
|
|
+ nlohmann::json trailer{{"type", "trailer"},
|
|
|
|
|
+ {"rows_sent", summary.rowsSent},
|
|
|
|
|
+ {"last_id", summary.lastId},
|
|
|
|
|
+ {"complete", summary.complete}};
|
|
|
|
|
+ // Omitted rather than empty if it could not be computed,
|
|
|
|
|
+ // so a caller never compares against a placeholder.
|
|
|
|
|
+ if (const std::string d = digest.finish(); !d.empty())
|
|
|
|
|
+ trailer["content_sha256"] = d;
|
|
|
|
|
+ emit(trailer);
|
|
|
if (!summary.complete)
|
|
if (!summary.complete)
|
|
|
spdlog::info("export of {} ended early at '{}' after {} rows - resume with "
|
|
spdlog::info("export of {} ended early at '{}' after {} rows - resume with "
|
|
|
"after_id", qualified, summary.lastId, summary.rowsSent);
|
|
"after_id", qualified, summary.lastId, summary.rowsSent);
|