Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
Loading...
Searching...
No Matches
mutate_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
22
23#include <optional>
24
25namespace couchbase
26{
27
35{
36public:
41 struct entry {
42 std::string path;
44 std::size_t original_index;
45 };
46
51 mutate_in_result() = default;
52
66 std::vector<entry> entries,
67 bool is_deleted)
68 : mutation_result{ cas, std::move(token) }
69 , entries_(std::move(entries))
70 , is_deleted_(is_deleted)
71 {
72 }
73
84 template<typename Document>
85 [[nodiscard]] auto content_as(std::size_t index) const -> Document
86 {
87 for (const entry& e : entries_) {
88 if (e.original_index == index) {
89 return codec::tao_json_serializer::deserialize<Document>(e.value);
90 }
91 }
92 throw std::system_error(errc::key_value::path_invalid,
93 "invalid index for mutate_in result: " + std::to_string(index));
94 }
95
106 template<typename Document>
107 [[nodiscard]] auto content_as(const std::string& path) const -> Document
108 {
109 for (const entry& e : entries_) {
110 if (e.path == path) {
111 return codec::tao_json_serializer::deserialize<Document>(e.value);
112 }
113 }
114 throw std::system_error(errc::key_value::path_invalid,
115 "invalid path for mutate_in result: " + path);
116 }
117
130 [[nodiscard]] auto is_deleted() const -> bool
131 {
132 return is_deleted_;
133 }
134
144 [[nodiscard]] auto has_value(std::size_t index) const -> bool
145 {
146 for (const entry& e : entries_) {
147 if (e.original_index == index) {
148 return !e.value.empty();
149 }
150 }
151 throw std::system_error(errc::key_value::path_invalid,
152 "invalid index for mutate_in result: " + std::to_string(index));
153 }
154
164 [[nodiscard]] auto has_value(const std::string& path) const -> bool
165 {
166 for (const entry& e : entries_) {
167 if (e.path == path) {
168 return !e.value.empty();
169 }
170 }
171 throw std::system_error(errc::key_value::path_invalid,
172 "invalid path for mutate_in result: " + path);
173 }
174
175private:
176 std::vector<entry> entries_{};
177 bool is_deleted_{ false };
178};
179
180} // namespace couchbase
CAS is a special type that represented in protocol using unsigned 64-bit integer, but only equality c...
Definition cas.hxx:34
Represents result of mutate_in operations.
Definition mutate_in_result.hxx:35
mutate_in_result(couchbase::cas cas, couchbase::mutation_token token, std::vector< entry > entries, bool is_deleted)
Constructs result for mutate_in_result operation.
Definition mutate_in_result.hxx:64
auto has_value(const std::string &path) const -> bool
Returns whether the field has value.
Definition mutate_in_result.hxx:164
auto content_as(const std::string &path) const -> Document
Decodes field of the document into type.
Definition mutate_in_result.hxx:107
auto has_value(std::size_t index) const -> bool
Returns whether the field has value.
Definition mutate_in_result.hxx:144
auto content_as(std::size_t index) const -> Document
Decodes field of the document into type.
Definition mutate_in_result.hxx:85
auto is_deleted() const -> bool
Returns whether this document was deleted (a tombstone).
Definition mutate_in_result.hxx:130
Represents result of mutation operations.
Definition mutation_result.hxx:35
Value object to contain partition details and sequence number.
Definition mutation_token.hxx:32
std::vector< std::byte > binary
Definition encoded_value.hxx:25
@ path_invalid
The path provided for a sub-document operation was not syntactically correct.
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
Definition mutate_in_result.hxx:41
std::string path
Definition mutate_in_result.hxx:42
codec::binary value
Definition mutate_in_result.hxx:43
std::size_t original_index
Definition mutate_in_result.hxx:44