YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
59
  //------------------------------------------------------------------------------------------------
60
  // Access funtions.
61
4.32M
  const rocksdb::QueryId QueryId() const {
62
4.32M
    return query_id_;
63
4.32M
  }
64
65
4.33M
  bool is_forward_scan() const {
66
4.33M
    return is_forward_scan_;
67
4.33M
  }
68
69
  //------------------------------------------------------------------------------------------------
70
  // Filters.
71
  std::shared_ptr<rocksdb::ReadFileFilter> CreateFileFilter() const;
72
73
  // Return the inclusive lower and upper bounds of the scan.
74
  Result<KeyBytes> LowerBound() const;
75
  Result<KeyBytes> UpperBound() const;
76
77
  // Returns the lower/upper range components of the key.
78
  std::vector<PrimitiveValue> range_components(const bool lower_bound) const;
79
80
4.33M
  const QLScanRange* range_bounds() const {
81
4.33M
    return range_bounds_.get();
82
4.33M
  }
83
84
4.33M
  const std::shared_ptr<std::vector<std::vector<PrimitiveValue>>>& range_options() const {
85
4.33M
    return range_options_;
86
4.33M
  }
87
88
4.72k
  const std::vector<ColumnId> range_options_indexes() const {
89
4.72k
    return range_options_indexes_;
90
4.72k
  }
91
92
4.72k
  const std::vector<ColumnId> range_bounds_indexes() const {
93
4.72k
    return range_bounds_indexes_;
94
4.72k
  }
95
96
 private:
97
  static const DocKey& DefaultStartDocKey();
98
99
  // Return inclusive lower/upper range doc key considering the start_doc_key.
100
  Result<KeyBytes> Bound(const bool lower_bound) const;
101
102
  // Returns the lower/upper doc key based on the range components.
103
  KeyBytes bound_key(const Schema& schema, const bool lower_bound) const;
104
105
  // The scan range within the hash key when a WHERE condition is specified.
106
  const std::unique_ptr<const QLScanRange> range_bounds_;
107
108
  // Indexes of columns that have range bounds such as c2 < 4 AND c2 >= 1
109
  std::vector<ColumnId> range_bounds_indexes_;
110
111
  // Initialize range_options_ if hashed_components_ in set and all range columns have one or more
112
  // options (i.e. using EQ/IN conditions). Otherwise range_options_ will stay null and we will
113
  // only use the range_bounds for scanning.
114
  void InitRangeOptions(const PgsqlConditionPB& condition);
115
116
  // The range value options if set. (possibly more than one due to IN conditions).
117
  std::shared_ptr<std::vector<std::vector<PrimitiveValue>>> range_options_;
118
119
  // Indexes of columns that have range option filters such as
120
  // c2 IN (1, 5, 6, 9)
121
  std::vector<ColumnId> range_options_indexes_;
122
123
  // Schema of the columns to scan.
124
  const Schema& schema_;
125
126
  // Query ID of this scan.
127
  const rocksdb::QueryId query_id_;
128
129
  // The hashed_components are owned by the caller of QLScanSpec.
130
  const std::vector<PrimitiveValue> *hashed_components_;
131
  // The range_components are owned by the caller of QLScanSpec.
132
  const std::vector<PrimitiveValue> *range_components_;
133
134
  // Hash code is used if hashed_components_ vector is empty.
135
  // hash values are positive int16_t.
136
  const boost::optional<int32_t> hash_code_;
137
138
  // Max hash code is used if hashed_components_ vector is empty.
139
  // hash values are positive int16_t.
140
  const boost::optional<int32_t> max_hash_code_;
141
142
  // Starting doc key when requested by the client.
143
  const KeyBytes start_doc_key_;
144
145
  // Lower and upper keys for range condition.
146
  KeyBytes lower_doc_key_;
147
  KeyBytes upper_doc_key_;
148
149
  // Scan behavior.
150
  bool is_forward_scan_;
151
152
  DISALLOW_COPY_AND_ASSIGN(DocPgsqlScanSpec);
153
};
154
155
}  // namespace docdb
156
}  // namespace yb
157
158
#endif // YB_DOCDB_DOC_PGSQL_SCANSPEC_H