-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIndex.cc
More file actions
572 lines (516 loc) · 16.1 KB
/
Index.cc
File metadata and controls
572 lines (516 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* Copyright (c) [2025-2026] [Zhao Song]
*/
#include "Index.h"
#include "Table.h"
#include "JSONHelpers.h"
#include <cassert>
#include <cstring>
#include <iostream>
namespace ibd_ninja {
/* ------ Index ------ */
const std::set<std::string> Index::default_valid_option_keys = {
"block_size",
"flags",
"parser_name",
"gipk" /* generated implicit primary key */
};
bool Index::Init(const rapidjson::Value& dd_index_obj,
const std::vector<Column*>& columns) {
Read(&dd_name_, dd_index_obj, "name");
Read(&dd_hidden_, dd_index_obj, "hidden");
Read(&dd_is_generated_, dd_index_obj, "is_generated");
Read(&dd_ordinal_position_, dd_index_obj, "ordinal_position");
Read(&dd_comment_, dd_index_obj, "comment");
ReadProperties(&dd_options_, dd_index_obj, "options");
ReadProperties(&dd_se_private_data_, dd_index_obj, "se_private_data");
ReadEnum(&dd_type_, dd_index_obj, "type");
ReadEnum(&dd_algorithm_, dd_index_obj, "algorithm");
Read(&dd_is_algorithm_explicit_, dd_index_obj, "is_algorithm_explicit");
Read(&dd_is_visible_, dd_index_obj, "is_visible");
Read(&dd_engine_, dd_index_obj, "engine");
Read(&dd_engine_attribute_, dd_index_obj, "engine_attribute");
Read(&dd_secondary_engine_attribute_, dd_index_obj,
"secondary_engine_attribute");
if (!dd_index_obj.HasMember("elements") ||
!dd_index_obj["elements"].IsArray()) {
std::cerr << "[SDI]Can't find index elements" << std::endl;
return false;
}
const rapidjson::Value& elements = dd_index_obj["elements"].GetArray();
for (rapidjson::SizeType i = 0; i < elements.Size(); i++) {
if (!elements[i].IsObject()) {
std::cerr << "[SDI]Index element isn't an object" << std::endl;
return false;
}
IndexColumn* element = IndexColumn::CreateIndexColumn(elements[i], columns);
if (element == nullptr) {
return false;
}
dd_elements_.push_back(element);
}
Read(&dd_tablespace_ref_, dd_index_obj, "tablespace_ref");
return true;
}
Index* Index::CreateIndex(const rapidjson::Value& dd_index_obj,
const std::vector<Column*>& columns,
Table* table) {
Index* index = new Index(table);
bool init_ret = index->Init(dd_index_obj, columns);
if (!init_ret) {
delete index;
index = nullptr;
}
return index;
}
#define HA_NOSAME 1
#define HA_FULLTEXT (1 << 7)
#define HA_SPATIAL (1 << 10)
bool Index::FillIndex(uint32_t ind) {
s_user_defined_key_parts_ = 0;
s_key_length_ = 0;
s_flags_ = 0;
for (auto* iter : dd_elements_) {
if (iter->hidden()) {
continue;
}
s_user_defined_key_parts_++;
s_key_length_ += iter->length();
}
switch (type()) {
case Index::IT_MULTIPLE:
s_flags_ = 0;
break;
case Index::IT_FULLTEXT:
s_flags_ = HA_FULLTEXT;
break;
case Index::IT_SPATIAL:
s_flags_ = HA_SPATIAL;
break;
case Index::IT_PRIMARY:
case Index::IT_UNIQUE:
s_flags_ = HA_NOSAME;
break;
default:
assert(0);
s_flags_ = 0;
}
FillSeIndex(ind);
return true;
}
#define FTS_DOC_ID_COL_NAME "FTS_DOC_ID"
bool Index::IndexAddCol(Column* col, uint32_t prefix_len) {
if (col->index_column() != nullptr) {
ib_fields_.push_back(col->index_column());
} else if (col->name() == FTS_DOC_ID_COL_NAME) {
/*
* The FTS_DOC_ID column is not defined in the SDI's PRIMARY index columns,
* so we need to create it manually.
*/
IndexColumn* index_column = IndexColumn::CreateIndexFTSDocIdColumn(col);
ib_fields_.push_back(index_column);
} else {
assert(col->IsInstantDropped());
IndexColumn* index_column = IndexColumn::CreateIndexDroppedColumn(col);
ib_fields_.push_back(index_column);
}
ib_n_def_++;
if (ib_type_ & HA_SPATIAL &&
(col->ib_mtype() == DATA_POINT ||
col->ib_mtype() == DATA_VAR_POINT) &&
ib_n_def_ == 1) {
col->index_column()->set_ib_fixed_len(DATA_MBR_LEN);
} else {
col->index_column()->set_ib_fixed_len(col->GetFixedSize());
}
if (prefix_len && col->index_column()->ib_fixed_len() > prefix_len) {
col->index_column()->set_ib_fixed_len(prefix_len);
}
if (col->index_column()->ib_fixed_len() > DICT_MAX_FIXED_COL_LEN) {
col->index_column()->set_ib_fixed_len(0);
}
if (col->is_nullable() &&
!col->IsInstantDropped()) {
ib_n_nullable_++;
}
return true;
}
uint32_t Index::GetNOriginalFields() {
assert(table_->HasInstantCols());
uint32_t n_inst_cols_v1 = table_->GetNInstantAddedColV1();
uint32_t n_drop_cols = table_->GetNInstantDropCols();
uint32_t n_add_cols = table_->GetNInstantAddCols();
uint32_t n_instant_fields =
ib_n_fields_ + n_drop_cols - n_add_cols - n_inst_cols_v1;
return n_instant_fields;
}
uint32_t Index::GetNNullableBefore(uint32_t nth) {
uint32_t nullable = 0;
for (uint32_t i = 0; i < nth; i++) {
const IndexColumn* index_col = ib_fields_[i];
assert(!index_col->column()->IsInstantDropped());
if (index_col->column()->is_nullable()) {
nullable++;
}
}
return nullable;
}
uint32_t Index::CalculateNInstantNullable(uint32_t n_fields) {
if (!table_->HasRowVersions()) {
return GetNNullableBefore(n_fields);
}
uint32_t n_drop_nullable_cols = 0;
uint32_t new_n_nullable = 0;
for (uint32_t i = 0; i < ib_n_def_; i++) {
const IndexColumn* index_col = ib_fields_[i];
if (index_col->column()->IsInstantAdded()) {
continue;
}
if (index_col->column()->IsInstantDropped()) {
if (index_col->column()->ib_phy_pos() < n_fields &&
index_col->column()->is_nullable()) {
n_drop_nullable_cols++;
}
continue;
}
if (index_col->column()->ib_phy_pos() < n_fields) {
if (index_col->column()->is_nullable()) {
new_n_nullable++;
}
}
}
new_n_nullable += n_drop_nullable_cols;
return new_n_nullable;
}
bool Index::HasInstantColsOrRowVersions() {
if (!IsClustered()) {
return false;
}
return (ib_row_versions_ || ib_instant_cols_);
}
uint32_t Index::GetNullableInVersion(uint8_t version) {
return ib_nullables_[version];
}
uint16_t Index::GetNullableBeforeInstantAddDrop() {
if (ib_instant_cols_) {
return ib_n_instant_nullable_;
}
if (ib_row_versions_) {
return GetNullableInVersion(0);
}
return ib_n_nullable_;
}
uint16_t Index::GetNUniqueInTree() {
if (IsClustered()) {
return static_cast<uint16_t>(ib_n_uniq_);
} else {
return static_cast<uint16_t>(GetNFields());
}
}
uint16_t Index::GetNUniqueInTreeNonleaf() {
if (ib_type_ & DICT_SPATIAL) {
// TODO(zhao): support spatial Index
assert(0);
return DICT_INDEX_SPATIAL_NODEPTR_SIZE;
} else {
return static_cast<uint16_t>(GetNUniqueInTree());
}
}
IndexColumn* Index::GetPhysicalField(size_t pos) {
if (ib_row_versions_) {
return ib_fields_[ib_fields_array_[pos]];
}
return ib_fields_[pos];
}
uint32_t Index::GetNFields() const {
if (table_->HasRowVersions()) {
return ib_n_total_fields_;
}
return ib_n_fields_;
}
#define UNSUPP_INDEX_MASK 0x7
#define UNSUPP_INDEX_MASK_VIRTUAL 0x1
#define UNSUPP_INDEX_MASK_FTS 0x2
#define UNSUPP_INDEX_MASK_SPATIAL 0x4
void Index::PreCheck() {
if (dd_type_ == IT_FULLTEXT) {
unsupported_reason_ |= UNSUPP_INDEX_MASK_FTS;
}
if (dd_type_ == IT_SPATIAL) {
unsupported_reason_ |= UNSUPP_INDEX_MASK_SPATIAL;
}
for (auto* iter : dd_elements_) {
if (iter->hidden()) {
continue;
}
if (iter->column()->is_virtual()) {
unsupported_reason_ |= UNSUPP_INDEX_MASK_VIRTUAL;
break;
}
}
}
bool Index::IsIndexSupported() {
return (unsupported_reason_ & UNSUPP_INDEX_MASK) == 0;
}
std::string Index::UnsupportedReason() {
assert(!IsIndexSupported());
std::string reason = "";
if (unsupported_reason_ & UNSUPP_INDEX_MASK_VIRTUAL) {
reason += "[Index using virtual columns as keys]";
}
if (unsupported_reason_ & UNSUPP_INDEX_MASK_FTS) {
reason += "[Fulltext index]";
}
if (unsupported_reason_ & UNSUPP_INDEX_MASK_SPATIAL) {
reason += "[Spatial index]";
}
return reason;
}
bool Index::IsIndexParsingRecSupported() {
if (!table_->IsTableParsingRecSupported()) {
return false;
}
return IsIndexSupported();
// Additional checks may be needed here.
}
#define FTS_DOC_ID_INDEX_NAME "FTS_DOC_ID_INDEX"
bool Index::FillSeIndex(uint32_t ind) {
PreCheck();
if (!IsIndexSupported()) {
return true;
}
ib_n_fields_ = s_user_defined_key_parts_;
ib_n_uniq_ = ib_n_fields_;
if (s_flags_ & HA_SPATIAL) {
ib_type_ = DICT_SPATIAL;
assert(ib_n_fields_ == 1);
} else if (s_flags_ & HA_FULLTEXT) {
ib_type_ = DICT_FTS;
ib_n_uniq_ = 0;
} else if (ind == 0) {
assert(s_flags_ & HA_NOSAME);
// dd_hidden_ == true means there no explicit primary index
assert(ib_n_uniq_ > 0 || dd_hidden_ == true);
if (dd_hidden_ == false) {
ib_type_ = DICT_CLUSTERED | DICT_UNIQUE;
} else {
/*
* The implicit primary index type is DICT_CLUSTERED.
* This is made consistent with InnoDB.
*/
ib_type_ = DICT_CLUSTERED;
}
} else {
ib_type_ = (s_flags_ & HA_NOSAME) ? DICT_UNIQUE : 0;
}
ib_n_def_ = 0;
ib_n_nullable_ = 0;
ib_fields_.clear();
for (auto* iter : dd_elements_) {
if (iter->hidden()) {
continue;
}
uint32_t prefix_len = 0;
IndexAddCol(iter->column(), prefix_len);
}
/*
* Special case: "FTS_DOC_ID_INDEX"
* The columns of FTS_DOC_ID_INDEX defined in the SDI are correct,
* but the information for the FTS_DOC_ID column is
* missing (e.g., ib_mtype_).
* Note that we have created a new FTS_DOC_ID in Table::ib_cols_,
* so use that instead.
*/
if (dd_name_ == FTS_DOC_ID_INDEX_NAME) {
for (auto* iter : dd_elements_) {
if (iter->column()->name() == FTS_DOC_ID_COL_NAME) {
for (auto* col : *table()->ib_cols()) {
if (col->name() == FTS_DOC_ID_COL_NAME) {
iter->set_column(col);
}
}
}
/*
* The user has explicitly defined the FTS_DOC_ID column,
* so no need to add another.
*/
if (iter->hidden()) {
IndexAddCol(iter->column(), 0);
}
}
}
if (IsClustered()) {
ib_n_user_defined_cols_ = s_user_defined_key_parts_;
if (!IsIndexUnique()) {
ib_n_uniq_ += 1;
}
uint32_t n_fields_processed = 0;
while (n_fields_processed < ib_n_def_) {
IndexColumn* col = ib_fields_[n_fields_processed];
if (!table()->HasRowVersions()) {
col->column()->set_ib_phy_pos(n_fields_processed);
} else {
assert(col->column()->ib_phy_pos() != UINT32_UNDEFINED);
}
n_fields_processed++;
}
bool found_db_row_id = false;
bool found_db_trx_id = false;
bool found_db_roll_ptr = false;
for (auto* col : *(table()->ib_cols())) {
if (!IsIndexUnique() && col->name() == "DB_ROW_ID") {
found_db_row_id = true;
if (!table()->HasRowVersions()) {
col->set_ib_phy_pos(n_fields_processed);
} else {
assert(col->ib_phy_pos() != UINT32_UNDEFINED);
}
IndexAddCol(col, 0);
n_fields_processed++;
ib_n_fields_++;
}
if (col->name() == "DB_TRX_ID") {
found_db_trx_id = true;
if (!table()->HasRowVersions()) {
col->set_ib_phy_pos(n_fields_processed);
} else {
assert(col->ib_phy_pos() != UINT32_UNDEFINED);
}
IndexAddCol(col, 0);
n_fields_processed++;
ib_n_fields_++;
}
if (col->name() == "DB_ROLL_PTR") {
found_db_roll_ptr = true;
if (!table()->HasRowVersions()) {
col->set_ib_phy_pos(n_fields_processed);
} else {
assert(col->ib_phy_pos() != UINT32_UNDEFINED);
}
IndexAddCol(col, 0);
n_fields_processed++;
ib_n_fields_++;
}
}
assert((IsIndexUnique() || found_db_row_id) &&
found_db_trx_id && found_db_roll_ptr);
std::vector<bool> indexed(table()->GetTotalCols(), false);
for (auto* iter : ib_fields_) {
indexed[iter->column()->ib_ind()] = true;
}
for (uint32_t i = 0; i < table()->ib_n_cols() - DATA_N_SYS_COLS; i++) {
Column* col = table()->ib_cols()->at(i);
assert(col->ib_mtype() != DATA_SYS);
if (indexed[col->ib_ind()]) {
continue;
}
if (!table()->HasRowVersions()) {
col->set_ib_phy_pos(n_fields_processed);
} else {
assert(col->ib_phy_pos() != UINT32_UNDEFINED);
}
IndexAddCol(col, 0);
n_fields_processed++;
ib_n_fields_++;
}
for (uint32_t i = table()->ib_n_cols(); i < table()->GetTotalCols(); i++) {
Column* col = table()->ib_cols()->at(i);
assert(col->ib_mtype() != DATA_SYS);
IndexAddCol(col, 0);
n_fields_processed++;
// Do not add ib_n_fields_ here, as we are adding dropped columns.
}
if (!table()->ib_is_system_table()) {
ib_fields_array_.clear();
memset(ib_nullables_, 0,
(MAX_ROW_VERSION + 1) * sizeof(ib_nullables_[0]));
if (table()->HasRowVersions()) {
ib_fields_array_.resize(ib_n_def_);
for (uint32_t i = 0; i < ib_n_def_; i++) {
IndexColumn *index_col = ib_fields_[i];
assert(index_col != nullptr && index_col->column() != nullptr);
size_t pos = index_col->column()->ib_phy_pos();
ib_fields_array_[pos] = i;
}
uint32_t current_row_version = table()->ib_current_row_version();
auto update_nullable = [&](size_t start_version, bool is_increment) {
for (size_t i = start_version; i <= current_row_version; i++) {
assert(is_increment || ib_nullables_[i] > 0);
if (is_increment) {
++ib_nullables_[i];
} else {
--ib_nullables_[i];
}
}
};
for (uint32_t i = 0; i < ib_n_def_; i++) {
IndexColumn *index_col = ib_fields_[i];
if (index_col->column()->name() == "DB_ROW_ID" ||
index_col->column()->name() == "DB_TRX_ID" ||
index_col->column()->name() == "DB_ROLL_PTR") {
continue;
}
if (!index_col->column()->is_nullable()) {
continue;
}
size_t start_from = 0;
if (index_col->column()->IsInstantAdded()) {
start_from = index_col->column()->GetVersionAdded();
}
update_nullable(start_from, true);
if (index_col->column()->IsInstantDropped()) {
update_nullable(index_col->column()->GetVersionDropped(), false);
}
}
}
}
assert(table_->clust_index() == nullptr);
table_->set_clust_index(this);
} else {
ib_n_user_defined_cols_ = s_user_defined_key_parts_;
std::vector<bool> indexed(table()->GetTotalCols(), false);
for (auto* iter : ib_fields_) {
if (iter->column()->is_virtual()) {
continue;
}
indexed[iter->column()->ib_ind()] = true;
}
assert(table_->clust_index() != nullptr);
for (uint32_t i = 0; i < table_->clust_index()->ib_n_uniq(); i++) {
IndexColumn* clust_index_col = table_->clust_index()->ib_fields()->at(i);
if (!indexed[clust_index_col->column()->ib_ind()]) {
uint32_t prefix_len = 0;
IndexAddCol(clust_index_col->column(), prefix_len);
} else {
// TODO(Zhao): Support spatial index
}
}
if (IsIndexUnique()) {
ib_n_uniq_ = ib_n_fields_;
} else {
ib_n_uniq_ = ib_n_def_;
}
ib_n_fields_ = ib_n_def_;
}
dd_se_private_data_.Get("id", &ib_id_);
dd_se_private_data_.Get("root", &ib_page_);
ib_n_fields_ = ib_n_def_;
if (IsClustered() && table_->HasRowVersions()) {
ib_n_fields_ = ib_n_def_ - table_->GetNInstantDropCols();
}
ib_n_total_fields_ = ib_n_def_;
ib_row_versions_ = false;
ib_instant_cols_ = false;
ib_n_instant_nullable_ = ib_n_nullable_;
if (IsClustered()) {
ib_row_versions_ = table_->HasRowVersions();
if (table_->HasInstantCols()) {
ib_instant_cols_ = true;
uint32_t n_instant_fields = GetNOriginalFields();
uint32_t new_n_nullable = CalculateNInstantNullable(n_instant_fields);
ib_n_instant_nullable_ = new_n_nullable;
}
}
return true;
}
} // namespace ibd_ninja