Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
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
22#include <couchbase/error.hxx>
27
28#include <chrono>
29#include <functional>
30#include <memory>
31#include <optional>
32
33namespace couchbase
34{
41struct query_options : public common_options<query_options> {
48 struct built : public common_options<query_options>::built {
49 const bool adhoc;
50 const bool metrics;
51 const bool readonly;
52 const bool flex_index;
53 const bool preserve_expiry;
54 std::optional<bool> use_replica;
55 std::optional<std::uint64_t> max_parallelism;
56 std::optional<std::uint64_t> scan_cap;
57 std::optional<std::chrono::milliseconds> scan_wait;
58 std::optional<std::uint64_t> pipeline_batch;
59 std::optional<std::uint64_t> pipeline_cap;
60 std::optional<std::string> client_context_id;
61 std::optional<query_scan_consistency> scan_consistency;
62 std::vector<mutation_token> mutation_state;
63 std::optional<query_profile> profile;
64 std::vector<codec::binary> positional_parameters;
65 std::map<std::string, codec::binary, std::less<>> named_parameters;
66 std::map<std::string, codec::binary, std::less<>> raw;
67 };
68
80 [[nodiscard]] auto build() const -> built
81 {
82 return {
84 adhoc_,
85 metrics_,
86 readonly_,
87 flex_index_,
88 preserve_expiry_,
89 use_replica_,
90 max_parallelism_,
91 scan_cap_,
92 scan_wait_,
93 pipeline_batch_,
94 pipeline_cap_,
95 client_context_id_,
96 scan_consistency_,
97 mutation_state_,
98 profile_,
99 positional_parameters_,
100 named_parameters_,
101 raw_,
102 };
103 }
104
123 auto adhoc(bool adhoc) -> query_options&
124 {
125 adhoc_ = adhoc;
126 return self();
127 }
128
143 auto metrics(bool metrics) -> query_options&
144 {
145 metrics_ = metrics;
146 return self();
147 }
148
166 {
167 profile_ = profile;
168 return self();
169 }
170
199 {
200 readonly_ = readonly;
201 return self();
202 }
203
214 {
215 flex_index_ = flex_index;
216 return self();
217 }
218
232 {
233 preserve_expiry_ = preserve_expiry;
234 return self();
235 }
236
249 {
250 use_replica_ = use_replica;
251 return self();
252 }
253
267 {
268 max_parallelism_ = max_parallelism;
269 return self();
270 }
271
284 auto scan_cap(std::uint64_t scan_cap) -> query_options&
285 {
286 scan_cap_ = scan_cap;
287 return self();
288 }
289
303 auto scan_wait(std::chrono::milliseconds wait) -> query_options&
304 {
305 if (scan_consistency_ == query_scan_consistency::not_bounded) {
306 scan_wait_.reset();
307 } else {
308 scan_wait_ = wait;
309 }
310 return self();
311 }
312
326 {
327 pipeline_batch_ = pipeline_batch;
328 return self();
329 }
330
345 {
346 pipeline_cap_ = pipeline_cap;
347 return self();
348 }
349
363 {
364 if (client_context_id.empty()) {
365 client_context_id_.reset();
366 } else {
367 client_context_id_ = std::move(client_context_id);
368 }
369 return self();
370 }
371
395 {
396 scan_consistency_ = scan_consistency;
397 mutation_state_.clear();
398 return self();
399 }
400
422 {
423 mutation_state_ = state.tokens();
424 scan_consistency_.reset();
425 return self();
426 }
427
438 template<typename Value>
439 auto raw(std::string name, const Value& value) -> query_options&
440 {
441 raw_[std::move(name)] = std::move(codec::tao_json_serializer::serialize(value));
442 return self();
443 }
444
455 template<typename... Parameters>
456 auto positional_parameters(const Parameters&... parameters) -> query_options&
457 {
458 named_parameters_.clear();
459 positional_parameters_.clear();
460 encode_positional_parameters(parameters...);
461 return self();
462 }
463
474 template<typename... Parameters>
475 auto named_parameters(const Parameters&... parameters) -> query_options&
476 {
477 named_parameters_.clear();
478 positional_parameters_.clear();
479 encode_named_parameters(parameters...);
480 return self();
481 }
482
497 auto encoded_raw_options(std::map<std::string, codec::binary, std::less<>> options)
498 -> query_options&
499 {
500 raw_ = std::move(options);
501 return self();
502 }
503
519 auto encoded_positional_parameters(std::vector<codec::binary> parameters) -> query_options&
520 {
521 named_parameters_.clear();
522 positional_parameters_ = std::move(parameters);
523 return self();
524 }
525
540 auto encoded_named_parameters(std::map<std::string, codec::binary, std::less<>> parameters)
541 -> query_options&
542 {
543 named_parameters_ = std::move(parameters);
544 positional_parameters_.clear();
545 return self();
546 }
547
548private:
549 template<typename Parameter, typename... Rest>
550 void encode_positional_parameters(const Parameter& parameter, Rest... args)
551 {
552 positional_parameters_.emplace_back(
553 std::move(codec::tao_json_serializer::serialize(parameter)));
554 if constexpr (sizeof...(args) > 0) {
555 encode_positional_parameters(args...);
556 }
557 }
558
559 template<typename Name, typename Parameter, typename... Rest>
560 void encode_named_parameters(const std::pair<Name, Parameter>& parameter, Rest... args)
561 {
562 named_parameters_[parameter.first] =
563 std::move(codec::tao_json_serializer::serialize(parameter.second));
564 if constexpr (sizeof...(args) > 0) {
565 encode_named_parameters(args...);
566 }
567 }
568
569 bool adhoc_{ true };
570 bool metrics_{ false };
571 bool readonly_{ false };
572 bool flex_index_{ false };
573 bool preserve_expiry_{ false };
574 std::optional<bool> use_replica_{};
575 std::optional<std::uint64_t> max_parallelism_{};
576 std::optional<std::uint64_t> scan_cap_{};
577 std::optional<std::uint64_t> pipeline_batch_{};
578 std::optional<std::uint64_t> pipeline_cap_{};
579 std::optional<std::string> client_context_id_{};
580 std::optional<std::chrono::milliseconds> scan_wait_{};
581 std::optional<query_scan_consistency> scan_consistency_{};
582 std::vector<mutation_token> mutation_state_{};
583 std::optional<query_profile> profile_{};
584 std::vector<codec::binary> positional_parameters_{};
585 std::map<std::string, codec::binary, std::less<>> raw_{};
586 std::map<std::string, codec::binary, std::less<>> named_parameters_{};
587};
588
595using query_handler = std::function<void(error, query_result)>;
596} // namespace couchbase
static auto serialize(Document document) -> binary
Definition tao_json_serializer.hxx:47
Common options that used by most operations.
Definition common_options.hxx:37
auto self() -> query_options &
Allows to return the right options builder instance for child implementations.
Definition common_options.hxx:102
auto build_common_options() const -> built
Definition common_options.hxx:89
Definition error.hxx:30
Aggregation of one or more mutation_tokens for specifying consistency requirements of N1QL or FTS que...
Definition mutation_state.hxx:35
Represents result of cluster::query() and scope::query() calls.
Definition query_result.hxx:35
std::vector< std::byte > binary
Definition encoded_value.hxx:25
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
query_profile
Definition query_profile.hxx:22
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.
std::function< void(error, query_result)> query_handler
The signature for the handler of the cluster::query() and scope::query() operations.
Definition query_options.hxx:595
Immutable value object representing consistent options.
Definition query_options.hxx:48
std::vector< mutation_token > mutation_state
Definition query_options.hxx:62
std::optional< std::uint64_t > max_parallelism
Definition query_options.hxx:55
std::map< std::string, codec::binary, std::less<> > named_parameters
Definition query_options.hxx:65
const bool metrics
Definition query_options.hxx:50
std::optional< query_scan_consistency > scan_consistency
Definition query_options.hxx:61
std::optional< query_profile > profile
Definition query_options.hxx:63
const bool readonly
Definition query_options.hxx:51
std::optional< std::chrono::milliseconds > scan_wait
Definition query_options.hxx:57
std::map< std::string, codec::binary, std::less<> > raw
Definition query_options.hxx:66
const bool adhoc
Definition query_options.hxx:49
std::optional< std::uint64_t > scan_cap
Definition query_options.hxx:56
std::optional< std::string > client_context_id
Definition query_options.hxx:60
std::optional< bool > use_replica
Definition query_options.hxx:54
const bool flex_index
Definition query_options.hxx:52
const bool preserve_expiry
Definition query_options.hxx:53
std::vector< codec::binary > positional_parameters
Definition query_options.hxx:64
std::optional< std::uint64_t > pipeline_batch
Definition query_options.hxx:58
std::optional< std::uint64_t > pipeline_cap
Definition query_options.hxx:59
Options for cluster::query() and scope::query().
Definition query_options.hxx:41
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:362
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:231
auto positional_parameters(const Parameters &... parameters) -> query_options &
Set list of positional parameters for a query.
Definition query_options.hxx:456
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:497
auto metrics(bool metrics) -> query_options &
Enables per-request metrics in the trailing section of the query.
Definition query_options.hxx:143
auto named_parameters(const Parameters &... parameters) -> query_options &
Set list of named parameters for a query.
Definition query_options.hxx:475
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:266
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:540
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:344
auto build() const -> built
Validates options and returns them as an immutable value.
Definition query_options.hxx:80
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:303
auto encoded_positional_parameters(std::vector< codec::binary > parameters) -> query_options &
Set list of positional parameters for a query.
Definition query_options.hxx:519
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:284
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:325
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:198
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:248
auto adhoc(bool adhoc) -> query_options &
Allows turning this request into a prepared statement query.
Definition query_options.hxx:123
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:213
auto raw(std::string name, const Value &value) -> query_options &
Definition query_options.hxx:439
auto consistent_with(const mutation_state &state) -> query_options &
Sets the mutation_tokens this query should be consistent with.
Definition query_options.hxx:421
auto scan_consistency(query_scan_consistency scan_consistency) -> query_options &
Customizes the consistency guarantees for this query.
Definition query_options.hxx:394
auto profile(query_profile profile) -> query_options &
Customizes the server profiling level for this query.
Definition query_options.hxx:165