trine-kv 0.5.13

Embedded LSM MVCC key-value database.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
use bytes::Bytes;
use std::{
    collections::{BTreeMap, BTreeSet},
    mem::size_of,
    ops::{Bound, Range},
    path::{Path, PathBuf},
    sync::{
        Arc, RwLock,
        atomic::{AtomicU64, AtomicUsize, Ordering},
    },
};

use crate::{
    blob::{BlobIndex, ValueRef},
    block::{BlockHandle, BlockManager, BlockReadSource, DecodedBlock, block_bounds, checksum},
    cache::{BlockCache, BlockCacheKey, CacheKind},
    codec::CodecId,
    error::{Error, Result},
    filter::{PointKeyFilter, PrefixFilter},
    internal_key::{InternalKey, ValueKind},
    iterator::{
        Direction, ForwardKeyState, RecordGroup, ReverseKeyState, ScanRecord, ScanSelector,
        prefix_successor, record_group_from_first_and_rest,
    },
    limits,
    options::{
        DurabilityMode, FilterDepthCurve, FilterPolicy, IndexSearchPolicy, PrefixFilterPolicy,
    },
    point_value::PointValueSource,
    prefix::PrefixExtractor,
    range_tombstone::{self, RangeTombstoneIndex, RangeTombstoneLike},
    search,
    stats::{FilterStats, ReadPathStats},
    storage::{
        BlockingStorageObjectListBackend, BlockingStorageObjectWriteBackend,
        BlockingStorageReadBackend, BlockingStorageReadObject, MemoryStorageBackend,
        NativeFileBackend, NativeFileObject, NativeFileReadSource, StorageCapability,
        StorageObjectId, StorageObjectKind, StorageObjectListBackend, StorageObjectListRequest,
        StorageObjectWriteBackend, StorageReadBackend, StorageReadBuffer, StorageReadObject,
        StorageReadSource,
    },
    types::{KeyRange, Sequence},
};

pub const TABLE_FILE_EXTENSION: &str = "trinet";
const TABLE_MAGIC: u32 = 0x5452_5442;
const TABLE_VERSION: u16 = 6;
const HEADER_LEN: usize = 14;
const FOOTER_MAGIC: u32 = 0x5452_5446;
const FOOTER_LEN: usize = 90;
const DATA_BLOCK_RESTART_INTERVAL: usize = 16;
const INDEX_PARTITION_TARGET_ENTRIES: usize = 128;
const PINNED_READ_METADATA_MAX_LEVEL: u32 = 1;
const WHOLE_TABLE_SYNC_OPEN_MAX_BYTES: u64 = 256 * 1024;

const VALUE_KIND_PUT: u8 = 1;
const VALUE_KIND_POINT_DELETE: u8 = 2;
const VALUE_KIND_RANGE_DELETE: u8 = 3;

const VALUE_NONE: u8 = 0;
const VALUE_INLINE: u8 = 1;
const VALUE_BLOB: u8 = 2;
const VALUE_BLOB_INDEX: u8 = 3;

const BOUND_UNBOUNDED: u8 = 0;
const BOUND_INCLUDED: u8 = 1;
const BOUND_EXCLUDED: u8 = 2;

const PREFIX_FILTER_ABSENT: u8 = 0;
const PREFIX_FILTER_PRESENT: u8 = 1;

const POINT_KEY_FILTER_ABSENT: u8 = 0;
const POINT_KEY_FILTER_PRESENT: u8 = 1;

const PREFIX_EXTRACTOR_DISABLED: u8 = 0;
const PREFIX_EXTRACTOR_FIXED_LEN: u8 = 1;
const PREFIX_EXTRACTOR_SEPARATOR: u8 = 2;
const PREFIX_EXTRACTOR_CUSTOM: u8 = 3;
const TABLE_STAT_SHARDS: usize = 32;

static NEXT_TABLE_STAT_SHARD: AtomicUsize = AtomicUsize::new(0);

thread_local! {
    static TABLE_STAT_SHARD: usize =
        NEXT_TABLE_STAT_SHARD.fetch_add(1, Ordering::Relaxed) % TABLE_STAT_SHARDS;
}

// These are on-disk lower bounds. Decoders use them to reject impossible
// record counts before reserving memory; real entries may be larger because
// keys, values, and filters carry byte fields.
const MIN_INTERNAL_KEY_BYTES: usize = 17;
const MIN_VALUE_REF_BYTES: usize = 1;
const MIN_DATA_RECORD_BYTES: usize = MIN_INTERNAL_KEY_BYTES + MIN_VALUE_REF_BYTES;
const MIN_DATA_BLOCK_HASH_ENTRY_BYTES: usize = 16;
const MIN_INDEX_ENTRY_BYTES: usize = MIN_INTERNAL_KEY_BYTES * 2 + 16 + 1 + 1;
const MIN_INDEX_PARTITION_ENTRY_BYTES: usize = MIN_INTERNAL_KEY_BYTES * 2 + 16 + 8;
const MIN_RANGE_TOMBSTONE_BYTES: usize = 14;
const RESTART_POINT_BYTES: usize = 4;
const INLINE_VALUE_HEADER_BYTES: usize = 5;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TableId(pub u64);

impl TableId {
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }

    #[must_use]
    pub const fn next(self) -> Option<Self> {
        match self.0.checked_add(1) {
            Some(value) => Some(Self(value)),
            None => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TableLevel(pub u32);

impl TableLevel {
    pub const ZERO: Self = Self(0);

    #[must_use]
    pub const fn get(self) -> u32 {
        self.0
    }

    #[must_use]
    pub const fn next(self) -> Option<Self> {
        match self.0.checked_add(1) {
            Some(value) => Some(Self(value)),
            None => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TableSection {
    DataBlocks,
    RangeTombstones,
    Filters,
    Indexes,
    Properties,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableBlobReference {
    pub file_id: u64,
    pub referenced_bytes: u64,
    pub referenced_record_count: u64,
    pub smallest_internal_key: InternalKey,
    pub largest_internal_key: InternalKey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableProperties {
    pub id: TableId,
    pub level: TableLevel,
    pub smallest_user_key: Vec<u8>,
    pub largest_user_key: Vec<u8>,
    pub smallest_sequence: Sequence,
    pub largest_sequence: Sequence,
    pub codec: CodecId,
    pub(crate) blob_file_ids: Vec<u64>,
    pub(crate) blob_references: Vec<TableBlobReference>,
}

impl TableProperties {
    #[must_use]
    pub fn blob_file_ids(&self) -> &[u64] {
        &self.blob_file_ids
    }

    #[must_use]
    pub fn blob_references(&self) -> &[TableBlobReference] {
        &self.blob_references
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TableWriteOptions {
    pub(crate) codec: CodecId,
    pub(crate) block_bytes: usize,
    pub(crate) filter_policy: FilterPolicy,
    pub(crate) prefix_extractor: PrefixExtractor,
    pub(crate) prefix_filter_policy: PrefixFilterPolicy,
    pub(crate) filter_depth_curve: FilterDepthCurve,
    pub(crate) blob_threshold_bytes: usize,
    pub(crate) rewrite_blob_indexes: bool,
}

fn sort_point_records_if_needed(point_records: &mut [(InternalKey, Option<ValueRef>)]) {
    if point_records.windows(2).all(|pair| pair[0].0 <= pair[1].0) {
        return;
    }
    point_records.sort_by(|left, right| left.0.cmp(&right.0));
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TablePointRecord {
    pub(crate) internal_key: InternalKey,
    pub(crate) value: Option<ValueRef>,
}

#[derive(Debug)]
pub(crate) struct TablePointValueRecord {
    pub(crate) internal_key: InternalKey,
    pub(crate) value: Option<PointValueSource>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SectionHandle {
    offset: u64,
    len: u64,
}

impl SectionHandle {
    fn from_span(start: usize, end: usize) -> Result<Self> {
        Ok(Self {
            offset: usize_to_u64(start, "section offset")?,
            len: usize_to_u64(end.saturating_sub(start), "section length")?,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct TableFooter {
    data_blocks: SectionHandle,
    range_tombstones: SectionHandle,
    filters: SectionHandle,
    indexes: SectionHandle,
    properties: SectionHandle,
}

struct EncodedTable {
    payload: Vec<u8>,
    payload_len: usize,
    footer: TableFooter,
    data_block_count: usize,
    index_partitions: Vec<IndexPartitionEntry>,
    pinned_index_partitions: BTreeMap<usize, Arc<Vec<TableDataBlock>>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct DataBlockIndexEntry {
    smallest_internal_key: InternalKey,
    largest_internal_key: InternalKey,
    block: BlockHandle,
    point_key_filter: Option<PointKeyFilter>,
    prefix_filter: Option<PrefixFilter>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct IndexPartitionEntry {
    smallest_internal_key: InternalKey,
    largest_internal_key: InternalKey,
    block: BlockHandle,
    first_data_block_index: usize,
    data_block_count: usize,
}

#[derive(Debug, Clone)]
pub(crate) struct DecodedDataBlock {
    bytes: Bytes,
    payload_range: Range<usize>,
    record_headers: Box<[DataBlockRecordHeader]>,
    restart_indices: Box<[u32]>,
    point_lookup_index: DataBlockPointLookupIndex,
}

impl DecodedDataBlock {
    fn from_records(records: &[TablePointRecord]) -> Result<Self> {
        decode_data_block(encode_data_block(records)?)
    }

    pub(crate) fn estimated_bytes(&self) -> u64 {
        let bytes = usize_to_u64_saturating(self.bytes.len());
        let record_headers = usize_to_u64_saturating(
            self.record_headers
                .len()
                .saturating_mul(size_of::<DataBlockRecordHeader>()),
        );
        let restarts =
            usize_to_u64_saturating(self.restart_indices.len().saturating_mul(size_of::<u32>()));
        bytes
            .saturating_add(record_headers)
            .saturating_add(restarts)
            .saturating_add(self.point_lookup_index.estimated_bytes())
            .max(1)
    }

    fn record_count(&self) -> usize {
        self.record_headers.len()
    }

    fn record_view(&self, index: usize) -> Result<DataBlockRecordView<'_>> {
        let payload = self.payload_bytes();
        self.record_headers
            .get(index)
            .ok_or_else(|| invalid_table("record index outside data block"))?
            .view(payload)
    }

    fn record_owned(&self, index: usize) -> Result<TablePointRecord> {
        self.record_view(index).map(DataBlockRecordView::to_owned)
    }

    fn point_value_record(&self, index: usize) -> Result<TablePointValueRecord> {
        let header = self
            .record_headers
            .get(index)
            .ok_or_else(|| invalid_table("record index outside data block"))?;
        let payload = self.payload_bytes();
        let record = header.view(payload)?;
        let value = match header.value {
            Some(ValueRefHeader::Inline { offset, len }) => {
                let range = inline_value_range(offset, len, payload.len())?;
                Some(PointValueSource::from_shared(
                    self.bytes.clone(),
                    self.payload_absolute_range(range)?,
                )?)
            }
            Some(value) => Some(PointValueSource::from_value_ref(
                value.view(payload)?.to_owned(),
            )),
            None => None,
        };

        Ok(TablePointValueRecord {
            internal_key: InternalKey::new(
                record.user_key.to_vec(),
                record.sequence,
                record.kind,
                record.batch_index,
            ),
            value,
        })
    }

    fn records_owned(&self) -> Result<Vec<TablePointRecord>> {
        (0..self.record_count())
            .map(|index| self.record_owned(index))
            .collect()
    }

    fn restart_indices_with_base(&self, base: usize) -> Vec<usize> {
        self.restart_indices
            .iter()
            .map(|index| base.saturating_add(u32_to_usize(*index)))
            .collect()
    }

    fn payload_bytes(&self) -> &[u8] {
        &self.bytes[self.payload_range.clone()]
    }

    fn payload_absolute_range(&self, range: Range<usize>) -> Result<Range<usize>> {
        let start = self
            .payload_range
            .start
            .checked_add(range.start)
            .ok_or_else(|| invalid_table("data block payload range overflow"))?;
        let end = self
            .payload_range
            .start
            .checked_add(range.end)
            .ok_or_else(|| invalid_table("data block payload range overflow"))?;
        if end > self.payload_range.end {
            return Err(invalid_table(
                "data block payload range outside shared bytes",
            ));
        }
        Ok(start..end)
    }

    #[cfg(test)]
    pub(crate) fn empty_for_cache_test() -> Self {
        Self {
            bytes: Bytes::new(),
            payload_range: 0..0,
            record_headers: Box::default(),
            restart_indices: Box::default(),
            point_lookup_index: DataBlockPointLookupIndex::empty(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct DataBlockPointLookupIndex {
    entries: Box<[BlockHashEntry]>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct BlockHashEntry {
    key_hash: u64,
    start_record: u32,
    end_record: u32,
}

impl DataBlockPointLookupIndex {
    #[cfg(test)]
    fn empty() -> Self {
        Self {
            entries: Box::default(),
        }
    }

    fn from_records(records: &[TablePointRecord]) -> Result<Self> {
        let mut entries = Vec::new();
        let mut start = 0;
        while start < records.len() {
            let key = records[start].internal_key.user_key();
            let mut end = start + 1;
            while end < records.len() && records[end].internal_key.user_key() == key {
                end += 1;
            }
            entries.push(BlockHashEntry {
                key_hash: user_key_hash(key),
                start_record: usize_to_u32(start, "data block hash range start")?,
                end_record: usize_to_u32(end, "data block hash range end")?,
            });
            start = end;
        }
        Ok(Self::from_entries(entries))
    }

    fn from_entries(mut entries: Vec<BlockHashEntry>) -> Self {
        entries.sort_unstable_by_key(|entry| (entry.key_hash, entry.start_record));
        Self {
            entries: entries.into_boxed_slice(),
        }
    }

    fn matching_entries(&self, key_hash: u64) -> &[BlockHashEntry] {
        let start = self
            .entries
            .partition_point(|entry| entry.key_hash < key_hash);
        let end = start + self.entries[start..].partition_point(|entry| entry.key_hash == key_hash);
        &self.entries[start..end]
    }

    fn estimated_bytes(&self) -> u64 {
        usize_to_u64_saturating(
            self.entries
                .len()
                .saturating_mul(size_of::<BlockHashEntry>()),
        )
    }
}

#[derive(Debug, Clone, Copy)]
struct DataBlockRecordHeader {
    record_offset: u32,
    record_end: u32,
    user_key_offset: u32,
    user_key_len: u32,
    sequence: Sequence,
    kind: ValueKind,
    batch_index: u32,
    value: Option<ValueRefHeader>,
}

impl DataBlockRecordHeader {
    fn view<'block>(&self, bytes: &'block [u8]) -> Result<DataBlockRecordView<'block>> {
        let record_start = u32_to_usize(self.record_offset);
        let record_end = u32_to_usize(self.record_end);
        if record_start >= record_end || record_end > bytes.len() {
            return Err(invalid_table("record header points outside data block"));
        }
        let user_key_end = self
            .user_key_offset
            .checked_add(self.user_key_len)
            .ok_or_else(|| invalid_table("record user key length overflows"))?;
        let user_key = bytes
            .get(u32_to_usize(self.user_key_offset)..u32_to_usize(user_key_end))
            .ok_or_else(|| invalid_table("record user key points outside data block"))?;
        let value = self.value.map(|value| value.view(bytes)).transpose()?;
        Ok(DataBlockRecordView {
            user_key,
            sequence: self.sequence,
            kind: self.kind,
            batch_index: self.batch_index,
            value,
        })
    }
}

#[derive(Debug, Clone, Copy)]
enum ValueRefHeader {
    Inline {
        offset: u32,
        len: u32,
    },
    BlobIndex(BlobIndex),
    Blob {
        file_id: u64,
        offset: u64,
        len: u64,
        checksum: u32,
    },
}

impl ValueRefHeader {
    fn view<'block>(&self, bytes: &'block [u8]) -> Result<ValueRefView<'block>> {
        match *self {
            Self::Inline { offset, len } => {
                let start = u32_to_usize(offset);
                let end = start
                    .checked_add(u32_to_usize(len))
                    .ok_or_else(|| invalid_table("inline value length overflows"))?;
                let bytes = bytes
                    .get(start..end)
                    .ok_or_else(|| invalid_table("inline value points outside data block"))?;
                Ok(ValueRefView::Inline(bytes))
            }
            Self::BlobIndex(index) => Ok(ValueRefView::BlobIndex(index)),
            Self::Blob {
                file_id,
                offset,
                len,
                checksum,
            } => Ok(ValueRefView::Blob {
                file_id,
                offset,
                len,
                checksum,
            }),
        }
    }
}

fn inline_value_range(offset: u32, len: u32, block_len: usize) -> Result<Range<usize>> {
    let start = u32_to_usize(offset);
    let end = start
        .checked_add(u32_to_usize(len))
        .ok_or_else(|| invalid_table("inline value length overflows"))?;
    if end > block_len {
        return Err(invalid_table("inline value points outside data block"));
    }
    Ok(start..end)
}

#[derive(Debug, Clone, Copy)]
struct DataBlockRecordView<'block> {
    user_key: &'block [u8],
    sequence: Sequence,
    kind: ValueKind,
    batch_index: u32,
    value: Option<ValueRefView<'block>>,
}

impl DataBlockRecordView<'_> {
    fn to_owned(self) -> TablePointRecord {
        TablePointRecord {
            internal_key: InternalKey::new(
                self.user_key.to_vec(),
                self.sequence,
                self.kind,
                self.batch_index,
            ),
            value: self.value.map(ValueRefView::to_owned),
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum ValueRefView<'block> {
    Inline(&'block [u8]),
    BlobIndex(BlobIndex),
    Blob {
        file_id: u64,
        offset: u64,
        len: u64,
        checksum: u32,
    },
}

impl ValueRefView<'_> {
    fn to_owned(self) -> ValueRef {
        match self {
            Self::Inline(bytes) => ValueRef::Inline(bytes.to_vec()),
            Self::BlobIndex(index) => ValueRef::BlobIndex(index),
            Self::Blob {
                file_id,
                offset,
                len,
                checksum,
            } => ValueRef::Blob {
                file_id,
                offset,
                len,
                checksum,
            },
        }
    }
}

struct BufferedBlockReadSource<'src> {
    bytes: &'src [u8],
}

impl BlockReadSource for BufferedBlockReadSource<'_> {
    fn read_exact_at(&self, offset: usize, bytes: &mut [u8]) -> Result<()> {
        let end = offset
            .checked_add(bytes.len())
            .ok_or_else(|| invalid_table("read offset overflow"))?;
        let source = self
            .bytes
            .get(offset..end)
            .ok_or_else(|| invalid_table("read past end"))?;
        bytes.copy_from_slice(source);
        Ok(())
    }

    fn read_exact_at_owned(&self, offset: usize, len: usize) -> Result<StorageReadBuffer> {
        let end = offset
            .checked_add(len)
            .ok_or_else(|| invalid_table("read offset overflow"))?;
        let bytes = self
            .bytes
            .get(offset..end)
            .ok_or_else(|| invalid_table("read past end"))?
            .to_vec();
        Ok(StorageReadBuffer::from_vec(offset, bytes))
    }
}

#[derive(Debug)]
struct TableFilterStats {
    shards: Box<[TableFilterStatsShard; TABLE_STAT_SHARDS]>,
}

#[derive(Debug)]
struct TableReadPathStats {
    shards: Box<[TableReadPathStatsShard; TABLE_STAT_SHARDS]>,
}

#[derive(Debug, Default)]
#[repr(align(64))]
struct TableReadPathStatsShard {
    point_table_probes: AtomicU64,
    point_l0_table_probes: AtomicU64,
    point_non_l0_table_probes: AtomicU64,
    point_index_partition_probes: AtomicU64,
    point_block_metadata_probes: AtomicU64,
    point_data_block_reads: AtomicU64,
    point_filter_misses: AtomicU64,
    range_table_probes: AtomicU64,
    range_l0_table_probes: AtomicU64,
    range_non_l0_table_probes: AtomicU64,
    range_tombstone_table_probes: AtomicU64,
    prefix_table_probes: AtomicU64,
    prefix_tombstone_table_probes: AtomicU64,
    prefix_block_metadata_probes: AtomicU64,
    prefix_data_block_reads: AtomicU64,
    prefix_filter_misses: AtomicU64,
}

#[derive(Debug, Default)]
#[repr(align(64))]
struct TableFilterStatsShard {
    table_point_hits: AtomicU64,
    table_point_misses: AtomicU64,
    table_point_false_positives: AtomicU64,
    table_prefix_hits: AtomicU64,
    table_prefix_misses: AtomicU64,
    table_prefix_false_positives: AtomicU64,
    block_point_hits: AtomicU64,
    block_point_misses: AtomicU64,
    block_point_false_positives: AtomicU64,
    block_prefix_hits: AtomicU64,
    block_prefix_misses: AtomicU64,
    block_prefix_false_positives: AtomicU64,
}

impl Default for TableReadPathStats {
    fn default() -> Self {
        Self {
            shards: Box::new(std::array::from_fn(|_| TableReadPathStatsShard::default())),
        }
    }
}

impl Default for TableFilterStats {
    fn default() -> Self {
        Self {
            shards: Box::new(std::array::from_fn(|_| TableFilterStatsShard::default())),
        }
    }
}

impl TableReadPathStats {
    fn snapshot(&self) -> ReadPathStats {
        let mut stats = ReadPathStats::default();
        for shard in self.shards.iter() {
            stats.point_table_probes = stats
                .point_table_probes
                .saturating_add(shard.point_table_probes.load(Ordering::Acquire));
            stats.point_l0_table_probes = stats
                .point_l0_table_probes
                .saturating_add(shard.point_l0_table_probes.load(Ordering::Acquire));
            stats.point_non_l0_table_probes = stats
                .point_non_l0_table_probes
                .saturating_add(shard.point_non_l0_table_probes.load(Ordering::Acquire));
            stats.point_index_partition_probes = stats
                .point_index_partition_probes
                .saturating_add(shard.point_index_partition_probes.load(Ordering::Acquire));
            stats.point_block_metadata_probes = stats
                .point_block_metadata_probes
                .saturating_add(shard.point_block_metadata_probes.load(Ordering::Acquire));
            stats.point_data_block_reads = stats
                .point_data_block_reads
                .saturating_add(shard.point_data_block_reads.load(Ordering::Acquire));
            stats.point_filter_misses = stats
                .point_filter_misses
                .saturating_add(shard.point_filter_misses.load(Ordering::Acquire));
            stats.range_table_probes = stats
                .range_table_probes
                .saturating_add(shard.range_table_probes.load(Ordering::Acquire));
            stats.range_l0_table_probes = stats
                .range_l0_table_probes
                .saturating_add(shard.range_l0_table_probes.load(Ordering::Acquire));
            stats.range_non_l0_table_probes = stats
                .range_non_l0_table_probes
                .saturating_add(shard.range_non_l0_table_probes.load(Ordering::Acquire));
            stats.range_tombstone_table_probes = stats
                .range_tombstone_table_probes
                .saturating_add(shard.range_tombstone_table_probes.load(Ordering::Acquire));
            stats.prefix_table_probes = stats
                .prefix_table_probes
                .saturating_add(shard.prefix_table_probes.load(Ordering::Acquire));
            stats.prefix_tombstone_table_probes = stats
                .prefix_tombstone_table_probes
                .saturating_add(shard.prefix_tombstone_table_probes.load(Ordering::Acquire));
            stats.prefix_block_metadata_probes = stats
                .prefix_block_metadata_probes
                .saturating_add(shard.prefix_block_metadata_probes.load(Ordering::Acquire));
            stats.prefix_data_block_reads = stats
                .prefix_data_block_reads
                .saturating_add(shard.prefix_data_block_reads.load(Ordering::Acquire));
            stats.prefix_filter_misses = stats
                .prefix_filter_misses
                .saturating_add(shard.prefix_filter_misses.load(Ordering::Acquire));
        }
        stats
    }

    fn record_point_table_probe(&self, level: TableLevel) {
        let shard = self.shard();
        shard.point_table_probes.fetch_add(1, Ordering::Relaxed);
        if level == TableLevel::ZERO {
            shard.point_l0_table_probes.fetch_add(1, Ordering::Relaxed);
        } else {
            shard
                .point_non_l0_table_probes
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    fn record_point_index_partition_probe(&self) {
        self.shard()
            .point_index_partition_probes
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_point_block_metadata_probe(&self) {
        self.shard()
            .point_block_metadata_probes
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_point_data_block_read(&self) {
        self.shard()
            .point_data_block_reads
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_point_filter_miss(&self) {
        self.shard()
            .point_filter_misses
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_range_table_probe(&self, level: TableLevel) {
        let shard = self.shard();
        shard.range_table_probes.fetch_add(1, Ordering::Relaxed);
        if level == TableLevel::ZERO {
            shard.range_l0_table_probes.fetch_add(1, Ordering::Relaxed);
        } else {
            shard
                .range_non_l0_table_probes
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    fn record_range_tombstone_table_probe(&self) {
        self.shard()
            .range_tombstone_table_probes
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_prefix_table_probe(&self) {
        self.shard()
            .prefix_table_probes
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_prefix_tombstone_table_probe(&self) {
        self.shard()
            .prefix_tombstone_table_probes
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_prefix_block_metadata_probe(&self) {
        self.shard()
            .prefix_block_metadata_probes
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_prefix_data_block_read(&self) {
        self.shard()
            .prefix_data_block_reads
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_prefix_filter_miss(&self) {
        self.shard()
            .prefix_filter_misses
            .fetch_add(1, Ordering::Relaxed);
    }

    fn shard(&self) -> &TableReadPathStatsShard {
        &self.shards[table_stat_shard_index()]
    }
}

impl TableFilterStats {
    fn snapshot(&self) -> FilterStats {
        let mut stats = FilterStats::default();
        for shard in self.shards.iter() {
            stats.table_point_hits = stats
                .table_point_hits
                .saturating_add(shard.table_point_hits.load(Ordering::Acquire));
            stats.table_point_misses = stats
                .table_point_misses
                .saturating_add(shard.table_point_misses.load(Ordering::Acquire));
            stats.table_point_false_positives = stats
                .table_point_false_positives
                .saturating_add(shard.table_point_false_positives.load(Ordering::Acquire));
            stats.table_prefix_hits = stats
                .table_prefix_hits
                .saturating_add(shard.table_prefix_hits.load(Ordering::Acquire));
            stats.table_prefix_misses = stats
                .table_prefix_misses
                .saturating_add(shard.table_prefix_misses.load(Ordering::Acquire));
            stats.table_prefix_false_positives = stats
                .table_prefix_false_positives
                .saturating_add(shard.table_prefix_false_positives.load(Ordering::Acquire));
            stats.block_point_hits = stats
                .block_point_hits
                .saturating_add(shard.block_point_hits.load(Ordering::Acquire));
            stats.block_point_misses = stats
                .block_point_misses
                .saturating_add(shard.block_point_misses.load(Ordering::Acquire));
            stats.block_point_false_positives = stats
                .block_point_false_positives
                .saturating_add(shard.block_point_false_positives.load(Ordering::Acquire));
            stats.block_prefix_hits = stats
                .block_prefix_hits
                .saturating_add(shard.block_prefix_hits.load(Ordering::Acquire));
            stats.block_prefix_misses = stats
                .block_prefix_misses
                .saturating_add(shard.block_prefix_misses.load(Ordering::Acquire));
            stats.block_prefix_false_positives = stats
                .block_prefix_false_positives
                .saturating_add(shard.block_prefix_false_positives.load(Ordering::Acquire));
        }
        stats
    }

    fn record_table_point(&self, allowed: bool) {
        let shard = self.shard();
        record_filter_result(&shard.table_point_hits, &shard.table_point_misses, allowed);
    }

    fn record_table_prefix(&self, allowed: bool) {
        let shard = self.shard();
        record_filter_result(
            &shard.table_prefix_hits,
            &shard.table_prefix_misses,
            allowed,
        );
    }

    fn record_block_point(&self, allowed: bool) {
        let shard = self.shard();
        record_filter_result(&shard.block_point_hits, &shard.block_point_misses, allowed);
    }

    fn record_block_prefix(&self, allowed: bool) {
        let shard = self.shard();
        record_filter_result(
            &shard.block_prefix_hits,
            &shard.block_prefix_misses,
            allowed,
        );
    }

    fn record_table_point_false_positive(&self) {
        self.shard()
            .table_point_false_positives
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_block_point_false_positive(&self) {
        self.shard()
            .block_point_false_positives
            .fetch_add(1, Ordering::Relaxed);
    }

    fn record_block_prefix_false_positive(&self) {
        self.shard()
            .block_prefix_false_positives
            .fetch_add(1, Ordering::Relaxed);
    }

    fn shard(&self) -> &TableFilterStatsShard {
        &self.shards[table_stat_shard_index()]
    }
}

fn table_stat_shard_index() -> usize {
    TABLE_STAT_SHARD.with(|index| *index)
}

fn record_filter_result(hits: &AtomicU64, misses: &AtomicU64, allowed: bool) {
    if allowed {
        hits.fetch_add(1, Ordering::Relaxed);
    } else {
        misses.fetch_add(1, Ordering::Relaxed);
    }
}

// Loaded in-memory tables keep one sorted record array. Persistent table
// blocks keep only key bounds and file handles here; the data-block cache owns
// the compact block bytes after a block is read.
#[derive(Debug, Clone)]
pub(crate) struct TableDataBlock {
    smallest_internal_key: InternalKey,
    largest_internal_key: InternalKey,
    block: BlockHandle,
    record_range: Range<usize>,
    point_key_filter: Option<PointKeyFilter>,
    prefix_filter: Option<PrefixFilter>,
}

impl TableDataBlock {
    fn from_record_range(
        point_records: &[TablePointRecord],
        record_range: Range<usize>,
        restart_indices: &[usize],
        point_key_filter: Option<PointKeyFilter>,
        prefix_filter: Option<PrefixFilter>,
    ) -> Result<Self> {
        Self::from_record_range_and_block(
            point_records,
            record_range,
            restart_indices,
            BlockHandle { offset: 0, len: 0 },
            point_key_filter,
            prefix_filter,
        )
    }

    fn from_record_range_and_block(
        point_records: &[TablePointRecord],
        record_range: Range<usize>,
        restart_indices: &[usize],
        block: BlockHandle,
        point_key_filter: Option<PointKeyFilter>,
        prefix_filter: Option<PrefixFilter>,
    ) -> Result<Self> {
        let records = point_records
            .get(record_range.clone())
            .ok_or_else(|| invalid_table("data block record range outside table"))?;
        if records.is_empty() {
            return Err(invalid_table("empty data block"));
        }
        if restart_indices.first().copied() != Some(record_range.start) {
            return Err(invalid_table(
                "data block first restart is not first record",
            ));
        }
        for restart_index in restart_indices {
            if !record_range.contains(restart_index) {
                return Err(invalid_table("data block restart outside record range"));
            }
        }
        let first = records
            .first()
            .ok_or_else(|| invalid_table("empty data block"))?;
        let last = records
            .last()
            .ok_or_else(|| invalid_table("empty data block"))?;

        Ok(Self {
            smallest_internal_key: first.internal_key.clone(),
            largest_internal_key: last.internal_key.clone(),
            block,
            record_range,
            point_key_filter,
            prefix_filter,
        })
    }

    fn from_index_entry(entry: DataBlockIndexEntry) -> Result<Self> {
        if entry.smallest_internal_key > entry.largest_internal_key {
            return Err(Error::Corruption {
                message: "data block index key bounds are inverted".to_owned(),
            });
        }

        Ok(Self {
            smallest_internal_key: entry.smallest_internal_key,
            largest_internal_key: entry.largest_internal_key,
            block: entry.block,
            record_range: 0..0,
            point_key_filter: entry.point_key_filter,
            prefix_filter: entry.prefix_filter,
        })
    }

    fn overlaps_range(&self, range: &KeyRange) -> bool {
        !key_is_after_end(self.smallest_internal_key.user_key(), &range.end)
            && !key_is_before_start(self.largest_internal_key.user_key(), &range.start)
    }

    fn key_bounds_may_contain(&self, key: &[u8]) -> bool {
        self.smallest_internal_key.user_key() <= key && key <= self.largest_internal_key.user_key()
    }

    fn point_filter_result(&self, key: &[u8]) -> Option<bool> {
        self.point_key_filter
            .as_ref()
            .map(|filter| filter.may_contain_key(key))
    }

    fn prefix_filter_result(&self, prefix: &[u8], extractor: &PrefixExtractor) -> Option<bool> {
        let filter = self.prefix_filter.as_ref()?;
        if filter.extractor() != extractor {
            return None;
        }
        let filter_prefix = extractor.query_filter_prefix(prefix)?;
        Some(filter.may_contain_prefix(filter_prefix))
    }

    fn prefix_bounds_may_overlap(&self, prefix: &[u8]) -> bool {
        self.largest_internal_key.user_key() >= prefix
            && (self.smallest_internal_key.user_key().starts_with(prefix)
                || self.smallest_internal_key.user_key() <= prefix)
    }

    pub(crate) fn estimated_bytes(&self) -> u64 {
        usize_to_u64_saturating(size_of::<Self>())
            .saturating_add(usize_to_u64_saturating(
                self.smallest_internal_key.user_key().len(),
            ))
            .saturating_add(usize_to_u64_saturating(
                self.largest_internal_key.user_key().len(),
            ))
            .saturating_add(self.filter_bytes())
    }

    fn filter_bytes(&self) -> u64 {
        let point = self
            .point_key_filter
            .as_ref()
            .map_or(0, |filter| usize_to_u64_saturating(filter.bytes().len()));
        let prefix = self
            .prefix_filter
            .as_ref()
            .map_or(0, |filter| usize_to_u64_saturating(filter.bytes().len()));
        point.saturating_add(prefix)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TableRangeTombstone {
    pub(crate) range: KeyRange,
    pub(crate) sequence: Sequence,
    pub(crate) batch_index: u32,
}

impl RangeTombstoneLike for TableRangeTombstone {
    fn range(&self) -> &KeyRange {
        &self.range
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Table {
    path: Option<PathBuf>,
    file: Option<Arc<NativeFileObject>>,
    payload_len: usize,
    footer: TableFooter,
    properties: TableProperties,
    point_records: Option<Vec<TablePointRecord>>,
    data_blocks: Option<Vec<TableDataBlock>>,
    data_block_count: usize,
    index_partitions: Vec<IndexPartitionEntry>,
    index_partition_cache: Arc<RwLock<BTreeMap<usize, Arc<Vec<TableDataBlock>>>>>,
    range_tombstones: Arc<RwLock<Option<Arc<RangeTombstoneIndex<TableRangeTombstone>>>>>,
    may_have_range_tombstones: bool,
    point_key_filter: Option<PointKeyFilter>,
    prefix_filter: Option<PrefixFilter>,
    filter_stats: Arc<TableFilterStats>,
    read_path_stats: Arc<TableReadPathStats>,
}

mod block_access;
mod cursor;
mod format;
mod metadata;
mod read;

pub(crate) use cursor::*;
use format::{
    decode_data_block, decode_filter_block, decode_index_block, decode_index_top_level,
    decode_properties_block, decode_range_tombstone_block, decode_table_bytes, empty_footer,
    encode_data_block, encode_table_for_write, invalid_table, key_is_after_end,
    key_is_before_start, point_record_encoded_len, read_checked_block_from_source_shared,
    read_checked_block_from_storage_object_shared_async, read_data_block_from_file,
    read_data_block_from_file_async, read_data_block_from_source,
    read_first_block_in_section_from_source_shared, read_footer_from_source,
    read_single_block_section_from_file_shared, read_single_block_section_from_file_shared_async,
    read_single_block_section_from_source_shared, read_u16_at, read_u32_at, u32_to_usize,
    user_key_hash, usize_to_u32, usize_to_u64, usize_to_u64_saturating, validate_block_codec,
    validate_footer_sections_by_len, validate_index_partition, validate_index_top_level,
    validate_index_top_level_codec, validate_sorted_point_records, validate_table_filters_for_key,
};
pub(crate) use metadata::*;
#[cfg(test)]
mod tests;