Couchbase C++ SDK 1.0.4 (rev. 5355b0f)
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 <memory>
36#include <vector>
37
38namespace couchbase
39{
40#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
41namespace
42{
43template<typename Value>
44auto
45encode_array(const Value& value) -> std::vector<std::vector<std::byte>>
46{
47 return { std::move(codec::default_json_transcoder::encode(value).data) };
48}
49
50template<typename Value>
51auto
52encode_array(std::vector<std::vector<std::byte>>&& output,
53 const Value& value) -> std::vector<std::vector<std::byte>>
54{
55 output.emplace_back(std::move(codec::default_json_transcoder::encode(value).data));
56 return std::move(output);
57}
58
59template<typename Value, typename... Rest>
60auto
61encode_array(std::vector<std::vector<std::byte>>&& output,
62 const Value& value,
63 Rest... args) -> std::vector<std::vector<std::byte>>
64{
65 output.emplace_back(std::move(codec::default_json_transcoder::encode(value).data));
66 return encode_array(std::move(output), args...);
67}
68
69template<typename Value, typename... Rest>
70auto
71encode_array(const Value& value, Rest... args) -> std::vector<std::vector<std::byte>>
72{
73 return encode_array(encode_array(value), args...);
74}
75
76} // namespace
77#endif
78
80{
81public:
82 mutate_in_specs() = default;
83
84 template<typename... Operation>
85 explicit mutate_in_specs(Operation... args)
86 {
87 push_back(args...);
88 }
89
103 template<typename Value>
104 static auto replace(std::string path, const Value& value) -> subdoc::replace
105 {
106 return { std::move(path), std::move(codec::default_json_transcoder::encode(value).data) };
107 }
108
121 static auto replace(std::string path, subdoc::mutate_in_macro value) -> subdoc::replace
122 {
123 return { std::move(path), value };
124 }
125
142 static auto replace_raw(std::string path,
143 std::vector<std::byte> value,
144 bool expand_macro = false) -> subdoc::replace
145 {
146 return { std::move(path), std::move(value), expand_macro };
147 }
148
161 template<typename Value>
162 static auto insert(std::string path, const Value& value) -> subdoc::insert
163 {
164 return { std::move(path), std::move(codec::default_json_transcoder::encode(value).data) };
165 }
166
180 static auto insert(std::string path, subdoc::mutate_in_macro value) -> subdoc::insert
181 {
182 return { std::move(path), value };
183 }
184
200 static auto insert_raw(std::string path,
201 std::vector<std::byte> value,
202 bool expand_macro = false) -> subdoc::insert
203 {
204 return { std::move(path), std::move(value), expand_macro };
205 }
206
218 static auto remove(std::string path) -> subdoc::remove
219 {
220 return subdoc::remove{ std::move(path) };
221 }
222
235 template<typename Value, typename Transcoder = codec::default_json_transcoder>
236 static auto upsert(std::string path, const Value& value) -> subdoc::upsert
237 {
238 return { std::move(path), std::move(Transcoder::template encode<const Value&>(value).data) };
239 }
240
254 static auto upsert(std::string path, subdoc::mutate_in_macro value) -> subdoc::upsert
255 {
256 return { std::move(path), value };
257 }
258
274 static auto upsert_raw(std::string path,
275 std::vector<std::byte> value,
276 bool expand_macro = false) -> subdoc::upsert
277 {
278 return { std::move(path), std::move(value), expand_macro };
279 }
280
293 static auto increment(std::string path, std::int64_t delta) -> subdoc::counter
294 {
295 if (delta < 0) {
296 throw std::system_error(errc::common::invalid_argument,
297 "only positive delta allowed in subdoc increment, given: " +
298 std::to_string(delta));
299 }
300 return { std::move(path), delta };
301 }
302
313 static auto decrement(std::string path, std::int64_t delta) -> subdoc::counter
314 {
315 if (delta < 0) {
316 throw std::system_error(errc::common::invalid_argument,
317 "only positive delta allowed in subdoc decrement, given: " +
318 std::to_string(delta));
319 }
320 return { std::move(path), -1 * delta };
321 }
322
335 template<typename... Values>
336 static auto array_append(std::string path, Values... values) -> subdoc::array_append
337 {
338 return { std::move(path), encode_array(values...) };
339 }
340
355 static auto array_append_raw(std::string path,
356 std::vector<std::byte> values) -> subdoc::array_append
357 {
358 return { std::move(path), { std::move(values) } };
359 }
360
373 template<typename... Values>
374 static auto array_prepend(std::string path, Values... values) -> subdoc::array_prepend
375 {
376 return { std::move(path), encode_array(values...) };
377 }
378
393 static auto array_prepend_raw(std::string path,
394 std::vector<std::byte> values) -> subdoc::array_prepend
395 {
396 return { std::move(path), { std::move(values) } };
397 }
398
412 template<typename... Values>
413 static auto array_insert(std::string path, Values... values) -> subdoc::array_insert
414 {
415 return { std::move(path), encode_array(values...) };
416 }
417
433 static auto array_insert_raw(std::string path,
434 std::vector<std::byte> values) -> subdoc::array_insert
435 {
436 return { std::move(path), { std::move(values) } };
437 }
438
453 template<typename Value, typename Transcoder = codec::default_json_transcoder>
454 static auto array_add_unique(std::string path, const Value& value) -> subdoc::array_add_unique
455 {
456 return { std::move(path), std::move(Transcoder::template encode<Value>(value).data) };
457 }
458
474 static auto array_add_unique(std::string path,
476 {
477 return { std::move(path), value };
478 }
479
497 static auto array_add_unique_raw(std::string path,
498 std::vector<std::byte> value,
499 bool expand_macro = false) -> subdoc::array_add_unique
500 {
501 return { std::move(path), std::move(value), expand_macro };
502 }
503
513 template<typename Operation>
514 void push_back(const Operation& operation)
515 {
516 operation.encode(bundle());
517 }
518
530 template<typename Operation, typename... Rest>
531 void push_back(const Operation& operation, Rest... args)
532 {
533 push_back(operation);
534 push_back(args...);
535 }
536
544 [[nodiscard]] auto specs() const -> const std::vector<core::impl::subdoc::command>&;
545
546private:
547 [[nodiscard]] auto bundle() -> core::impl::subdoc::command_bundle&;
548
549 std::shared_ptr<core::impl::subdoc::command_bundle> specs_{};
550};
551} // namespace couchbase
static auto encode(Document document) -> encoded_value
Definition json_transcoder.hxx:33
Definition mutate_in_specs.hxx:80
void push_back(const Operation &operation)
Add subdocument operation to list of specs.
Definition mutate_in_specs.hxx:514
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:293
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:218
void push_back(const Operation &operation, Rest... args)
Add subdocument operations to list of specs.
Definition mutate_in_specs.hxx:531
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:162
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:274
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:355
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:200
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:497
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:104
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:85
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:433
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:454
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:474
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:393
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:336
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:142
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:236
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:254
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:180
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:413
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:121
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:374
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:313
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