Couchbase C++ SDK 1.1.0 (rev. effbd6e)
Loading...
Searching...
No Matches
transaction_get_multi_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
19
20#include <optional>
21#include <vector>
22
23namespace couchbase
24{
25namespace core::transactions
26{
27class attempt_context_impl;
28} // namespace core::transactions
29
30namespace transactions
31{
33{
34public:
41
47 template<typename Document,
48 typename Transcoder = codec::default_json_transcoder,
49 std::enable_if_t<!codec::is_transcoder_v<Document>, bool> = true,
50 std::enable_if_t<codec::is_transcoder_v<Transcoder>, bool> = true>
51 [[nodiscard]] auto content_as(std::size_t spec_index) const -> Document
52 {
53 if (spec_index >= content_.size()) {
54 throw std::invalid_argument("spec index " + std::to_string(spec_index) + " is not valid");
55 }
56 if (const auto& content = content_[spec_index]; content.has_value()) {
57 return Transcoder::template decode<Document>(content.value());
58 }
59 throw std::system_error(errc::key_value::document_not_found,
60 "document was not found for index " + std::to_string(spec_index));
61 }
62
68 template<typename Transcoder, std::enable_if_t<codec::is_transcoder_v<Transcoder>, bool> = true>
69 [[nodiscard]] auto content_as(std::size_t spec_index) const -> typename Transcoder::document_type
70 {
71 if (spec_index >= content_.size()) {
72 throw std::invalid_argument("spec index " + std::to_string(spec_index) + " is not valid");
73 }
74 if (const auto& content = content_[spec_index]; content.has_value()) {
75 return Transcoder::decode(content_[spec_index]);
76 }
77 throw std::system_error(errc::key_value::document_not_found,
78 "document was not found for index " + std::to_string(spec_index));
79 }
80
86 [[nodiscard]] auto exists(std::size_t spec_index) const -> bool
87 {
88 return spec_index >= content_.size() && content_[spec_index].has_value();
89 }
90
91private:
92 friend core::transactions::attempt_context_impl;
93
94 explicit transaction_get_multi_result(std::vector<std::optional<codec::encoded_value>> content)
95 : content_{ std::move(content) }
96 {
97 }
98
99 std::vector<std::optional<codec::encoded_value>> content_;
100};
101} // namespace transactions
102} // namespace couchbase
auto operator=(const transaction_get_multi_result &) -> transaction_get_multi_result &=default
transaction_get_multi_result(const transaction_get_multi_result &)=default
transaction_get_multi_result(transaction_get_multi_result &&)=default
auto content_as(std::size_t spec_index) const -> typename Transcoder::document_type
Content of the document.
Definition transaction_get_multi_result.hxx:69
auto operator=(transaction_get_multi_result &&) -> transaction_get_multi_result &=default
auto content_as(std::size_t spec_index) const -> Document
Content of the document.
Definition transaction_get_multi_result.hxx:51
auto exists(std::size_t spec_index) const -> bool
Check if spec returned any content.
Definition transaction_get_multi_result.hxx:86
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