YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/docdb/doc_pgsql_scanspec.h
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_DOCDB_DOC_PGSQL_SCANSPEC_H
15
#define YB_DOCDB_DOC_PGSQL_SCANSPEC_H
16
17
#include <functional>
18
19
#include "yb/common/ql_scanspec.h"
20
21
#include "yb/docdb/docdb_fwd.h"
22
#include "yb/docdb/key_bytes.h"
23
24
#include "yb/rocksdb/options.h"
25
26
namespace yb {
27
namespace docdb {
28
29
// DocDB variant of scanspec.
30
class DocPgsqlScanSpec : public PgsqlScanSpec {
31
 public:
32
33
  // Scan for the specified doc_key.
34
  DocPgsqlScanSpec(const Schema& schema,
35
                   const rocksdb::QueryId query_id,
36
                   const DocKey& doc_key,
37
                   const boost::optional<int32_t> hash_code = boost::none,
38
                   const boost::optional<int32_t> max_hash_code = boost::none,
39
                   const DocKey& start_doc_key = DefaultStartDocKey(),
40
                   bool is_forward_scan = true);
41
42
  // Scan for the given hash key, a condition, and optional doc_key.
43
  //
44
  // Note: std::reference_wrapper is used instead of raw lvalue reference to prevent
45
  // temporary objects usage. The following code wont compile:
46
  //
47
  // DocPgsqlScanSpec spec(...{} /* hashed_components */, {} /* range_components */...);
48
  DocPgsqlScanSpec(const Schema& schema,
49
                   const rocksdb::QueryId query_id,
50
                   std::reference_wrapper<const std::vector<PrimitiveValue>> hashed_components,
51
                   std::reference_wrapper<const std::vector<PrimitiveValue>> range_components,
52
                   const PgsqlConditionPB* condition,
53
                   boost::optional<int32_t> hash_code,
54
                   boost::optional<int32_t> max_hash_code,
55
                   const PgsqlExpressionPB *where_expr,
56
                   const DocKey& start_doc_key = DefaultStartDocKey(),
57
                   bool is_forward_scan = true,
58
                   const DocKey& lower_doc_key = DefaultStartDocKey(),
59
                   const DocKey& upper_doc_key = DefaultStartDocKey());
60
61
  //------------------------------------------------------------------------------------------------
62
  // Access funtions.
63
11.6M
  const rocksdb::QueryId QueryId() const {
64
11.6M
    return query_id_;
65
11.6M
  }
66
67
11.7M
  bool is_forward_scan() const {
68
11.7M
    return is_forward_scan_;
69
11.7M
  }
70
71
  //------------------------------------------------------------------------------------------------
72
  // Filters.
73
  std::shared_ptr<rocksdb::ReadFileFilter> CreateFileFilter() const;
74
75
  // Return the inclusive lower and upper bounds of the scan.
76
  Result<KeyBytes> LowerBound() const;
77
  Result<KeyBytes> UpperBound() const;
78
79
  // Returns the lower/upper range components of the key.
80
  std::vector<PrimitiveValue> range_components(const bool lower_bound) const;
81
82
11.7M
  const QLScanRange* range_bounds() const {
83
11.7M
    return range_bounds_.get();
84
11.7M
  }
85
86
11.7M
  const std::shared_ptr<std::vector<std::vector<PrimitiveValue>>>& range_options() const {
87
11.7M
    return range_options_;
88
11.7M
  }
89
90
13.2k
  const std::vector<ColumnId> range_options_indexes() const {
91
13.2k
    return range_options_indexes_;
92
13.2k
  }
93
94
13.2k
  const std::vector<ColumnId> range_bounds_indexes() const {
95
13.2k
    return range_bounds_indexes_;
96
13.2k
  }
97
98
 private:
99
  static const DocKey& DefaultStartDocKey();
100
101
  // Return inclusive lower/upper range doc key considering the start_doc_key.
102
  Result<KeyBytes> Bound(const bool lower_bound) const;
103
104
  // Returns the lower/upper doc key based on the range components.
105
  KeyBytes bound_key(const Schema& schema, const bool lower_bound) const;
106
107
  // The scan range within the hash key when a WHERE condition is specified.
108
  const std::unique_ptr<const QLScanRange> range_bounds_;
109
110
  // Indexes of columns that have range bounds such as c2 < 4 AND c2 >= 1
111
  std::vector<ColumnId> range_bounds_indexes_;
112
113
  // Initialize range_options_ if hashed_components_ in set and all range columns have one or more
114
  // options (i.e. using EQ/IN conditions). Otherwise range_options_ will stay null and we will
115
  // only use the range_bounds for scanning.
116
  void InitRangeOptions(const PgsqlConditionPB& condition);
117
118
  // The range value options if set. (possibly more than one due to IN conditions).
119
  std::shared_ptr<std::vector<std::vector<PrimitiveValue>>> range_options_;
120
121
  // Indexes of columns that have range option filters such as
122
  // c2 IN (1, 5, 6, 9)
123
  std::vector<ColumnId> range_options_indexes_;
124
125
  // Schema of the columns to scan.
126
  const Schema& schema_;
127
128
  // Query ID of this scan.
129
  const rocksdb::QueryId query_id_;
130
131
  // The hashed_components are owned by the caller of QLScanSpec.
132
  const std::vector<PrimitiveValue> *hashed_components_;
133
  // The range_components are owned by the caller of QLScanSpec.
134
  const std::vector<PrimitiveValue> *range_components_;
135
136
  // Hash code is used if hashed_components_ vector is empty.
137
  // hash values are positive int16_t.
138
  const boost::optional<int32_t> hash_code_;
139
140
  // Max hash code is used if hashed_components_ vector is empty.
141
  // hash values are positive int16_t.
142
  const boost::optional<int32_t> max_hash_code_;
143
144
  // Starting doc key when requested by the client.
145
  const KeyBytes start_doc_key_;
146
147
  // Lower and upper keys for range condition.
148
  KeyBytes lower_doc_key_;
149
  KeyBytes upper_doc_key_;
150
151
  // Scan behavior.
152
  bool is_forward_scan_;
153
154
  DISALLOW_COPY_AND_ASSIGN(DocPgsqlScanSpec);
155
};
156
157
}  // namespace docdb
158
}  // namespace yb
159
160
#endif // YB_DOCDB_DOC_PGSQL_SCANSPEC_H