Couchbase C++ SDK 1.0.2 (rev. 51f4775)
Loading...
Searching...
No Matches
lookup_in_result.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright 2020-Present Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
23#include <couchbase/result.hxx>
25
26#include <optional>
27
28namespace couchbase
29{
30
31namespace codec
32{
33class tao_json_serializer;
34} // namespace codec
35
43{
44public:
49 struct entry {
50 std::string path;
52 std::size_t original_index;
53 bool exists;
54 std::error_code ec;
55 };
56
61 lookup_in_result() = default;
62
73 lookup_in_result(couchbase::cas cas, std::vector<entry> entries, bool is_deleted)
74 : result{ cas }
75 , entries_{ std::move(entries) }
76 , is_deleted_{ is_deleted }
77 {
78 }
79
90 template<typename Document,
91 typename Serializer = codec::tao_json_serializer,
92 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
93 [[nodiscard]] auto content_as(std::size_t index) const -> Document
94 {
95 for (const entry& e : entries_) {
96 if (e.original_index == index) {
97 if (e.ec) {
98 throw std::system_error(e.ec,
99 "error getting result for spec at index " +
100 std::to_string(index) + ", path \"" + e.path + "\"");
101 }
102
103 return Serializer::template deserialize<Document>(e.value);
104 }
105 }
106 throw std::system_error(errc::key_value::path_invalid,
107 "invalid index for lookup_in result: {}" + std::to_string(index));
108 }
109
120 template<typename Document,
121 typename Serializer = codec::tao_json_serializer,
122 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
123 [[nodiscard]] auto content_as(const std::string& path) const -> Document
124 {
125 for (const entry& e : entries_) {
126 if (e.path == path) {
127 if (e.ec) {
128 throw std::system_error(e.ec, "error getting result for path \"" + e.path + "\"");
129 }
130
131 return Serializer::template deserialize<Document>(e.value);
132 }
133 }
134 throw std::system_error(errc::key_value::path_invalid,
135 "invalid path for lookup_in result: " + path);
136 }
137
148 template<typename Document,
149 typename Serializer = codec::tao_json_serializer,
150 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
151 [[nodiscard]] auto content_as(subdoc::lookup_in_macro macro) const -> Document
152 {
153 const auto& macro_string = subdoc::to_string(macro);
154 for (const entry& e : entries_) {
155 if (e.path == macro_string) {
156 if (e.ec) {
157 throw std::system_error(e.ec, "error getting result for macro \"" + macro_string + "\"");
158 }
159
160 return Serializer::template deserialize<Document>(e.value);
161 }
162 }
163 throw std::system_error(errc::key_value::path_invalid,
164 "invalid path for lookup_in result: macro#" +
165 std::to_string(static_cast<std::uint32_t>(macro)));
166 }
167
177 [[nodiscard]] auto exists(std::size_t index) const -> bool
178 {
179 for (const entry& e : entries_) {
180 if (e.original_index == index) {
181 if (e.ec && e.ec != couchbase::errc::key_value::path_not_found) {
182 throw std::system_error(e.ec, "error getting result for path \"" + e.path + "\"");
183 }
184
185 return e.exists;
186 }
187 }
188 return false;
189 }
190
200 [[nodiscard]] auto exists(subdoc::lookup_in_macro macro) const -> bool
201 {
202 const auto& macro_string = subdoc::to_string(macro);
203 for (const entry& e : entries_) {
204 if (e.path == macro_string) {
205 if (e.ec && e.ec != couchbase::errc::key_value::path_not_found) {
206 throw std::system_error(e.ec, "error getting result for macro \"" + macro_string + "\"");
207 }
208
209 return e.exists;
210 }
211 }
212 return false;
213 }
214
224 [[nodiscard]] auto exists(const std::string& path) const -> bool
225 {
226 for (const entry& e : entries_) {
227 if (e.path == path) {
228 if (e.ec && e.ec != couchbase::errc::key_value::path_not_found) {
229 throw std::system_error(e.ec, "error getting result for path \"" + e.path + "\"");
230 }
231
232 return e.exists;
233 }
234 }
235 return false;
236 }
237
250 [[nodiscard]] auto is_deleted() const -> bool
251 {
252 return is_deleted_;
253 }
254
264 [[nodiscard]] auto has_value(std::size_t index) const -> bool
265 {
266 for (const entry& e : entries_) {
267 if (e.original_index == index) {
268 return !e.value.empty();
269 }
270 }
271 throw std::system_error(errc::key_value::path_invalid,
272 "invalid index for mutate_in result: " + std::to_string(index));
273 }
274
284 [[nodiscard]] auto has_value(const std::string& path) const -> bool
285 {
286 for (const entry& e : entries_) {
287 if (e.path == path) {
288 return !e.value.empty();
289 }
290 }
291 throw std::system_error(errc::key_value::path_invalid,
292 "invalid path for mutate_in result: " + path);
293 }
294
295private:
296 std::vector<entry> entries_{};
297 bool is_deleted_{ false };
298};
299
300} // namespace couchbase
CAS is a special type that represented in protocol using unsigned 64-bit integer, but only equality c...
Definition cas.hxx:34
Definition tao_json_serializer.hxx:42
Represents result of lookup_in operations.
Definition lookup_in_result.hxx:43
auto exists(subdoc::lookup_in_macro macro) const -> bool
Allows to check if a value at the given index exists.
Definition lookup_in_result.hxx:200
auto content_as(subdoc::lookup_in_macro macro) const -> Document
Decodes field of the document into type.
Definition lookup_in_result.hxx:151
lookup_in_result(couchbase::cas cas, std::vector< entry > entries, bool is_deleted)
Constructs result for lookup_in_result operation.
Definition lookup_in_result.hxx:73
auto exists(std::size_t index) const -> bool
Allows to check if a value at the given index exists.
Definition lookup_in_result.hxx:177
auto content_as(const std::string &path) const -> Document
Decodes field of the document into type.
Definition lookup_in_result.hxx:123
auto has_value(const std::string &path) const -> bool
Returns whether the field has value.
Definition lookup_in_result.hxx:284
auto has_value(std::size_t index) const -> bool
Returns whether the field has value.
Definition lookup_in_result.hxx:264
auto is_deleted() const -> bool
Returns whether this document was deleted (a tombstone).
Definition lookup_in_result.hxx:250
auto content_as(std::size_t index) const -> Document
Decodes field of the document into type.
Definition lookup_in_result.hxx:93
auto exists(const std::string &path) const -> bool
Allows to check if a value at the given index exists.
Definition lookup_in_result.hxx:224
Base class for operations of data service.
Definition result.hxx:32
std::vector< std::byte > binary
Definition encoded_value.hxx:25
@ path_not_found
The path provided for a sub-document operation was not found.
@ path_invalid
The path provided for a sub-document operation was not syntactically correct.
lookup_in_macro
Definition lookup_in_macro.hxx:29
auto to_string(lookup_in_macro value) -> std::string
Converts macro into binary array suitable for sending to the server.
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
Definition lookup_in_result.hxx:49
std::error_code ec
Definition lookup_in_result.hxx:54
std::string path
Definition lookup_in_result.hxx:50
codec::binary value
Definition lookup_in_result.hxx:51
std::size_t original_index
Definition lookup_in_result.hxx:52
bool exists
Definition lookup_in_result.hxx:53