solistore 0.2.0

Embedded LSM key-value store for SoliDB: pure Rust, durable on every commit by default
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
//! What a reader reads: the memtables and table files live at one moment.
//!
//! A [`SuperVersion`] is immutable. Writers that change the set (a memtable
//! switch, a flush, a compaction) build a new one and swap it in; a reader
//! that loaded the old one keeps it, and with it every file it names, until
//! the reader is done. A file dropped from the live set is deleted when the
//! last version naming it goes away.
//!
//! A reader must load the super version *before* its snapshot sequence. A
//! flush keeps only the newest version of each key in a memtable, and every
//! entry of a sealed memtable is at or below the snapshot of anyone who
//! loads a later super version, so such a reader never needs what was
//! dropped. Loading in the other order would allow a stale snapshot against
//! a flushed file.

use std::cmp::Ordering;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
use std::sync::{Arc, OnceLock};

use bytes::Bytes;

use crate::error::{Error, Result};
use crate::fs::Fs;
use crate::iter::InternalIterator;
use crate::key::{self, Kind};
use crate::memtable::{covering_seq, MemIter, Memtable, RangeTombstone, Version as Found};
use crate::sst::{bloom, BlockCache, TableIter, TableProps, TableReader};

pub const NUM_LEVELS: usize = 7;

pub fn table_name(number: u64) -> String {
    format!("{number:06}.sst")
}

pub fn parse_table_name(name: &str) -> Option<u64> {
    name.strip_suffix(".sst")?.parse().ok()
}

/// Table files whose delete failed, to try again later. On Windows a file
/// another handle holds open cannot always be deleted; without a retry it
/// would stay until the next open removes orphans.
pub type Graveyard = Arc<parking_lot::Mutex<Vec<PathBuf>>>;

/// Delete what the graveyard holds; keep what still cannot be deleted.
pub fn retry_deletes(fs: &dyn Fs, graveyard: &Graveyard) {
    let mut pending = graveyard.lock();
    if pending.is_empty() {
        return;
    }
    pending.retain(|path| fs.remove(path).is_err() && fs.exists(path));
}

/// A table file, opened on first use and deleted once obsolete and unused.
pub struct TableFile {
    pub number: u64,
    pub props: TableProps,
    path: PathBuf,
    fs: Arc<dyn Fs>,
    cache: Arc<BlockCache>,
    reader: OnceLock<Arc<TableReader>>,
    /// Held while the reader is opened, so it is opened once: concurrent
    /// readers of a table nobody had opened yet each read its index and
    /// filter (a few MB) and all but one threw theirs away. With hundreds
    /// of SoliDB query threads reaching a compaction's new tables at once,
    /// that briefly took 600 MB.
    opening: parking_lot::Mutex<()>,
    obsolete: AtomicBool,
    graveyard: Option<Graveyard>,
}

impl TableFile {
    pub fn new(
        fs: Arc<dyn Fs>,
        cache: Arc<BlockCache>,
        dir: &std::path::Path,
        number: u64,
        props: TableProps,
    ) -> Self {
        TableFile {
            number,
            props,
            path: dir.join(table_name(number)),
            fs,
            cache,
            reader: OnceLock::new(),
            opening: parking_lot::Mutex::new(()),
            obsolete: AtomicBool::new(false),
            graveyard: None,
        }
    }

    /// Where a failed delete of this file goes to be retried.
    pub fn with_graveyard(mut self, graveyard: Graveyard) -> Self {
        self.graveyard = Some(graveyard);
        self
    }

    /// The table's reader, opened on first use. Lent: a point read holds
    /// the version naming this file for as long as it reads, and cloning the
    /// `Arc` would write its count, a line every thread reading the table
    /// contends on. A cursor that outlives the call clones it.
    pub fn reader(&self) -> Result<&Arc<TableReader>> {
        if let Some(reader) = self.reader.get() {
            return Ok(reader);
        }
        let _opening = self.opening.lock();
        if let Some(reader) = self.reader.get() {
            return Ok(reader);
        }
        let reader = TableReader::open(self.fs.open_random(&self.path)?)?
            .with_cache(self.cache.clone(), self.number);
        Ok(self.reader.get_or_init(|| Arc::new(reader)))
    }

    /// No longer in the live version: delete the file when the last reader
    /// still holding it lets go.
    pub fn mark_obsolete(&self) {
        self.obsolete.store(true, AtomicOrdering::Release);
    }

    /// Whether `cf ‖ user_key` falls between the table's first and last keys.
    pub fn may_hold(&self, cf_and_user_key: &[u8]) -> bool {
        key::cf_and_user_key(&self.props.smallest) <= cf_and_user_key
            && cf_and_user_key <= key::cf_and_user_key(&self.props.largest)
    }
}

impl Drop for TableFile {
    fn drop(&mut self) {
        if self.obsolete.load(AtomicOrdering::Acquire) {
            // Close this table's own handle first: Windows refuses to delete
            // a file that is open without FILE_SHARE_DELETE.
            drop(self.reader.take());
            if self.fs.remove(&self.path).is_err() && self.fs.exists(&self.path) {
                if let Some(graveyard) = &self.graveyard {
                    graveyard.lock().push(self.path.clone());
                }
            }
        }
    }
}

/// The table files and range tombstones of the database.
#[derive(Clone, Default)]
pub struct Version {
    /// Level 0 newest first (its files overlap); deeper levels sorted by key.
    pub levels: Vec<Vec<Arc<TableFile>>>,
    pub tombstones: Vec<RangeTombstone>,
}

impl Version {
    pub fn empty() -> Self {
        Version {
            levels: vec![Vec::new(); NUM_LEVELS],
            tombstones: Vec::new(),
        }
    }

    pub fn files(&self) -> impl Iterator<Item = &Arc<TableFile>> {
        self.levels.iter().flatten()
    }

    /// Sort level 0 newest first: a newer file shadows an older one.
    pub fn sort_level0(&mut self) {
        self.levels[0].sort_by_key(|f| std::cmp::Reverse(f.props.largest_seq));
    }
}

#[derive(Clone)]
pub struct SuperVersion {
    pub mem: Arc<Memtable>,
    /// Sealed memtables waiting to be flushed, newest first.
    pub imms: Vec<Arc<Memtable>>,
    pub version: Arc<Version>,
}

impl SuperVersion {
    /// The value of `user_key` at `snapshot`.
    pub fn get(&self, cf: u32, user_key: &[u8], snapshot: u64) -> Result<Option<Bytes>> {
        let Some(found) = self.newest(cf, user_key, snapshot)? else {
            return Ok(None);
        };
        if found.kind == Kind::Delete || self.tombstone_seq(cf, user_key, snapshot) > found.seq {
            return Ok(None);
        }
        Ok(Some(found.value))
    }

    /// The newest version at or below `snapshot`: sources are searched newest
    /// first, so the first hit wins.
    fn newest(&self, cf: u32, user_key: &[u8], snapshot: u64) -> Result<Option<Found>> {
        // One key for every source, and one bloom hash for every table,
        // taken at the first table probed.
        let lookup = key::LookupKey::new(cf, user_key, snapshot);
        for mem in std::iter::once(&self.mem).chain(&self.imms) {
            if let Some(found) = mem.get_at(&lookup) {
                return Ok(Some(found));
            }
        }
        let probe = lookup.id();
        let mut hash = None;
        let mut hash = || *hash.get_or_insert_with(|| bloom::hash(probe));
        for file in &self.version.levels[0] {
            if file.may_hold(probe) {
                if let Some(found) = file.reader()?.get_at(&lookup, hash())? {
                    return Ok(Some(found));
                }
            }
        }
        for level in &self.version.levels[1..] {
            // Sorted and disjoint: at most one file can hold the key.
            let at = level.partition_point(|f| key::cf_and_user_key(&f.props.largest) < probe);
            if let Some(file) = level.get(at).filter(|f| f.may_hold(probe)) {
                if let Some(found) = file.reader()?.get_at(&lookup, hash())? {
                    return Ok(Some(found));
                }
            }
        }
        Ok(None)
    }

    pub fn tombstone_seq(&self, cf: u32, user_key: &[u8], snapshot: u64) -> u64 {
        let mut seq = covering_seq(&self.version.tombstones, cf, user_key, snapshot);
        for mem in std::iter::once(&self.mem).chain(&self.imms) {
            seq = seq.max(mem.range_tombstone_seq(cf, user_key, snapshot));
        }
        seq
    }

    /// Every range tombstone of `cf` visible at `snapshot`.
    pub fn tombstones(&self, cf: u32, snapshot: u64) -> Vec<RangeTombstone> {
        let mut all: Vec<RangeTombstone> = self.version.tombstones.clone();
        for mem in std::iter::once(&self.mem).chain(&self.imms) {
            all.extend(mem.tombstones());
        }
        all.retain(|t| t.cf == cf && t.seq <= snapshot);
        all
    }

    /// One cursor per source that may hold keys of `cf`.
    pub fn iterators(&self, cf: u32) -> Result<Vec<Box<dyn InternalIterator>>> {
        let mut out: Vec<Box<dyn InternalIterator>> = Vec::new();
        for mem in std::iter::once(&self.mem).chain(&self.imms) {
            out.push(Box::new(MemIter::new(mem.clone())));
        }
        let holds_cf = |f: &TableFile| {
            let (lo, hi) = (key::parse(&f.props.smallest), key::parse(&f.props.largest));
            match (lo, hi) {
                (Some(lo), Some(hi)) => lo.cf <= cf && cf <= hi.cf,
                _ => true,
            }
        };
        for file in &self.version.levels[0] {
            if holds_cf(file) {
                out.push(Box::new(TableIter::new(file.reader()?.clone())));
            }
        }
        // A deeper level is one sorted run: one cursor that opens only the
        // file it is in, however many files the level has.
        for level in &self.version.levels[1..] {
            let files: Vec<Arc<TableFile>> =
                level.iter().filter(|f| holds_cf(f)).cloned().collect();
            if !files.is_empty() {
                out.push(Box::new(LevelIter::new(files)));
            }
        }
        Ok(out)
    }
}

/// A cursor over a sorted run of disjoint tables (a level below 0).
pub struct LevelIter {
    files: Vec<Arc<TableFile>>,
    at: usize,
    current: Option<TableIter>,
    error: Option<Error>,
    fill_cache: bool,
}

impl LevelIter {
    pub fn new(files: Vec<Arc<TableFile>>) -> Self {
        LevelIter {
            files,
            at: 0,
            current: None,
            error: None,
            fill_cache: true,
        }
    }

    /// For a compaction: see [`TableIter::without_filling_cache`].
    pub fn without_filling_cache(mut self) -> Self {
        self.fill_cache = false;
        self
    }

    fn open(&mut self, at: usize) -> bool {
        self.at = at;
        self.current = None;
        match self.files[at].reader() {
            Ok(reader) => {
                let it = TableIter::new(reader.clone());
                self.current = Some(if self.fill_cache {
                    it
                } else {
                    it.without_filling_cache()
                });
                true
            }
            Err(e) => {
                self.error = Some(e);
                false
            }
        }
    }

    fn current_valid(&self) -> bool {
        self.current.as_ref().is_some_and(|c| c.valid())
    }

    /// Move to the first entry of the following files until one has one.
    fn forward_to_valid(&mut self) {
        while !self.current_valid() && self.error.is_none() {
            if let Some(Err(e)) = self.current.as_ref().map(|c| c.status()) {
                self.error = Some(e);
                return;
            }
            if self.at + 1 >= self.files.len() {
                self.current = None;
                return;
            }
            if !self.open(self.at + 1) {
                return;
            }
            let first = self.files[self.at].props.smallest.clone();
            self.current.as_mut().expect("opened").seek_ge(&first);
        }
    }

    /// Move to the last entry of the preceding files until one has one.
    fn backward_to_valid(&mut self) {
        while !self.current_valid() && self.error.is_none() {
            if let Some(Err(e)) = self.current.as_ref().map(|c| c.status()) {
                self.error = Some(e);
                return;
            }
            if self.at == 0 {
                self.current = None;
                return;
            }
            if !self.open(self.at - 1) {
                return;
            }
            self.current.as_mut().expect("opened").seek_to_last();
        }
    }
}

impl InternalIterator for LevelIter {
    fn seek_ge(&mut self, target: &[u8]) {
        self.error = None;
        let at = self
            .files
            .partition_point(|f| key::compare(&f.props.largest, target) == Ordering::Less);
        if at >= self.files.len() {
            self.current = None;
            return;
        }
        if self.open(at) {
            self.current.as_mut().expect("opened").seek_ge(target);
            self.forward_to_valid();
        }
    }

    fn seek_le(&mut self, target: &[u8]) {
        self.error = None;
        let after = self
            .files
            .partition_point(|f| key::compare(&f.props.smallest, target) != Ordering::Greater);
        if after == 0 {
            self.current = None;
            return;
        }
        if self.open(after - 1) {
            self.current.as_mut().expect("opened").seek_le(target);
            self.backward_to_valid();
        }
    }

    fn seek_to_last(&mut self) {
        self.error = None;
        if self.files.is_empty() {
            self.current = None;
            return;
        }
        if self.open(self.files.len() - 1) {
            self.current.as_mut().expect("opened").seek_to_last();
            self.backward_to_valid();
        }
    }

    fn next(&mut self) {
        if let Some(c) = self.current.as_mut() {
            c.next();
        }
        self.forward_to_valid();
    }

    fn prev(&mut self) {
        if let Some(c) = self.current.as_mut() {
            c.prev();
        }
        self.backward_to_valid();
    }

    fn valid(&self) -> bool {
        self.error.is_none() && self.current_valid()
    }

    fn key(&self) -> &[u8] {
        self.current.as_ref().expect("valid").key()
    }

    fn value(&self) -> Bytes {
        self.current.as_ref().expect("valid").value()
    }

    fn status(&self) -> Result<()> {
        match &self.error {
            Some(e) => Err(e.clone()),
            None => self.current.as_ref().map_or(Ok(()), |c| c.status()),
        }
    }
}

/// Order two tables by their first key, for the sorted levels.
pub fn by_smallest(a: &Arc<TableFile>, b: &Arc<TableFile>) -> Ordering {
    key::compare(&a.props.smallest, &b.props.smallest)
}