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
// Copyright (c) 2025-present, fjall-rs
// This source code is licensed under both the Apache 2.0 and MIT License
// (found in the LICENSE-* files in the repository)
use crate::{
InternalValue, SeqNo,
comparator::SharedComparator,
double_ended_peekable::{DoubleEndedPeekable, DoubleEndedPeekableExt},
table::{
block::{Decoder, ParsedItem},
data_block::DataBlockParsedItem,
},
};
/// The data block iterator handles double-ended scans over a data block
pub struct Iter<'a> {
bytes: &'a [u8],
decoder:
DoubleEndedPeekable<DataBlockParsedItem, Decoder<'a, InternalValue, DataBlockParsedItem>>,
comparator: SharedComparator,
}
impl<'a> Iter<'a> {
/// Creates a new iterator over a data block.
#[must_use]
pub fn new(
bytes: &'a [u8],
decoder: Decoder<'a, InternalValue, DataBlockParsedItem>,
comparator: SharedComparator,
) -> Self {
let decoder = decoder.double_ended_peekable();
Self {
bytes,
decoder,
comparator,
}
}
/// Seek the iterator to an byte offset.
///
/// This is used when the hash index returns a hit.
pub fn seek_to_offset(&mut self, offset: usize) -> bool {
self.decoder.inner_mut().set_lo_offset(offset);
true
}
/// Seeks to the last restart interval whose head key is strictly below the
/// target `needle`, or equal to it with a seqno that is at least the given
/// snapshot boundary.
///
/// Here `seqno` is a snapshot boundary: point reads return the first item
/// with `item.seqno < seqno`. Using the internal key ordering
/// (`user_key` ASC, `seqno` DESC), this skips restart intervals that can only
/// contain versions newer than the snapshot, so any visible version for
/// `needle` will be found within roughly one restart interval of the
/// resulting position.
pub fn seek_to_key_seqno(&mut self, needle: &[u8], seqno: SeqNo) -> bool {
// Lex fast path: `<[u8]>::cmp` is statically dispatched and inlinable
// — no vtable lookup per probe. The custom-comparator branch retains
// the existing `dyn UserComparator::compare` call. Each closure has a
// distinct type, so `Decoder::seek` monomorphizes both shapes and the
// inner binary-search loop is virtual-call-free on the lex path.
let cmp = &self.comparator;
if cmp.is_lexicographic() {
self.decoder.inner_mut().seek(
|head_key, head_seqno| match head_key.cmp(needle) {
std::cmp::Ordering::Less => true,
std::cmp::Ordering::Equal => head_seqno >= seqno,
std::cmp::Ordering::Greater => false,
},
false,
)
} else {
self.decoder.inner_mut().seek(
|head_key, head_seqno| match cmp.compare(head_key, needle) {
std::cmp::Ordering::Less => true,
std::cmp::Ordering::Equal => head_seqno >= seqno,
std::cmp::Ordering::Greater => false,
},
false,
)
}
}
pub fn seek(&mut self, needle: &[u8], seqno: SeqNo) -> bool {
// Reuse the seqno-aware binary search from `seek_to_key_seqno`, then
// follow up with a linear scan to position at the exact key.
if !self.seek_to_key_seqno(needle, seqno) {
return false;
}
// TODO: make sure we only linear scan over the current restart interval
// TODO: if we do more steps, something has gone wrong with the seek probably, maybe...?
// Linear scan
loop {
let Some(item) = self.decoder.peek() else {
return false;
};
match item.compare_key(needle, self.bytes, self.comparator.as_ref()) {
std::cmp::Ordering::Equal => {
return true;
}
std::cmp::Ordering::Greater => {
return false;
}
std::cmp::Ordering::Less => {
// Continue
#[expect(
clippy::expect_used,
reason = "we peeked a value successfully, so there must be a next item in the stream"
)]
self.decoder.next().expect("should exist");
}
}
}
}
/// Reverse inclusive seek: position at the last key `<= needle`.
///
/// `seqno` is accepted for API uniformity with the forward seek methods
/// ([`seek`], [`seek_exclusive`]) but is **intentionally unused** here.
/// Backward binary search cannot leverage seqno because intervals are
/// visited from the selected one toward index 0 — a tighter predicate
/// would skip intervals that may contain the visible version.
pub fn seek_upper(&mut self, needle: &[u8], _seqno: SeqNo) -> bool {
let cmp = &self.comparator;
let landed = if cmp.is_lexicographic() {
self.decoder
.inner_mut()
.seek_upper(|head_key, _| head_key <= needle, false)
} else {
self.decoder.inner_mut().seek_upper(
|head_key, _| cmp.compare(head_key, needle) != std::cmp::Ordering::Greater,
false,
)
};
if !landed {
return false;
}
// Linear scan
loop {
let Some(item) = self.decoder.peek_back() else {
return false;
};
match item.compare_key(needle, self.bytes, self.comparator.as_ref()) {
std::cmp::Ordering::Equal => {
return true;
}
std::cmp::Ordering::Less => {
return false;
}
std::cmp::Ordering::Greater => {
// Continue
#[expect(
clippy::expect_used,
reason = "we peeked a value successfully, so there must be a next item in the stream"
)]
self.decoder.next_back().expect("should exist");
}
}
}
}
pub fn seek_exclusive(&mut self, needle: &[u8], seqno: SeqNo) -> bool {
// Exclusive lower bound: same seqno-aware binary search, but the linear
// scan below skips entries equal to `needle`.
if !self.seek_to_key_seqno(needle, seqno) {
return false;
}
loop {
let Some(item) = self.decoder.peek() else {
return false;
};
match item.compare_key(needle, self.bytes, self.comparator.as_ref()) {
std::cmp::Ordering::Greater => {
return true;
}
std::cmp::Ordering::Equal | std::cmp::Ordering::Less => {
#[expect(
clippy::expect_used,
reason = "we peeked a value successfully, so there must be a next item in the stream"
)]
self.decoder.next().expect("should exist");
}
}
}
}
/// Reverse exclusive seek: position at the last key `< needle`.
///
/// See [`seek_upper`] for why `seqno` is accepted but unused in reverse
/// seeks.
pub fn seek_upper_exclusive(&mut self, needle: &[u8], _seqno: SeqNo) -> bool {
let cmp = &self.comparator;
let landed = if cmp.is_lexicographic() {
self.decoder
.inner_mut()
.seek_upper(|head_key, _| head_key <= needle, false)
} else {
self.decoder.inner_mut().seek_upper(
|head_key, _| cmp.compare(head_key, needle) != std::cmp::Ordering::Greater,
false,
)
};
if !landed {
return false;
}
loop {
let Some(item) = self.decoder.peek_back() else {
return false;
};
match item.compare_key(needle, self.bytes, self.comparator.as_ref()) {
std::cmp::Ordering::Less => {
return true;
}
std::cmp::Ordering::Equal | std::cmp::Ordering::Greater => {
#[expect(
clippy::expect_used,
reason = "we peeked a value successfully, so there must be a next item in the stream"
)]
self.decoder.next_back().expect("should exist");
}
}
}
}
}
impl Iterator for Iter<'_> {
type Item = DataBlockParsedItem;
fn next(&mut self) -> Option<Self::Item> {
self.decoder.next()
}
}
impl DoubleEndedIterator for Iter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.decoder.next_back()
}
}