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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026-present, Structured World Foundation
//! Seekable source iterator trait for the merge-iterator path.
//!
//! `MergeSource` is what `SeekingMerger` consumes: a stream that
//! can be advanced in either direction, AND that exposes a key-
//! addressed [`MergeSource::seek`] primitive whose strength varies
//! by source flavour. Independent-cursor sources MUST treat `seek`
//! as a real reposition (the only way they can maintain the
//! no-duplicates invariant under mixed direction); coherent-cursor
//! sources (see [`CoherentMergeSource`]) MAY treat it as a no-op
//! because their shared `next`/`next_back` cursor discipline
//! already enforces no-duplicates without external reseek (the
//! `CoherentIterSource` adapter ships such a no-op `seek`).
//!
//! `SeekingMerger` itself does NOT invoke [`MergeSource::seek`] on
//! direction switches — its two-tree architecture relies on each
//! source's own `(front_idx, back_idx)` window (or equivalent
//! self-coordination between `next` and `next_back`) to keep mixed
//! direction duplicate-free. The seek primitive is exposed on the
//! trait for user-initiated repositioning (range scan starting
//! key, jump to a known key, etc.), not as a direction-switch
//! reconciliation mechanism.
//!
//! # Contract
//!
//! - [`MergeSource::next`] yields items in ascending `InternalKey` order
//! from the source's "front" cursor.
//! - [`MergeSource::next_back`] yields items in descending order from the
//! source's "back" cursor.
//! - [`MergeSource::seek`] guarantees depend on the source's cursor model:
//!
//! **Independent-cursor sources** (LSM SST scanners, `RunReader`s,
//! where calling `next` repeatedly does NOT advance the back
//! cursor and vice versa) — seek MUST reposition so that:
//! * the next `next()` yields the first item with `key >= target`
//! * the next `next_back()` yields the first item with `key < target`
//!
//! If no such item exists in a direction, that direction returns
//! `None` on the next call. NOTE: `SeekingMerger` does NOT call
//! `seek` on direction switches — its two-tree architecture
//! relies on the source's own `(front_idx, back_idx)` window
//! (or equivalent self-coordination) to keep mixed direction
//! duplicate-free. `seek` is exposed here for user-initiated
//! repositioning (range scan starting key, etc.).
//!
//! **Coherent-cursor sources** (those marked [`CoherentMergeSource`]
//! — `alloc::vec::IntoIter`, `alloc::collections::VecDeque`, range
//! iterators from `alloc::collections::BTreeMap`, and our
//! `CoherentIterSource` wrapper) — seek MAY be a no-op. The shared
//! front/back cursor discipline already maintains the "no item
//! yielded twice" invariant for mixed-direction consumption
//! without any explicit reposition; the no-op satisfies the
//! contract trivially.
//!
//! # Why a custom trait, not just `DoubleEndedIterator + Seek`
//!
//! Rust's standard library has no `Seek` trait for iterators
//! (`io::Seek` is for byte streams). We need a domain-specific
//! seek that targets an `InternalKey`. Combining this with
//! `DoubleEndedIterator` would over-constrain the trait: not every
//! `MergeSource` impl needs the full `DoubleEndedIterator` API
//! surface, and adapter wrappers (filter, map) need their own
//! seek handling. Keeping `next` / `next_back` / `seek` as three
//! distinct methods on one trait keeps the wrapper story simple.
use crate::;
use Box;
/// What every merge source yields.
pub type IterItem = crateResult;
/// A source iterator consumable from either end, repositionable by
/// key. Used by `SeekingMerger` (RocksDB-style merging iterator with
/// O(n log shard) direction switches and O(log n) per-step
/// `replace_*`).
/// Marker that promises a `MergeSource` impl's `next()` and
/// `next_back()` will never yield the same item twice under mixed
/// forward/backward consumption — regardless of HOW the impl
/// achieves that.
///
/// Two flavours qualify:
///
/// - **Literally shared cursor state**: `alloc::vec::IntoIter`,
/// `alloc::collections::VecDeque`, range iterators from
/// `alloc::collections::BTreeMap`. Their `DoubleEndedIterator`
/// impls shrink a single remaining range from both ends.
/// `CoherentIterSource` wraps these.
///
/// - **Self-coordinating index window**: an impl backed by a
/// sorted buffer plus `(front_idx, back_idx)` pointers that
/// refuses to yield once `front_idx >= back_idx`. SST scanners
/// and `RunReader`-style impls qualify ONLY IF they actually
/// enforce a single shrinking window — i.e. `next()` advances
/// `front_idx` and `next_back()` retreats `back_idx`, and either
/// refuses to yield when the indices cross. An impl with two
/// genuinely independent cursors (each side has its own offset
/// that the other never reads) is NOT coherent and MUST NOT
/// implement this marker, even though it might happen to behave
/// correctly under a specific consumption pattern.
///
/// **What this marker gates:** `SeekingMerger`'s
/// `DoubleEndedIterator` impl is bounded on this trait — sources
/// without the promise cannot use mixed direction through the
/// merger.
///
/// **What this marker says about `seek`:** the no-duplicates
/// promise covers mixed `next` / `next_back` consumption WITHOUT
/// an intervening user-initiated `seek`. A user `seek` is an
/// explicit reposition: observing previously-yielded items after
/// a seek is expected behaviour, not a contract violation. Impls
/// are free to hard-reset their cursors on `seek` — a production
/// independent-cursor source typically will. The marker promise
/// re-engages on the next forward / backward step pair.
/// Pass-through impl so callers can build `Vec<Box<dyn MergeSource +
/// 'a>>` and pass that to `SeekingMerger`. Each method just
/// dispatches to the inner trait object.
/// Adapter that wraps any `DoubleEndedIterator<Item = IterItem>` as
/// a `MergeSource` with a no-op `seek`.
///
/// Useful for source iterators whose front and back cursors share
/// state natively (`alloc::vec::IntoIter`,
/// `alloc::collections::VecDeque`, range iterators from
/// `alloc::collections::BTreeMap`)
/// — those don't need seek to maintain the "no item yielded twice"
/// invariant under mixed forward/backward consumption.
///
/// For source iterators with independent front/back cursors (LSM
/// SST scanners), use a dedicated `MergeSource` impl that uses
/// real `seek` instead of this adapter.