raindb 1.0.0

A persistent key-value store based on an LSM tree implemented in Rust
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright (c) 2021 Google LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

use std::sync::Arc;

use rand::distributions::{Distribution, Uniform};
use rand::rngs::StdRng;
use rand::SeedableRng;

use crate::compaction::{CompactionWorker, TaskKind};
use crate::config::ITERATION_READ_BYTES_PERIOD;
use crate::db::PortableDatabaseState;
use crate::errors::RainDBError;
use crate::key::{InternalKey, RainDbKeyType};
use crate::versioning::file_iterators::MergingIterator;
use crate::{Operation, DB};

/**
A RainDB specific iterator implementation that has more cursor-like behavior.

The RainDB iterator differs from the [`std::iter::DoubleEndedIterator`] in that the RainDB iterator
moves one cursor back and forth on the range of values. The `DoubleEndedIterator` essentially moves
two pointers toward each other and ends iteration onces the two pointers cross.
*/
pub trait RainDbIterator
where
    Self::Key: RainDbKeyType,
{
    /// The type of key that is iterated over.
    type Key;

    /// The type of error returned when there is an error during iteration.
    type Error;

    /// The iterator is only valid if the cursor is currently positioned at a key-value pair.
    fn is_valid(&self) -> bool;

    /**
    Position cursor to the first key that is at or past the target.

    Returns an error if there was an issue seeking the target and sets the iterator to invalid.
    */
    fn seek(&mut self, target: &Self::Key) -> Result<(), Self::Error>;

    /**
    Position cursor to the first element.

    Returns an error if there was an issue seeking the target and sets the iterator to invalid.
    */
    fn seek_to_first(&mut self) -> Result<(), Self::Error>;

    /**
    Position cursor to the last element.

    Returns an error if there was an issue seeking the target and sets the iterator to invalid.
    */
    fn seek_to_last(&mut self) -> Result<(), Self::Error>;

    /**
    Move to the next element.

    Returns a tuple (&Self::Key, &V) at the position moved to. If the cursor was on the last
    element, `None` is returned.
    */
    fn next(&mut self) -> Option<(&Self::Key, &Vec<u8>)>;

    /**
    Move to the previous element.

    Returns a tuple (&Self::Key, &V) at the position moved to. If the cursor was on the first
    element, `None` is returned.
    */
    fn prev(&mut self) -> Option<(&Self::Key, &Vec<u8>)>;

    /**
    Return the key and value at the current cursor position.

    Returns a tuple (&Self::Key, &V) at current position if the iterator is valid. Otherwise,
    returns `None`.
    */
    fn current(&self) -> Option<(&Self::Key, &Vec<u8>)>;
}

/**
An wrapper around an iterator that caches the `current()` and `is_valid()` results.

This maps to the LevelDB's `IteratorWrapper` and according to LevelDB is supposed to help avoid
costs from virtual function calls and also "gives better cache locality". Not sure how much the
latter actually saves.
*/
pub(crate) struct CachingIterator {
    /// The underlying iterator.
    iterator: Box<dyn RainDbIterator<Key = InternalKey, Error = RainDBError>>,

    /// The cached validity value.
    is_valid: bool,

    /// The cached key-value pair.
    cached_entry: Option<(InternalKey, Vec<u8>)>,
}

/// Crate-only methods
impl CachingIterator {
    /// Create a new instance of [`CachingIterator`].
    pub(crate) fn new(
        iterator: Box<dyn RainDbIterator<Key = InternalKey, Error = RainDBError>>,
    ) -> Self {
        let is_valid = iterator.is_valid();
        let current_entry = iterator
            .current()
            .map(|(key, value)| (key.clone(), value.clone()));

        Self {
            iterator,
            is_valid,
            cached_entry: current_entry,
        }
    }
}

/// Private methods
impl CachingIterator {
    /// Update the cached values.
    fn update_cached_values(&mut self) {
        self.is_valid = self.iterator.is_valid();
        if self.is_valid {
            self.cached_entry = self
                .iterator
                .current()
                .map(|(key, value)| (key.clone(), value.clone()));
        }
    }
}

impl RainDbIterator for CachingIterator {
    type Key = InternalKey;

    type Error = RainDBError;

    fn is_valid(&self) -> bool {
        self.is_valid
    }

    fn seek(&mut self, target: &Self::Key) -> Result<(), Self::Error> {
        self.iterator.seek(target)?;
        self.update_cached_values();

        Ok(())
    }

    fn seek_to_first(&mut self) -> Result<(), Self::Error> {
        self.iterator.seek_to_first()?;
        self.update_cached_values();

        Ok(())
    }

    fn seek_to_last(&mut self) -> Result<(), Self::Error> {
        self.iterator.seek_to_last()?;
        self.update_cached_values();

        Ok(())
    }

    fn next(&mut self) -> Option<(&Self::Key, &Vec<u8>)> {
        self.iterator.next();
        self.update_cached_values();

        self.current()
    }

    fn prev(&mut self) -> Option<(&Self::Key, &Vec<u8>)> {
        self.iterator.prev();
        self.update_cached_values();

        self.current()
    }

    fn current(&self) -> Option<(&Self::Key, &Vec<u8>)> {
        self.cached_entry.as_ref().map(|entry| (&entry.0, &entry.1))
    }
}

/// Enum for indicating the direction of iteration.
#[derive(Eq, PartialEq)]
enum DbIterationDirection {
    /// When moving forward, the internal iterator is positioned exactly at the current node.
    Forward,

    /**
    When moving backwards, the internal iterator is positioned before all entries whose user key is
    equal to the current node.
    */
    Backward,
}

/**
An iterator that yields client-facing state of the database.

The iterator will yield key-value pairs while accounting for recency via sequence numbers and for
deletion tombstones.
*/

pub struct DatabaseIterator {
    /// Database state that can be accessed from the iterator.
    db_state: PortableDatabaseState,

    /// The database compaction worker to process compaction requests.
    compaction_worker: Arc<CompactionWorker>,

    /// The direction that the iterator is moving.
    direction: DbIterationDirection,

    /// An internal iterator that yields the raw state of the database.
    inner_iter: MergingIterator,

    /// The most recent sequence number to consider when merging records for
    sequence_snapshot: u64,

    /// If the iterator is in a valid state.
    is_valid: bool,

    /**
    Random number generator for determining when to update database read statistics and,
    potentially, schedule a compaction.
    */
    rng: StdRng,

    /// The random distribution for sampling compaction periods from.
    distribution: Uniform<u64>,

    /// The number of bytes to read before performing read statistic sampling.
    bytes_until_read_sampling: usize,

    /// A cached user key. This value does not necessarily correlate to the `cached_value` field.
    cached_user_key: Option<Vec<u8>>,

    /// A cached value. This value does not necessarily correlate to the `cached_key` field.
    cached_value: Option<Vec<u8>>,
}

/// Crate-only methods
impl DatabaseIterator {
    /// Create a new instance of [`DatabaseIterator`].
    pub(crate) fn new(
        db_state: PortableDatabaseState,
        inner_iter: MergingIterator,
        sequence_snapshot: u64,
        sampling_seed: u64,
        compaction_worker: Arc<CompactionWorker>,
    ) -> Self {
        Self {
            db_state,
            direction: DbIterationDirection::Forward,
            inner_iter,
            sequence_snapshot,
            is_valid: false,
            rng: StdRng::seed_from_u64(sampling_seed),
            distribution: Uniform::from(0..(2 * ITERATION_READ_BYTES_PERIOD)),
            bytes_until_read_sampling: 0,
            cached_user_key: None,
            cached_value: None,
            compaction_worker,
        }
    }
}

/// Private methods
impl DatabaseIterator {
    /**
    Get samples of read statistics for the current key.

    This is to check if a seek compaction would be required during iteration.

    # Legacy

    This is synonomous to LevelDB's `DBIter::ParseKey`.
    */
    fn sample_read_stats_for_current_key(&mut self) {
        if !self.inner_iter.is_valid() {
            return;
        }

        let bytes_read = {
            let (current_key, current_val) = self.inner_iter.current().unwrap();
            current_key.get_approximate_size() + current_val.len()
        };
        while self.bytes_until_read_sampling < bytes_read {
            self.bytes_until_read_sampling += self.random_compaction_period();
            let mut db_fields_guard = self.db_state.guarded_db_fields.lock();
            let current_version = &mut db_fields_guard.version_set.get_current_version();
            let (current_key, _current_val) = self.inner_iter.current().unwrap();
            let needs_seek_compaction = current_version
                .write()
                .element
                .record_read_sample(current_key);
            if needs_seek_compaction
                && DB::should_schedule_compaction(&self.db_state, &mut db_fields_guard)
            {
                self.compaction_worker.schedule_task(TaskKind::Compaction);
            }
        }

        self.bytes_until_read_sampling -= bytes_read;
    }

    /// Picks a random number of bytes that can be read before a compaction is scheduled.
    fn random_compaction_period(&mut self) -> usize {
        self.distribution.sample(&mut self.rng) as usize
    }

    /**
    Move the internal iterator forward until there is a valid user value to yield e.g. keys that
    have not been marked with a tombstone.

    Set `initial_is_skipping` to `true` if the inner iterator is positioned such that it is in a
    run of obsolete records for a user key. Set to `false` if we know we are at the first record
    for a given user key.

    # Panics

    This method will panic if the inner iterator is not in a valid position and if the iterator
    is not iterating forward.
    */
    fn find_next_client_entry(&mut self, initial_is_skipping: bool) {
        assert!(self.inner_iter.is_valid());
        assert!(self.direction == DbIterationDirection::Forward);

        let mut is_skipping: bool = initial_is_skipping;
        loop {
            self.sample_read_stats_for_current_key();
            let (current_key, _) = self.inner_iter.current().unwrap();
            let curr_sequence_num = current_key.get_sequence_number();
            let curr_operation = current_key.get_operation();
            if curr_sequence_num > self.sequence_snapshot {
                // This record is more recent than our snapshot allows, don't process it
            } else {
                match curr_operation {
                    Operation::Delete => {
                        // Skip all upcoming entries for this user key because they are hidden by this
                        // deletion
                        is_skipping = true;

                        let current_user_key =
                            self.inner_iter.current().unwrap().0.get_user_key().to_vec();
                        self.cached_user_key = Some(current_user_key);
                    }
                    Operation::Put => {
                        if is_skipping
                            && self.cached_user_key.is_some()
                            && current_key.get_user_key() <= self.cached_user_key.as_ref().unwrap()
                        {
                            // This entry is hidden by more recent updates to the same user key.
                            // Shadowed values (i.e. older records) may be hit first before a different
                            // user key is found because internal keys are sorted in decreasing order
                            // for sequence numbers.
                        } else {
                            // Found the most up-to-date value for a key
                            self.is_valid = true;
                            self.cached_user_key = None;

                            return;
                        }
                    }
                }
            }

            self.inner_iter.next();
            if !self.inner_iter.is_valid() {
                break;
            }
        }

        self.cached_user_key = None;
        self.is_valid = false;
    }

    /**
    Move the internal iterator backward until there is a valid user value to yield e.g. keys that
    have not been marked with a tombstone.

    # Panics

    This method will panic if the inner iterator is moving in the backward direction.
    */
    fn find_prev_client_entry(&mut self) {
        assert!(self.direction == DbIterationDirection::Backward);

        let mut last_operation_type = Operation::Delete;
        if self.inner_iter.is_valid() {
            loop {
                self.sample_read_stats_for_current_key();
                let (current_key, _) = self.inner_iter.current().unwrap();
                let curr_sequence_num = current_key.get_sequence_number();
                let curr_operation = current_key.get_operation();
                if curr_sequence_num > self.sequence_snapshot {
                    // This record is more recent than our snapshot allows, don't process it
                } else {
                    if last_operation_type != Operation::Delete
                        && current_key.get_user_key() < self.cached_user_key.as_ref().unwrap()
                    {
                        // Encountered a non-deleted value in records for the previous user key
                        break;
                    }

                    last_operation_type = curr_operation;
                    match curr_operation {
                        Operation::Delete => {
                            // Encountered a more recent deletion for the user, so clear the cached
                            // values
                            self.cached_user_key = None;
                            self.cached_value = None;
                        }
                        Operation::Put => {
                            let (current_key, current_val) = self.inner_iter.current().unwrap();
                            self.cached_user_key = Some(current_key.get_user_key().to_vec());
                            self.cached_value = Some(current_val.clone());
                        }
                    }
                }

                self.inner_iter.prev();
                if !self.inner_iter.is_valid() {
                    break;
                }
            }
        }

        if last_operation_type == Operation::Delete {
            self.is_valid = false;
            self.cached_user_key = None;
            self.cached_value = None;
            self.direction = DbIterationDirection::Forward;
        } else {
            self.is_valid = true;
        }
    }
}

impl RainDbIterator for DatabaseIterator {
    type Key = Vec<u8>;

    type Error = RainDBError;

    fn is_valid(&self) -> bool {
        self.is_valid
    }

    fn seek(&mut self, target: &Self::Key) -> Result<(), Self::Error> {
        self.direction = DbIterationDirection::Forward;
        self.cached_value = None;
        self.cached_user_key = Some(target.clone());

        let lookup_key = InternalKey::new_for_seeking(target.clone(), self.sequence_snapshot);
        self.inner_iter.seek(&lookup_key)?;

        if self.inner_iter.is_valid() {
            self.find_next_client_entry(false);

            return Ok(());
        }

        self.is_valid = false;

        Ok(())
    }

    fn seek_to_first(&mut self) -> Result<(), Self::Error> {
        self.direction = DbIterationDirection::Forward;
        self.cached_value = None;
        self.inner_iter.seek_to_first()?;

        if self.inner_iter.is_valid() {
            self.find_next_client_entry(false);

            return Ok(());
        }

        self.is_valid = false;

        Ok(())
    }

    fn seek_to_last(&mut self) -> Result<(), Self::Error> {
        self.direction = DbIterationDirection::Backward;
        self.cached_value = None;
        self.inner_iter.seek_to_last()?;
        self.find_prev_client_entry();

        Ok(())
    }

    fn next(&mut self) -> Option<(&Self::Key, &Vec<u8>)> {
        assert!(self.is_valid);

        if self.direction == DbIterationDirection::Backward {
            self.direction = DbIterationDirection::Forward;

            /*
            The inner iterator is pointing just before the entries for the cached key, so we must
            first seek into the run of records for the cached user key before we can use the
            `find_next_client_entry` to find valid records.
            */
            if !self.inner_iter.is_valid() {
                let _seek_result = self.inner_iter.seek_to_first();
            } else {
                self.inner_iter.next();
            }

            if !self.inner_iter.is_valid() {
                self.is_valid = false;
                self.cached_user_key = None;
                return None;
            }
        } else {
            // Save the current key of the inner iterator so that we skip it and it's older records
            self.cached_user_key =
                Some(self.inner_iter.current().unwrap().0.get_user_key().to_vec());

            self.inner_iter.next();
            if !self.inner_iter.is_valid() {
                self.is_valid = false;
                self.cached_user_key = None;
                return None;
            }
        }

        self.find_next_client_entry(true);

        if !self.is_valid() {
            return None;
        }

        self.current()
    }

    fn prev(&mut self) -> Option<(&Self::Key, &Vec<u8>)> {
        assert!(self.is_valid);

        if self.direction == DbIterationDirection::Forward {
            // The inner iterator is point at the current entry. Scan backwards until the user key
            // changes and use `DatabaseIterator::find_prev_client_entry` to find the most recent
            // record for the user key
            self.cached_user_key =
                Some(self.inner_iter.current().unwrap().0.get_user_key().to_vec());
            loop {
                if let Some((current_key, _)) = self.inner_iter.prev() {
                    if current_key.get_user_key() < self.cached_user_key.as_ref().unwrap() {
                        break;
                    }
                } else {
                    self.is_valid = false;
                    self.cached_user_key = None;
                    self.cached_value = None;
                    return None;
                }
            }

            self.direction = DbIterationDirection::Backward;
        }

        self.find_prev_client_entry();

        if !self.is_valid() {
            return None;
        }

        self.current()
    }

    fn current(&self) -> Option<(&Self::Key, &Vec<u8>)> {
        assert!(self.is_valid);

        match self.direction {
            DbIterationDirection::Forward => {
                let (curr_key, curr_val) = self.inner_iter.current().unwrap();
                return Some((curr_key.get_user_key_as_vec(), curr_val));
            }
            DbIterationDirection::Backward => {
                return Some((
                    self.cached_user_key.as_ref().unwrap(),
                    self.cached_value.as_ref().unwrap(),
                ));
            }
        }
    }
}