Couchbase C++ SDK 1.0.2 (rev. 51f4775)
Loading...
Searching...
No Matches
scan_type.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 2024. 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
20#include <cstdint>
21#include <optional>
22#include <string>
23#include <utility>
24
25namespace couchbase
26{
33struct scan_term {
34public:
43 explicit scan_term(std::string term)
44 : term_{ std::move(term) }
45 {
46 }
47
56 {
57 exclusive_ = exclusive;
58 return *this;
59 }
60
67 struct built {
68 std::string term;
70 };
71
80 [[nodiscard]] auto build() const -> built
81 {
82 return { term_, exclusive_ };
83 }
84
85private:
86 std::string term_{};
87 bool exclusive_{ false };
88};
89
96struct scan_type {
97 virtual ~scan_type() = default;
98
105 struct built {
112
113 std::string prefix{};
114
115 std::optional<scan_term::built> from{};
116 std::optional<scan_term::built> to{};
117
118 std::size_t limit{};
119 std::optional<std::uint64_t> seed{};
120 };
121
130 [[nodiscard]] virtual auto build() const -> built = 0;
131};
132
140public:
149 explicit prefix_scan(std::string prefix)
150 : prefix_{ std::move(prefix) }
151 {
152 }
153
162 [[nodiscard]] auto build() const -> built override
163 {
164 return {
166 prefix_,
167 };
168 }
169
170private:
171 std::string prefix_{};
172};
173
181public:
188 range_scan() = default;
189
199 range_scan(std::optional<scan_term> from, std::optional<scan_term> to)
200 : from_{ std::move(from) }
201 , to_{ std::move(to) }
202 {
203 }
204
214 auto from(scan_term from) -> range_scan&
215 {
216 from_ = std::move(from);
217 return *this;
218 }
219
230 {
231 to_ = std::move(to);
232 return *this;
233 }
234
243 [[nodiscard]] auto build() const -> built override
244 {
245 return {
246 scan_type::built::type::range_scan,
247 {},
248 (from_) ? std::make_optional(from_->build()) : std::nullopt,
249 (to_) ? std::make_optional(to_->build()) : std::nullopt,
250 };
251 }
252
253private:
254 std::optional<scan_term> from_{};
255 std::optional<scan_term> to_{};
256};
257
265public:
274 explicit sampling_scan(std::size_t limit)
275 : limit_{ limit }
276 {
277 }
278
288 sampling_scan(std::size_t limit, std::uint64_t seed)
289 : limit_{ limit }
290 , seed_{ seed }
291 {
292 }
293
303 auto seed(std::uint64_t seed) -> sampling_scan&
304 {
305 seed_ = seed;
306 return *this;
307 }
308
317 [[nodiscard]] auto build() const -> built override
318 {
319 return {
320 scan_type::built::type::sampling_scan, {}, {}, {}, limit_, seed_,
321 };
322 }
323
324private:
325 std::size_t limit_{};
326 std::optional<std::uint64_t> seed_{};
327};
328} // namespace couchbase
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
A prefix scan performs a scan that includes all documents whose keys start with the given prefix.
Definition scan_type.hxx:139
auto build() const -> built override
Returns the prefix scan type as an immutable value.
Definition scan_type.hxx:162
prefix_scan(std::string prefix)
Creates an instance of a prefix scan type.
Definition scan_type.hxx:149
A range scan performs a scan on a range of keys.
Definition scan_type.hxx:180
range_scan(std::optional< scan_term > from, std::optional< scan_term > to)
Creates an instance of a range scan type.
Definition scan_type.hxx:199
auto build() const -> built override
Returns the range scan type as an immutable value.
Definition scan_type.hxx:243
range_scan()=default
Creates an instance of a range scan type with no bounds.
auto to(scan_term to) -> range_scan &
Specifies the upper bound of the range.
Definition scan_type.hxx:229
auto from(scan_term from) -> range_scan &
Specifies the lower bound of the range.
Definition scan_type.hxx:214
A sampling scan performs a scan that randomly selects documents up to a configured limit.
Definition scan_type.hxx:264
auto build() const -> built override
Returns the sampling scan type as an immutable value.
Definition scan_type.hxx:317
sampling_scan(std::size_t limit)
Creates an instance of a sampling scan type.
Definition scan_type.hxx:274
sampling_scan(std::size_t limit, std::uint64_t seed)
Creates an instance of a sampling scan type with a seed.
Definition scan_type.hxx:288
auto seed(std::uint64_t seed) -> sampling_scan &
Sets the seed for the sampling scan.
Definition scan_type.hxx:303
Immutable value representing the scan term.
Definition scan_type.hxx:67
bool exclusive
Definition scan_type.hxx:69
std::string term
Definition scan_type.hxx:68
A scan term used to specify the bounds of a range scan operation.
Definition scan_type.hxx:33
auto exclusive(bool exclusive) -> scan_term &
Specifies whether this term is excluded from the scan results.
Definition scan_type.hxx:55
auto build() const -> built
Returns the scan term as an immutable value.
Definition scan_type.hxx:80
scan_term(std::string term)
Constructs an instance representing the scan term for the given term.
Definition scan_type.hxx:43
Immutable value representing the scan type.
Definition scan_type.hxx:105
type type
Definition scan_type.hxx:111
std::optional< scan_term::built > to
Definition scan_type.hxx:116
std::optional< scan_term::built > from
Definition scan_type.hxx:115
std::string prefix
Definition scan_type.hxx:113
type
Definition scan_type.hxx:106
@ range_scan
Definition scan_type.hxx:108
@ prefix_scan
Definition scan_type.hxx:107
std::optional< std::uint64_t > seed
Definition scan_type.hxx:119
std::size_t limit
Definition scan_type.hxx:118
The base class for the different scan types.
Definition scan_type.hxx:96
virtual ~scan_type()=default
virtual auto build() const -> built=0
Returns the scan type as an immutable value.