fastskip 0.1.1

Lock-free arena-backed skip list memtable for LSM-tree storage engines
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
//! Iterator, snapshot, and cursor types for traversing the skip list.
//!
//! This module provides three traversal modes:
//!
//! - [`Iter`] — live iterator that reflects the current state, including
//!   concurrent inserts that happen mid-iteration.
//! - [`Snapshot`] / [`SnapshotIter`] — point-in-time view captured at creation.
//!   Nodes inserted after the snapshot are invisible to the iterator.
//! - [`Cursor`] — seekable forward cursor for range scans. Supports lower-bound
//!   seek and manual advancement via [`next_entry`](Cursor::next_entry).
//!
//! All iterators yield [`Entry`] values containing the key, value, and a
//! tombstone flag indicating whether the entry was deleted.

use std::marker::PhantomData;
use std::sync::atomic::Ordering;

use crate::node::{is_tombstone, node_key, node_seq, node_value, tower_load};
use crate::ConcurrentSkipList;

// ─── Entry ─────────────────────────────────────────────────────────────────────

/// A single key-value entry yielded by iterators and cursors.
///
/// Contains the raw key and value byte slices, plus a flag indicating
/// whether this entry has been deleted (tombstoned). Tombstone entries
/// are preserved in the skip list until the memtable is compacted or
/// dropped, allowing the LSM-tree to merge deletions during flush.
#[derive(Debug, Clone, Copy)]
pub struct Entry<'a> {
    /// The key bytes. May be empty for the internal sentinel node
    /// (never yielded by public iterators).
    pub key: &'a [u8],
    /// The value bytes. For tombstone entries, this is the value that
    /// was present before deletion (typically the last live value).
    pub value: &'a [u8],
    /// `true` if this entry was deleted via [`ConcurrentSkipList::delete`].
    /// Tombstone entries are visible in live iteration and snapshots,
    /// allowing the caller to propagate deletions to SSTables.
    pub is_tombstone: bool,
}

// ─── Helpers ────────────────────────────────────────────────────────────────────

/// Advance `current` to the next node at level 0 by following the tower[0] pointer.
///
/// Returns the new node pointer, or a null pointer if `current` is the
/// last node in the chain. This is the core step shared by all iterators.
///
/// # Safety
///
/// `current` must point to a valid, fully initialized node whose tower
/// entries have been written. The returned pointer (if non-null) is valid
/// for the lifetime of the owning [`ConcurrentSkipList`].
#[inline]
fn advance_node(current: *const u8) -> *const u8 {
    let next = unsafe { tower_load(current, 0) };
    if next.is_null() {
        std::ptr::null()
    } else {
        next.ptr()
    }
}

// ─── Snapshot (point-in-time) ──────────────────────────────────────────────────

/// A point-in-time snapshot of the skip list.
///
/// Created by [`ConcurrentSkipList::snapshot`]. The snapshot captures a
/// monotonic sequence number at creation time. When iterated via
/// [`Snapshot::iter`], only nodes that were fully inserted before the
/// snapshot was taken are visible — any node inserted concurrently after
/// the snapshot is skipped.
///
/// This provides true snapshot isolation without copying data or taking
/// locks, making it ideal for flushing memtables to SSTables where a
/// consistent view is required.
///
/// # Lifetime
///
/// The snapshot borrows the [`ConcurrentSkipList`], so it cannot outlive
/// the list and the borrow checker prevents the list from being dropped
/// while snapshots exist.
#[derive(Debug)]
pub struct Snapshot<'a> {
    pub(crate) head: *const u8,
    /// Sequence number ceiling: only nodes with `seq < snap_seq` are visible
    /// to the snapshot iterator. Nodes inserted at or after this sequence
    /// number are hidden.
    pub(crate) snap_seq: u64,
    /// Borrows the skip list to enforce lifetime and `Send`/`Sync`.
    pub(crate) _owner: PhantomData<&'a ConcurrentSkipList>,
}

// SAFETY: The snapshot contains a raw pointer into arena memory owned by
// ConcurrentSkipList, which is Send+Sync. The lifetime parameter ensures
// the arena outlives the snapshot. The arena is thread-safe (per-thread
// shards with atomic assignment).
unsafe impl<'a> Send for Snapshot<'a> where ConcurrentSkipList: Sync {}
unsafe impl<'a> Sync for Snapshot<'a> where ConcurrentSkipList: Sync {}

impl<'a> Snapshot<'a> {
    /// Create an iterator over this snapshot's point-in-time view.
    ///
    /// The returned [`SnapshotIter`] walks the level-0 chain in sorted order,
    /// skipping any node whose insertion sequence number is `>= snap_seq`.
    /// This guarantees that only nodes present at the time of the snapshot
    /// are yielded.
    ///
    /// # Example
    ///
    /// ```rust
    /// use fastskip::ConcurrentSkipList;
    ///
    /// let sl = ConcurrentSkipList::new();
    /// sl.insert(b"a", b"1");
    /// let snap = sl.snapshot();
    ///
    /// let entries: Vec<_> = snap.iter().collect();
    /// assert_eq!(entries.len(), 1);
    /// ```
    pub fn iter(&self) -> SnapshotIter<'a> {
        SnapshotIter {
            current: self.head,
            snap_seq: self.snap_seq,
            _owner: PhantomData,
        }
    }
}

/// Iterator over a point-in-time snapshot of the skip list.
///
/// Walks the level-0 chain in sorted order, yielding only nodes whose
/// insertion sequence number is strictly less than the snapshot's
/// `snap_seq` ceiling. Nodes inserted concurrently after the snapshot
/// was taken are silently skipped.
///
/// Created by [`Snapshot::iter`].
#[derive(Debug)]
pub struct SnapshotIter<'a> {
    current: *const u8,
    snap_seq: u64,
    _owner: PhantomData<&'a ConcurrentSkipList>,
}

unsafe impl<'a> Send for SnapshotIter<'a> where ConcurrentSkipList: Sync {}
unsafe impl<'a> Sync for SnapshotIter<'a> where ConcurrentSkipList: Sync {}

impl<'a> Iterator for SnapshotIter<'a> {
    type Item = Entry<'a>;

    /// Advance to the next visible entry in the snapshot.
    ///
    /// Nodes with `seq >= snap_seq` are skipped to maintain snapshot
    /// isolation. Tombstone entries are included in the output — the
    /// caller decides how to handle them (e.g., writing tombstone
    /// markers to SSTables).
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            self.current = advance_node(self.current);

            if self.current.is_null() {
                return None;
            }

            // Skip nodes inserted at or after this snapshot was taken
            if unsafe { node_seq(self.current) } >= self.snap_seq {
                continue;
            }

            return Some(Entry {
                key: unsafe { node_key(self.current) },
                value: unsafe { node_value(self.current) },
                is_tombstone: unsafe { is_tombstone(self.current) },
            });
        }
    }
}

// ─── Live iterator ─────────────────────────────────────────────────────────────

/// Live iterator over the current skip list state.
///
/// Walks the level-0 chain in sorted order, yielding all entries
/// including tombstones. Unlike [`SnapshotIter`], this iterator
/// reflects the live state and **may observe concurrent inserts**
/// that happen mid-iteration. If a consistent point-in-time view
/// is needed, use [`ConcurrentSkipList::snapshot`] instead.
///
/// Created by [`ConcurrentSkipList::iter`].
#[derive(Debug)]
pub struct Iter<'a> {
    pub(crate) current: *const u8,
    pub(crate) _owner: PhantomData<&'a ConcurrentSkipList>,
}

unsafe impl<'a> Send for Iter<'a> where ConcurrentSkipList: Sync {}
unsafe impl<'a> Sync for Iter<'a> where ConcurrentSkipList: Sync {}

impl<'a> Iterator for Iter<'a> {
    type Item = Entry<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.current = advance_node(self.current);

        if self.current.is_null() {
            return None;
        }

        Some(Entry {
            key: unsafe { node_key(self.current) },
            value: unsafe { node_value(self.current) },
            is_tombstone: unsafe { is_tombstone(self.current) },
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // We can't know the exact count without traversing, but we can
        // report 0 if we haven't started and the list might be empty.
        (0, None)
    }
}

// ─── Cursor (range scan) ───────────────────────────────────────────────────────

/// A seekable forward cursor for range scans over the skip list.
///
/// Provides lower-bound seek ([`seek`](Self::seek)) and manual advancement
/// ([`next_entry`](Self::next_entry)) for efficient range iteration. Also
/// implements [`Iterator`] for use in `for` loops and iterator adapters.
///
/// Created by [`ConcurrentSkipList::cursor`] (starts at first entry) or
/// [`ConcurrentSkipList::cursor_at`] (seeks to first key `>= target`).
///
/// # Example
///
/// ```rust
/// # use fastskip::ConcurrentSkipList;
/// let sl = ConcurrentSkipList::new();
/// sl.insert(b"b", b"2");
/// sl.insert(b"d", b"4");
/// sl.insert(b"f", b"6");
///
/// // Range scan: keys in [b"b", b"e")
/// if let Some(cursor) = sl.cursor_at(b"b") {
///     for entry in cursor.take_while(|e| e.key < b"e") {
///         println!("{:?}", entry.key);
///     }
/// }
/// ```
pub struct Cursor<'a> {
    pub(crate) current: *const u8,
    pub(crate) _owner: PhantomData<&'a ConcurrentSkipList>,
}

unsafe impl<'a> Send for Cursor<'a> where ConcurrentSkipList: Sync {}
unsafe impl<'a> Sync for Cursor<'a> where ConcurrentSkipList: Sync {}

impl<'a> Cursor<'a> {
    /// Seek to the first key `>= target` (lower bound).
    ///
    /// Walks the skip list from the highest level down, using the tower
    /// pointers to skip over intermediate nodes. Uses Masstree-style
    /// lookahead prefetching for cache efficiency.
    ///
    /// Returns `true` if a key was found (cursor is now valid),
    /// `false` if all keys in the list are `< target` (cursor is invalid).
    ///
    /// # Example
    ///
    /// ```rust
    /// use fastskip::ConcurrentSkipList;
    ///
    /// let sl = ConcurrentSkipList::new();
    /// sl.insert(b"a", b"1");
    /// sl.insert(b"c", b"3");
    ///
    /// let mut cursor = sl.cursor();
    /// assert!(cursor.seek(&sl, b"b")); // lands on "c"
    /// assert_eq!(cursor.entry().unwrap().key, b"c");
    /// ```
    pub fn seek(&mut self, skiplist: &'a ConcurrentSkipList, target: &[u8]) -> bool {
        let mut x = skiplist.skiplist.head;
        let h = skiplist.skiplist.height.load(Ordering::Relaxed);
        let mut level = if h > 0 { h - 1 } else { 0 };

        loop {
            let next = unsafe { crate::node::tower_load(x, level) };
            if next.is_null() {
                if level == 0 {
                    self.current = std::ptr::null();
                    return false;
                }
                level -= 1;
                continue;
            }
            let next_node = next.ptr();
            let next_key = unsafe { node_key(next_node) };
            match crate::util::compare_keys(next_key, target) {
                std::cmp::Ordering::Less => {
                    x = next_node;
                }
                std::cmp::Ordering::Equal | std::cmp::Ordering::Greater => {
                    if level == 0 {
                        self.current = next_node;
                        return true;
                    }
                    level -= 1;
                }
            }
        }
    }

    /// Advance the cursor to the next entry at level 0.
    ///
    /// Returns `true` if a next entry exists (cursor remains valid),
    /// `false` if the end of the list was reached (cursor becomes invalid).
    ///
    /// # Example
    ///
    /// ```rust
    /// use fastskip::ConcurrentSkipList;
    ///
    /// let sl = ConcurrentSkipList::new();
    /// sl.insert(b"a", b"1");
    /// sl.insert(b"b", b"2");
    ///
    /// let mut cursor = sl.cursor();
    /// assert!(cursor.next_entry()); // "a"
    /// assert!(cursor.next_entry()); // "b"
    /// assert!(!cursor.next_entry()); // end
    /// ```
    pub fn next_entry(&mut self) -> bool {
        if self.current.is_null() {
            return false;
        }
        self.current = advance_node(self.current);
        !self.current.is_null()
    }

    /// Get the entry at the cursor's current position.
    ///
    /// Returns `None` if the cursor is invalid (at end or not yet seeked).
    ///
    /// # Example
    ///
    /// ```rust
    /// use fastskip::ConcurrentSkipList;
    ///
    /// let sl = ConcurrentSkipList::new();
    /// sl.insert(b"a", b"1");
    ///
    /// let cursor = sl.cursor_at(b"a").unwrap();
    /// let entry = cursor.entry().unwrap();
    /// assert_eq!(entry.key, b"a");
    /// assert_eq!(entry.value, b"1");
    /// ```
    pub fn entry(&self) -> Option<Entry<'a>> {
        if self.current.is_null() {
            return None;
        }
        Some(Entry {
            key: unsafe { node_key(self.current) },
            value: unsafe { node_value(self.current) },
            is_tombstone: unsafe { is_tombstone(self.current) },
        })
    }

    /// Returns `true` if the cursor points to a valid entry.
    ///
    /// A cursor is invalid if it was never seeked, if `seek` found no
    /// matching key, or if `next_entry` reached the end of the list.
    ///
    /// # Example
    ///
    /// ```rust
    /// use fastskip::ConcurrentSkipList;
    ///
    /// let sl = ConcurrentSkipList::new();
    /// sl.insert(b"a", b"1");
    ///
    /// let cursor = sl.cursor_at(b"a").unwrap();
    /// assert!(cursor.valid());
    ///
    /// assert!(sl.cursor_at(b"z").is_none());
    /// ```
    pub fn valid(&self) -> bool {
        !self.current.is_null()
    }
}

impl<'a> Iterator for Cursor<'a> {
    type Item = Entry<'a>;

    /// Yield the current entry and advance to the next.
    ///
    /// Returns `None` when the cursor is invalid (at end or past all keys).
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if !self.valid() {
            return None;
        }
        let entry = self.entry();
        self.next_entry();
        entry
    }
}