YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/index.h
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
// Classes that implement secondary index.
15
//--------------------------------------------------------------------------------------------------
16
17
#ifndef YB_COMMON_INDEX_H_
18
#define YB_COMMON_INDEX_H_
19
20
#include <memory>
21
#include <set>
22
#include <string>
23
#include <unordered_map>
24
#include <unordered_set>
25
#include <utility>
26
#include <vector>
27
28
#include <boost/functional/hash.hpp>
29
#include <boost/optional.hpp>
30
31
#include "yb/common/common_fwd.h"
32
#include "yb/common/column_id.h"
33
#include "yb/common/common_types.pb.h"
34
#include "yb/common/entity_ids_types.h"
35
36
namespace yb {
37
38
// A class to maintain the information of an index.
39
class IndexInfo {
40
 public:
41
  explicit IndexInfo(const IndexInfoPB& pb);
42
  IndexInfo(const IndexInfo& rhs);
43
  IndexInfo(IndexInfo&& rhs);
44
  IndexInfo();
45
  ~IndexInfo();
46
47
  void ToPB(IndexInfoPB* pb) const;
48
49
87.2k
  const TableId& table_id() const { return table_id_; }
50
4
  const TableId& indexed_table_id() const { return indexed_table_id_; }
51
0
  uint32_t schema_version() const { return schema_version_; }
52
16.9k
  bool is_local() const { return is_local_; }
53
57.5k
  bool is_unique() const { return is_unique_; }
54
55
190k
  const std::vector<IndexColumn>& columns() const { return columns_; }
56
  const IndexColumn& column(const size_t idx) const;
57
184k
  size_t hash_column_count() const { return hash_column_count_; }
58
29.8k
  size_t range_column_count() const { return range_column_count_; }
59
539k
  size_t key_column_count() const { return hash_column_count_ + range_column_count_; }
60
61
0
  const std::vector<ColumnId>& indexed_hash_column_ids() const {
62
0
    return indexed_hash_column_ids_;
63
0
  }
64
0
  const std::vector<ColumnId>& indexed_range_column_ids() const {
65
0
    return indexed_range_column_ids_;
66
0
  }
67
1.70k
  IndexPermissions index_permissions() const { return index_permissions_; }
68
69
263k
  std::shared_ptr<const IndexInfoPB_WherePredicateSpecPB>& where_predicate_spec() const {
70
263k
    return where_predicate_spec_;
71
263k
  }
72
73
  // Return column ids that are primary key columns of the indexed table.
74
  std::vector<ColumnId> index_key_column_ids() const;
75
76
  // Index primary key columns of the indexed table only?
77
  bool PrimaryKeyColumnsOnly(const Schema& indexed_schema) const;
78
79
  // Is column covered by this index? (Note: indexed columns are always covered)
80
  bool IsColumnCovered(ColumnId column_id) const;
81
  bool IsColumnCovered(const std::string& column_name) const;
82
83
  // Check if this INDEX contain the column being referenced by the given selected expression.
84
  // - If found, return the location of the column (columns_[loc]).
85
  // - Otherwise, return -1.
86
  int32_t IsExprCovered(const std::string& expr_content) const;
87
88
  // Are read operations allowed to use the index?  During CREATE INDEX, reads are not allowed until
89
  // the index backfill is successfully completed.
90
17.0k
  bool HasReadPermission() const { return index_permissions_ == INDEX_PERM_READ_WRITE_AND_DELETE; }
91
92
  // Should write operations to the index update the index table?  This includes INSERT and UPDATE.
93
49.7k
  bool HasWritePermission() const {
94
49.7k
    return index_permissions_ >= INDEX_PERM_WRITE_AND_DELETE &&
95
48.9k
           index_permissions_ <= INDEX_PERM_WRITE_AND_DELETE_WHILE_REMOVING;
96
49.7k
  }
97
98
  // Should delete operations to the index update the index table?  This includes DELETE and UPDATE.
99
2.65k
  bool HasDeletePermission() const {
100
2.65k
    return index_permissions_ >= INDEX_PERM_DELETE_ONLY &&
101
2.65k
           index_permissions_ <= INDEX_PERM_DELETE_ONLY_WHILE_REMOVING;
102
2.65k
  }
103
104
  // Is the index being backfilled?
105
0
  bool IsBackfilling() const { return index_permissions_ == INDEX_PERM_DO_BACKFILL; }
106
107
674
  const std::string& backfill_error_message() const {
108
674
    return backfill_error_message_;
109
674
  }
110
111
  std::string ToString() const;
112
113
  // Same as "IsExprCovered" but only search the key columns.
114
  boost::optional<size_t> FindKeyIndex(const std::string& key_name) const;
115
116
30.9k
  bool use_mangled_column_name() const {
117
30.9k
    return use_mangled_column_name_;
118
30.9k
  }
119
120
8.70k
  bool has_index_by_expr() const {
121
8.70k
    return has_index_by_expr_;
122
8.70k
  }
123
124
  // Check if this index is dependent on the given column.
125
  bool CheckColumnDependency(ColumnId column_id) const;
126
127
 private:
128
  const TableId table_id_;            // Index table id.
129
  const TableId indexed_table_id_;    // Indexed table id.
130
  const uint32_t schema_version_ = 0; // Index table's schema version.
131
  const bool is_local_ = false;       // Whether this is a local index.
132
  const bool is_unique_ = false;      // Whether this is a unique index.
133
  const std::vector<IndexColumn> columns_; // Index columns.
134
  const size_t hash_column_count_ = 0;     // Number of hash columns in the index.
135
  const size_t range_column_count_ = 0;    // Number of range columns in the index.
136
  const std::vector<ColumnId> indexed_hash_column_ids_;  // Hash column ids in the indexed table.
137
  const std::vector<ColumnId> indexed_range_column_ids_; // Range column ids in the indexed table.
138
  const IndexPermissions index_permissions_ = INDEX_PERM_READ_WRITE_AND_DELETE;
139
  const std::string backfill_error_message_;
140
141
  // Column ids covered by the index (include indexed columns).
142
  std::unordered_set<ColumnId, boost::hash<ColumnIdRep>> covered_column_ids_;
143
144
  // Newer INDEX use mangled column name instead of ID.
145
  bool use_mangled_column_name_ = false;
146
  bool has_index_by_expr_ = false;
147
148
  mutable std::shared_ptr<const IndexInfoPB_WherePredicateSpecPB> where_predicate_spec_ = nullptr;
149
};
150
151
// A map to look up an index by its index table id.
152
class IndexMap : public std::unordered_map<TableId, IndexInfo> {
153
 public:
154
  explicit IndexMap(const google::protobuf::RepeatedPtrField<IndexInfoPB>& indexes);
155
1.28M
  IndexMap() {}
156
157
  void FromPB(const google::protobuf::RepeatedPtrField<IndexInfoPB>& indexes);
158
  void ToPB(google::protobuf::RepeatedPtrField<IndexInfoPB>* indexes) const;
159
160
  Result<const IndexInfo*> FindIndex(const TableId& index_id) const;
161
};
162
163
}  // namespace yb
164
165
#endif  // YB_COMMON_INDEX_H_