Couchbase C++ SDK 1.1.0 (rev. effbd6e)
Loading...
Searching...
No Matches
transaction_get_multi_replicas_from_preferred_server_group_result.hxx
Go to the documentation of this file.
1/*
2 * Copyright 2021-Present Couchbase, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
20
21#include <optional>
22#include <stdexcept>
23#include <system_error>
24#include <vector>
25
26namespace couchbase
27{
28namespace core::transactions
29{
30class attempt_context_impl;
31} // namespace core::transactions
32
33namespace transactions
34{
36{
37public:
47
53 template<typename Document,
54 typename Transcoder = codec::default_json_transcoder,
55 std::enable_if_t<!codec::is_transcoder_v<Document>, bool> = true,
56 std::enable_if_t<codec::is_transcoder_v<Transcoder>, bool> = true>
57 [[nodiscard]] auto content_as(std::size_t spec_index) const -> Document
58 {
59 if (spec_index >= content_.size()) {
60 throw std::invalid_argument("spec index " + std::to_string(spec_index) + " is not valid");
61 }
62 if (const auto& content = content_[spec_index]; content.has_value()) {
63 return Transcoder::template decode<Document>(content.value());
64 }
65 throw std::system_error(errc::key_value::document_not_found,
66 "document was not found for index " + std::to_string(spec_index));
67 }
68
74 template<typename Transcoder, std::enable_if_t<codec::is_transcoder_v<Transcoder>, bool> = true>
75 [[nodiscard]] auto content_as(std::size_t spec_index) const -> typename Transcoder::document_type
76 {
77 if (spec_index >= content_.size()) {
78 throw std::invalid_argument("spec index " + std::to_string(spec_index) + " is not valid");
79 }
80 if (const auto& content = content_[spec_index]; content.has_value()) {
81 return Transcoder::decode(content.value());
82 }
83 throw std::system_error(errc::key_value::document_not_found,
84 "document was not found for index " + std::to_string(spec_index));
85 }
86
92 [[nodiscard]] auto exists(std::size_t spec_index) const -> bool
93 {
94 return spec_index >= content_.size() && content_[spec_index].has_value();
95 }
96
97private:
98 friend core::transactions::attempt_context_impl;
99
101 std::vector<std::optional<codec::encoded_value>> content)
102 : content_{ std::move(content) }
103 {
104 }
105
106 std::vector<std::optional<codec::encoded_value>> content_;
107};
108} // namespace transactions
109} // namespace couchbase
auto content_as(std::size_t spec_index) const -> typename Transcoder::document_type
Content of the document.
Definition transaction_get_multi_replicas_from_preferred_server_group_result.hxx:75
auto operator=(transaction_get_multi_replicas_from_preferred_server_group_result &&) -> transaction_get_multi_replicas_from_preferred_server_group_result &=default
transaction_get_multi_replicas_from_preferred_server_group_result(transaction_get_multi_replicas_from_preferred_server_group_result &&)=default
transaction_get_multi_replicas_from_preferred_server_group_result(const transaction_get_multi_replicas_from_preferred_server_group_result &)=default
auto content_as(std::size_t spec_index) const -> Document
Content of the document.
Definition transaction_get_multi_replicas_from_preferred_server_group_result.hxx:57
auto exists(std::size_t spec_index) const -> bool
Check if spec returned any content.
Definition transaction_get_multi_replicas_from_preferred_server_group_result.hxx:92
auto operator=(const transaction_get_multi_replicas_from_preferred_server_group_result &) -> transaction_get_multi_replicas_from_preferred_server_group_result &=default
json_transcoder< tao_json_serializer > default_json_transcoder
Definition default_json_transcoder.hxx:28
Definition transaction_get_multi_options.hxx:23
@ document_not_found
Indicates an operation failed because the key does not exist.
Definition error_codes.hxx:374
Definition transactions.hxx:28
Represents a single item from the result of scan()
Definition allow_querying_search_index_options.hxx:28