Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
Loading...
Searching...
No Matches
mutate_in_specs.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
34
35#include <vector>
36
37namespace couchbase
38{
39#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
40namespace
41{
42template<typename Value>
43std::vector<std::vector<std::byte>>
44encode_array(const Value& value)
45{
46 return { std::move(codec::default_json_transcoder::encode(value).data) };
47}
48
49template<typename Value>
50std::vector<std::vector<std::byte>>
51encode_array(std::vector<std::vector<std::byte>>&& output, const Value& value)
52{
53 output.emplace_back(std::move(codec::default_json_transcoder::encode(value).data));
54 return std::move(output);
55}
56
57template<typename Value, typename... Rest>
58std::vector<std::vector<std::byte>>
59encode_array(std::vector<std::vector<std::byte>>&& output, const Value& value, Rest... args)
60{
61 output.emplace_back(std::move(codec::default_json_transcoder::encode(value).data));
62 return encode_array(std::move(output), args...);
63}
64
65template<typename Value, typename... Rest>
66std::vector<std::vector<std::byte>>
67encode_array(const Value& value, Rest... args)
68{
69 return encode_array(encode_array(value), args...);
70}
71
72} // namespace
73#endif
74
76{
77public:
78 mutate_in_specs() = default;
79
80 template<typename... Operation>
81 explicit mutate_in_specs(Operation... args)
82 {
83 push_back(args...);
84 }
85
99 template<typename Value>
100 static auto replace(std::string path, const Value& value) -> subdoc::replace
101 {
102 return { std::move(path), std::move(codec::default_json_transcoder::encode(value).data) };
103 }
104
117 static auto replace(std::string path, subdoc::mutate_in_macro value) -> subdoc::replace
118 {
119 return { std::move(path), value };
120 }
121
138 static auto replace_raw(std::string path,
139 std::vector<std::byte> value,
140 bool expand_macro = false) -> subdoc::replace
141 {
142 return { std::move(path), std::move(value), expand_macro };
143 }
144
157 template<typename Value>
158 static auto insert(std::string path, const Value& value) -> subdoc::insert
159 {
160 return { std::move(path), std::move(codec::default_json_transcoder::encode(value).data) };
161 }
162
176 static auto insert(std::string path, subdoc::mutate_in_macro value) -> subdoc::insert
177 {
178 return { std::move(path), value };
179 }
180
196 static auto insert_raw(std::string path,
197 std::vector<std::byte> value,
198 bool expand_macro = false) -> subdoc::insert
199 {
200 return { std::move(path), std::move(value), expand_macro };
201 }
202
214 static auto remove(std::string path) -> subdoc::remove
215 {
216 return subdoc::remove{ std::move(path) };
217 }
218
231 template<typename Value>
232 static auto upsert(std::string path, const Value& value) -> subdoc::upsert
233 {
234 return { std::move(path), std::move(codec::default_json_transcoder::encode(value).data) };
235 }
236
250 static auto upsert(std::string path, subdoc::mutate_in_macro value) -> subdoc::upsert
251 {
252 return { std::move(path), value };
253 }
254
270 static auto upsert_raw(std::string path,
271 std::vector<std::byte> value,
272 bool expand_macro = false) -> subdoc::upsert
273 {
274 return { std::move(path), std::move(value), expand_macro };
275 }
276
289 static auto increment(std::string path, std::int64_t delta) -> subdoc::counter
290 {
291 if (delta < 0) {
292 throw std::system_error(errc::common::invalid_argument,
293 "only positive delta allowed in subdoc increment, given: " +
294 std::to_string(delta));
295 }
296 return { std::move(path), delta };
297 }
298
309 static auto decrement(std::string path, std::int64_t delta) -> subdoc::counter
310 {
311 if (delta < 0) {
312 throw std::system_error(errc::common::invalid_argument,
313 "only positive delta allowed in subdoc decrement, given: " +
314 std::to_string(delta));
315 }
316 return { std::move(path), -1 * delta };
317 }
318
331 template<typename... Values>
332 static auto array_append(std::string path, Values... values) -> subdoc::array_append
333 {
334 return { std::move(path), encode_array(values...) };
335 }
336
351 static auto array_append_raw(std::string path,
352 std::vector<std::byte> values) -> subdoc::array_append
353 {
354 return { std::move(path), { std::move(values) } };
355 }
356
369 template<typename... Values>
370 static auto array_prepend(std::string path, Values... values) -> subdoc::array_prepend
371 {
372 return { std::move(path), encode_array(values...) };
373 }
374
389 static auto array_prepend_raw(std::string path,
390 std::vector<std::byte> values) -> subdoc::array_prepend
391 {
392 return { std::move(path), { std::move(values) } };
393 }
394
408 template<typename... Values>
409 static auto array_insert(std::string path, Values... values) -> subdoc::array_insert
410 {
411 return { std::move(path), encode_array(values...) };
412 }
413
429 static auto array_insert_raw(std::string path,
430 std::vector<std::byte> values) -> subdoc::array_insert
431 {
432 return { std::move(path), { std::move(values) } };
433 }
434
449 template<typename Value>
450 static auto array_add_unique(std::string path, const Value& value) -> subdoc::array_add_unique
451 {
452 return { std::move(path), std::move(codec::default_json_transcoder::encode(value).data) };
453 }
454
470 static auto array_add_unique(std::string path,
472 {
473 return { std::move(path), value };
474 }
475
493 static auto array_add_unique_raw(std::string path,
494 std::vector<std::byte> value,
495 bool expand_macro = false) -> subdoc::array_add_unique
496 {
497 return { std::move(path), std::move(value), expand_macro };
498 }
499
509 template<typename Operation>
510 void push_back(const Operation& operation)
511 {
512 operation.encode(bundle());
513 }
514
526 template<typename Operation, typename... Rest>
527 void push_back(const Operation& operation, Rest... args)
528 {
529 push_back(operation);
530 push_back(args...);
531 }
532
540 [[nodiscard]] auto specs() const -> const std::vector<core::impl::subdoc::command>&;
541
542private:
543 [[nodiscard]] auto bundle() -> core::impl::subdoc::command_bundle&;
544
545 std::shared_ptr<core::impl::subdoc::command_bundle> specs_{};
546};
547} // namespace couchbase
static auto encode(Document document) -> encoded_value
Definition json_transcoder.hxx:33
Definition mutate_in_specs.hxx:76
void push_back(const Operation &operation)
Add subdocument operation to list of specs.
Definition mutate_in_specs.hxx:510
static auto increment(std::string path, std::int64_t delta) -> subdoc::counter
Creates a command with the intent of incrementing a numerical field in a JSON object.
Definition mutate_in_specs.hxx:289
static auto remove(std::string path) -> subdoc::remove
Creates a command with the intention of removing an existing value in a JSON object.
Definition mutate_in_specs.hxx:214
void push_back(const Operation &operation, Rest... args)
Add subdocument operations to list of specs.
Definition mutate_in_specs.hxx:527
static auto insert(std::string path, const Value &value) -> subdoc::insert
Creates a command with the intention of inserting a new value in a JSON object.
Definition mutate_in_specs.hxx:158
static auto upsert_raw(std::string path, std::vector< std::byte > value, bool expand_macro=false) -> subdoc::upsert
Creates a command with the intention of upserting a value in a JSON object.
Definition mutate_in_specs.hxx:270
static auto array_append_raw(std::string path, std::vector< std::byte > values) -> subdoc::array_append
Creates a command with the intention of appending a value to an existing JSON array.
Definition mutate_in_specs.hxx:351
static auto upsert(std::string path, const Value &value) -> subdoc::upsert
Creates a command with the intention of upserting a value in a JSON object.
Definition mutate_in_specs.hxx:232
static auto insert_raw(std::string path, std::vector< std::byte > value, bool expand_macro=false) -> subdoc::insert
Creates a command with the intention of inserting a new value in a JSON object.
Definition mutate_in_specs.hxx:196
static auto array_add_unique_raw(std::string path, std::vector< std::byte > value, bool expand_macro=false) -> subdoc::array_add_unique
Creates a command with the intent of inserting a value into an existing JSON array,...
Definition mutate_in_specs.hxx:493
static auto replace(std::string path, const Value &value) -> subdoc::replace
Creates a spec with the intention of replacing an existing value in a JSON document.
Definition mutate_in_specs.hxx:100
auto specs() const -> const std::vector< core::impl::subdoc::command > &
Returns internal representation of the specs.
mutate_in_specs(Operation... args)
Definition mutate_in_specs.hxx:81
static auto array_insert_raw(std::string path, std::vector< std::byte > values) -> subdoc::array_insert
Creates a command with the intention of inserting a value into an existing JSON array.
Definition mutate_in_specs.hxx:429
static auto array_add_unique(std::string path, subdoc::mutate_in_macro value) -> subdoc::array_add_unique
Creates a command with the intent of inserting a value into an existing JSON array,...
Definition mutate_in_specs.hxx:470
static auto array_prepend_raw(std::string path, std::vector< std::byte > values) -> subdoc::array_prepend
Creates a command with the intention of prepending a value to an existing JSON array.
Definition mutate_in_specs.hxx:389
static auto array_append(std::string path, Values... values) -> subdoc::array_append
Creates a command with the intention of appending a value to an existing JSON array.
Definition mutate_in_specs.hxx:332
static auto replace_raw(std::string path, std::vector< std::byte > value, bool expand_macro=false) -> subdoc::replace
Creates a spec with the intention of replacing an existing value in a JSON document.
Definition mutate_in_specs.hxx:138
static auto upsert(std::string path, subdoc::mutate_in_macro value) -> subdoc::upsert
Creates a command with the intention of upserting a value in a JSON object.
Definition mutate_in_specs.hxx:250
static auto insert(std::string path, subdoc::mutate_in_macro value) -> subdoc::insert
Creates a command with the intention of inserting a new value in a JSON object.
Definition mutate_in_specs.hxx:176
static auto array_insert(std::string path, Values... values) -> subdoc::array_insert
Creates a command with the intention of inserting a value into an existing JSON array.
Definition mutate_in_specs.hxx:409
static auto array_add_unique(std::string path, const Value &value) -> subdoc::array_add_unique
Creates a command with the intent of inserting a value into an existing JSON array,...
Definition mutate_in_specs.hxx:450
static auto replace(std::string path, subdoc::mutate_in_macro value) -> subdoc::replace
Creates a spec with the intention of replacing an existing value in a JSON document.
Definition mutate_in_specs.hxx:117
static auto array_prepend(std::string path, Values... values) -> subdoc::array_prepend
Creates a command with the intention of prepending a value to an existing JSON array.
Definition mutate_in_specs.hxx:370
static auto decrement(std::string path, std::int64_t delta) -> subdoc::counter
Creates a command with the intent of decrementing a numerical field in a JSON object.
Definition mutate_in_specs.hxx:309
An intention to perform a SubDocument array_add_unique operation.
Definition array_add_unique.hxx:41
An intention to perform a SubDocument array_append operation.
Definition array_append.hxx:41
An intention to perform a SubDocument array_insert operation.
Definition array_insert.hxx:41
An intention to perform a SubDocument array_prepend operation.
Definition array_prepend.hxx:41
An intention to perform a SubDocument counter operation.
Definition counter.hxx:39
An intention to perform a SubDocument insert operation.
Definition insert.hxx:41
An intention to perform a SubDocument remove operation.
Definition remove.hxx:39
An intention to perform a SubDocument replace operation.
Definition replace.hxx:41
An intention to perform a SubDocument upsert operation.
Definition upsert.hxx:41
@ invalid_argument
It is unambiguously determined that the error was caused because of invalid arguments from the user.
mutate_in_macro
Definition mutate_in_macro.hxx:29
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28