|
@@ -18,7 +18,68 @@
|
|
|
|
|
|
|
|
namespace smartbotic::database {
|
|
namespace smartbotic::database {
|
|
|
|
|
|
|
|
|
|
+// v2.11.4 - defined out of line on purpose. An exception type whose members
|
|
|
|
|
+// are all inline has its typeinfo emitted in every object that uses it; with
|
|
|
|
|
+// the destructors here it lives in this library, so a throw inside the .so
|
|
|
|
|
+// and a catch in the consumer agree on the type by construction.
|
|
|
|
|
+Client::Error::Error(ErrorCode code, const std::string& message)
|
|
|
|
|
+ : std::runtime_error(message), code_(code) {}
|
|
|
|
|
+Client::Error::~Error() = default;
|
|
|
|
|
+
|
|
|
|
|
+Client::AlreadyExists::AlreadyExists(const std::string& message)
|
|
|
|
|
+ : Error(ErrorCode::AlreadyExists, message) {}
|
|
|
|
|
+Client::AlreadyExists::~AlreadyExists() = default;
|
|
|
|
|
+
|
|
|
|
|
+Client::FailedPrecondition::FailedPrecondition(const std::string& message)
|
|
|
|
|
+ : Error(ErrorCode::FailedPrecondition, message) {}
|
|
|
|
|
+Client::FailedPrecondition::~FailedPrecondition() = default;
|
|
|
|
|
+
|
|
|
|
|
+Client::PermissionDenied::PermissionDenied(const std::string& message)
|
|
|
|
|
+ : Error(ErrorCode::PermissionDenied, message) {}
|
|
|
|
|
+Client::PermissionDenied::~PermissionDenied() = default;
|
|
|
|
|
+
|
|
|
namespace {
|
|
namespace {
|
|
|
|
|
+// v2.11.4 - Client::ErrorCode mirrors grpc::StatusCode number for number, so
|
|
|
|
|
+// the conversion is a cast. The asserts pin the values callers branch on; the
|
|
|
|
|
+// gRPC status codes are fixed by its wire protocol and have never been
|
|
|
|
|
+// renumbered.
|
|
|
|
|
+static_assert(static_cast<int>(Client::ErrorCode::NotFound) == grpc::StatusCode::NOT_FOUND);
|
|
|
|
|
+static_assert(static_cast<int>(Client::ErrorCode::AlreadyExists) == grpc::StatusCode::ALREADY_EXISTS);
|
|
|
|
|
+static_assert(static_cast<int>(Client::ErrorCode::PermissionDenied) == grpc::StatusCode::PERMISSION_DENIED);
|
|
|
|
|
+static_assert(static_cast<int>(Client::ErrorCode::FailedPrecondition) == grpc::StatusCode::FAILED_PRECONDITION);
|
|
|
|
|
+static_assert(static_cast<int>(Client::ErrorCode::Unavailable) == grpc::StatusCode::UNAVAILABLE);
|
|
|
|
|
+static_assert(static_cast<int>(Client::ErrorCode::Unauthenticated) == grpc::StatusCode::UNAUTHENTICATED);
|
|
|
|
|
+
|
|
|
|
|
+Client::ErrorCode toErrorCode(grpc::StatusCode code) {
|
|
|
|
|
+ return static_cast<Client::ErrorCode>(static_cast<int>(code));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Throw the Client::Error subtype matching a failed status. `message` becomes
|
|
|
|
|
+// what(): every site passes what it threw before 2.11.4, so the text an
|
|
|
|
|
+// existing consumer logs or matches on is unchanged.
|
|
|
|
|
+[[noreturn]] void throwStatus(const grpc::Status& status, const std::string& message) {
|
|
|
|
|
+ switch (status.error_code()) {
|
|
|
|
|
+ case grpc::StatusCode::ALREADY_EXISTS: throw Client::AlreadyExists(message);
|
|
|
|
|
+ case grpc::StatusCode::FAILED_PRECONDITION: throw Client::FailedPrecondition(message);
|
|
|
|
|
+ case grpc::StatusCode::PERMISSION_DENIED: throw Client::PermissionDenied(message);
|
|
|
|
|
+ default: throw Client::Error(toErrorCode(status.error_code()), message);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Record a failure for the calls that report through their return value.
|
|
|
|
|
+// `out` is null for the original overloads, which keep their old behaviour.
|
|
|
|
|
+void recordFailure(Client::Failure* out, const grpc::Status& status) {
|
|
|
|
|
+ if (out == nullptr) return;
|
|
|
|
|
+ out->code = toErrorCode(status.error_code());
|
|
|
|
|
+ out->message = status.error_message();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void recordFailure(Client::Failure* out, Client::ErrorCode code, const std::string& message) {
|
|
|
|
|
+ if (out == nullptr) return;
|
|
|
|
|
+ out->code = code;
|
|
|
|
|
+ out->message = message;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// v1.7.0 T11 — transient transport errors that are safe to retry for idempotent
|
|
// v1.7.0 T11 — transient transport errors that are safe to retry for idempotent
|
|
|
// writes (insert with explicit ID, updateIfVersion, upsert, remove, patch).
|
|
// writes (insert with explicit ID, updateIfVersion, upsert, remove, patch).
|
|
|
// All other codes (INVALID_ARGUMENT, FAILED_PRECONDITION, NOT_FOUND, ...) are
|
|
// All other codes (INVALID_ARGUMENT, FAILED_PRECONDITION, NOT_FOUND, ...) are
|
|
@@ -264,7 +325,7 @@ public:
|
|
|
}
|
|
}
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::insert failed after retries: {}", status.error_message());
|
|
spdlog::error("Client::insert failed after retries: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return response.id();
|
|
return response.id();
|
|
@@ -288,7 +349,7 @@ public:
|
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
|
// collection.
|
|
// collection.
|
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
|
- throw std::runtime_error("access denied: get");
|
|
|
|
|
|
|
+ throw Client::PermissionDenied("access denied: get");
|
|
|
}
|
|
}
|
|
|
spdlog::error("Client::get failed: {}", status.error_message());
|
|
spdlog::error("Client::get failed: {}", status.error_message());
|
|
|
return std::nullopt;
|
|
return std::nullopt;
|
|
@@ -309,7 +370,7 @@ public:
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool update(const std::string& collection, const std::string& id, const nlohmann::json& data,
|
|
bool update(const std::string& collection, const std::string& id, const nlohmann::json& data,
|
|
|
- const std::string& actor) {
|
|
|
|
|
|
|
+ const std::string& actor, Client::Failure* failureOut = nullptr) {
|
|
|
// Strip metadata fields from data — callers pass the JSON from get() which
|
|
// Strip metadata fields from data — callers pass the JSON from get() which
|
|
|
// includes _version, _id, etc. We use _version for optimistic locking.
|
|
// includes _version, _id, etc. We use _version for optimistic locking.
|
|
|
auto cleanData = data;
|
|
auto cleanData = data;
|
|
@@ -328,12 +389,19 @@ public:
|
|
|
// Get current version
|
|
// Get current version
|
|
|
auto current = get(collection, id);
|
|
auto current = get(collection, id);
|
|
|
if (!current) {
|
|
if (!current) {
|
|
|
|
|
+ // The one failure here the client can name itself, without
|
|
|
|
|
+ // reading a server message: get() just said there is no
|
|
|
|
|
+ // such document.
|
|
|
|
|
+ recordFailure(failureOut, Client::ErrorCode::NotFound,
|
|
|
|
|
+ "document not found: " + collection + "/" + id);
|
|
|
return false; // document doesn't exist
|
|
return false; // document doesn't exist
|
|
|
}
|
|
}
|
|
|
uint64_t version = (*current)["_version"].get<uint64_t>();
|
|
uint64_t version = (*current)["_version"].get<uint64_t>();
|
|
|
|
|
|
|
|
- // Attempt version-checked update
|
|
|
|
|
- if (updateIfVersion(collection, id, cleanData, version, actor)) {
|
|
|
|
|
|
|
+ // Attempt version-checked update. Each attempt overwrites
|
|
|
|
|
+ // failureOut, so what the caller reads is the LAST attempt's
|
|
|
|
|
+ // reason, and a success leaves it clean.
|
|
|
|
|
+ if (updateIfVersion(collection, id, cleanData, version, actor, failureOut)) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -351,7 +419,9 @@ public:
|
|
|
|
|
|
|
|
bool updateIfVersion(const std::string& collection, const std::string& id,
|
|
bool updateIfVersion(const std::string& collection, const std::string& id,
|
|
|
const nlohmann::json& data, uint64_t expectedVersion,
|
|
const nlohmann::json& data, uint64_t expectedVersion,
|
|
|
- const std::string& actor) {
|
|
|
|
|
|
|
+ const std::string& actor,
|
|
|
|
|
+ Client::Failure* failureOut = nullptr) {
|
|
|
|
|
+ if (failureOut != nullptr) *failureOut = Client::Failure{};
|
|
|
smartbotic::databasepb::UpdateRequest request;
|
|
smartbotic::databasepb::UpdateRequest request;
|
|
|
request.set_collection(qualify(collection));
|
|
request.set_collection(qualify(collection));
|
|
|
request.set_id(id);
|
|
request.set_id(id);
|
|
@@ -383,15 +453,23 @@ public:
|
|
|
}
|
|
}
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::updateIfVersion failed after retries: {}", status.error_message());
|
|
spdlog::error("Client::updateIfVersion failed after retries: {}", status.error_message());
|
|
|
|
|
+ recordFailure(failureOut, status);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (!response.success()) {
|
|
|
|
|
+ // Refused in-band: status OK, no code on the wire. See
|
|
|
|
|
+ // Client::ErrorCode::Unknown.
|
|
|
|
|
+ recordFailure(failureOut, Client::ErrorCode::Unknown, response.error());
|
|
|
|
|
+ }
|
|
|
return response.success();
|
|
return response.success();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint64_t patch(const std::string& collection, const std::string& id,
|
|
uint64_t patch(const std::string& collection, const std::string& id,
|
|
|
const nlohmann::json& fields, const std::string& actor,
|
|
const nlohmann::json& fields, const std::string& actor,
|
|
|
- std::optional<uint32_t> ttlSeconds = std::nullopt) {
|
|
|
|
|
|
|
+ std::optional<uint32_t> ttlSeconds = std::nullopt,
|
|
|
|
|
+ Client::Failure* failureOut = nullptr) {
|
|
|
|
|
+ if (failureOut != nullptr) *failureOut = Client::Failure{};
|
|
|
smartbotic::databasepb::PatchDocumentRequest request;
|
|
smartbotic::databasepb::PatchDocumentRequest request;
|
|
|
request.set_collection(qualify(collection));
|
|
request.set_collection(qualify(collection));
|
|
|
// v2.8.0 — presence distinguishes "leave the expiry alone" (absent) from
|
|
// v2.8.0 — presence distinguishes "leave the expiry alone" (absent) from
|
|
@@ -425,11 +503,13 @@ public:
|
|
|
}
|
|
}
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::patch failed after retries: {}", status.error_message());
|
|
spdlog::error("Client::patch failed after retries: {}", status.error_message());
|
|
|
|
|
+ recordFailure(failureOut, status);
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!response.success()) {
|
|
if (!response.success()) {
|
|
|
spdlog::error("Client::patch failed: {}", response.error());
|
|
spdlog::error("Client::patch failed: {}", response.error());
|
|
|
|
|
+ recordFailure(failureOut, Client::ErrorCode::Unknown, response.error());
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -474,7 +554,7 @@ public:
|
|
|
}
|
|
}
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::upsert failed after retries: {}", status.error_message());
|
|
spdlog::error("Client::upsert failed after retries: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {response.id(), response.inserted()};
|
|
return {response.id(), response.inserted()};
|
|
@@ -487,7 +567,8 @@ public:
|
|
|
// FAILED_PRECONDITION, not deleted=false, and that message is the only
|
|
// FAILED_PRECONDITION, not deleted=false, and that message is the only
|
|
|
// way an operator learns WHAT blocked the delete.
|
|
// way an operator learns WHAT blocked the delete.
|
|
|
bool removeImpl(const std::string& collection, const std::string& id,
|
|
bool removeImpl(const std::string& collection, const std::string& id,
|
|
|
- std::string* errorOut) {
|
|
|
|
|
|
|
+ std::string* errorOut, Client::Failure* failureOut = nullptr) {
|
|
|
|
|
+ if (failureOut != nullptr) *failureOut = Client::Failure{};
|
|
|
smartbotic::databasepb::DeleteRequest request;
|
|
smartbotic::databasepb::DeleteRequest request;
|
|
|
request.set_collection(qualify(collection));
|
|
request.set_collection(qualify(collection));
|
|
|
request.set_id(id);
|
|
request.set_id(id);
|
|
@@ -515,6 +596,7 @@ public:
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::remove failed after retries: {}", status.error_message());
|
|
spdlog::error("Client::remove failed after retries: {}", status.error_message());
|
|
|
if (errorOut != nullptr) *errorOut = status.error_message();
|
|
if (errorOut != nullptr) *errorOut = status.error_message();
|
|
|
|
|
+ recordFailure(failureOut, status);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -530,6 +612,10 @@ public:
|
|
|
return removeImpl(collection, id, &errorOut);
|
|
return removeImpl(collection, id, &errorOut);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ bool remove(const std::string& collection, const std::string& id, Client::Failure& failureOut) {
|
|
|
|
|
+ return removeImpl(collection, id, nullptr, &failureOut);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
bool exists(const std::string& collection, const std::string& id) {
|
|
bool exists(const std::string& collection, const std::string& id) {
|
|
|
smartbotic::databasepb::ExistsRequest request;
|
|
smartbotic::databasepb::ExistsRequest request;
|
|
|
request.set_collection(qualify(collection));
|
|
request.set_collection(qualify(collection));
|
|
@@ -548,7 +634,7 @@ public:
|
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
|
// collection.
|
|
// collection.
|
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
|
- throw std::runtime_error("access denied: exists");
|
|
|
|
|
|
|
+ throw Client::PermissionDenied("access denied: exists");
|
|
|
}
|
|
}
|
|
|
spdlog::error("Client::exists failed: {}", status.error_message());
|
|
spdlog::error("Client::exists failed: {}", status.error_message());
|
|
|
return false;
|
|
return false;
|
|
@@ -601,7 +687,7 @@ public:
|
|
|
// caller bug, and returning {} here is indistinguishable from a
|
|
// caller bug, and returning {} here is indistinguishable from a
|
|
|
// legitimately empty result. Other failures keep the old
|
|
// legitimately empty result. Other failures keep the old
|
|
|
// log-and-return-empty behaviour.
|
|
// log-and-return-empty behaviour.
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
// v2.8.0 — a permission denial must NOT look like "no data".
|
|
// v2.8.0 — a permission denial must NOT look like "no data".
|
|
@@ -611,7 +697,7 @@ public:
|
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
|
// collection.
|
|
// collection.
|
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
|
- throw std::runtime_error("access denied: find");
|
|
|
|
|
|
|
+ throw Client::PermissionDenied("access denied: find");
|
|
|
}
|
|
}
|
|
|
spdlog::error("Client::find failed: {}", status.error_message());
|
|
spdlog::error("Client::find failed: {}", status.error_message());
|
|
|
return {};
|
|
return {};
|
|
@@ -671,7 +757,7 @@ public:
|
|
|
|
|
|
|
|
auto status = stub_->Find(&context, request, &response);
|
|
auto status = stub_->Find(&context, request, &response);
|
|
|
if (status.error_code() == grpc::StatusCode::NOT_FOUND) {
|
|
if (status.error_code() == grpc::StatusCode::NOT_FOUND) {
|
|
|
- throw std::runtime_error(status.error_message()); // see find()
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message()); // see find()
|
|
|
}
|
|
}
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
// v2.8.0 — a permission denial must NOT look like "no data".
|
|
// v2.8.0 — a permission denial must NOT look like "no data".
|
|
@@ -681,7 +767,7 @@ public:
|
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
|
// collection.
|
|
// collection.
|
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
|
- throw std::runtime_error("access denied: findWithMetrics");
|
|
|
|
|
|
|
+ throw Client::PermissionDenied("access denied: findWithMetrics");
|
|
|
}
|
|
}
|
|
|
spdlog::error("Client::findWithMetrics failed: {}", status.error_message());
|
|
spdlog::error("Client::findWithMetrics failed: {}", status.error_message());
|
|
|
return {};
|
|
return {};
|
|
@@ -736,7 +822,7 @@ public:
|
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
// v2.4.2 change that stopped Find returning empty for a missing
|
|
|
// collection.
|
|
// collection.
|
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
if (status.error_code() == grpc::StatusCode::PERMISSION_DENIED) {
|
|
|
- throw std::runtime_error("access denied: count");
|
|
|
|
|
|
|
+ throw Client::PermissionDenied("access denied: count");
|
|
|
}
|
|
}
|
|
|
spdlog::error("Client::count failed: {}", status.error_message());
|
|
spdlog::error("Client::count failed: {}", status.error_message());
|
|
|
return 0;
|
|
return 0;
|
|
@@ -842,7 +928,7 @@ public:
|
|
|
auto status = stub_->SimilaritySearch(&context, request, &response);
|
|
auto status = stub_->SimilaritySearch(&context, request, &response);
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::similaritySearch failed: {}", status.error_message());
|
|
spdlog::error("Client::similaritySearch failed: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
std::vector<Client::SimilarityResult> results;
|
|
std::vector<Client::SimilarityResult> results;
|
|
@@ -1007,8 +1093,7 @@ public:
|
|
|
auto status = stub_->CreateProject(&context, request, &response);
|
|
auto status = stub_->CreateProject(&context, request, &response);
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::createProject failed: {}", status.error_message());
|
|
spdlog::error("Client::createProject failed: {}", status.error_message());
|
|
|
- throw std::runtime_error("createProject transport failure: " +
|
|
|
|
|
- status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, "createProject transport failure: " + status.error_message());
|
|
|
}
|
|
}
|
|
|
if (!response.error().empty()) {
|
|
if (!response.error().empty()) {
|
|
|
// Validation error (invalid name etc.) — surfaced via the
|
|
// Validation error (invalid name etc.) — surfaced via the
|
|
@@ -1030,8 +1115,7 @@ public:
|
|
|
auto status = stub_->DropProject(&context, request, &response);
|
|
auto status = stub_->DropProject(&context, request, &response);
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::dropProject failed: {}", status.error_message());
|
|
spdlog::error("Client::dropProject failed: {}", status.error_message());
|
|
|
- throw std::runtime_error("dropProject transport failure: " +
|
|
|
|
|
- status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, "dropProject transport failure: " + status.error_message());
|
|
|
}
|
|
}
|
|
|
if (!response.error().empty()) {
|
|
if (!response.error().empty()) {
|
|
|
throw std::runtime_error("dropProject('" + name + "') refused: " +
|
|
throw std::runtime_error("dropProject('" + name + "') refused: " +
|
|
@@ -1152,6 +1236,52 @@ public:
|
|
|
|| response.config().relations_enforced();
|
|
|| response.config().relations_enforced();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // v2.11.4 - decides what a relations_enforced read REPORTS, given what
|
|
|
|
|
+ // the RPC returned. Split out from the transport so the policy is one
|
|
|
|
|
+ // small function with no gRPC plumbing around it.
|
|
|
|
|
+ //
|
|
|
|
|
+ // Every way of not knowing collapses to nullopt, and nothing throws:
|
|
|
|
|
+ //
|
|
|
|
|
+ // - A failed status, whatever the code. get()/find()/count() throw on
|
|
|
|
|
+ // PERMISSION_DENIED (v2.8.0) because their empty result is a VALID
|
|
|
|
|
+ // answer, so a denial returned as empty would be read as "no data".
|
|
|
|
|
+ // That hazard does not exist here: nullopt is not a setting, it is
|
|
|
|
|
+ // the absence of one, and a caller cannot mistake it for on or off.
|
|
|
|
|
+ // One failure channel is also what the name promises.
|
|
|
|
|
+ //
|
|
|
|
|
+ // - A successful reply WITHOUT the field. A 2.11.0+ server writes it
|
|
|
|
|
+ // on every GetCollectionConfig reply - the handler sets it
|
|
|
|
|
+ // unconditionally, including for a collection never configured or
|
|
|
|
|
+ // not yet created, both of which report the default (true). So an
|
|
|
|
|
+ // absent field can only be a server older than 2.11.0, which has no
|
|
|
|
|
+ // relations. `true` there would claim enforcement that does not
|
|
|
|
|
+ // exist, and `false` would offer a switch that server silently
|
|
|
|
|
+ // ignores; the honest reading is that the setting is not there.
|
|
|
|
|
+ // getRelationsEnforced() answers true for this case and is left
|
|
|
|
|
+ // alone - it is the fail-toward-enforced read by contract.
|
|
|
|
|
+ static std::optional<bool> interpretRelationsEnforcedRead(
|
|
|
|
|
+ const grpc::Status& status,
|
|
|
|
|
+ const smartbotic::databasepb::GetCollectionConfigResponse& response
|
|
|
|
|
+ ) {
|
|
|
|
|
+ if (!status.ok()) return std::nullopt;
|
|
|
|
|
+ if (!response.config().has_relations_enforced()) return std::nullopt;
|
|
|
|
|
+ return response.config().relations_enforced();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ std::optional<bool> tryGetRelationsEnforced(const std::string& collection) {
|
|
|
|
|
+ smartbotic::databasepb::GetCollectionConfigRequest request;
|
|
|
|
|
+ request.set_collection(qualify(collection));
|
|
|
|
|
+ smartbotic::databasepb::GetCollectionConfigResponse response;
|
|
|
|
|
+ grpc::ClientContext context;
|
|
|
|
|
+ setDeadline(context);
|
|
|
|
|
+
|
|
|
|
|
+ auto status = stub_->GetCollectionConfig(&context, request, &response);
|
|
|
|
|
+ if (!status.ok()) {
|
|
|
|
|
+ spdlog::error("Client::tryGetRelationsEnforced failed: {}", status.error_message());
|
|
|
|
|
+ }
|
|
|
|
|
+ return interpretRelationsEnforcedRead(status, response);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Client::TimestampMigrationResult migrateCollectionTimestamps(
|
|
Client::TimestampMigrationResult migrateCollectionTimestamps(
|
|
|
const std::string& collection,
|
|
const std::string& collection,
|
|
|
const std::string& fromPrecision,
|
|
const std::string& fromPrecision,
|
|
@@ -2077,7 +2207,7 @@ public:
|
|
|
auto status = writer->Finish();
|
|
auto status = writer->Finish();
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::uploadFile failed: {}", status.error_message());
|
|
spdlog::error("Client::uploadFile failed: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {response.id(), response.size(), response.checksum(), response.deduplicated()};
|
|
return {response.id(), response.size(), response.checksum(), response.deduplicated()};
|
|
@@ -2108,7 +2238,7 @@ public:
|
|
|
auto status = reader->Finish();
|
|
auto status = reader->Finish();
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::downloadFile failed: {}", status.error_message());
|
|
spdlog::error("Client::downloadFile failed: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
return fileData;
|
|
return fileData;
|
|
|
}
|
|
}
|
|
@@ -2126,7 +2256,7 @@ public:
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
if (status.error_code() == grpc::StatusCode::NOT_FOUND) return std::nullopt;
|
|
if (status.error_code() == grpc::StatusCode::NOT_FOUND) return std::nullopt;
|
|
|
spdlog::error("Client::getFileInfo failed: {}", status.error_message());
|
|
spdlog::error("Client::getFileInfo failed: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Client::FileRecord record;
|
|
Client::FileRecord record;
|
|
@@ -2203,7 +2333,7 @@ public:
|
|
|
auto status = stub_->ListFiles(&context, request, &response);
|
|
auto status = stub_->ListFiles(&context, request, &response);
|
|
|
if (!status.ok()) {
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::listFiles failed: {}", status.error_message());
|
|
spdlog::error("Client::listFiles failed: {}", status.error_message());
|
|
|
- throw std::runtime_error(status.error_message());
|
|
|
|
|
|
|
+ throwStatus(status, status.error_message());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Client::FileListResult result;
|
|
Client::FileListResult result;
|
|
@@ -2304,6 +2434,30 @@ bool Client::updateIfVersion(const std::string& collection, const std::string& i
|
|
|
return impl_->updateIfVersion(collection, id, data, expectedVersion, actor);
|
|
return impl_->updateIfVersion(collection, id, data, expectedVersion, actor);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+bool Client::update(const std::string& collection, const std::string& id, const nlohmann::json& data,
|
|
|
|
|
+ Failure& failureOut, const std::string& actor) {
|
|
|
|
|
+ failureOut = Failure{};
|
|
|
|
|
+ return impl_->update(collection, id, data, actor, &failureOut);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool Client::updateIfVersion(const std::string& collection, const std::string& id,
|
|
|
|
|
+ const nlohmann::json& data, uint64_t expectedVersion,
|
|
|
|
|
+ Failure& failureOut, const std::string& actor) {
|
|
|
|
|
+ return impl_->updateIfVersion(collection, id, data, expectedVersion, actor, &failureOut);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+uint64_t Client::patch(const std::string& collection, const std::string& id,
|
|
|
|
|
+ const nlohmann::json& fields, Failure& failureOut,
|
|
|
|
|
+ const std::string& actor) {
|
|
|
|
|
+ return impl_->patch(collection, id, fields, actor, std::nullopt, &failureOut);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+uint64_t Client::patchWithTtl(const std::string& collection, const std::string& id,
|
|
|
|
|
+ const nlohmann::json& fields, uint32_t ttlSeconds,
|
|
|
|
|
+ Failure& failureOut, const std::string& actor) {
|
|
|
|
|
+ return impl_->patch(collection, id, fields, actor, ttlSeconds, &failureOut);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
uint64_t Client::patch(const std::string& collection, const std::string& id,
|
|
uint64_t Client::patch(const std::string& collection, const std::string& id,
|
|
|
const nlohmann::json& fields, const std::string& actor) {
|
|
const nlohmann::json& fields, const std::string& actor) {
|
|
|
return impl_->patch(collection, id, fields, actor);
|
|
return impl_->patch(collection, id, fields, actor);
|
|
@@ -2329,6 +2483,10 @@ bool Client::remove(const std::string& collection, const std::string& id, std::s
|
|
|
return impl_->remove(collection, id, errorOut);
|
|
return impl_->remove(collection, id, errorOut);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+bool Client::remove(const std::string& collection, const std::string& id, Failure& failureOut) {
|
|
|
|
|
+ return impl_->remove(collection, id, failureOut);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
bool Client::exists(const std::string& collection, const std::string& id) {
|
|
bool Client::exists(const std::string& collection, const std::string& id) {
|
|
|
return impl_->exists(collection, id);
|
|
return impl_->exists(collection, id);
|
|
|
}
|
|
}
|
|
@@ -2544,6 +2702,10 @@ bool Client::getRelationsEnforced(const std::string& collection) {
|
|
|
return impl_->getRelationsEnforced(collection);
|
|
return impl_->getRelationsEnforced(collection);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+std::optional<bool> Client::tryGetRelationsEnforced(const std::string& collection) {
|
|
|
|
|
+ return impl_->tryGetRelationsEnforced(collection);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
Client::DescribeDeleteResult Client::describeDelete(const std::string& collection,
|
|
Client::DescribeDeleteResult Client::describeDelete(const std::string& collection,
|
|
|
const std::string& id) {
|
|
const std::string& id) {
|
|
|
return impl_->describeDelete(collection, id);
|
|
return impl_->describeDelete(collection, id);
|