diff --git a/core/impl/transaction_get_result.cxx b/core/impl/transaction_get_result.cxx index ddb38231f..2726c2d8d 100644 --- a/core/impl/transaction_get_result.cxx +++ b/core/impl/transaction_get_result.cxx @@ -33,20 +33,8 @@ transaction_get_result::content() const -> const codec::encoded_value& return base_->content(); } -void -transaction_get_result::content(const codec::encoded_value& content) -{ - return base_->content(content); -} - -void -transaction_get_result::content(codec::encoded_value&& content) -{ - return base_->content(std::move(content)); -} - auto -transaction_get_result::key() const -> const std::string& +transaction_get_result::id() const -> const std::string& { return base_->key(); } diff --git a/core/meta/features.hxx b/core/meta/features.hxx index 3a8597a1c..7287f4987 100644 --- a/core/meta/features.hxx +++ b/core/meta/features.hxx @@ -179,3 +179,8 @@ * transaction_op error codes do not have 'exception' suffix */ #define COUCHBASE_CXX_CLIENT_TRANSACTION_OP_ERRC_NO_EXCEPTION_SUFFIX 1 + +/** + * transactions_get_result has `content_as` and `id` methods + */ +#define COUCHBASE_CXX_CLIENT_TXNS_GET_RESULT_FINAL_API diff --git a/core/transactions/transaction_get_result.hxx b/core/transactions/transaction_get_result.hxx index 94561410d..3429c41bd 100644 --- a/core/transactions/transaction_get_result.hxx +++ b/core/transactions/transaction_get_result.hxx @@ -80,7 +80,7 @@ public: explicit transaction_get_result(const couchbase::transactions::transaction_get_result& res) : cas_(res.cas()) - , document_id_(res.bucket(), res.scope(), res.collection(), res.key()) + , document_id_(res.bucket(), res.scope(), res.collection(), res.id()) , links_(res.base_->links()) , content_(std::move(res.content())) , metadata_(res.base_->metadata_) diff --git a/couchbase/transactions/transaction_get_result.hxx b/couchbase/transactions/transaction_get_result.hxx index ef6197d5e..488857559 100644 --- a/couchbase/transactions/transaction_get_result.hxx +++ b/couchbase/transactions/transaction_get_result.hxx @@ -27,6 +27,7 @@ // forward declarations... namespace couchbase::core::transactions { +class attempt_context_impl; class transaction_get_result; class transaction_links; class document_metadata; @@ -37,21 +38,12 @@ namespace couchbase::transactions class transaction_get_result { - friend class couchbase::core::transactions::transaction_get_result; - -private: - std::shared_ptr base_{}; - - explicit transaction_get_result( - std::shared_ptr base) - : base_(std::move(base)) - { - } - public: - /** @private */ - + /** + * @private + */ transaction_get_result(); + /** * Content of the document. * @@ -61,70 +53,41 @@ public: typename Transcoder = codec::default_json_transcoder, std::enable_if_t, bool> = true, std::enable_if_t, bool> = true> - [[nodiscard]] auto content() const -> Document + [[nodiscard]] auto content_as() const -> Document { return Transcoder::template decode(content()); } template, bool> = true> - [[nodiscard]] auto content() const -> typename Transcoder::document_type + [[nodiscard]] auto content_as() const -> typename Transcoder::document_type { return Transcoder::decode(content()); } /** - * Content of the document as raw byte vector + * Get document id. * - * @return content + * @return the id of this document. */ - [[nodiscard]] auto content() const -> const codec::encoded_value&; + [[nodiscard]] auto id() const -> const std::string&; - /** - * Copy content into document - * @param content - */ - void content(const codec::encoded_value& content); +private: + std::shared_ptr base_{}; - /** - * Move content into document - * - * @param content - */ - void content(codec::encoded_value&& content); + explicit transaction_get_result( + std::shared_ptr base) + : base_(std::move(base)) + { + } - /** - * Get document id. - * - * @return the id of this document. - */ - [[nodiscard]] auto key() const -> const std::string&; + [[nodiscard]] auto content() const -> const codec::encoded_value&; - /** - * Get the name of the bucket this document is in. - * - * @return name of the bucket which contains the document. - */ - [[nodiscard]] auto bucket() const -> const std::string&; + friend class couchbase::core::transactions::transaction_get_result; + friend class couchbase::core::transactions::attempt_context_impl; - /** - * Get the name of the scope this document is in. - * - * @return name of the scope which contains the document. - */ + [[nodiscard]] auto bucket() const -> const std::string&; [[nodiscard]] auto scope() const -> const std::string&; - - /** - * Get the name of the collection this document is in. - * - * @return name of the collection which contains the document. - */ [[nodiscard]] auto collection() const -> const std::string&; - - /** - * Get the CAS fot this document - * - * @return the CAS of the document. - */ [[nodiscard]] auto cas() const -> couchbase::cas; }; } // namespace couchbase::transactions diff --git a/examples/async_game_server.cxx b/examples/async_game_server.cxx index 50c84908b..89ce236af 100644 --- a/examples/async_game_server.cxx +++ b/examples/async_game_server.cxx @@ -164,7 +164,7 @@ class GameServer exists = false; return; } - auto monster_body = monster.template content(); + auto monster_body = monster.template content_as(); auto monster_hitpoints = monster_body.hitpoints; auto monster_new_hitpoints = monster_hitpoints - damage; @@ -188,7 +188,7 @@ class GameServer std::cout << "error getting player: " << e.ec().message() << std::endl; return; } - const Player& player_body = player.template content(); + const Player& player_body = player.template content_as(); // the player earns experience for killing the monster int experience_for_killing_monster = monster_body.experience_when_killed; diff --git a/examples/game_server.cxx b/examples/game_server.cxx index f1c878a6f..9d25d8cbf 100644 --- a/examples/game_server.cxx +++ b/examples/game_server.cxx @@ -156,7 +156,7 @@ class GameServer exists = false; return {}; } - const Monster& monster_body = monster.content(); + const Monster& monster_body = monster.content_as(); int monster_hitpoints = monster_body.hitpoints; int monster_new_hitpoints = monster_hitpoints - damage_; @@ -176,7 +176,7 @@ class GameServer // set a "dead" flag or similar. ctx->remove(monster); - const Player& player_body = player.content(); + const Player& player_body = player.content_as(); // the player earns experience for killing the monster int experience_for_killing_monster = monster_body.experience_when_killed; diff --git a/test/test_transaction_examples.cxx b/test/test_transaction_examples.cxx index 60d3ea64a..7223ffca1 100644 --- a/test/test_transaction_examples.cxx +++ b/test/test_transaction_examples.cxx @@ -129,14 +129,14 @@ main(int argc, const char* argv[]) // [4.4] replace document's content ctx->replace(doc, tao::json::value{ { "some", "other async content" } }, - [=](auto err_ctx_2, auto res) { + [=](auto err_ctx_2, auto /*res*/) { if (err_ctx_2.ec()) { fmt::print(stderr, "error replacing content in doc {}: {}\n", id_1, err_ctx_2.ec().message()); } else { - fmt::print("successfully replaced: {}, cas={}\n", id_1, res.cas()); + fmt::print("successfully replaced: {}\n", id_1); } }); }); @@ -147,14 +147,14 @@ main(int argc, const char* argv[]) } ctx->replace(doc, tao::json::value{ { "some", "other async content" } }, - [=](auto err_ctx_2, auto res) { + [=](auto err_ctx_2, auto /*res*/) { if (err_ctx_2.ec()) { fmt::print(stderr, "error replacing content in doc {}: {}\n", id_2, err_ctx_2.ec().message()); } else { - fmt::print("successfully replaced: {}, cas={}\n", id_2, res.cas()); + fmt::print("successfully replaced: {}\n", id_2); } }); }); @@ -165,14 +165,14 @@ main(int argc, const char* argv[]) } ctx->replace(doc, tao::json::value{ { "some", "other async content" } }, - [=](auto err_ctx_2, auto res) { + [=](auto err_ctx_2, auto /*res*/) { if (err_ctx_2.ec()) { fmt::print(stderr, "error replacing content in doc {}: {}\n", id_3, err_ctx_2.ec().message()); } else { - fmt::print("successfully replaced: {}, cas={}\n", id_3, res.cas()); + fmt::print("successfully replaced: {}\n", id_3); } }); }); @@ -286,7 +286,7 @@ main(int argc, const char* argv[]) return {}; } fmt::println("document content: {}", - tao::json::to_string(doc.template content())); + tao::json::to_string(doc.template content_as())); return {}; }); @@ -313,7 +313,7 @@ main(int argc, const char* argv[]) return; } fmt::println("document content: {}", - tao::json::to_string(doc.template content())); + tao::json::to_string(doc.template content_as())); }); return {}; }, diff --git a/test/test_transaction_public_async_api.cxx b/test/test_transaction_public_async_api.cxx index 7d52ad097..99fa9d5d2 100644 --- a/test/test_transaction_public_async_api.cxx +++ b/test/test_transaction_public_async_api.cxx @@ -49,8 +49,8 @@ TEST_CASE("transactions public async API: can async get", "[transactions]") std::shared_ptr ctx) -> couchbase::error { ctx->get(coll, id, [id](auto e, auto res) { CHECK_FALSE(e.ec()); - CHECK(res.key() == id); - CHECK(res.template content() == async_content); + CHECK(res.id() == id); + CHECK(res.template content_as() == async_content); }); return {}; }, @@ -236,9 +236,9 @@ TEST_CASE("transactions public async API: can async replace", "[transactions]") c.transactions()->run( [id, coll, new_content]( std::shared_ptr ctx) -> couchbase::error { - ctx->get(coll, id, [new_content, ctx](auto, auto res) { - ctx->replace(res, new_content, [](auto replace_e, auto replace_result) { - CHECK(!replace_result.cas().empty()); + ctx->get(coll, id, [new_content, ctx, id](auto, auto res) { + ctx->replace(res, new_content, [id](auto replace_e, auto replace_result) { + CHECK(replace_result.id() == id); CHECK_FALSE(replace_e.ec()); }); }); diff --git a/test/test_transaction_public_blocking_api.cxx b/test/test_transaction_public_blocking_api.cxx index d6a3099a4..ea96eca02 100644 --- a/test/test_transaction_public_blocking_api.cxx +++ b/test/test_transaction_public_blocking_api.cxx @@ -121,9 +121,8 @@ TEST_CASE("transactions public blocking API: can get", "[transactions]") [id, &coll](std::shared_ptr ctx) -> couchbase::error { auto [e, doc] = ctx->get(coll, id); CHECK_FALSE(e.ec()); - CHECK(doc.key() == id); - CHECK_FALSE(doc.cas().empty()); - CHECK(doc.content() == content); + CHECK(doc.id() == id); + CHECK(doc.content_as() == content); return {}; }, txn_opts()); @@ -164,11 +163,10 @@ TEST_CASE("transactions public blocking API: can insert", "[transactions]") [id, coll](std::shared_ptr ctx) -> couchbase::error { auto [e, doc] = ctx->insert(coll, id, content); CHECK_FALSE(e.ec()); - CHECK(doc.key() == id); - CHECK_FALSE(doc.cas().empty()); + CHECK(doc.id() == id); auto [e2, inserted_doc] = ctx->get(coll, id); CHECK_FALSE(e2.ec()); - CHECK(inserted_doc.content() == content); + CHECK(inserted_doc.content_as() == content); return {}; }, txn_opts()); @@ -231,9 +229,8 @@ TEST_CASE("transactions public blocking API: can replace", "[transactions]") auto [_, doc] = ctx->get(coll, id); auto [e, replaced_doc] = ctx->replace(doc, new_content); CHECK_FALSE(e.ec()); - CHECK(doc.key() == replaced_doc.key()); - CHECK(doc.cas() != replaced_doc.cas()); - CHECK(doc.content() == content); + CHECK(doc.id() == replaced_doc.id()); + CHECK(doc.content_as() == content); // FIXME(JCBC-2152) // CHECK(replaced_doc.content() == new_content); return {}; @@ -665,7 +662,7 @@ TEST_CASE("transactions public blocking API: can get doc from bucket not yet ope &coll](std::shared_ptr ctx) -> couchbase::error { auto [e, doc] = ctx->get(coll, id); CHECK_FALSE(e.ec()); - CHECK(doc.content() == content); + CHECK(doc.content_as() == content); return {}; }, txn_opts()); @@ -691,7 +688,7 @@ TEST_CASE("transactions public blocking API: can insert doc into bucket not yet &coll](std::shared_ptr ctx) -> couchbase::error { auto [e, doc] = ctx->insert(coll, id, content); CHECK_FALSE(e.ec()); - CHECK_FALSE(doc.cas().empty()); + CHECK(doc.id() == id); return {}; }, txn_opts()); @@ -729,7 +726,7 @@ TEST_CASE("transactions public blocking API: can replace doc in bucket not yet o CHECK_FALSE(get_err.ec()); auto [e, doc] = ctx->replace(get_doc, new_content); CHECK_FALSE(e.ec()); - CHECK_FALSE(doc.cas().empty()); + CHECK(doc.id() == id); return {}; }, txn_opts()); diff --git a/test/test_unit_transaction_utils.cxx b/test/test_unit_transaction_utils.cxx index 9a112938c..86bf49267 100644 --- a/test/test_unit_transaction_utils.cxx +++ b/test/test_unit_transaction_utils.cxx @@ -302,13 +302,8 @@ TEST_CASE("transaction_get_result: can convert core transaction_get_result to " couchbase::core::transactions::transaction_get_result core_result( { bucket, scope, collection, key }, json_content, cas.value(), links, metadata); auto public_result = core_result.to_public_result(); - REQUIRE(public_result.collection() == collection); - REQUIRE(public_result.bucket() == bucket); - REQUIRE(public_result.scope() == scope); - REQUIRE(public_result.cas() == cas); - REQUIRE(public_result.key() == key); - REQUIRE(public_result.content() == json_content); - REQUIRE(public_result.content() == content); + REQUIRE(public_result.id() == key); + REQUIRE(public_result.content_as() == content); REQUIRE(core_result.collection() == collection); REQUIRE(core_result.bucket() == bucket); REQUIRE(core_result.scope() == scope); @@ -360,13 +355,13 @@ TEST_CASE("transaction_get_result: can convert core transaction_get_result to " { couchbase::core::transactions::transaction_get_result core_result{}; auto final_public_result = core_result.to_public_result(); - REQUIRE(final_public_result.cas().empty()); + REQUIRE(final_public_result.id().empty()); } SECTION("default constructed public->core->public") { couchbase::transactions::transaction_get_result public_res; couchbase::core::transactions::transaction_get_result core_res(public_res); auto final_public_res = core_res.to_public_result(); - REQUIRE(final_public_res.cas().empty()); + REQUIRE(final_public_res.id().empty()); } }