Couchbase C++ SDK 1.4.0 (rev. 59aa900)
Loading...
Searching...
No Matches
query_options.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
21#include <couchbase/error.hxx>
27
28#include <chrono>
29#include <functional>
30#include <map>
31#include <optional>
32#include <vector>
33
34namespace couchbase
35{
36namespace codec
37{
39} // namespace codec
40
47struct query_options : public common_options<query_options> {
54 struct built : public common_options<query_options>::built {
55 const bool adhoc;
56 const bool metrics;
57 const bool readonly;
58 const bool flex_index;
59 const bool preserve_expiry;
60 std::optional<bool> use_replica;
61 std::optional<std::uint64_t> max_parallelism;
62 std::optional<std::uint64_t> scan_cap;
63 std::optional<std::chrono::milliseconds> scan_wait;
64 std::optional<std::uint64_t> pipeline_batch;
65 std::optional<std::uint64_t> pipeline_cap;
66 std::optional<std::string> client_context_id;
67 std::optional<query_scan_consistency> scan_consistency;
68 std::vector<mutation_token> mutation_state;
69 std::optional<query_profile> profile;
70 std::vector<codec::binary> positional_parameters;
71 std::map<std::string, codec::binary, std::less<>> named_parameters;
72 std::map<std::string, codec::binary, std::less<>> raw;
73 };
74
86 [[nodiscard]] auto build() const -> built
87 {
88 return {
90 adhoc_,
91 metrics_,
92 readonly_,
93 flex_index_,
94 preserve_expiry_,
95 use_replica_,
96 max_parallelism_,
97 scan_cap_,
98 scan_wait_,
99 pipeline_batch_,
100 pipeline_cap_,
101 client_context_id_,
102 scan_consistency_,
103 mutation_state_,
104 profile_,
105 positional_parameters_,
106 named_parameters_,
107 raw_,
108 };
109 }
110
129 auto adhoc(bool adhoc) -> query_options&
130 {
131 adhoc_ = adhoc;
132 return self();
133 }
134
151 {
152 metrics_ = metrics;
153 return self();
154 }
155
173 {
174 profile_ = profile;
175 return self();
176 }
177
206 {
207 readonly_ = readonly;
208 return self();
209 }
210
221 {
222 flex_index_ = flex_index;
223 return self();
224 }
225
239 {
240 preserve_expiry_ = preserve_expiry;
241 return self();
242 }
243
256 {
257 use_replica_ = use_replica;
258 return self();
259 }
260
274 {
275 max_parallelism_ = max_parallelism;
276 return self();
277 }
278
291 auto scan_cap(std::uint64_t scan_cap) -> query_options&
292 {
293 scan_cap_ = scan_cap;
294 return self();
295 }
296
310 auto scan_wait(std::chrono::milliseconds wait) -> query_options&
311 {
312 if (scan_consistency_ == query_scan_consistency::not_bounded) {
313 scan_wait_.reset();
314 } else {
315 scan_wait_ = wait;
316 }
317 return self();
318 }
319
333 {
334 pipeline_batch_ = pipeline_batch;
335 return self();
336 }
337
352 {
353 pipeline_cap_ = pipeline_cap;
354 return self();
355 }
356
370 {
371 if (client_context_id.empty()) {
372 client_context_id_.reset();
373 } else {
374 client_context_id_ = std::move(client_context_id);
375 }
376 return self();
377 }
378
403 {
404 scan_consistency_ = scan_consistency;
405 mutation_state_.clear();
406 return self();
407 }
408
430 {
431 mutation_state_ = state.tokens();
432 scan_consistency_.reset();
433 return self();
434 }
435
446 template<typename Serializer = codec::tao_json_serializer,
447 typename Value,
448 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
449 auto raw(std::string name, const Value& value) -> query_options&
450 {
451 raw_[std::move(name)] = std::move(Serializer::template serialize<const Value&>(value));
452 return self();
453 }
454
466 template<typename Serializer = codec::tao_json_serializer,
467 typename... Parameters,
468 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
469 auto positional_parameters(const Parameters&... parameters) -> query_options&
470 {
471 positional_parameters_.clear();
472 encode_positional_parameters<Serializer>(parameters...);
473 return self();
474 }
475
486 template<typename Serializer = codec::tao_json_serializer,
487 typename... Parameters,
488 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
489 auto named_parameters(const Parameters&... parameters) -> query_options&
490 {
491 named_parameters_.clear();
492 encode_named_parameters<Serializer>(parameters...);
493 return self();
494 }
495
506 template<typename Serializer = codec::tao_json_serializer,
507 typename Parameter,
508 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
509 auto add_positional_parameter(const Parameter& parameter) -> query_options&
510 {
511 encode_positional_parameters<Serializer>(parameter);
512 return self();
513 }
514
526 template<typename Serializer = codec::tao_json_serializer,
527 typename Value,
528 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
529 auto add_named_parameter(const std::string& name, const Value& value) -> query_options&
530 {
531 encode_named_parameters<Serializer>(std::make_pair(name, value));
532 return self();
533 }
534
544 {
545 positional_parameters_.clear();
546 return self();
547 }
548
558 {
559 named_parameters_.clear();
560 return self();
561 }
562
577 auto encoded_raw_options(std::map<std::string, codec::binary, std::less<>> options)
578 -> query_options&
579 {
580 raw_ = std::move(options);
581 return self();
582 }
583
599 auto encoded_positional_parameters(std::vector<codec::binary> parameters) -> query_options&
600 {
601 positional_parameters_ = std::move(parameters);
602 return self();
603 }
604
619 auto encoded_named_parameters(std::map<std::string, codec::binary, std::less<>> parameters)
620 -> query_options&
621 {
622 named_parameters_ = std::move(parameters);
623 return self();
624 }
625
626private:
627 template<typename Serializer = codec::tao_json_serializer,
628 typename Parameter,
629 typename... Rest,
630 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
631 void encode_positional_parameters(const Parameter& parameter, Rest... args)
632 {
633 positional_parameters_.emplace_back(
634 std::move(Serializer::template serialize<const Parameter&>(parameter)));
635 if constexpr (sizeof...(args) > 0) {
636 encode_positional_parameters<Serializer>(args...);
637 }
638 }
639
640 template<typename Serializer = codec::tao_json_serializer,
641 typename Name,
642 typename Parameter,
643 typename... Rest,
644 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
645 void encode_named_parameters(const std::pair<Name, Parameter>& parameter, Rest... args)
646 {
647 named_parameters_[parameter.first] =
648 std::move(Serializer::template serialize<Parameter>(parameter.second));
649 if constexpr (sizeof...(args) > 0) {
650 encode_named_parameters<Serializer>(args...);
651 }
652 }
653
654 bool adhoc_{ true };
655 bool metrics_{ false };
656 bool readonly_{ false };
657 bool flex_index_{ false };
658 bool preserve_expiry_{ false };
659 std::optional<bool> use_replica_{};
660 std::optional<std::uint64_t> max_parallelism_{};
661 std::optional<std::uint64_t> scan_cap_{};
662 std::optional<std::uint64_t> pipeline_batch_{};
663 std::optional<std::uint64_t> pipeline_cap_{};
664 std::optional<std::string> client_context_id_{};
665 std::optional<std::chrono::milliseconds> scan_wait_{};
666 std::optional<query_scan_consistency> scan_consistency_{};
667 std::vector<mutation_token> mutation_state_{};
668 std::optional<query_profile> profile_{};
669 std::vector<codec::binary> positional_parameters_{};
670 std::map<std::string, codec::binary, std::less<>> raw_{};
671 std::map<std::string, codec::binary, std::less<>> named_parameters_{};
672};
673
680using query_handler = std::function<void(error, query_result)>;
681
688using query_stream_handler = std::function<void(error, query_stream_result)>;
689} // namespace couchbase
Definition tao_json_serializer.hxx:42
Common options that used by most operations.
Definition common_options.hxx:38
auto self() -> query_options &
Definition common_options.hxx:116
auto build_common_options() const -> built
Definition common_options.hxx:103
Definition error.hxx:31
Aggregation of one or more mutation_tokens for specifying consistency requirements of N1QL or FTS que...
Definition mutation_state.hxx:35
Represents result of query() and query() calls.
Definition query_result.hxx:42
A streaming result handle for N1QL queries.
Definition query_stream_result.hxx:68
Definition analytics_options.hxx:36
std::vector< std::byte > binary
Definition encoded_value.hxx:25
Represents a single item from the result of scan().
Definition allow_querying_search_index_options.hxx:28
query_profile
Definition query_profile.hxx:22
std::function< void(error, query_stream_result)> query_stream_handler
The signature for the handler of a streaming query operation.
Definition query_options.hxx:688
query_scan_consistency
Definition query_scan_consistency.hxx:22
@ not_bounded
The indexer will return whatever state it has to the query engine at the time of query.
Definition query_scan_consistency.hxx:33
std::function< void(error, query_result)> query_handler
The signature for the handler of the query() and query() operations.
Definition query_options.hxx:680
Immutable value object representing consistent options.
Definition query_options.hxx:54
std::vector< mutation_token > mutation_state
Definition query_options.hxx:68
std::optional< std::uint64_t > max_parallelism
Definition query_options.hxx:61
std::map< std::string, codec::binary, std::less<> > named_parameters
Definition query_options.hxx:71
const bool metrics
Definition query_options.hxx:56
std::optional< query_scan_consistency > scan_consistency
Definition query_options.hxx:67
std::optional< query_profile > profile
Definition query_options.hxx:69
const bool readonly
Definition query_options.hxx:57
std::optional< std::chrono::milliseconds > scan_wait
Definition query_options.hxx:63
std::map< std::string, codec::binary, std::less<> > raw
Definition query_options.hxx:72
const bool adhoc
Definition query_options.hxx:55
std::optional< std::uint64_t > scan_cap
Definition query_options.hxx:62
std::optional< std::string > client_context_id
Definition query_options.hxx:66
std::optional< bool > use_replica
Definition query_options.hxx:60
const bool flex_index
Definition query_options.hxx:58
const bool preserve_expiry
Definition query_options.hxx:59
std::vector< codec::binary > positional_parameters
Definition query_options.hxx:70
std::optional< std::uint64_t > pipeline_batch
Definition query_options.hxx:64
std::optional< std::uint64_t > pipeline_cap
Definition query_options.hxx:65
Options for cluster::query() and scope::query().
Definition query_options.hxx:47
auto client_context_id(std::string client_context_id) -> query_options &
Supports providing a custom client context ID for this query.
Definition query_options.hxx:369
auto named_parameters(const Parameters &... parameters) -> query_options &
Set list of named parameters for a query.
Definition query_options.hxx:489
auto preserve_expiry(bool preserve_expiry) -> query_options &
Tells the query engine to preserve expiration values set on any documents modified by this query.
Definition query_options.hxx:238
auto clear_named_parameters() -> query_options &
Clears the list of named parameters.
Definition query_options.hxx:557
auto encoded_raw_options(std::map< std::string, codec::binary, std::less<> > options) -> query_options &
Set map of raw options for a query.
Definition query_options.hxx:577
auto metrics(bool metrics) -> query_options &
Enables per-request metrics in the trailing section of the query.
Definition query_options.hxx:150
auto clear_positional_parameters() -> query_options &
Clears the list of positional parameters.
Definition query_options.hxx:543
auto max_parallelism(std::uint64_t max_parallelism) -> query_options &
Allows overriding the default maximum parallelism for the query execution on the server side.
Definition query_options.hxx:273
auto encoded_named_parameters(std::map< std::string, codec::binary, std::less<> > parameters) -> query_options &
Set map of named parameters for a query.
Definition query_options.hxx:619
auto raw(std::string name, const Value &value) -> query_options &
Definition query_options.hxx:449
auto pipeline_cap(std::uint64_t pipeline_cap) -> query_options &
Allows customizing the maximum number of items each execution operator can buffer between various ope...
Definition query_options.hxx:351
auto build() const -> built
Validates options and returns them as an immutable value.
Definition query_options.hxx:86
auto scan_wait(std::chrono::milliseconds wait) -> query_options &
Allows customizing how long the query engine is willing to wait until the index catches up to whateve...
Definition query_options.hxx:310
auto encoded_positional_parameters(std::vector< codec::binary > parameters) -> query_options &
Set list of positional parameters for a query.
Definition query_options.hxx:599
auto scan_cap(std::uint64_t scan_cap) -> query_options &
Supports customizing the maximum buffered channel size between the indexer and the query service.
Definition query_options.hxx:291
auto pipeline_batch(std::uint64_t pipeline_batch) -> query_options &
Supports customizing the number of items execution operators can batch for fetch from the KV layer on...
Definition query_options.hxx:332
auto readonly(bool readonly) -> query_options &
Allows explicitly marking a query as being readonly and not mutating and documents on the server side...
Definition query_options.hxx:205
auto use_replica(bool use_replica) -> query_options &
Specifies that the query engine should use replica nodes for KV fetches if the active node is down.
Definition query_options.hxx:255
auto adhoc(bool adhoc) -> query_options &
Allows turning this request into a prepared statement query.
Definition query_options.hxx:129
auto flex_index(bool flex_index) -> query_options &
Tells the query engine to use a flex index (utilizing the search service).
Definition query_options.hxx:220
auto add_named_parameter(const std::string &name, const Value &value) -> query_options &
Adds a named parameter to the current list of named parameters.
Definition query_options.hxx:529
auto consistent_with(const mutation_state &state) -> query_options &
Sets the mutation_tokens this query should be consistent with.
Definition query_options.hxx:429
auto scan_consistency(query_scan_consistency scan_consistency) -> query_options &
Customizes the consistency guarantees for this query.
Definition query_options.hxx:402
auto add_positional_parameter(const Parameter &parameter) -> query_options &
Adds a positional parameter to the current list of positional parameters.
Definition query_options.hxx:509
auto profile(query_profile profile) -> query_options &
Customizes the server profiling level for this query.
Definition query_options.hxx:172
auto positional_parameters(const Parameters &... parameters) -> query_options &
Set list of positional parameters for a query.
Definition query_options.hxx:469