Couchbase C++ SDK 1.0.2 (rev. 51f4775)
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
24
25#include <string>
26#include <vector>
27
28namespace couchbase
29{
30namespace codec
31{
32class tao_json_serializer;
33} // namespace codec
34
42{
43public:
48 struct entry {
49 std::string path;
51 std::size_t original_index;
52 };
53
58 mutate_in_result() = default;
59
73 std::vector<entry> entries,
74 bool is_deleted)
75 : mutation_result{ cas, std::move(token) }
76 , entries_(std::move(entries))
77 , is_deleted_(is_deleted)
78 {
79 }
80
91 template<typename Document,
92 typename Serializer = codec::tao_json_serializer,
93 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
94 [[nodiscard]] auto content_as(std::size_t index) const -> Document
95 {
96 for (const entry& e : entries_) {
97 if (e.original_index == index) {
98 return Serializer::template deserialize<Document>(e.value);
99 }
100 }
101 throw std::system_error(errc::key_value::path_invalid,
102 "invalid index for mutate_in result: " + std::to_string(index));
103 }
104
115 template<typename Document,
116 typename Serializer = codec::tao_json_serializer,
117 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
118 [[nodiscard]] auto content_as(const std::string& path) const -> Document
119 {
120 for (const entry& e : entries_) {
121 if (e.path == path) {
122 return Serializer::template deserialize<Document>(e.value);
123 }
124 }
125 throw std::system_error(errc::key_value::path_invalid,
126 "invalid path for mutate_in result: " + path);
127 }
128
141 [[nodiscard]] auto is_deleted() const -> bool
142 {
143 return is_deleted_;
144 }
145
155 [[nodiscard]] auto has_value(std::size_t index) const -> bool
156 {
157 for (const entry& e : entries_) {
158 if (e.original_index == index) {
159 return !e.value.empty();
160 }
161 }
162 throw std::system_error(errc::key_value::path_invalid,
163 "invalid index for mutate_in result: " + std::to_string(index));
164 }
165
175 [[nodiscard]] auto has_value(const std::string& path) const -> bool
176 {
177 for (const entry& e : entries_) {
178 if (e.path == path) {
179 return !e.value.empty();
180 }
181 }
182 throw std::system_error(errc::key_value::path_invalid,
183 "invalid path for mutate_in result: " + path);
184 }
185
186private:
187 std::vector<entry> entries_{};
188 bool is_deleted_{ false };
189};
190
191} // 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 mutate_in operations.
Definition mutate_in_result.hxx:42
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:71
auto content_as(const std::string &path) const -> Document
Decodes field of the document into type.
Definition mutate_in_result.hxx:118
auto has_value(const std::string &path) const -> bool
Returns whether the field has value.
Definition mutate_in_result.hxx:175
auto has_value(std::size_t index) const -> bool
Returns whether the field has value.
Definition mutate_in_result.hxx:155
auto is_deleted() const -> bool
Returns whether this document was deleted (a tombstone).
Definition mutate_in_result.hxx:141
auto content_as(std::size_t index) const -> Document
Decodes field of the document into type.
Definition mutate_in_result.hxx:94
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:48
std::string path
Definition mutate_in_result.hxx:49
codec::binary value
Definition mutate_in_result.hxx:50
std::size_t original_index
Definition mutate_in_result.hxx:51