|
|
@@ -91,14 +91,6 @@ bool StorageClient::isConnected() const {
|
|
|
return impl_ && impl_->client_ && impl_->client_->isConnected();
|
|
|
}
|
|
|
|
|
|
-Result<nlohmann::json> StorageClient::get(const std::string& collection, const std::string& id) {
|
|
|
- auto opt = impl_->client_->get(collection, id);
|
|
|
- if (!opt) {
|
|
|
- return Error(ErrorCode::DocumentNotFound, "Document not found: " + collection + "/" + id);
|
|
|
- }
|
|
|
- return *opt;
|
|
|
-}
|
|
|
-
|
|
|
namespace {
|
|
|
|
|
|
// "Insert failed: credentials" told a reader nothing. 2.12.0 carries a reason,
|
|
|
@@ -108,8 +100,16 @@ std::string describeDbFailure(const char* what,
|
|
|
const std::string& collection,
|
|
|
const std::string& id,
|
|
|
const database::Client::Error& e) {
|
|
|
- std::string detail = std::string(what) + " failed: " + collection;
|
|
|
- if (!id.empty()) detail += "/" + id;
|
|
|
+ // An instance-wide read (listCollections, listViews) names no collection,
|
|
|
+ // so the target is omitted rather than left as an empty gap - that read
|
|
|
+ // "listCollections failed: : failed to connect".
|
|
|
+ std::string detail = std::string(what) + " failed";
|
|
|
+ if (!collection.empty()) {
|
|
|
+ detail += ": " + collection;
|
|
|
+ if (!id.empty()) detail += "/" + id;
|
|
|
+ } else if (!id.empty()) {
|
|
|
+ detail += ": " + id;
|
|
|
+ }
|
|
|
detail += ": ";
|
|
|
detail += e.what();
|
|
|
|
|
|
@@ -131,8 +131,46 @@ std::string describeDbFailure(const char* what,
|
|
|
return detail;
|
|
|
}
|
|
|
|
|
|
+// From client 2.12.4 a failed READ throws instead of answering "nothing there".
|
|
|
+// Before, a blip was indistinguishable from a genuine absence: get() returned
|
|
|
+// nullopt, the list reads returned empty and getCollectionConfig() returned the
|
|
|
+// DEFAULTS. Upstream changed it because a consumer turned one such blip into a
|
|
|
+// definite 404 and lost a six-minute ingest.
|
|
|
+//
|
|
|
+// Every read below therefore catches. The exception must not cross this
|
|
|
+// adapter: StorageClient's whole contract is that it returns a Result and never
|
|
|
+// throws, and two of its callers depend on that for more than tidiness - the
|
|
|
+// scheduler's execution-finished check runs on the scheduler thread, where an
|
|
|
+// escaping exception is std::terminate, and the runner's storage.get() binding
|
|
|
+// is called from QuickJS, which a C++ exception unwinds straight through.
|
|
|
+//
|
|
|
+// A genuine absence still comes back as nullopt/empty and does not throw, so
|
|
|
+// "not found" keeps its own code and only a real failure gets the transient one.
|
|
|
+Error dbReadError(const char* what,
|
|
|
+ const std::string& collection,
|
|
|
+ const std::string& id,
|
|
|
+ const database::Client::Error& e) {
|
|
|
+ return Error(mapDbError(e.code()), describeDbFailure(what, collection, id, e));
|
|
|
+}
|
|
|
+
|
|
|
} // namespace
|
|
|
|
|
|
+Result<nlohmann::json> StorageClient::get(const std::string& collection, const std::string& id) {
|
|
|
+ std::optional<nlohmann::json> opt;
|
|
|
+ try {
|
|
|
+ opt = impl_->client_->get(collection, id);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("Get", collection, id, e);
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ return Error(ErrorCode::DatabaseError,
|
|
|
+ "Get failed: " + collection + "/" + id + ": " + e.what());
|
|
|
+ }
|
|
|
+ if (!opt) {
|
|
|
+ return Error(ErrorCode::DocumentNotFound, "Document not found: " + collection + "/" + id);
|
|
|
+ }
|
|
|
+ return *opt;
|
|
|
+}
|
|
|
+
|
|
|
Result<std::string> StorageClient::insert(const std::string& collection,
|
|
|
const nlohmann::json& data,
|
|
|
const std::string& id,
|
|
|
@@ -287,9 +325,21 @@ Result<QueryResult> StorageClient::query(const std::string& collection, const Qu
|
|
|
// instead of returning an empty result, so the miss is not mistaken for
|
|
|
// an empty collection. Convert it to our Result contract - callers of
|
|
|
// query() expect an Error, never an exception.
|
|
|
+ // The classification matters as much as the catch. This caught
|
|
|
+ // std::exception and answered CollectionNotFound for every one of them, so
|
|
|
+ // once 2.12.4 made a failed read throw, an outage mid-query reported "no
|
|
|
+ // such collection" - a definite, unretryable answer, which is the very
|
|
|
+ // mistake the upstream change was written to stop. A thrown Client::Error
|
|
|
+ // carries the real code; only an unknown exception is still read as a miss.
|
|
|
dbc::Client::FindResult fr;
|
|
|
try {
|
|
|
fr = impl_->client_->findWithMetrics(collection, up);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ if (e.code() == Client::ErrorCode::NotFound) {
|
|
|
+ return Error(ErrorCode::CollectionNotFound,
|
|
|
+ "Query failed for '" + collection + "': " + e.what());
|
|
|
+ }
|
|
|
+ return dbReadError("Query", collection, "", e);
|
|
|
} catch (const std::exception& e) {
|
|
|
return Error(ErrorCode::CollectionNotFound,
|
|
|
"Query failed for '" + collection + "': " + e.what());
|
|
|
@@ -405,21 +455,38 @@ std::shared_ptr<void> StorageClient::subscribeWithActor(
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-std::vector<CollectionInfo> StorageClient::listCollectionInfos() {
|
|
|
+Result<std::vector<CollectionInfo>> StorageClient::listCollectionInfos() {
|
|
|
+ auto names = listCollections();
|
|
|
+ if (names.failed()) return Error(names.error().code(), names.error().message());
|
|
|
+
|
|
|
std::vector<CollectionInfo> out;
|
|
|
- for (const auto& name : listCollections()) {
|
|
|
+ for (const auto& name : names.value()) {
|
|
|
auto info = getCollectionInfo(name);
|
|
|
if (info.ok()) {
|
|
|
// The name as this project knows it, not as the server spelled it.
|
|
|
info.value().name = name;
|
|
|
out.push_back(info.value());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // A collection dropped between the listing and this lookup is an
|
|
|
+ // ordinary race and is simply left out. Anything else is not: showing
|
|
|
+ // a short list as though it were complete is how a page comes to say
|
|
|
+ // the data is gone.
|
|
|
+ if (info.error().code() != ErrorCode::CollectionNotFound &&
|
|
|
+ info.error().code() != ErrorCode::DocumentNotFound) {
|
|
|
+ return Error(info.error().code(), info.error().message());
|
|
|
}
|
|
|
}
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
Result<CollectionInfo> StorageClient::getCollectionInfo(const std::string& name) {
|
|
|
- auto upstream = impl_->client_->getCollectionInfo(name);
|
|
|
+ std::optional<database::Client::CollectionInfo> upstream;
|
|
|
+ try {
|
|
|
+ upstream = impl_->client_->getCollectionInfo(name);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("getCollectionInfo", name, "", e);
|
|
|
+ }
|
|
|
if (!upstream.has_value()) {
|
|
|
return Error(ErrorCode::CollectionNotFound, "No such collection: " + name);
|
|
|
}
|
|
|
@@ -453,7 +520,15 @@ Result<void> StorageClient::configureCollection(const std::string& collection,
|
|
|
}
|
|
|
|
|
|
Result<CollectionConfig> StorageClient::getCollectionConfig(const std::string& collection) {
|
|
|
- auto upstream = impl_->client_->getCollectionConfig(collection);
|
|
|
+ // Worth singling out: before 2.12.4 a failed read here answered the
|
|
|
+ // DEFAULTS, so an outage reported versioning as disabled on a collection
|
|
|
+ // that has it on. There was no way for a caller to tell.
|
|
|
+ database::Client::CollectionConfig upstream;
|
|
|
+ try {
|
|
|
+ upstream = impl_->client_->getCollectionConfig(collection);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("getCollectionConfig", collection, "", e);
|
|
|
+ }
|
|
|
CollectionConfig cfg;
|
|
|
cfg.timestamp_precision = upstream.timestampPrecision;
|
|
|
cfg.versioning_enabled = upstream.versioningEnabled;
|
|
|
@@ -478,9 +553,15 @@ Result<void> StorageClient::dropIndex(const std::string& collection, const std::
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
-std::vector<StorageClient::IndexInfo> StorageClient::listIndexes(const std::string& collection) {
|
|
|
+Result<std::vector<StorageClient::IndexInfo>> StorageClient::listIndexes(const std::string& collection) {
|
|
|
+ std::vector<database::Client::IndexDefinition> defs;
|
|
|
+ try {
|
|
|
+ defs = impl_->client_->listIndexes(collection);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("listIndexes", collection, "", e);
|
|
|
+ }
|
|
|
std::vector<IndexInfo> out;
|
|
|
- for (const auto& def : impl_->client_->listIndexes(collection)) {
|
|
|
+ for (const auto& def : defs) {
|
|
|
out.push_back(IndexInfo{def.field, static_cast<int64_t>(def.distinctValues),
|
|
|
static_cast<int64_t>(def.entries)});
|
|
|
}
|
|
|
@@ -499,12 +580,19 @@ Result<void> StorageClient::dropCollection(const std::string& name) {
|
|
|
// Config::project itself, so an unfiltered list both leaks other projects'
|
|
|
// collections and hands callers names that resolve to nothing when read back.
|
|
|
// Scope to our own project and strip the prefix, so the names round-trip.
|
|
|
-std::vector<std::string> StorageClient::listCollections() {
|
|
|
+Result<std::vector<std::string>> StorageClient::listCollections() {
|
|
|
const bool is_default = project_isDefault(impl_->project_);
|
|
|
const std::string prefix = (is_default ? std::string("default") : impl_->project_) + ":";
|
|
|
|
|
|
+ std::vector<std::string> upstream_names;
|
|
|
+ try {
|
|
|
+ upstream_names = impl_->client_->listCollections();
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("listCollections", "", "", e);
|
|
|
+ }
|
|
|
+
|
|
|
std::vector<std::string> out;
|
|
|
- for (const auto& name : impl_->client_->listCollections()) {
|
|
|
+ for (const auto& name : upstream_names) {
|
|
|
if (name.rfind(prefix, 0) == 0) {
|
|
|
out.push_back(name.substr(prefix.size()));
|
|
|
continue;
|
|
|
@@ -521,8 +609,21 @@ std::vector<std::string> StorageClient::listCollections() {
|
|
|
//
|
|
|
// So the name is asked about rather than guessed at: a lookup is
|
|
|
// project-scoped, and answers only for a collection that is really ours.
|
|
|
- if (getCollectionInfo(name).ok()) {
|
|
|
+ //
|
|
|
+ // Only a definite "no such collection" excludes it. Since 2.12.4 this
|
|
|
+ // probe can also fail transiently, and treating that as "not ours"
|
|
|
+ // would quietly shorten the list - one timeout here and a collection
|
|
|
+ // disappears from the database page while it is still there.
|
|
|
+ auto probe = getCollectionInfo(name);
|
|
|
+ if (probe.ok()) {
|
|
|
out.push_back(name);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (probe.error().code() != ErrorCode::CollectionNotFound &&
|
|
|
+ probe.error().code() != ErrorCode::DocumentNotFound) {
|
|
|
+ return Error(probe.error().code(),
|
|
|
+ "Could not tell whether '" + name + "' belongs to this project: " +
|
|
|
+ probe.error().message());
|
|
|
}
|
|
|
}
|
|
|
(void)is_default;
|
|
|
@@ -535,7 +636,12 @@ std::vector<std::string> StorageClient::listCollections() {
|
|
|
Result<nlohmann::json> StorageClient::getVersion(const std::string& collection,
|
|
|
const std::string& id,
|
|
|
int64_t version) {
|
|
|
- auto entry = impl_->client_->getDocumentVersion(collection, id, static_cast<uint64_t>(version));
|
|
|
+ std::optional<database::Client::VersionEntry> entry;
|
|
|
+ try {
|
|
|
+ entry = impl_->client_->getDocumentVersion(collection, id, static_cast<uint64_t>(version));
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("getVersion", collection, id, e);
|
|
|
+ }
|
|
|
if (!entry) return Error(ErrorCode::DocumentNotFound, "Version not found");
|
|
|
return entry->data;
|
|
|
}
|
|
|
@@ -544,9 +650,14 @@ Result<VersionListResult> StorageClient::listVersions(const std::string& collect
|
|
|
const std::string& id,
|
|
|
int32_t limit,
|
|
|
int32_t offset) {
|
|
|
- auto h = impl_->client_->getVersionHistory(collection, id,
|
|
|
- static_cast<uint32_t>(limit),
|
|
|
- static_cast<uint32_t>(offset));
|
|
|
+ database::Client::VersionHistoryResult h;
|
|
|
+ try {
|
|
|
+ h = impl_->client_->getVersionHistory(collection, id,
|
|
|
+ static_cast<uint32_t>(limit),
|
|
|
+ static_cast<uint32_t>(offset));
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("listVersions", collection, id, e);
|
|
|
+ }
|
|
|
VersionListResult out;
|
|
|
out.total_count = static_cast<int64_t>(h.totalCount);
|
|
|
out.has_more = (offset + static_cast<int32_t>(h.versions.size())) <
|
|
|
@@ -584,9 +695,15 @@ Result<void> StorageClient::dropView(const std::string& name) {
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
-std::vector<ViewInfo> StorageClient::listViews() {
|
|
|
+Result<std::vector<ViewInfo>> StorageClient::listViews() {
|
|
|
+ std::vector<database::Client::ViewDefinition> defs;
|
|
|
+ try {
|
|
|
+ defs = impl_->client_->listViews();
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ return dbReadError("listViews", "", "", e);
|
|
|
+ }
|
|
|
std::vector<ViewInfo> out;
|
|
|
- for (const auto& v : impl_->client_->listViews()) {
|
|
|
+ for (const auto& v : defs) {
|
|
|
ViewInfo info;
|
|
|
info.name = v.name;
|
|
|
info.collection = v.collection;
|
|
|
@@ -635,9 +752,18 @@ Result<std::vector<uint8_t>> StorageClient::downloadFile(const std::string& id)
|
|
|
// return empty bytes. Without this the exception escapes the adapter and
|
|
|
// every caller's Result handling is bypassed - and once files carry a TTL,
|
|
|
// fetching an expired one is an ordinary event rather than a fault.
|
|
|
+ // Same distinction as query(): a missing or expired file is an ordinary
|
|
|
+ // event, but an outage is not, and reporting both as DocumentNotFound told
|
|
|
+ // the caller a file was gone when the daemon was merely unreachable.
|
|
|
std::vector<uint8_t> bytes;
|
|
|
try {
|
|
|
bytes = impl_->client_->downloadFile(id);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ if (e.code() == Client::ErrorCode::NotFound) {
|
|
|
+ return Error(ErrorCode::DocumentNotFound,
|
|
|
+ "File not found: " + id + " (" + e.what() + ")");
|
|
|
+ }
|
|
|
+ return dbReadError("downloadFile", "files", id, e);
|
|
|
} catch (const std::exception& e) {
|
|
|
return Error(ErrorCode::DocumentNotFound,
|
|
|
"File not found: " + id + " (" + e.what() + ")");
|