forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation_partition.hh
1510 lines (1327 loc) · 56.6 KB
/
mutation_partition.hh
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
/*
* Copyright (C) 2014-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <iosfwd>
#include <boost/intrusive/set.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/intrusive/parent_from_member.hpp>
#include <seastar/core/bitset-iter.hh>
#include <seastar/util/optimized_optional.hh>
#include <ranges>
#include "schema/schema_fwd.hh"
#include "tombstone.hh"
#include "keys.hh"
#include "position_in_partition.hh"
#include "atomic_cell_or_collection.hh"
#include "hashing_partition_visitor.hh"
#include "range_tombstone_list.hh"
#include "utils/assert.hh"
#include "utils/intrusive_btree.hh"
#include "utils/preempt.hh"
#include "utils/lru.hh"
#include "utils/managed_ref.hh"
#include "utils/compact-radix-tree.hh"
#include "utils/immutable-collection.hh"
#include "tombstone_gc.hh"
#include "mutation/compact_and_expire_result.hh"
class mutation_fragment;
class mutation_partition_view;
class mutation_partition_visitor;
namespace query {
class clustering_key_filter_ranges;
} // namespace query
struct cell_hash {
using size_type = uint64_t;
static constexpr size_type no_hash = 0;
size_type hash = no_hash;
explicit operator bool() const noexcept {
return hash != no_hash;
}
};
template<>
struct appending_hash<cell_hash> {
template<typename Hasher>
void operator()(Hasher& h, const cell_hash& ch) const {
feed_hash(h, ch.hash);
}
};
using cell_hash_opt = seastar::optimized_optional<cell_hash>;
struct cell_and_hash {
atomic_cell_or_collection cell;
mutable cell_hash_opt hash;
cell_and_hash() = default;
cell_and_hash(cell_and_hash&&) noexcept = default;
cell_and_hash& operator=(cell_and_hash&&) noexcept = default;
cell_and_hash(atomic_cell_or_collection&& cell, cell_hash_opt hash)
: cell(std::move(cell))
, hash(hash)
{ }
};
class compaction_garbage_collector;
//
// Container for cells of a row. Cells are identified by column_id.
//
// All cells must belong to a single column_kind. The kind is not stored
// for space-efficiency reasons. Whenever a method accepts a column_kind,
// the caller must always supply the same column_kind.
//
//
class row {
friend class size_calculator;
using size_type = std::make_unsigned_t<column_id>;
size_type _size = 0;
using sparse_array_type = compact_radix_tree::tree<cell_and_hash, column_id>;
sparse_array_type _cells;
public:
row();
~row();
row(const schema&, column_kind, const row&);
static row construct(const schema& our_schema, const schema& their_schema, column_kind, const row&);
row(row&& other) noexcept;
row& operator=(row&& other) noexcept;
size_t size() const { return _size; }
bool empty() const { return _size == 0; }
const atomic_cell_or_collection& cell_at(column_id id) const;
// Returns a pointer to cell's value or nullptr if column is not set.
const atomic_cell_or_collection* find_cell(column_id id) const;
// Returns a pointer to cell's value and hash or nullptr if column is not set.
const cell_and_hash* find_cell_and_hash(column_id id) const;
template<typename Func>
void remove_if(Func&& func) {
_cells.weed([func, this] (column_id id, cell_and_hash& cah) {
if (!func(id, cah.cell)) {
return false;
}
_size--;
return true;
});
}
private:
template<typename Func>
void consume_with(Func&&);
// Func obeys the same requirements as for for_each_cell below.
template<typename Func, typename MaybeConstCellAndHash>
static constexpr auto maybe_invoke_with_hash(Func& func, column_id id, MaybeConstCellAndHash& c_a_h) {
if constexpr (std::is_invocable_v<Func, column_id, const cell_and_hash&>) {
return func(id, c_a_h);
} else {
return func(id, c_a_h.cell);
}
}
public:
// Calls Func(column_id, cell_and_hash&) or Func(column_id, atomic_cell_and_collection&)
// for each cell in this row, depending on the concrete Func type.
// noexcept if Func doesn't throw.
template<typename Func>
void for_each_cell(Func&& func) {
_cells.walk([func] (column_id id, cell_and_hash& cah) {
maybe_invoke_with_hash(func, id, cah);
return true;
});
}
template<typename Func>
void for_each_cell(Func&& func) const {
_cells.walk([func] (column_id id, const cell_and_hash& cah) {
maybe_invoke_with_hash(func, id, cah);
return true;
});
}
template<typename Func>
void for_each_cell_until(Func&& func) const {
_cells.walk([func] (column_id id, const cell_and_hash& cah) {
return maybe_invoke_with_hash(func, id, cah) != stop_iteration::yes;
});
}
// Merges cell's value into the row.
// Weak exception guarantees.
void apply(const column_definition& column, const atomic_cell_or_collection& cell, cell_hash_opt hash = cell_hash_opt());
// Merges cell's value into the row.
// Weak exception guarantees.
void apply(const column_definition& column, atomic_cell_or_collection&& cell, cell_hash_opt hash = cell_hash_opt());
// Monotonic exception guarantees. In case of exception the sum of cell and this remains the same as before the exception.
void apply_monotonically(const column_definition& column, atomic_cell_or_collection&& cell, cell_hash_opt hash = cell_hash_opt());
// Adds cell to the row. The column must not be already set.
void append_cell(column_id id, atomic_cell_or_collection cell);
// Weak exception guarantees
void apply(const schema&, column_kind, const row& src);
void apply(const schema&, column_kind, row&& src);
void apply(const schema& our_schema, const schema& their_schema, column_kind kind, const row& other);
void apply(const schema& our_schema, const schema& their_schema, column_kind kind, row&& other);
// Monotonic exception guarantees
void apply_monotonically(const schema&, column_kind, row&& src);
void apply_monotonically(const schema&, column_kind, const row& src);
void apply_monotonically(const schema& our_schema, const schema& their_schema, column_kind, row&& src);
void apply_monotonically(const schema& our_schema, const schema& their_schema, column_kind, const row& src);
// Expires cells based on query_time. Expires tombstones based on gc_before
// and max_purgeable. Removes cells covered by tomb.
// Returns true iff there are any live cells left.
compact_and_expire_result compact_and_expire(
const schema& s,
column_kind kind,
row_tombstone tomb,
gc_clock::time_point query_time,
can_gc_fn&,
gc_clock::time_point gc_before,
const row_marker& marker,
compaction_garbage_collector* collector = nullptr);
compact_and_expire_result compact_and_expire(
const schema& s,
column_kind kind,
row_tombstone tomb,
gc_clock::time_point query_time,
can_gc_fn&,
gc_clock::time_point gc_before,
compaction_garbage_collector* collector = nullptr);
row difference(const schema&, column_kind, const row& other) const;
bool equal(column_kind kind, const schema& this_schema, const row& other, const schema& other_schema) const;
size_t external_memory_usage(const schema&, column_kind) const;
cell_hash_opt cell_hash_for(column_id id) const;
void prepare_hash(const schema& s, column_kind kind) const;
void clear_hash() const;
bool is_live(const schema&, column_kind kind, tombstone tomb = tombstone(), gc_clock::time_point now = gc_clock::time_point::min()) const;
class printer {
const schema& _schema;
column_kind _kind;
const row& _row;
public:
printer(const schema& s, column_kind k, const row& r) : _schema(s), _kind(k), _row(r) { }
printer(const printer&) = delete;
printer(printer&&) = delete;
friend std::ostream& operator<<(std::ostream& os, const printer& p);
};
friend std::ostream& operator<<(std::ostream& os, const printer& p);
};
// Like row, but optimized for the case where the row doesn't exist (e.g. static rows)
class lazy_row {
managed_ref<row> _row;
static inline const row _empty_row;
public:
lazy_row() = default;
explicit lazy_row(row&& r) {
if (!r.empty()) {
_row = make_managed<row>(std::move(r));
}
}
lazy_row(const schema& s, column_kind kind, const lazy_row& r) {
if (!r.empty()) {
_row = make_managed<row>(s, kind, *r._row);
}
}
lazy_row(const schema& s, column_kind kind, const row& r) {
if (!r.empty()) {
_row = make_managed<row>(s, kind, r);
}
}
row& maybe_create() {
if (!_row) {
_row = make_managed<row>();
}
return *_row;
}
const row& get_existing() const & {
return *_row;
}
row& get_existing() & {
return *_row;
}
row&& get_existing() && {
return std::move(*_row);
}
const row& get() const {
return _row ? *_row : _empty_row;
}
size_t size() const {
if (!_row) {
return 0;
}
return _row->size();
}
bool empty() const {
if (!_row) {
return true;
}
return _row->empty();
}
void reserve(column_id nr) {
if (nr) {
maybe_create();
}
}
const atomic_cell_or_collection& cell_at(column_id id) const {
if (!_row) {
throw_with_backtrace<std::out_of_range>(format("Column not found for id = {:d}", id));
} else {
return _row->cell_at(id);
}
}
// Returns a pointer to cell's value or nullptr if column is not set.
const atomic_cell_or_collection* find_cell(column_id id) const {
if (!_row) {
return nullptr;
}
return _row->find_cell(id);
}
// Returns a pointer to cell's value and hash or nullptr if column is not set.
const cell_and_hash* find_cell_and_hash(column_id id) const {
if (!_row) {
return nullptr;
}
return _row->find_cell_and_hash(id);
}
// Calls Func(column_id, cell_and_hash&) or Func(column_id, atomic_cell_and_collection&)
// for each cell in this row, depending on the concrete Func type.
// noexcept if Func doesn't throw.
template<typename Func>
void for_each_cell(Func&& func) {
if (!_row) {
return;
}
_row->for_each_cell(std::forward<Func>(func));
}
template<typename Func>
void for_each_cell(Func&& func) const {
if (!_row) {
return;
}
_row->for_each_cell(std::forward<Func>(func));
}
template<typename Func>
void for_each_cell_until(Func&& func) const {
if (!_row) {
return;
}
_row->for_each_cell_until(std::forward<Func>(func));
}
// Merges cell's value into the row.
// Weak exception guarantees.
void apply(const column_definition& column, const atomic_cell_or_collection& cell, cell_hash_opt hash = cell_hash_opt()) {
maybe_create().apply(column, cell, std::move(hash));
}
// Merges cell's value into the row.
// Weak exception guarantees.
void apply(const column_definition& column, atomic_cell_or_collection&& cell, cell_hash_opt hash = cell_hash_opt()) {
maybe_create().apply(column, std::move(cell), std::move(hash));
}
// Monotonic exception guarantees. In case of exception the sum of cell and this remains the same as before the exception.
void apply_monotonically(const column_definition& column, atomic_cell_or_collection&& cell, cell_hash_opt hash = cell_hash_opt()) {
maybe_create().apply_monotonically(column, std::move(cell), std::move(hash));
}
// Adds cell to the row. The column must not be already set.
void append_cell(column_id id, atomic_cell_or_collection cell) {
maybe_create().append_cell(id, std::move(cell));
}
// Weak exception guarantees
void apply(const schema& s, column_kind kind, const row& src) {
if (src.empty()) {
return;
}
maybe_create().apply(s, kind, src);
}
// Weak exception guarantees
void apply(const schema& s, column_kind kind, const lazy_row& src) {
if (src.empty()) {
return;
}
maybe_create().apply(s, kind, src.get_existing());
}
// Weak exception guarantees
void apply(const schema& s, column_kind kind, row&& src) {
if (src.empty()) {
return;
}
maybe_create().apply(s, kind, std::move(src));
}
// Monotonic exception guarantees
void apply_monotonically(const schema& s, column_kind kind, row&& src) {
if (src.empty()) {
return;
}
maybe_create().apply_monotonically(s, kind, std::move(src));
}
// Monotonic exception guarantees
void apply_monotonically(const schema& s, column_kind kind, lazy_row&& src) {
if (src.empty()) {
return;
}
if (!_row) {
_row = std::move(src._row);
return;
}
get_existing().apply_monotonically(s, kind, std::move(src.get_existing()));
}
// Monotonic exception guarantees
void apply_monotonically(const schema& our_schema, const schema& their_schema, column_kind kind, lazy_row&& src) {
if (src.empty()) {
return;
}
maybe_create().apply_monotonically(our_schema, their_schema, kind, std::move(src.get_existing()));
}
// Expires cells based on query_time. Expires tombstones based on gc_before
// and max_purgeable. Removes cells covered by tomb.
// Returns true iff there are any live cells left.
bool compact_and_expire(
const schema& s,
column_kind kind,
row_tombstone tomb,
gc_clock::time_point query_time,
can_gc_fn& can_gc,
gc_clock::time_point gc_before,
const row_marker& marker,
compaction_garbage_collector* collector = nullptr);
bool compact_and_expire(
const schema& s,
column_kind kind,
row_tombstone tomb,
gc_clock::time_point query_time,
can_gc_fn& can_gc,
gc_clock::time_point gc_before,
compaction_garbage_collector* collector = nullptr);
lazy_row difference(const schema& s, column_kind kind, const lazy_row& other) const {
if (!_row) {
return lazy_row();
}
if (!other._row) {
return lazy_row(s, kind, *_row);
}
return lazy_row(_row->difference(s, kind, *other._row));
}
bool equal(column_kind kind, const schema& this_schema, const lazy_row& other, const schema& other_schema) const {
bool e1 = empty();
bool e2 = other.empty();
if (e1 && e2) {
return true;
}
if (e1 != e2) {
return false;
}
// both non-empty
return _row->equal(kind, this_schema, *other._row, other_schema);
}
size_t external_memory_usage(const schema& s, column_kind kind) const {
if (!_row) {
return 0;
}
return _row.external_memory_usage() + _row->external_memory_usage(s, kind);
}
cell_hash_opt cell_hash_for(column_id id) const {
if (!_row) {
return cell_hash_opt{};
}
return _row->cell_hash_for(id);
}
void prepare_hash(const schema& s, column_kind kind) const {
if (!_row) {
return;
}
_row->prepare_hash(s, kind);
}
void clear_hash() const {
if (!_row) {
return;
}
_row->clear_hash();
}
bool is_live(const schema& s, column_kind kind, tombstone tomb = tombstone(), gc_clock::time_point now = gc_clock::time_point::min()) const {
if (!_row) {
return false;
}
return _row->is_live(s, kind, tomb, now);
}
class printer {
const schema& _schema;
column_kind _kind;
const lazy_row& _row;
public:
printer(const schema& s, column_kind k, const lazy_row& r) : _schema(s), _kind(k), _row(r) { }
printer(const printer&) = delete;
printer(printer&&) = delete;
friend std::ostream& operator<<(std::ostream& os, const printer& p);
};
};
// Used to return the timestamp of the latest update to the row
struct max_timestamp {
api::timestamp_type max = api::missing_timestamp;
void update(api::timestamp_type ts) {
max = std::max(max, ts);
}
};
template<>
struct appending_hash<row> {
static constexpr int null_hash_value = 0xbeefcafe;
template<typename Hasher>
void operator()(Hasher& h, const row& cells, const schema& s, column_kind kind, const query::column_id_vector& columns, max_timestamp& max_ts) const;
};
class row_marker;
int compare_row_marker_for_merge(const row_marker& left, const row_marker& right) noexcept;
class row_marker {
static constexpr gc_clock::duration no_ttl { 0 };
static constexpr gc_clock::duration dead { -1 };
static constexpr gc_clock::time_point no_expiry { gc_clock::duration(0) };
api::timestamp_type _timestamp = api::missing_timestamp;
gc_clock::duration _ttl = no_ttl;
gc_clock::time_point _expiry = no_expiry;
public:
row_marker() = default;
explicit row_marker(api::timestamp_type created_at) : _timestamp(created_at) { }
row_marker(api::timestamp_type created_at, gc_clock::duration ttl, gc_clock::time_point expiry)
: _timestamp(created_at), _ttl(ttl), _expiry(expiry)
{ }
explicit row_marker(tombstone deleted_at)
: _timestamp(deleted_at.timestamp), _ttl(dead), _expiry(deleted_at.deletion_time)
{ }
bool is_missing() const {
return _timestamp == api::missing_timestamp;
}
bool is_live() const {
return !is_missing() && _ttl != dead;
}
bool is_live(tombstone t, gc_clock::time_point now) const {
if (is_missing() || _ttl == dead) {
return false;
}
if (_ttl != no_ttl && _expiry <= now) {
return false;
}
return _timestamp > t.timestamp;
}
// Can be called only when !is_missing().
bool is_dead(gc_clock::time_point now) const {
if (_ttl == dead) {
return true;
}
return _ttl != no_ttl && _expiry <= now;
}
// Can be called only when is_live().
bool is_expiring() const {
return _ttl != no_ttl;
}
// Can be called only when is_expiring().
gc_clock::duration ttl() const {
return _ttl;
}
// Can be called only when is_expiring().
gc_clock::time_point expiry() const {
return _expiry;
}
// Should be called when is_dead() or is_expiring().
// Safe to be called when is_missing().
// When is_expiring(), returns the the deletion time of the marker when it finally expires.
gc_clock::time_point deletion_time() const {
return _ttl == dead ? _expiry : _expiry - _ttl;
}
api::timestamp_type timestamp() const {
return _timestamp;
}
void apply(const row_marker& rm) {
if (compare_row_marker_for_merge(*this, rm) < 0) {
*this = rm;
}
}
// Expires cells and tombstones. Removes items covered by higher level
// tombstones.
// Returns true if row marker is live.
bool compact_and_expire(tombstone tomb, gc_clock::time_point now,
can_gc_fn& can_gc, gc_clock::time_point gc_before, compaction_garbage_collector* collector = nullptr);
// Consistent with feed_hash()
bool operator==(const row_marker& other) const {
if (_timestamp != other._timestamp) {
return false;
}
if (is_missing()) {
return true;
}
if (_ttl != other._ttl) {
return false;
}
return _ttl == no_ttl || _expiry == other._expiry;
}
// Consistent with operator==()
template<typename Hasher>
void feed_hash(Hasher& h) const {
::feed_hash(h, _timestamp);
if (!is_missing()) {
::feed_hash(h, _ttl);
if (_ttl != no_ttl) {
::feed_hash(h, _expiry);
}
}
}
friend std::ostream& operator<<(std::ostream& os, const row_marker& rm);
};
template<>
struct appending_hash<row_marker> {
template<typename Hasher>
void operator()(Hasher& h, const row_marker& m) const {
m.feed_hash(h);
}
};
class shadowable_tombstone {
tombstone _tomb;
public:
explicit shadowable_tombstone(api::timestamp_type timestamp, gc_clock::time_point deletion_time)
: _tomb(timestamp, deletion_time) {
}
explicit shadowable_tombstone(tombstone tomb = tombstone())
: _tomb(std::move(tomb)) {
}
std::strong_ordering operator<=>(const shadowable_tombstone& t) const {
return _tomb <=> t._tomb;
}
bool operator==(const shadowable_tombstone&) const = default;
explicit operator bool() const {
return bool(_tomb);
}
const tombstone& tomb() const {
return _tomb;
}
// A shadowable row tombstone is valid only if the row has no live marker. In other words,
// the row tombstone is only valid as long as no newer insert is done (thus setting a
// live row marker; note that if the row timestamp set is lower than the tombstone's,
// then the tombstone remains in effect as usual). If a row has a shadowable tombstone
// with timestamp Ti and that row is updated with a timestamp Tj, such that Tj > Ti
// (and that update sets the row marker), then the shadowable tombstone is shadowed by
// that update. A concrete consequence is that if the update has cells with timestamp
// lower than Ti, then those cells are preserved (since the deletion is removed), and
// this is contrary to a regular, non-shadowable row tombstone where the tombstone is
// preserved and such cells are removed.
bool is_shadowed_by(const row_marker& marker) const {
return marker.is_live() && marker.timestamp() > _tomb.timestamp;
}
void maybe_shadow(tombstone t, row_marker marker) noexcept {
if (is_shadowed_by(marker)) {
_tomb = std::move(t);
}
}
void apply(tombstone t) noexcept {
_tomb.apply(t);
}
void apply(shadowable_tombstone t) noexcept {
_tomb.apply(t._tomb);
}
};
template <>
struct fmt::formatter<shadowable_tombstone> : fmt::formatter<string_view> {
template <typename FormatContext>
auto format(const shadowable_tombstone& t, FormatContext& ctx) const {
if (t) {
return fmt::format_to(ctx.out(),
"{{shadowable tombstone: timestamp={}, deletion_time={}}}",
t.tomb().timestamp, t.tomb(), t.tomb().deletion_time.time_since_epoch().count());
} else {
return fmt::format_to(ctx.out(),
"{{shadowable tombstone: none}}");
}
}
};
template<>
struct appending_hash<shadowable_tombstone> {
template<typename Hasher>
void operator()(Hasher& h, const shadowable_tombstone& t) const {
feed_hash(h, t.tomb());
}
};
/*
The rules for row_tombstones are as follows:
- The shadowable tombstone is always >= than the regular one;
- The regular tombstone works as expected;
- The shadowable tombstone doesn't erase or compact away the regular
row tombstone, nor dead cells;
- The shadowable tombstone can erase live cells, but only provided they
can be recovered (e.g., by including all cells in a MV update, both
updated cells and pre-existing ones);
- The shadowable tombstone can be erased or compacted away by a newer
row marker.
*/
class row_tombstone {
tombstone _regular;
shadowable_tombstone _shadowable; // _shadowable is always >= _regular
public:
explicit row_tombstone(tombstone regular, shadowable_tombstone shadowable)
: _regular(std::move(regular))
, _shadowable(std::move(shadowable)) {
}
explicit row_tombstone(tombstone regular)
: row_tombstone(regular, shadowable_tombstone(regular)) {
}
row_tombstone() = default;
std::strong_ordering operator<=>(const row_tombstone& t) const {
return _shadowable <=> t._shadowable;
}
bool operator==(const row_tombstone& t) const {
return _shadowable == t._shadowable;
}
explicit operator bool() const {
return bool(_shadowable);
}
const tombstone& tomb() const {
return _shadowable.tomb();
}
const gc_clock::time_point max_deletion_time() const {
return std::max(_regular.deletion_time, _shadowable.tomb().deletion_time);
}
const tombstone& regular() const {
return _regular;
}
const shadowable_tombstone& shadowable() const {
return _shadowable;
}
is_shadowable is_shadowable() const {
return ::is_shadowable(_shadowable.tomb() > _regular);
}
void maybe_shadow(const row_marker& marker) noexcept {
_shadowable.maybe_shadow(_regular, marker);
}
void apply(tombstone regular) noexcept {
_shadowable.apply(regular);
_regular.apply(regular);
}
void apply(shadowable_tombstone shadowable, row_marker marker) noexcept {
_shadowable.apply(shadowable.tomb());
_shadowable.maybe_shadow(_regular, marker);
}
void apply(row_tombstone t, row_marker marker) noexcept {
_regular.apply(t._regular);
_shadowable.apply(t._shadowable);
_shadowable.maybe_shadow(_regular, marker);
}
friend std::ostream& operator<<(std::ostream& out, const row_tombstone& t) {
if (t) {
fmt::print(out, "{{row_tombstone: {}{}}}", t._regular, t.is_shadowable() ? t._shadowable : shadowable_tombstone());
} else {
fmt::print(out, "{{row_tombstone: none}}");
}
return out;
}
};
template<>
struct appending_hash<row_tombstone> {
template<typename Hasher>
void operator()(Hasher& h, const row_tombstone& t) const {
feed_hash(h, t.regular());
if (t.is_shadowable()) {
feed_hash(h, t.shadowable());
}
}
};
class deletable_row final {
row_tombstone _deleted_at;
row_marker _marker;
row _cells;
public:
deletable_row() {}
deletable_row(const schema& s, const deletable_row& other)
: _deleted_at(other._deleted_at)
, _marker(other._marker)
, _cells(s, column_kind::regular_column, other._cells)
{ }
deletable_row(const schema& our_schema, const schema& their_schema, const deletable_row& other)
: _deleted_at(other._deleted_at)
, _marker(other._marker)
, _cells(row::construct(our_schema, their_schema, column_kind::regular_column, other._cells))
{ }
deletable_row(row_tombstone&& tomb, row_marker&& marker, row&& cells)
: _deleted_at(std::move(tomb)), _marker(std::move(marker)), _cells(std::move(cells))
{}
void apply(tombstone deleted_at) {
_deleted_at.apply(deleted_at);
maybe_shadow();
}
void apply(shadowable_tombstone deleted_at) {
_deleted_at.apply(deleted_at, _marker);
}
void apply(row_tombstone deleted_at) {
_deleted_at.apply(deleted_at, _marker);
}
void apply(const row_marker& rm) {
_marker.apply(rm);
maybe_shadow();
}
void remove_tombstone() {
_deleted_at = {};
}
void maybe_shadow() {
_deleted_at.maybe_shadow(_marker);
}
// Weak exception guarantees. After exception, both src and this will commute to the same value as
// they would should the exception not happen.
void apply(const schema&, deletable_row&& src);
void apply(const schema&, const deletable_row& src);
void apply(const schema& our_schema, const schema& their_schema, const deletable_row& src);
void apply(const schema& our_schema, const schema& their_schema, deletable_row&& src);
void apply_monotonically(const schema&, deletable_row&& src);
void apply_monotonically(const schema&, const deletable_row& src);
void apply_monotonically(const schema& our_schema, const schema& their_schema, deletable_row&& src);
void apply_monotonically(const schema& our_schema, const schema& their_schema, const deletable_row& src);
public:
row_tombstone deleted_at() const { return _deleted_at; }
api::timestamp_type created_at() const { return _marker.timestamp(); }
// Call `maybe_shadow()` if the marker's timestamp is mutated.
row_marker& marker() { return _marker; }
const row_marker& marker() const { return _marker; }
const row& cells() const { return _cells; }
row& cells() { return _cells; }
bool equal(column_kind, const schema& s, const deletable_row& other, const schema& other_schema) const;
bool is_live(const schema& s, column_kind kind, tombstone base_tombstone = tombstone(), gc_clock::time_point query_time = gc_clock::time_point::min()) const;
bool empty() const { return !_deleted_at && _marker.is_missing() && !_cells.size(); }
deletable_row difference(const schema&, column_kind, const deletable_row& other) const;
// Expires cells and tombstones. Removes items covered by higher level
// tombstones.
// Returns true iff the row is still alive.
// When empty() after the call, the row can be removed without losing writes
// given that tomb will be still in effect for the row after it is removed,
// as a range tombstone, partition tombstone, etc.
bool compact_and_expire(const schema&,
tombstone tomb,
gc_clock::time_point query_time,
can_gc_fn& can_gc,
gc_clock::time_point gc_before,
compaction_garbage_collector* collector = nullptr);
class printer {
const schema& _schema;
const deletable_row& _deletable_row;
public:
printer(const schema& s, const deletable_row& r) : _schema(s), _deletable_row(r) { }
printer(const printer&) = delete;
printer(printer&&) = delete;
friend std::ostream& operator<<(std::ostream& os, const printer& p);
};
friend std::ostream& operator<<(std::ostream& os, const printer& p);
};
class cache_tracker;
class rows_entry final : public evictable {
friend class size_calculator;
intrusive_b::member_hook _link;
clustering_key _key;
deletable_row _row;
// Given p is the preceding rows_entry&,
// this tombstone applies to the range (p.position(), position()] if continuous()
// and to [position(), position()] if !continuous().
// So the tombstone applies only to the continuous interval, to the left.
// On top of that, _row.deleted_at() may still apply new information.
// So it's not deoverlapped with the row tombstone.
// Set only when in mutation_partition_v2.
tombstone _range_tombstone;
struct flags {
// _before_ck and _after_ck encode position_in_partition::weight
bool _before_ck : 1;
bool _after_ck : 1;
bool _continuous : 1; // See doc of is_continuous.
bool _dummy : 1;
// Marks a dummy entry which is after_all_clustered_rows() position.
// Needed so that eviction, which can't use comparators, can check if it's dealing with it.
bool _last_dummy : 1;
flags() : _before_ck(0), _after_ck(0), _continuous(true), _dummy(false), _last_dummy(false) { }
} _flags{};
public:
struct last_dummy_tag {};
explicit rows_entry(clustering_key&& key)
: _key(std::move(key))
{ }
explicit rows_entry(const clustering_key& key)
: _key(key)
{ }
rows_entry(const schema& s, position_in_partition_view pos, is_dummy dummy, is_continuous continuous)
: _key(pos.key())
{
_flags._last_dummy = bool(dummy) && pos.is_after_all_clustered_rows(s);
_flags._dummy = bool(dummy);
_flags._continuous = bool(continuous);
_flags._before_ck = pos.is_before_key();
_flags._after_ck = pos.is_after_key();
}
rows_entry(const schema& s, last_dummy_tag, is_continuous continuous)
: rows_entry(s, position_in_partition_view::after_all_clustered_rows(), is_dummy::yes, continuous)
{ }
rows_entry(const clustering_key& key, deletable_row&& row)
: _key(key), _row(std::move(row))
{ }
rows_entry(const schema& s, const clustering_key& key, const deletable_row& row)
: _key(key), _row(s, row)
{ }
rows_entry(rows_entry&& o) noexcept;
rows_entry(const schema& s, const rows_entry& e)
: _key(e._key)
, _row(s, e._row)
, _range_tombstone(e._range_tombstone)
, _flags(e._flags)
{ }
rows_entry(const schema& our_schema, const schema& their_schema, const rows_entry& e)
: _key(e._key)
, _row(our_schema, their_schema, e._row)
, _range_tombstone(e._range_tombstone)
, _flags(e._flags)
{ }
// Valid only if !dummy()
clustering_key& key() {
return _key;
}
// Valid only if !dummy()
const clustering_key& key() const {
return _key;
}
deletable_row& row() {
return _row;
}