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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024-present, fjall-rs
// Copyright (c) 2026-present, Structured World Foundation
use alloc::boxed::Box;
use alloc::sync::Arc;
use super::{Block, DataBlock};
use crate::io::BufReader;
use crate::path::Path;
use crate::{
CompressionType, InternalValue, SeqNo,
comparator::SharedComparator,
encryption::EncryptionProvider,
fs::{FileHint, Fs, FsFile, FsOpenOptions},
table::{block::BlockType, iter::OwnedDataBlockIter},
};
/// Table reader that is optimized for consuming an entire table
pub struct Scanner {
reader: BufReader<Box<dyn FsFile>>,
iter: OwnedDataBlockIter,
compression: CompressionType,
block_count: usize,
read_count: usize,
global_seqno: SeqNo,
encryption: Option<Arc<dyn EncryptionProvider>>,
comparator: SharedComparator,
/// Per-SST Page-ECC scheme from table metadata: data blocks omit the
/// `block_flags` byte, so the scanner's block-read transform is told
/// here the parity scheme (sizing + recovery). `None` = no parity.
ecc: Option<crate::table::block::EccParams>,
/// Per-SST per-KV-footer flag (`kv_checksum_algo.is_some()`): supplied to
/// `from_loaded` so it strips the footer (data blocks omit the byte).
has_kv_footer: bool,
#[cfg(zstd_any)]
zstd_dictionary: Option<Arc<crate::compression::ZstdDictionary>>,
/// Table id of the SST being scanned; threaded through to
/// per-block reads via `BlockIdentity`.
table_id: crate::TableId,
/// Whether this SST stores columnar (PAX) data blocks. When set, each
/// fetched block is read as `BlockType::Columnar` and reconstructed into a
/// row-major block so the scan iterator is unchanged.
columnar: bool,
/// Data-block restart interval, used to re-encode a reconstructed columnar
/// block (matches the value the SST recorded).
restart_interval: u8,
}
impl Scanner {
#[expect(
clippy::too_many_arguments,
reason = "scanner ctor takes one local per piece of state it needs to thread \
through fetch_next_block; collapsing them into a config struct would \
add an indirection without removing any per-call decision the caller \
makes about the values"
)]
pub fn new(
fs: &Arc<dyn Fs>,
path: &Path,
block_count: usize,
compression: CompressionType,
global_seqno: SeqNo,
encryption: Option<Arc<dyn EncryptionProvider>>,
ecc: Option<crate::table::block::EccParams>,
has_kv_footer: bool,
#[cfg(zstd_any)] zstd_dictionary: Option<Arc<crate::compression::ZstdDictionary>>,
comparator: SharedComparator,
table_id: crate::TableId,
columnar: bool,
restart_interval: u8,
) -> crate::Result<Self> {
// 2 MiB buffer matches RocksDB's `compaction_readahead_size`
// default and is large enough that the kernel can fold the
// sequential-scan readahead heuristic into a single big
// userspace fill per ~500 typical (4 KiB) data blocks instead
// of one syscall every 8 blocks. Picked to dominate any
// pre-#133-Phase1c micro-cost: the loss is at most a few MB
// of allocator overhead per concurrent compaction, and
// compaction concurrency is bounded by the scheduler. HDD-
// tuning beyond this would benefit from a configurable knob,
// tracked as the configurable-readahead follow-up under #133.
const SCANNER_READAHEAD_BYTES: usize = 2 * 1024 * 1024;
let file = fs.open(path, &FsOpenOptions::new().read(true))?;
// The scanner walks every block in order — tell the kernel so
// it can ramp readahead aggressively and evict already-read
// pages instead of pinning them. Best-effort: hint() is
// advisory and any failure here would just leave the kernel
// on the default heuristic, so drop the error rather than
// failing the open.
let _ = file.hint(FileHint::Sequential);
let mut reader = BufReader::with_capacity(SCANNER_READAHEAD_BYTES, file);
let block = Self::fetch_next_block(
&mut reader,
table_id,
compression,
encryption.as_deref(),
ecc,
has_kv_footer,
columnar,
restart_interval,
#[cfg(zstd_any)]
zstd_dictionary.as_deref(),
)?;
let cmp = comparator.clone();
let iter = OwnedDataBlockIter::try_new(block, |b| b.try_iter(cmp))?;
Ok(Self {
reader,
iter,
compression,
block_count,
read_count: 1,
global_seqno,
encryption,
comparator,
ecc,
has_kv_footer,
#[cfg(zstd_any)]
zstd_dictionary,
table_id,
columnar,
restart_interval,
})
}
#[expect(
clippy::too_many_arguments,
reason = "per-block read threads each SST-level read parameter through; a config struct would add indirection without removing a caller decision"
)]
fn fetch_next_block(
reader: &mut BufReader<Box<dyn FsFile>>,
table_id: crate::TableId,
compression: CompressionType,
encryption: Option<&dyn EncryptionProvider>,
ecc: Option<crate::table::block::EccParams>,
has_kv_footer: bool,
columnar: bool,
restart_interval: u8,
#[cfg(zstd_any)] zstd_dict: Option<&crate::compression::ZstdDictionary>,
) -> crate::Result<DataBlock> {
// A columnar SST's blocks are tagged (and AAD-bound) as Columnar; read
// them under that identity so verification / decryption matches.
let block_type = if columnar {
BlockType::Columnar
} else {
BlockType::Data
};
let block = Block::from_reader(
reader,
crate::table::block::BlockIdentity {
table_id,
block_type,
dict_id: compression.dict_id(),
window_log: 0,
},
&{
// SST blocks omit the block_flags byte, so ECC presence is a
// per-SST property: upgrade the transform to its `*Ecc`
// variant when this table was written with Page ECC, so the
// reader expects the parity trailer. Identity without the
// `page_ecc` feature.
let t = crate::table::block::BlockTransform::from_parts(
compression,
encryption,
#[cfg(zstd_any)]
zstd_dict,
)?;
if let Some(ecc) = ecc {
t.with_ecc(ecc)
} else {
t
}
},
);
match block {
Ok(block) => {
// Columnar blocks are reconstructed into row blocks; a row block
// must be BlockType::Data. `from_loaded` strips the per-KV
// checksum footer when this SST carries one (per-SST
// `has_kv_footer`, since data blocks omit the byte).
if columnar {
return Self::reconstruct_columnar(&block, restart_interval);
}
if block.header.block_type != BlockType::Data {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
DataBlock::from_loaded(block, has_kv_footer)
}
Err(e) => Err(e),
}
}
/// Reconstructs a row-major [`DataBlock`] from a loaded columnar block. With
/// the `columnar` feature this re-encodes the PAX block; without the feature
/// a columnar SST cannot be read.
fn reconstruct_columnar(block: &Block, restart_interval: u8) -> crate::Result<DataBlock> {
#[cfg(feature = "columnar")]
{
DataBlock::from_columnar_block(&block.data, restart_interval)
}
#[cfg(not(feature = "columnar"))]
{
let _ = (block, restart_interval);
Err(crate::Error::FeatureUnsupported("columnar"))
}
}
}
impl Iterator for Scanner {
type Item = crate::Result<InternalValue>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(mut item) = self.iter.next() {
item.key.seqno += self.global_seqno;
return Some(Ok(item));
}
if self.read_count >= self.block_count {
return None;
}
// Init new block
let block = match Self::fetch_next_block(
&mut self.reader,
self.table_id,
self.compression,
self.encryption.as_deref(),
self.ecc,
self.has_kv_footer,
self.columnar,
self.restart_interval,
#[cfg(zstd_any)]
self.zstd_dictionary.as_deref(),
) {
Ok(block) => block,
Err(e) => {
self.read_count = self.block_count;
return Some(Err(e));
}
};
let cmp = self.comparator.clone();
match OwnedDataBlockIter::try_new(block, |b| b.try_iter(cmp)) {
Ok(iter) => {
self.iter = iter;
self.read_count += 1;
}
Err(e) => {
// Poison the scanner so callers cannot silently skip
// the corrupt block and resume on later blocks.
self.read_count = self.block_count;
return Some(Err(e));
}
}
}
}
}