Couchbase C++ SDK 1.0.4 (rev. 5355b0f)
Loading...
Searching...
No Matches
analytics_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
23#include <couchbase/error.hxx>
25
26#include <chrono>
27#include <functional>
28#include <map>
29#include <optional>
30
31namespace couchbase
32{
33
34namespace codec
35{
37} // namespace codec
38
45struct analytics_options : public common_options<analytics_options> {
52 struct built : public common_options<analytics_options>::built {
53 std::optional<std::string> client_context_id;
54 const bool priority;
55 const bool readonly;
56 std::optional<analytics_scan_consistency> scan_consistency;
57 std::optional<std::chrono::milliseconds> scan_wait;
58 std::vector<mutation_token> mutation_state;
59 std::vector<codec::binary> positional_parameters;
60 std::map<std::string, codec::binary, std::less<>> named_parameters;
61 std::map<std::string, codec::binary, std::less<>> raw;
62 };
63
75 [[nodiscard]] auto build() const -> built
76 {
77 return {
79 client_context_id_,
80 priority_,
81 readonly_,
82 scan_consistency_,
83 scan_wait_,
84 mutation_state_,
85 positional_parameters_,
86 named_parameters_,
87 raw_,
88 };
89 }
90
101 auto priority(bool prioritized) -> analytics_options&
102 {
103 priority_ = prioritized;
104 return self();
105 }
106
124 {
125 readonly_ = readonly;
126 return self();
127 }
128
142 auto scan_wait(std::chrono::milliseconds wait) -> analytics_options&
143 {
144 if (scan_consistency_ == analytics_scan_consistency::not_bounded) {
145 scan_wait_.reset();
146 } else {
147 scan_wait_ = wait;
148 }
149 return self();
150 }
151
165 {
166 if (client_context_id.empty()) {
167 client_context_id_.reset();
168 } else {
169 client_context_id_ = std::move(client_context_id);
170 }
171 return self();
172 }
173
197 {
198 scan_consistency_ = scan_consistency;
199 mutation_state_.clear();
200 return self();
201 }
202
224 {
225 mutation_state_ = state.tokens();
226 scan_consistency_.reset();
227 return self();
228 }
229
239 template<typename Serializer = codec::tao_json_serializer,
240 typename Value,
241 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
242 auto raw(std::string name, const Value& value) -> analytics_options&
243 {
244 raw_[std::move(name)] = std::move(Serializer::template serialize<const Value&>(value));
245 return self();
246 }
247
258 template<typename Serializer = codec::tao_json_serializer,
259 typename... Parameters,
260 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
261 auto positional_parameters(const Parameters&... parameters) -> analytics_options&
262 {
263 named_parameters_.clear();
264 positional_parameters_.clear();
265 encode_positional_parameters<Serializer>(parameters...);
266 return self();
267 }
268
279 template<typename Serializer = codec::tao_json_serializer,
280 typename... Parameters,
281 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
282 auto named_parameters(const Parameters&... parameters) -> analytics_options&
283 {
284 named_parameters_.clear();
285 positional_parameters_.clear();
286 encode_named_parameters<Serializer>(parameters...);
287 return self();
288 }
289
304 auto encoded_raw_options(std::map<std::string, codec::binary, std::less<>> options)
306 {
307 raw_ = std::move(options);
308 return self();
309 }
310
326 auto encoded_positional_parameters(std::vector<codec::binary> parameters) -> analytics_options&
327 {
328 named_parameters_.clear();
329 positional_parameters_ = std::move(parameters);
330 return self();
331 }
332
347 auto encoded_named_parameters(std::map<std::string, codec::binary, std::less<>> parameters)
349 {
350 named_parameters_ = std::move(parameters);
351 positional_parameters_.clear();
352 return self();
353 }
354
355private:
356 template<typename Serializer = codec::tao_json_serializer,
357 typename Parameter,
358 typename... Rest,
359 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
360 void encode_positional_parameters(const Parameter& parameter, Rest... args)
361 {
362 positional_parameters_.emplace_back(
363 std::move(Serializer::template serialize<Parameter>(parameter)));
364 if constexpr (sizeof...(args) > 0) {
365 encode_positional_parameters<Serializer>(args...);
366 }
367 }
368
369 template<typename Serializer = codec::tao_json_serializer,
370 typename Name,
371 typename Parameter,
372 typename... Rest,
373 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
374 void encode_named_parameters(const std::pair<Name, Parameter>& parameter, Rest... args)
375 {
376 named_parameters_[parameter.first] =
377 std::move(Serializer::template serialize<Parameter>(parameter.second));
378 if constexpr (sizeof...(args) > 0) {
379 encode_named_parameters<Serializer>(args...);
380 }
381 }
382
383 bool priority_{ true };
384 bool readonly_{ false };
385 std::optional<std::string> client_context_id_{};
386 std::optional<std::chrono::milliseconds> scan_wait_{};
387 std::optional<analytics_scan_consistency> scan_consistency_{};
388 std::vector<mutation_token> mutation_state_{};
389 std::vector<codec::binary> positional_parameters_{};
390 std::map<std::string, codec::binary, std::less<>> raw_{};
391 std::map<std::string, codec::binary, std::less<>> named_parameters_{};
392};
393
401using analytics_handler = std::function<void(error, analytics_result)>;
402} // namespace couchbase
Represents result of cluster::analytics_query() and scope::analytics_query() calls.
Definition analytics_result.hxx:41
Definition tao_json_serializer.hxx:42
Common options that used by most operations.
Definition common_options.hxx:38
auto self() -> analytics_options &
Definition common_options.hxx:116
auto build_common_options() const -> built
Definition common_options.hxx:103
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
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
analytics_scan_consistency
Definition analytics_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, analytics_result)> analytics_handler
The signature for the handler of the cluster::analytics_query() and scope::analytics_query() operatio...
Definition analytics_options.hxx:401
Immutable value object representing consistent options.
Definition analytics_options.hxx:52
std::vector< mutation_token > mutation_state
Definition analytics_options.hxx:58
std::map< std::string, codec::binary, std::less<> > named_parameters
Definition analytics_options.hxx:60
const bool priority
Definition analytics_options.hxx:54
const bool readonly
Definition analytics_options.hxx:55
std::optional< std::chrono::milliseconds > scan_wait
Definition analytics_options.hxx:57
std::map< std::string, codec::binary, std::less<> > raw
Definition analytics_options.hxx:61
std::optional< std::string > client_context_id
Definition analytics_options.hxx:53
std::optional< analytics_scan_consistency > scan_consistency
Definition analytics_options.hxx:56
std::vector< codec::binary > positional_parameters
Definition analytics_options.hxx:59
Options for cluster::analytics_query() and scope::analytics_query().
Definition analytics_options.hxx:45
auto raw(std::string name, const Value &value) -> analytics_options &
Definition analytics_options.hxx:242
auto client_context_id(std::string client_context_id) -> analytics_options &
Supports providing a custom client context ID for this query.
Definition analytics_options.hxx:164
auto positional_parameters(const Parameters &... parameters) -> analytics_options &
Set list of positional parameters for a query.
Definition analytics_options.hxx:261
auto scan_consistency(analytics_scan_consistency scan_consistency) -> analytics_options &
Customizes the consistency guarantees for this query.
Definition analytics_options.hxx:196
auto encoded_raw_options(std::map< std::string, codec::binary, std::less<> > options) -> analytics_options &
Set map of raw options for a query.
Definition analytics_options.hxx:304
auto encoded_positional_parameters(std::vector< codec::binary > parameters) -> analytics_options &
Set list of positional parameters for a query.
Definition analytics_options.hxx:326
auto readonly(bool readonly) -> analytics_options &
Allows explicitly marking a query as being readonly and not mutating and documents on the server side...
Definition analytics_options.hxx:123
auto scan_wait(std::chrono::milliseconds wait) -> analytics_options &
Allows customizing how long the query engine is willing to wait until the index catches up to whateve...
Definition analytics_options.hxx:142
auto priority(bool prioritized) -> analytics_options &
Allows to give certain requests higher priority than others.
Definition analytics_options.hxx:101
auto build() const -> built
Validates options and returns them as an immutable value.
Definition analytics_options.hxx:75
auto consistent_with(const mutation_state &state) -> analytics_options &
Sets the mutation_tokens this query should be consistent with.
Definition analytics_options.hxx:223
auto encoded_named_parameters(std::map< std::string, codec::binary, std::less<> > parameters) -> analytics_options &
Set map of named parameters for a query.
Definition analytics_options.hxx:347
auto named_parameters(const Parameters &... parameters) -> analytics_options &
Set list of named parameters for a query.
Definition analytics_options.hxx:282