forked from ydb-platform/ydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_part_iter.h
More file actions
1771 lines (1482 loc) · 63 KB
/
flat_part_iter.h
File metadata and controls
1771 lines (1482 loc) · 63 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "flat_part_iface.h"
#include "flat_part_index_iter_iface.h"
#include "flat_table_part.h"
#include "flat_row_eggs.h"
#include "flat_row_state.h"
#include "flat_part_pinout.h"
#include "flat_part_slice.h"
#include "flat_table_committed.h"
#include "flat_page_data.h"
namespace NKikimr {
namespace NTable {
/**
* Part group iterator that may be positioned on a specific row id
*/
class TPartGroupRowIter {
public:
TPartGroupRowIter(const TPart* part, IPages* env, NPage::TGroupId groupId)
: Part(part)
, Env(env)
, GroupId(groupId)
, Index_(CreateIndexIter(part, env, groupId))
, Index(*Index_.Get())
{
}
EReady Seek(TRowId rowId) noexcept {
// Fast path, check if we already have the needed data page
if (Data) {
TRowId minRowId = Page.BaseRow();
if (minRowId <= rowId) {
TRowId current = minRowId + Data.Off();
if (current == rowId) {
return EReady::Data;
}
TRowId endRowId = minRowId + Data.End();
if (rowId < endRowId) {
Data = Page->Begin() + (rowId - minRowId);
Y_ABORT_UNLESS(Data, "Unexpected failure to find column group record for RowId=%lu", rowId);
return EReady::Data;
}
}
}
Data = { };
// Make sure we're at the correct index position first
if (auto ready = Index.Seek(rowId); ready != EReady::Data) {
return ready;
}
// Make sure we have the correct data page loaded
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
return EReady::Page;
}
Y_DEBUG_ABORT_UNLESS(Page.BaseRow() <= rowId, "Index and row have an unexpected relation");
Data = Page->Begin() + (rowId - Page.BaseRow());
Y_ABORT_UNLESS(Data, "Unexpected failure to find record for RowId=%lu", rowId);
return EReady::Data;
}
Y_FORCE_INLINE bool IsValid() const noexcept {
return bool(Data);
}
Y_FORCE_INLINE TRowId GetRowId() const noexcept {
return IsValid() ? (Page.BaseRow() + Data.Off()) : Max<TRowId>();
}
const NPage::TDataPage::TRecord* GetRecord() const noexcept {
Y_DEBUG_ABORT_UNLESS(IsValid(), "Use of unpositioned iterator");
return Data.GetRecord();
}
protected:
bool LoadPage(TPageId pageId, TRowId baseRow) noexcept
{
if (PageId != pageId) {
Data = { };
if (!Page.Set(Env->TryGetPage(Part, pageId, GroupId))) {
PageId = Max<TPageId>();
return false;
}
PageId = pageId;
}
Y_DEBUG_ABORT_UNLESS(Page.BaseRow() == baseRow, "Index and data are out of sync");
return true;
}
protected:
const TPart* const Part;
IPages* const Env;
const NPage::TGroupId GroupId;
TPageId PageId = Max<TPageId>();
THolder<IPartGroupIndexIter> Index_;
IPartGroupIndexIter& Index;
NPage::TDataPage Page;
NPage::TDataPage::TIter Data;
};
/**
* Part group iterator that may be positioned on a specific key
*/
class TPartGroupKeyIter : private TPartGroupRowIter
{
public:
using TCells = NPage::TCells;
TPartGroupKeyIter(const TPart* part, IPages* env, NPage::TGroupId groupId)
: TPartGroupRowIter(part, env, groupId)
, BeginRowId(0)
, EndRowId(Index.GetEndRowId())
, RowId(Max<TRowId>())
{
}
void SetBounds(TRowId beginRowId, TRowId endRowId) noexcept
{
BeginRowId = beginRowId;
EndRowId = Min(endRowId, Index.GetEndRowId());
Y_DEBUG_ABORT_UNLESS(BeginRowId < EndRowId,
"Trying to iterate over empty bounds=[%lu,%lu)", BeginRowId, EndRowId);
RowId = Max<TRowId>();
}
EReady Seek(const TCells key, ESeek seek,
const TPartScheme::TGroupInfo& scheme, const TKeyCellDefaults* keyDefaults) noexcept
{
Y_DEBUG_ABORT_UNLESS(seek == ESeek::Exact || seek == ESeek::Lower || seek == ESeek::Upper,
"Only ESeek{Exact, Upper, Lower} are currently supported here");
if (auto ready = Index.Seek(seek, key, keyDefaults); ready != EReady::Data) {
return Terminate(ready);
}
if (Index.GetRowId() >= EndRowId) {
// Page is outside of bounds
return Exhausted();
}
if (Index.GetRowId() < BeginRowId) {
// Find an index record that has BeginRowId
if (seek == ESeek::Exact) {
if (Index.GetNextRowId() <= BeginRowId) {
// We cannot return any other row, don't even try
return Exhausted();
}
} else {
if (auto ready = SeekIndex(BeginRowId); ready != EReady::Data) {
return Terminate(ready);
}
}
}
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
// Exact RowId unknown, don't allow Next until another Seek
return Terminate(EReady::Page);
}
if (Data = Page.LookupKey(key, scheme, seek, keyDefaults)) {
RowId = Page.BaseRow() + Data.Off();
if (RowId >= EndRowId) {
// Row is outside of bounds
return Exhausted();
}
if (RowId < BeginRowId) {
if (seek == ESeek::Exact) {
// We cannot return any other row, don't even try
return Exhausted();
}
// Adjust to the first available row
Data += BeginRowId - RowId;
RowId = BeginRowId;
Y_DEBUG_ABORT_UNLESS(Data, "Unexpected failure to find BeginRowId=%lu", BeginRowId);
}
return EReady::Data;
}
if (seek != ESeek::Exact) {
// The row we seek is on the next page
if (auto ready = Index.Next(); ready != EReady::Data) {
return Terminate(ready);
}
RowId = Index.GetRowId();
Y_DEBUG_ABORT_UNLESS(RowId > BeginRowId,
"Unexpected next page RowId=%lu (BeginRowId=%lu)",
RowId, BeginRowId);
if (RowId < EndRowId) {
if (auto ready = LoadRowData(); ready != EReady::Data) {
return Terminate(ready);
}
return EReady::Data;
}
}
return Exhausted();
}
EReady SeekReverse(const TCells key, ESeek seek,
const TPartScheme::TGroupInfo& scheme, const TKeyCellDefaults* keyDefaults) noexcept
{
Y_DEBUG_ABORT_UNLESS(seek == ESeek::Exact || seek == ESeek::Lower || seek == ESeek::Upper,
"Only ESeek{Exact, Upper, Lower} are currently supported here");
if (auto ready = Index.SeekReverse(seek, key, keyDefaults); ready != EReady::Data) {
return Terminate(ready);
}
if (Index.GetRowId() < BeginRowId) {
// Page may be outside of bounds
if (Index.GetNextRowId() <= BeginRowId) {
// All rows are outside of bounds
return Exhausted();
}
}
if (EndRowId <= Index.GetRowId()) {
// Find an index record that has EndRowId - 1
if (auto ready = SeekIndex(EndRowId - 1); ready != EReady::Data) {
return Terminate(ready);
}
}
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
// Exact RowId unknown, don't allow Next until another Seek
return Terminate(EReady::Page);
}
if (Data = Page.LookupKeyReverse(key, scheme, seek, keyDefaults)) {
RowId = Page.BaseRow() + Data.Off();
if (RowId < BeginRowId) {
// Row is outside of bounds
return Exhausted();
}
if (RowId >= EndRowId) {
// Adjust to the first available row
auto diff = RowId - (EndRowId - 1);
Y_DEBUG_ABORT_UNLESS(Data.Off() >= diff, "Unexpected failure to find LastRowId=%lu", EndRowId - 1);
Data -= diff;
RowId = EndRowId - 1;
}
return EReady::Data;
}
if (seek != ESeek::Exact) {
// The row we seek is on the prev page
RowId = Index.GetRowId() - 1;
if (auto ready = Index.Prev(); ready != EReady::Data) {
return Terminate(ready);
}
Y_DEBUG_ABORT_UNLESS(RowId < EndRowId,
"Unexpected prev page RowId=%lu (EndRowId=%lu)",
RowId, EndRowId);
if (RowId >= BeginRowId) {
if (auto ready = LoadRowData(); ready != EReady::Data) {
return Terminate(ready);
}
return EReady::Data;
}
}
return Exhausted();
}
EReady Seek(TRowId rowId) noexcept
{
if (Y_UNLIKELY(rowId < BeginRowId || rowId >= EndRowId)) {
return Exhausted();
}
if (auto ready = SeekIndex(rowId); ready != EReady::Data) {
return Terminate(ready);
}
RowId = rowId;
if (auto ready = LoadRowData(); ready != EReady::Data) {
return Terminate(ready);
}
return EReady::Data;
}
EReady SeekToSliceFirstRow() noexcept
{
return Seek(BeginRowId);
}
EReady SeekToSliceLastRow() noexcept
{
return Seek(EndRowId - 1);
}
/**
* If has Data, goes to the next row
*
* If doesn't have Data, loads the current row
*/
EReady Next() noexcept
{
if (Y_UNLIKELY(RowId == Max<TRowId>())) {
return EReady::Gone;
}
Y_DEBUG_ABORT_UNLESS(RowId >= BeginRowId && RowId < EndRowId,
"Unexpected RowId=%lu outside of iteration bounds [%lu,%lu)",
RowId, BeginRowId, EndRowId);
if (Data) {
if (RowId + 1 >= EndRowId) {
return Exhausted();
}
if (Data + 1) {
++RowId;
++Data;
return EReady::Data;
}
// Go to the next data page
// Keep Data in case we have page faulted on Index.Next and need to call it again
auto ready = Index.Next();
if (ready == EReady::Gone) {
return Exhausted();
} else if (ready == EReady::Page) {
return ready;
} else {
++RowId;
Data = { };
}
}
if (auto ready = LoadRowData(); ready != EReady::Gone) {
return ready;
}
Y_ABORT_UNLESS(Index.Next() == EReady::Gone, "Unexpected failure to seek in a non-final data page");
return Exhausted();
}
/**
* If has Data, goes to the prev row
*
* If doesn't have Data, loads the current row
*/
EReady Prev() noexcept
{
if (Y_UNLIKELY(RowId == Max<TRowId>())) {
return EReady::Gone;
}
Y_DEBUG_ABORT_UNLESS(RowId >= BeginRowId && RowId < EndRowId,
"Unexpected RowId=%lu outside of iteration bounds [%lu,%lu)",
RowId, BeginRowId, EndRowId);
if (Data) {
if (RowId <= BeginRowId) {
return Exhausted();
}
if (Data.Off() != 0) {
--RowId;
--Data;
return EReady::Data;
}
// Go to the prev data page
// Keep Data in case we have page faulted on Index.Prev and need to call it again
auto ready = Index.Prev();
if (ready == EReady::Gone) {
return Exhausted();
} else if (ready == EReady::Page) {
return ready;
} else {
--RowId;
Data = { };
}
}
if (auto ready = LoadRowData(); ready != EReady::Gone) {
return ready;
}
Y_ABORT("Unexpected failure to seek in a non-final data page");
}
using TPartGroupRowIter::IsValid;
using TPartGroupRowIter::GetRecord;
Y_FORCE_INLINE TRowId GetRowId() const noexcept {
// N.B. we don't check IsValid because screen-related code may
// call GetRowId() even after EReady::Page or EReady::Gone.
return RowId;
}
const TSharedData& GetPageData() const noexcept {
return Page.GetData();
}
private:
Y_FORCE_INLINE EReady Exhausted() noexcept
{
return Terminate(EReady::Gone);
}
Y_FORCE_INLINE EReady Terminate(EReady ready) noexcept
{
Data = { };
RowId = Max<TRowId>();
return ready;
}
EReady LoadRowData() noexcept
{
Y_DEBUG_ABORT_UNLESS(Index.IsValid() && Index.GetRowId() <= RowId,
"Called without a valid index record");
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
return EReady::Page;
}
if (Data = Page->Begin() + (RowId - Page.BaseRow())) {
return EReady::Data;
}
return EReady::Gone;
}
EReady SeekIndex(TRowId rowId) noexcept
{
auto ready = Index.Seek(rowId);
Y_DEBUG_ABORT_UNLESS(ready != EReady::Gone,
"Unexpected failure to find index record for RowId=%lu, bounds=[%lu,%lu)",
rowId, BeginRowId, EndRowId);
return ready;
}
protected:
TRowId BeginRowId;
TRowId EndRowId;
TRowId RowId;
};
/**
* Part iterator over history data
*/
class TPartHistoryIter : private TPartGroupRowIter
{
public:
using TCells = NPage::TCells;
explicit TPartHistoryIter(const TPart* part, IPages* env)
: TPartGroupRowIter(part, env, NPage::TGroupId(0, /* historic */ true))
{ }
EReady Seek(TRowId rowId, const TRowVersion& rowVersion) noexcept
{
Y_DEBUG_ABORT_UNLESS(rowId != Max<TRowId>());
static_assert(sizeof(rowId) == sizeof(ui64), "sizes don't match");
static_assert(sizeof(rowVersion.Step) == sizeof(ui64), "sizes don't match");
static_assert(sizeof(rowVersion.TxId) == sizeof(ui64), "sizes don't match");
// Construct a synthetic (rowId, step, txId) key on the stack
TCell keyCells[3] = {
TCell::Make(rowId),
TCell::Make(rowVersion.Step),
TCell::Make(rowVersion.TxId),
};
TCells key{ keyCells, 3 };
// Directly use the history group scheme
const auto& scheme = Part->Scheme->HistoryGroup;
Y_DEBUG_ABORT_UNLESS(scheme.ColsKeyIdx.size() == 3);
Y_DEBUG_ABORT_UNLESS(scheme.ColsKeyData.size() == 3);
// Directly use the history key keyDefaults with correct sort order
const TKeyCellDefaults* keyDefaults = Part->Scheme->HistoryKeys.Get();
// Helper for loading row id and row version from the index
auto checkIndex = [&]() -> bool {
RowId = Index.GetKeyCell(0).AsValue<TRowId>();
RowVersion.Step = Index.GetKeyCell(1).AsValue<ui64>();
RowVersion.TxId = Index.GetKeyCell(2).AsValue<ui64>();
return rowId == RowId;
};
// Helper for loading row id and row version from the data page
auto checkData = [&]() -> bool {
RowId = Data->Cell(scheme.ColsKeyData[0]).AsValue<TRowId>();
RowVersion.Step = Data->Cell(scheme.ColsKeyData[1]).AsValue<ui64>();
RowVersion.TxId = Data->Cell(scheme.ColsKeyData[2]).AsValue<ui64>();
return rowId == RowId;
};
// Returns { } when current page is not sufficient
auto seekDownToRowVersion = [&]() -> std::optional<EReady> {
Y_DEBUG_ABORT_UNLESS(rowId == RowId && Data);
Y_DEBUG_ABORT_UNLESS(rowVersion < RowVersion);
// If we previously returned EReady::Data, then we are
// potentially seeking to an older row version on the same
// page. We want to optimize for two cases:
//
// 1. When the next row is very close, we dont want to perform
// expensive index seeks, page reloads and binary searches
//
// 2. When the next row is far away, we still attempt to find
// it on the current page, otherwise fallback to a binary
// search over the whole group.
for (int linear = 4; linear >= 0; --linear) {
++Data;
// Perform binary search on the last iteration
if (linear == 0 && Data) {
Data = Page.LookupKey(key, scheme, ESeek::Lower, keyDefaults);
}
if (!Data) {
// Row is not on current page, move to the next
switch (Index.Next()) {
case EReady::Page: {
// Fallback to full binary search (maybe we need some other index page)
RowId = Max<TRowId>();
return { };
};
case EReady::Gone: {
return Exhausted();
};
case EReady::Data: {
Y_DEBUG_ABORT_UNLESS(Index.GetKeyCellsCount(), "Non-first page is expected to have key cells");
if (Index.GetKeyCellsCount()) {
if (!checkIndex()) {
// First row for the next RowId
MaxVersion = TRowVersion::Max();
Y_DEBUG_ABORT_UNLESS(rowId < RowId);
return EReady::Gone;
}
// Next page has the same row id
// Save an estimate for MaxVersion
MaxVersion = rowVersion;
return { };
} else {
// Fallback to full binary search
RowId = Max<TRowId>();
return { };
}
};
};
}
if (!checkData()) {
// First row for the new RowId
MaxVersion = TRowVersion::Max();
Y_DEBUG_ABORT_UNLESS(rowId < RowId);
return EReady::Gone;
}
if (RowVersion <= rowVersion) {
// Save an estimate for MaxVersion
MaxVersion = rowVersion;
return EReady::Data;
}
}
// This lambda has to be terminated
Y_ABORT_UNLESS(false, "Data binary search bug");
};
// Special case when we already have data with the same row id
if (rowId == RowId && Data) {
// Check if the current row is correct.
// This is mainly for a case when we returned EReady::Gone
// and now we want the first version of the next row.
if (RowVersion <= rowVersion && rowVersion <= MaxVersion) {
return EReady::Data;
}
// Check if we are descending below the current row
if (Y_LIKELY(rowVersion < RowVersion)) {
if (std::optional<EReady> ready = seekDownToRowVersion(); ready.has_value()) {
return *ready;
}
Y_DEBUG_ABORT_UNLESS(!Data, "Shouldn't continue search with Data");
} else {
// High-level iterators never go up once descended to a
// particular version within a specific key. However a
// cached iterator may be reused by seeking to an earlier
// key. In that case history may be positioned very late in
// the version chain and we have to force a full seek.
Data = { };
RowId = Max<TRowId>();
}
}
// Special case when we are following a previous Index estimate
if (rowId == RowId && RowVersion <= rowVersion && rowVersion <= MaxVersion) {
Y_DEBUG_ABORT_UNLESS(Index.IsValid());
Y_DEBUG_ABORT_UNLESS(!Data);
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
return EReady::Page;
}
Data = Page->Begin();
Y_ABORT_UNLESS(checkData() && RowVersion <= rowVersion, "Index and Data are out of sync");
return EReady::Data;
}
// Full index binary search
if (auto ready = Index.Seek(ESeek::Lower, key, keyDefaults); ready != EReady::Data) {
return Terminate(ready);
}
// We need exact match on rowId, bail on larger values
Y_DEBUG_ABORT_UNLESS(Index.GetRowId() == 0 || Index.GetKeyCellsCount(), "Non-first page is expected to have key cells");
if (Index.GetKeyCellsCount()) {
TRowId indexRowId = Index.GetKeyCell(0).AsValue<TRowId>();
if (rowId < indexRowId) {
// We cannot compute MaxVersion anyway as indexRowId row may be presented on the previous page
// and last existing version for rowId is unknown
return Exhausted();
}
} else {
// No information about the current index row
RowId = Max<TRowId>();
}
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
// It's ok to repeat binary search on the next iteration,
// since page faults take a long time and optimizing it
// wouldn't be worth it.
return Terminate(EReady::Page);
}
// Full data binary search
if (Data = Page.LookupKey(key, scheme, ESeek::Lower, keyDefaults)) {
if (!checkData()) {
// First row for the next RowId
MaxVersion = TRowVersion::Max();
Y_DEBUG_ABORT_UNLESS(rowId < RowId);
return EReady::Gone;
}
Y_ABORT_UNLESS(RowVersion <= rowVersion, "Data binary search bug");
// Save an estimate for MaxVersion
MaxVersion = rowVersion;
return EReady::Data;
}
// Our key might be on the next page as lookups are not exact
if (auto ready = Index.Next(); ready != EReady::Data) {
// TODO: do not drop current index pages in case of a page fault
// Will also repeat index binary search in case of a page fault on the next iteration
return Terminate(ready);
}
Y_DEBUG_ABORT_UNLESS(Index.GetKeyCellsCount(), "Non-first page is expected to have key cells");
if (Y_LIKELY(Index.GetKeyCellsCount())) {
if (!checkIndex()) {
// First row for the nextRowId
MaxVersion = TRowVersion::Max();
Y_DEBUG_ABORT_UNLESS(rowId < RowId);
return EReady::Gone;
}
// The above binary search failed, but since we started with
// an index search the first row must be the one we want.
Y_ABORT_UNLESS(RowVersion <= rowVersion, "Index binary search bug");
} else {
// No information about the current index row
RowId = Max<TRowId>();
}
if (!LoadPage(Index.GetPageId(), Index.GetRowId())) {
// We don't want to repeat binary search on the next
// iteration, as we already know the row is not on a
// previous page, but index search would point to it
// again and reloading would be very expensive.
// Remember current MaxVersion estimate, so we just
// quickly check the same page again after restart.
MaxVersion = rowVersion;
return EReady::Page;
}
Data = Page->Begin();
Y_ABORT_UNLESS(Data);
if (Index.GetKeyCellsCount()) {
// Must have rowId as we have checked index
Y_ABORT_UNLESS(checkData() && RowVersion <= rowVersion, "Index and Data are out of sync");
// Save an estimate for MaxVersion
MaxVersion = rowVersion;
return EReady::Data;
} else {
if (checkData()) {
Y_ABORT_UNLESS(RowVersion <= rowVersion, "Index and Data are out of sync");
// Save an estimate for MaxVersion
MaxVersion = rowVersion;
return EReady::Data;
} else {
// First row for the nextRowId
MaxVersion = TRowVersion::Max();
Y_DEBUG_ABORT_UNLESS(rowId < RowId);
return EReady::Gone;
}
}
}
using TPartGroupRowIter::IsValid;
using TPartGroupRowIter::GetRecord;
TRowId GetHistoryRowId() const noexcept {
Y_DEBUG_ABORT_UNLESS(IsValid());
return TPartGroupRowIter::GetRowId();
}
TRowId GetRowId() const noexcept {
Y_DEBUG_ABORT_UNLESS(IsValid());
return RowId;
}
const TRowVersion& GetRowVersion() const noexcept {
Y_DEBUG_ABORT_UNLESS(IsValid());
return RowVersion;
}
private:
Y_FORCE_INLINE EReady Exhausted() noexcept
{
return Terminate(EReady::Gone);
}
Y_FORCE_INLINE EReady Terminate(EReady ready) noexcept
{
Data = { };
RowId = Max<TRowId>();
return ready;
}
private:
TRowId RowId = Max<TRowId>();
TRowVersion RowVersion;
TRowVersion MaxVersion;
};
class TPartIter final {
public:
using TCells = NPage::TCells;
using TGroupId = NPage::TGroupId;
TPartIter(const TPart* part, TTagsRef tags, TIntrusiveConstPtr<TKeyCellDefaults> keyDefaults, IPages* env)
: Part(part)
, Env(env)
, Pinout(Part->Scheme->MakePinout(tags))
, KeyCellDefaults(std::move(keyDefaults))
, Main(Part, Env, TGroupId(0))
, SkipMainDeltas(0)
, SkipMainVersion(false)
, SkipEraseVersion(false)
{
Groups.reserve(Pinout.AltGroups().size());
GroupRemap.resize(Part->Scheme->Groups.size(), Max<ui32>());
for (size_t idx : xrange(Pinout.AltGroups().size())) {
ui32 group = Pinout.AltGroups()[idx];
TGroupId groupId(group);
Groups.emplace_back(Part, Env, groupId);
GroupRemap[group] = idx;
}
size_t KeyCellDefaultsSize = KeyCellDefaults->Size();
Key.resize(KeyCellDefaultsSize);
size_t infoSize = Part->Scheme->Groups[0].ColsKeyData.size();
for (size_t pos = infoSize; pos < KeyCellDefaultsSize; ++pos) {
Key[pos] = (*KeyCellDefaults)[pos];
}
}
void SetBounds(const TSlice& slice) noexcept
{
Main.SetBounds(slice.BeginRowId(), slice.EndRowId());
}
void SetBounds(TRowId beginRowId, TRowId endRowId) noexcept
{
Main.SetBounds(beginRowId, endRowId);
}
EReady Seek(const TCells key, ESeek seek) noexcept
{
ClearKey();
return Main.Seek(key, seek, Part->Scheme->Groups[0], &*KeyCellDefaults);
}
EReady SeekReverse(const TCells key, ESeek seek) noexcept
{
ClearKey();
return Main.SeekReverse(key, seek, Part->Scheme->Groups[0], &*KeyCellDefaults);
}
EReady Seek(TRowId rowId) noexcept
{
ClearKey();
return Main.Seek(rowId);
}
EReady SeekToSliceFirstRow() noexcept
{
ClearKey();
return Main.SeekToSliceFirstRow();
}
EReady SeekToSliceLastRow() noexcept
{
ClearKey();
return Main.SeekToSliceLastRow();
}
EReady Next() noexcept
{
ClearKey();
return Main.Next();
}
EReady Prev() noexcept
{
ClearKey();
return Main.Prev();
}
Y_FORCE_INLINE bool IsValid() const noexcept
{
if (!SkipMainVersion) {
return Main.IsValid();
} else {
Y_DEBUG_ABORT_UNLESS(HistoryState, "SkipMainVersion set, but no HistoryState");
Y_DEBUG_ABORT_UNLESS(Main.IsValid());
return HistoryState->History.IsValid() && HistoryState->History.GetRowId() == Main.GetRowId();
}
}
Y_FORCE_INLINE TRowId GetRowId() const noexcept
{
return Main.GetRowId();
}
TDbTupleRef GetKey() const noexcept
{
InitKey();
return TDbTupleRef(KeyCellDefaults->BasicTypes().begin(), Key.begin(), Key.size());
}
const TSharedData& GetKeyPage() const noexcept
{
return Main.GetPageData();
}
TCells GetRawKey() const noexcept
{
InitKey();
return TCells(Key).Slice(0, Part->Scheme->Groups[0].ColsKeyData.size());
}
TRowVersion GetRowVersion() const noexcept
{
Y_DEBUG_ABORT_UNLESS(IsValid(), "Attempt to get invalid row version");
if (!SkipMainVersion) {
const auto& info = Part->Scheme->Groups[0];
const auto* data = Main.GetRecord()->GetAltRecord(SkipMainDeltas);
Y_ABORT_UNLESS(!data->IsDelta(), "GetRowVersion cannot be called on deltas");
if (!SkipEraseVersion && data->IsErased()) {
return data->GetMaxVersion(info);
}
if (data->IsVersioned()) {
return data->GetMinVersion(info);
} else {
return Part->MinRowVersion;
}
} else {
const auto* data = HistoryState->History.GetRecord();
const auto& info = Part->Scheme->HistoryGroup;
if (!SkipEraseVersion && data->IsErased()) {
return data->GetMaxVersion(info);
}
// History group stores version as part of its key
Y_DEBUG_ABORT_UNLESS(!data->IsVersioned());
return HistoryState->History.GetRowVersion();
}
}
EReady SkipToRowVersion(TRowVersion rowVersion, TIteratorStats& stats,
NTable::ITransactionMapSimplePtr committedTransactions,
NTable::ITransactionObserverSimplePtr transactionObserver,
const NTable::ITransactionSet& decidedTransactions) noexcept
{
// Temporary: we don't cache erases when there are uncompacted deltas
Y_UNUSED(decidedTransactions);
Y_DEBUG_ABORT_UNLESS(Main.IsValid(), "Attempt to use an invalid iterator");
// We cannot use min/max hints when part has uncommitted deltas
if (!Part->TxIdStats) {
if (Part->MaxRowVersion <= rowVersion) {
// Positioning to a known HEAD row version
return EReady::Data;
}
if (rowVersion < Part->MinRowVersion) {
// Don't bother seeking below the known first row version
if (!SkipMainVersion) {
const auto& info = Part->Scheme->Groups[0];
const auto* data = Main.GetRecord()->GetAltRecord(SkipMainDeltas);
Y_ABORT_UNLESS(!data->IsDelta(), "Unexpected delta without TxIdStats");
if (!SkipEraseVersion && data->IsErased()) {
transactionObserver.OnSkipCommitted(data->GetMaxVersion(info));
} else if (data->IsVersioned()) {
transactionObserver.OnSkipCommitted(data->GetMinVersion(info));
} else {
transactionObserver.OnSkipCommitted(Part->MinRowVersion);
}
stats.InvisibleRowSkips++;
stats.UncertainErase = true;
}
return EReady::Gone;
}
}
bool trustHistory;
if (!SkipMainVersion) {
const auto& info = Part->Scheme->Groups[0];
const auto* data = Main.GetRecord()->GetAltRecord(SkipMainDeltas);
while (data->IsDelta()) {
// We cannot cache when there are uncompacted deltas
stats.UncertainErase = true;
ui64 txId = data->GetDeltaTxId(info);
const auto* commitVersion = committedTransactions.Find(txId);
if (commitVersion && *commitVersion <= rowVersion) {
// Already committed and correct version
return EReady::Data;
}
if (commitVersion) {
// Skipping a newer committed delta
transactionObserver.OnSkipCommitted(*commitVersion, txId);
stats.InvisibleRowSkips++;
} else {
// Skipping an uncommitted delta
transactionObserver.OnSkipUncommitted(txId);
}
data = Main.GetRecord()->GetAltRecord(++SkipMainDeltas);
if (!data) {
// This was the last delta, nothing else for this key
SkipMainDeltas = 0;
return EReady::Gone;
}
}
if (!SkipEraseVersion && data->IsErased()) {
TRowVersion current = data->GetMaxVersion(info);
if (current <= rowVersion) {
// Already correct version
return EReady::Data;
}
SkipEraseVersion = true;
transactionObserver.OnSkipCommitted(current);