Skip to content

Commit bc60f81

Browse files
kungaadameat
authored andcommitted
KIKIMR-19522 BTreeIndex Precharge RowId (ydb-platform#754)
1 parent 5e35d35 commit bc60f81

15 files changed

+410
-146
lines changed

ydb/core/tablet_flat/benchmark/b_part_index.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace {
7070

7171
Cerr << "DataBytes = " << part->Stat.Bytes << " DataPages = " << IndexTools::CountMainPages(*part) << Endl;
7272
Cerr << "FlatIndexBytes = " << part->GetPageSize(part->IndexPages.Groups[groups ? 1 : 0], {}) << " BTreeIndexBytes = " << part->IndexPages.BTreeGroups[groups ? 1 : 0].IndexSize << Endl;
73-
Cerr << "Levels = " << part->IndexPages.BTreeGroups[groups ? 1 : 0].LevelsCount << Endl;
73+
Cerr << "Levels = " << part->IndexPages.BTreeGroups[groups ? 1 : 0].LevelCount << Endl;
7474

7575
// 100 MB
7676
UNIT_ASSERT_GE(part->Stat.Bytes, 100ull*1024*1024);

ydb/core/tablet_flat/flat_page_btree_index.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace NKikimr::NTable::NPage {
8585

8686
struct TShortChild {
8787
TPageId PageId;
88-
TRowId Count;
88+
TRowId RowCount;
8989
ui64 DataSize;
9090

9191
auto operator<=>(const TShortChild&) const = default;
@@ -95,22 +95,22 @@ namespace NKikimr::NTable::NPage {
9595

9696
struct TChild {
9797
TPageId PageId;
98-
TRowId Count;
98+
TRowId RowCount;
9999
ui64 DataSize;
100-
TRowId ErasedCount;
100+
TRowId ErasedRowCount;
101101

102102
auto operator<=>(const TChild&) const = default;
103103

104104
TString ToString() const noexcept
105105
{
106-
return TStringBuilder() << "PageId: " << PageId << " Count: " << Count << " DataSize: " << DataSize << " Erased: " << ErasedCount;
106+
return TStringBuilder() << "PageId: " << PageId << " RowCount: " << RowCount << " DataSize: " << DataSize << " ErasedRowCount: " << ErasedRowCount;
107107
}
108108
} Y_PACKED;
109109

110110
static_assert(sizeof(TChild) == 28, "Invalid TBtreeIndexNode TChild size");
111111

112112
static_assert(offsetof(TChild, PageId) == offsetof(TShortChild, PageId));
113-
static_assert(offsetof(TChild, Count) == offsetof(TShortChild, Count));
113+
static_assert(offsetof(TChild, RowCount) == offsetof(TShortChild, RowCount));
114114
static_assert(offsetof(TChild, DataSize) == offsetof(TShortChild, DataSize));
115115

116116
#pragma pack(pop)
@@ -306,7 +306,7 @@ namespace NKikimr::NTable::NPage {
306306
{
307307
if (Header->IsShortChildFormat) {
308308
const TShortChild* const shortChild = TDeref<const TShortChild>::At(Children, pos * sizeof(TShortChild));
309-
return { shortChild->PageId, shortChild->Count, shortChild->DataSize, 0 };
309+
return { shortChild->PageId, shortChild->RowCount, shortChild->DataSize, 0 };
310310
} else {
311311
return *TDeref<const TChild>::At(Children, pos * sizeof(TChild));
312312
}
@@ -316,7 +316,7 @@ namespace NKikimr::NTable::NPage {
316316
return beginRowId <= rowId && rowId < endRowId;
317317
}
318318

319-
TRecIdx Seek(TRowId rowId, std::optional<TRecIdx> on) const noexcept
319+
TRecIdx Seek(TRowId rowId, std::optional<TRecIdx> on = { }) const noexcept
320320
{
321321
const TRecIdx childrenCount = GetChildrenCount();
322322
if (on && on >= childrenCount) {
@@ -326,19 +326,19 @@ namespace NKikimr::NTable::NPage {
326326

327327
auto range = xrange(0u, childrenCount);
328328
const auto cmp = [this](TRowId rowId, TPos pos) {
329-
return rowId < GetShortChild(pos).Count;
329+
return rowId < GetShortChild(pos).RowCount;
330330
};
331331

332332
TRecIdx result;
333333
if (!on) {
334334
// Will do a full binary search on full range
335-
} else if (GetShortChild(*on).Count <= rowId) {
335+
} else if (GetShortChild(*on).RowCount <= rowId) {
336336
// Try a short linear search first
337337
result = *on;
338338
for (int linear = 0; linear < 4; ++linear) {
339339
result++;
340340
Y_ABORT_UNLESS(result < childrenCount, "Should always seek some child");
341-
if (GetShortChild(result).Count > rowId) {
341+
if (GetShortChild(result).RowCount > rowId) {
342342
return result;
343343
}
344344
}
@@ -352,7 +352,7 @@ namespace NKikimr::NTable::NPage {
352352
if (result == 0) {
353353
return 0;
354354
}
355-
if (GetShortChild(result - 1).Count <= rowId) {
355+
if (GetShortChild(result - 1).RowCount <= rowId) {
356356
return result;
357357
}
358358
result--;
@@ -464,14 +464,14 @@ namespace NKikimr::NTable::NPage {
464464
};
465465

466466
struct TBtreeIndexMeta : public TBtreeIndexNode::TChild {
467-
size_t LevelsCount;
467+
ui32 LevelCount;
468468
ui64 IndexSize;
469469

470470
auto operator<=>(const TBtreeIndexMeta&) const = default;
471471

472472
TString ToString() const noexcept
473473
{
474-
return TStringBuilder() << TBtreeIndexNode::TChild::ToString() << " LevelsCount: " << LevelsCount << " IndexSize: " << IndexSize;
474+
return TStringBuilder() << TBtreeIndexNode::TChild::ToString() << " LevelCount: " << LevelCount << " IndexSize: " << IndexSize;
475475
}
476476
};
477477
}

ydb/core/tablet_flat/flat_page_btree_index_writer.h

+16-15
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace NKikimr::NTable::NPage {
5050
}
5151

5252
void AddChild(TChild child) {
53-
Y_ABORT_UNLESS(child.ErasedCount == 0 || !IsShortChildFormat(), "Short format can't have ErasedCount");
53+
Y_ABORT_UNLESS(child.ErasedRowCount == 0 || !IsShortChildFormat(), "Short format can't have ErasedRowCount");
5454
Children.push_back(child);
5555
}
5656

@@ -249,7 +249,7 @@ namespace NKikimr::NTable::NPage {
249249
void PlaceChild(const TChild& child) noexcept
250250
{
251251
if (IsShortChildFormat()) {
252-
Place<TShortChild>() = TShortChild{child.PageId, child.Count, child.DataSize};
252+
Place<TShortChild>() = TShortChild{child.PageId, child.RowCount, child.DataSize};
253253
} else {
254254
Place<TChild>() = child;
255255
}
@@ -375,20 +375,21 @@ namespace NKikimr::NTable::NPage {
375375
}
376376

377377
void AddShortChild(TShortChild child) {
378-
AddChild(TChild{child.PageId, child.Count, child.DataSize, 0});
378+
AddChild(TChild{child.PageId, child.RowCount, child.DataSize, 0});
379379
}
380380

381381
void AddChild(TChild child) {
382382
// aggregate in order to perform search by row id from any leaf node
383-
child.Count = (ChildrenCount += child.Count);
384-
child.DataSize = (ChildrenSize += child.DataSize);
385-
child.ErasedCount = (ChildrenErasedCount += child.ErasedCount);
383+
child.RowCount = (ChildRowCount += child.RowCount);
384+
child.DataSize = (ChildSize += child.DataSize);
385+
child.ErasedRowCount = (ChildErasedRowCount += child.ErasedRowCount);
386386

387387
Levels[0].PushChild(child);
388388
}
389389

390390
std::optional<TBtreeIndexMeta> Flush(IPageWriter &pager, bool last) {
391-
for (size_t levelIndex = 0; levelIndex < Levels.size(); levelIndex++) {
391+
Y_ABORT_UNLESS(Levels.size() < Max<ui32>(), "Levels size is out of bounds");
392+
for (ui32 levelIndex = 0; levelIndex < Levels.size(); levelIndex++) {
392393
if (last && !Levels[levelIndex].GetKeysCount()) {
393394
Y_ABORT_UNLESS(Levels[levelIndex].GetChildrenCount() == 1, "Should be root");
394395
return TBtreeIndexMeta{ Levels[levelIndex].PopChild(), levelIndex, IndexSize };
@@ -408,13 +409,13 @@ namespace NKikimr::NTable::NPage {
408409
IndexSize = 0;
409410
Writer.Reset();
410411
Levels = { TLevel() };
411-
ChildrenCount = 0;
412-
ChildrenErasedCount = 0;
413-
ChildrenSize = 0;
412+
ChildRowCount = 0;
413+
ChildErasedRowCount = 0;
414+
ChildSize = 0;
414415
}
415416

416417
private:
417-
bool TryFlush(size_t levelIndex, IPageWriter &pager, bool last) {
418+
bool TryFlush(ui32 levelIndex, IPageWriter &pager, bool last) {
418419
if (!last && Levels[levelIndex].GetKeysCount() <= 2 * NodeKeysMax) {
419420
// Note: node should meet both NodeKeysMin and NodeSize restrictions for split
420421

@@ -462,7 +463,7 @@ namespace NKikimr::NTable::NPage {
462463
if (levelIndex + 1 == Levels.size()) {
463464
Levels.emplace_back();
464465
}
465-
Levels[levelIndex + 1].PushChild(TChild{pageId, lastChild.Count, lastChild.DataSize, lastChild.ErasedCount});
466+
Levels[levelIndex + 1].PushChild(TChild{pageId, lastChild.RowCount, lastChild.DataSize, lastChild.ErasedRowCount});
466467
if (!last) {
467468
Levels[levelIndex + 1].PushKey(Levels[levelIndex].PopKey());
468469
}
@@ -497,9 +498,9 @@ namespace NKikimr::NTable::NPage {
497498
const ui32 NodeKeysMin;
498499
const ui32 NodeKeysMax;
499500

500-
TRowId ChildrenCount = 0;
501-
TRowId ChildrenErasedCount = 0;
502-
ui64 ChildrenSize = 0;
501+
TRowId ChildRowCount = 0;
502+
TRowId ChildErasedRowCount = 0;
503+
ui64 ChildSize = 0;
503504
};
504505

505506
}

ydb/core/tablet_flat/flat_part_btree_index_iter.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include "flat_part_iface.h"
4-
#include "flat_page_index.h"
54
#include "flat_table_part.h"
65
#include "flat_part_index_iter_iface.h"
76

@@ -116,7 +115,7 @@ class TPartBtreeIndexIt : public IIndexIter {
116115
, GroupId(groupId)
117116
, GroupInfo(part->Scheme->GetLayout(groupId))
118117
, Meta(groupId.IsHistoric() ? part->IndexPages.BTreeHistoric[groupId.Index] : part->IndexPages.BTreeGroups[groupId.Index])
119-
, State(Reserve(Meta.LevelsCount + 1))
118+
, State(Reserve(Meta.LevelCount + 1))
120119
{
121120
const static TCellsIterable EmptyKey(static_cast<const char*>(nullptr), TColumns());
122121
State.emplace_back(Meta, 0, GetEndRowId(), EmptyKey, EmptyKey);
@@ -181,7 +180,7 @@ class TPartBtreeIndexIt : public IIndexIter {
181180
EReady Next() override {
182181
Y_ABORT_UNLESS(!IsExhausted());
183182

184-
if (Meta.LevelsCount == 0) {
183+
if (Meta.LevelCount == 0) {
185184
return Exhaust();
186185
}
187186

@@ -195,7 +194,7 @@ class TPartBtreeIndexIt : public IIndexIter {
195194
PushNextState(*State.back().Pos + 1);
196195
}
197196

198-
for (size_t level : xrange(State.size() - 1, Meta.LevelsCount)) {
197+
for (ui32 level : xrange<ui32>(State.size() - 1, Meta.LevelCount)) {
199198
if (!TryLoad(State[level])) {
200199
// exiting with an intermediate state
201200
Y_DEBUG_ABORT_UNLESS(!IsLeaf() && !IsExhausted());
@@ -212,7 +211,7 @@ class TPartBtreeIndexIt : public IIndexIter {
212211
EReady Prev() override {
213212
Y_ABORT_UNLESS(!IsExhausted());
214213

215-
if (Meta.LevelsCount == 0) {
214+
if (Meta.LevelCount == 0) {
216215
return Exhaust();
217216
}
218217

@@ -226,7 +225,7 @@ class TPartBtreeIndexIt : public IIndexIter {
226225
PushNextState(*State.back().Pos - 1);
227226
}
228227

229-
for (size_t level : xrange(State.size() - 1, Meta.LevelsCount)) {
228+
for (ui32 level : xrange<ui32>(State.size() - 1, Meta.LevelCount)) {
230229
if (!TryLoad(State[level])) {
231230
// exiting with an intermediate state
232231
Y_DEBUG_ABORT_UNLESS(!IsLeaf() && !IsExhausted());
@@ -247,7 +246,7 @@ class TPartBtreeIndexIt : public IIndexIter {
247246
}
248247

249248
TRowId GetEndRowId() const override {
250-
return Meta.Count;
249+
return Meta.RowCount;
251250
}
252251

253252
TPageId GetPageId() const override {
@@ -287,7 +286,7 @@ class TPartBtreeIndexIt : public IIndexIter {
287286
State[0].Pos = { };
288287
}
289288

290-
for (size_t level : xrange(State.size() - 1, Meta.LevelsCount)) {
289+
for (ui32 level : xrange<ui32>(State.size() - 1, Meta.LevelCount)) {
291290
auto &state = State[level];
292291
Y_DEBUG_ABORT_UNLESS(seek.BelongsTo(state));
293292
if (!TryLoad(state)) {
@@ -317,7 +316,7 @@ class TPartBtreeIndexIt : public IIndexIter {
317316
bool IsLeaf() const noexcept {
318317
// Note: it is possible to have 0 levels in B-Tree
319318
// so we may have exhausted state with leaf (data) node
320-
return State.size() == Meta.LevelsCount + 1 && !IsExhausted();
319+
return State.size() == Meta.LevelCount + 1 && !IsExhausted();
321320
}
322321

323322
EReady Exhaust() {
@@ -335,8 +334,8 @@ class TPartBtreeIndexIt : public IIndexIter {
335334

336335
auto child = current.Node->GetChild(pos);
337336

338-
TRowId beginRowId = pos ? current.Node->GetChild(pos - 1).Count : current.BeginRowId;
339-
TRowId endRowId = child.Count;
337+
TRowId beginRowId = pos ? current.Node->GetChild(pos - 1).RowCount : current.BeginRowId;
338+
TRowId endRowId = child.RowCount;
340339

341340
TCellsIterable beginKey = pos ? current.Node->GetKeyCellsIterable(pos - 1, GroupInfo.ColsKeyIdx) : current.BeginKey;
342341
TCellsIterable endKey = pos < current.Node->GetKeysCount() ? current.Node->GetKeyCellsIterable(pos, GroupInfo.ColsKeyIdx) : current.EndKey;

ydb/core/tablet_flat/flat_part_charge.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace NTable {
4242
}
4343
}
4444

45-
TResult Do(const TCells key1, const TCells key2, const TRowId row1, const TRowId row2,
45+
TResult Do(const TCells key1, const TCells key2, TRowId row1, TRowId row2,
4646
const TKeyCellDefaults &keyDefaults, ui64 itemsLimit, ui64 bytesLimit) const noexcept override
4747
{
4848
auto index = Index.TryLoadRaw();
@@ -110,7 +110,7 @@ namespace NTable {
110110
return { ready, overshot };
111111
}
112112

113-
TResult DoReverse(const TCells key1, const TCells key2, const TRowId row1, const TRowId row2,
113+
TResult DoReverse(const TCells key1, const TCells key2, TRowId row1, TRowId row2,
114114
const TKeyCellDefaults &keyDefaults, ui64 itemsLimit, ui64 bytesLimit) const noexcept override
115115
{
116116
auto index = Index.TryLoadRaw();
@@ -252,7 +252,7 @@ namespace NTable {
252252
}
253253
}
254254
if (itemsLimit && prechargeCurrentFirstRowId <= prechargeCurrentLastRowId) {
255-
ui64 left = itemsLimit - items; // we count only foolprof taken rows, so here we may precharge some extra rows
255+
ui64 left = itemsLimit - items; // we count only foolproof taken rows, so here we may precharge some extra rows
256256
if (prechargeCurrentLastRowId - prechargeCurrentFirstRowId > left) {
257257
prechargeCurrentLastRowId = prechargeCurrentFirstRowId + left;
258258
}
@@ -347,7 +347,7 @@ namespace NTable {
347347
}
348348

349349
if (itemsLimit && prechargeCurrentFirstRowId >= prechargeCurrentLastRowId) {
350-
ui64 left = itemsLimit - items; // we count only foolprof taken rows, so here we may precharge some extra rows
350+
ui64 left = itemsLimit - items; // we count only foolproof taken rows, so here we may precharge some extra rows
351351
if (prechargeCurrentFirstRowId - prechargeCurrentLastRowId > left) {
352352
prechargeCurrentLastRowId = prechargeCurrentFirstRowId - left;
353353
}

0 commit comments

Comments
 (0)