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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// 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 integer_encoding::{FixedInt, VarInt};
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::sync::Arc;

use crate::config::SIZE_OF_U32_BYTES;
use crate::iterator::RainDbIterator;
use crate::key::{InternalKey, RainDbKeyType};

use super::errors::{ReadError, TableReadResult};

/// A block where the keys of entries are RainDB internal keys.
pub type DataBlockReader = BlockReader<InternalKey>;

/// A block where the keys of entries are metaindex keys.
pub(crate) type MetaIndexBlockReader = BlockReader<MetaIndexKey>;

/**
An entry in a block.

The extra unused fields is stored for debugging purposes.
*/
#[allow(dead_code)]
#[derive(Debug)]
pub struct BlockEntry<K> {
    /// The offset that this block entry is at in the parent block.
    block_offset: usize,

    /**
    The number of bytes this key's prefix shares with a parent restart point.

    This is zero if the key is a restart point.
    */
    key_num_shared_bytes: u32,

    /// The number of bytes unique to this key i.e. bytes not shared with a parent restart point.
    key_num_unshared_bytes: u32,

    /// The length of the value.
    value_length: u32,

    /**
    The unique parts of the key.

    This necessarily has the same length as `key_num_shared_bytes`.
    */
    key_delta: Vec<u8>,

    /// The full, deserialized key.
    key: K,

    /// The value of the entry.
    value: Vec<u8>,
}

/**
Reader for deserializing a block from the table file and iterating its entries.

The extra unused fields is stored for debugging purposes.
*/
#[allow(dead_code)]
#[derive(Debug)]
pub struct BlockReader<K>
where
    K: RainDbKeyType,
{
    /// The unserialized data contained within the block.
    raw_data: Vec<u8>,

    /// The number of restart points that the block has.
    num_restart_points: u32,

    /// The offset within the raw data where the restart points are listed.
    restart_offset: usize,

    /**
    The deserialized entries within a block.

    The entries are wrapped in an [`Arc`] so that a cheap reference can be given to iterators
    without tying the lifetime of the iterator to the block reader.
    */
    block_entries: Arc<Vec<BlockEntry<K>>>,

    /// The indexes within `block_entries` that contain restart points.
    restart_point_indexes: Vec<usize>,
}

// Crate-only methods
impl<K> BlockReader<K>
where
    K: RainDbKeyType,
{
    /// Create a new instance of a [`BlockReader`].
    pub(crate) fn new(raw_data: Vec<u8>) -> TableReadResult<Self> {
        if raw_data.len() < SIZE_OF_U32_BYTES {
            return Err(ReadError::FailedToParse(
                "Failed to parse restart points. The buffer is too small.".to_string(),
            ));
        }

        // The number of restart points is in the last 4 bytes of the block when serialized.
        let num_restart_points_offset = raw_data.len() - SIZE_OF_U32_BYTES;
        let num_restart_points = u32::decode_fixed(&raw_data[num_restart_points_offset..]);

        // The array of restart points is right before the number of restart points when serialized.
        let restart_offset =
            raw_data.len() - (1 + (num_restart_points as usize)) * SIZE_OF_U32_BYTES;
        let restart_point_offsets = BlockReader::<K>::deserialize_restart_offsets(
            &raw_data[restart_offset..(raw_data.len() - SIZE_OF_U32_BYTES)],
            num_restart_points,
        )?;

        let (block_entries, restart_point_indexes) =
            BlockReader::deserialize_entries(&raw_data[..restart_offset], restart_point_offsets)?;

        Ok(Self {
            raw_data,
            num_restart_points,
            restart_offset,
            block_entries: Arc::new(block_entries),
            restart_point_indexes,
        })
    }

    /// Get the size of the block.
    pub(crate) fn size(&self) -> usize {
        self.raw_data.len()
    }

    /// Get a [`crate::iterator::RainDbIterator`] to the block entries.
    pub(crate) fn iter(&self) -> BlockIter<K> {
        BlockIter {
            current_index: 0,
            block_entries: Arc::clone(&self.block_entries),
        }
    }
}

// Private methods
impl<K> BlockReader<K>
where
    K: RainDbKeyType,
{
    /**
    Deserialize block entries.

    `restart_point_offsets` are the offsets to the restart points within the raw buffer.

    A block is laid out in the following format on disk:

    * key_num_shared_bytes: varint32
    * key_num_unshared_bytes: varint32
    * value_length: varint32
    * key_delta: Vec<u8> where this is a buffer the size of `key_num_unshared_bytes`
    * value: Vec<u8>

    Returns a tuple where the first element is the deserialized block entries and the second element
    is a vector of the indexes of the block entries that are also restart points.
    */
    fn deserialize_entries(
        buf: &[u8],
        restart_point_offsets: Vec<u32>,
    ) -> TableReadResult<(Vec<BlockEntry<K>>, Vec<usize>)> {
        let mut block_entries: Vec<BlockEntry<K>> = vec![];
        let mut restart_point_indexes: Vec<usize> = vec![];
        let mut restart_offsets_index = 0;
        let mut current_offset = 0;
        let mut current_full_key_buffer: Vec<u8> = vec![];

        while current_offset < buf.len() {
            let block_offset = current_offset;

            let maybe_key_shared_bytes = u32::decode_var(&buf[current_offset..]);
            if maybe_key_shared_bytes.is_none() {
                return Err(ReadError::FailedToParse(format!(
                    "Failed to parse number of shared bytes from buffer of size {} at offset {}",
                    buf.len(),
                    current_offset
                )));
            }
            let (key_num_shared_bytes, bytes_read) = maybe_key_shared_bytes.unwrap();
            current_offset += bytes_read;

            let maybe_key_unshared_bytes = u32::decode_var(&buf[current_offset..]);
            if maybe_key_unshared_bytes.is_none() {
                return Err(ReadError::FailedToParse(format!(
                    "Failed to parse number of unshared bytes from buffer of size {} at offset {}",
                    buf.len(),
                    current_offset
                )));
            }
            let (key_num_unshared_bytes, bytes_read) = maybe_key_unshared_bytes.unwrap();
            current_offset += bytes_read;

            let maybe_value_length = u32::decode_var(&buf[current_offset..]);
            if maybe_value_length.is_none() {
                return Err(ReadError::FailedToParse(format!(
                    "Failed to parse the value length from buffer of size {} at offset {}",
                    buf.len(),
                    current_offset
                )));
            }
            let (value_length, bytes_read) = maybe_value_length.unwrap();
            current_offset += bytes_read;

            // Parse key information
            let key_end_offset = current_offset + (key_num_unshared_bytes as usize);
            let key_delta = buf[current_offset..key_end_offset].to_vec();

            // Combine the two parts of the keys and try to deserialize
            current_full_key_buffer.truncate(key_num_shared_bytes as usize);
            current_full_key_buffer.extend_from_slice(&key_delta);
            let maybe_key = K::try_from(current_full_key_buffer.clone());
            let key: K = match maybe_key {
                Err(_base_err) => {
                    return Err(ReadError::FailedToParse(
                        "Failed to fully deserialize an entry. The key may be corrupted."
                            .to_string(),
                    ));
                }
                Ok(k) => k,
            };
            current_offset = key_end_offset;

            // Parse value
            let value_end_offset = current_offset + (value_length as usize);
            let value = buf[current_offset..value_end_offset].to_vec();
            current_offset = value_end_offset;

            // Check if the block offset is also the offset of a restart point.
            // Also, do a check to ensure that the number of shared bytes is zero since restart
            // points do not have any prefix compression. This second check should be largely
            // redundant but here just to be extra safe and verify all invariants.
            if restart_offsets_index < restart_point_offsets.len()
                && block_offset == (restart_point_offsets[restart_offsets_index] as usize)
                && key_num_shared_bytes == 0
            {
                restart_point_indexes.push(block_entries.len());
                restart_offsets_index += 1;
            }

            block_entries.push(BlockEntry {
                block_offset,
                key,
                key_num_shared_bytes,
                key_num_unshared_bytes,
                value_length,
                key_delta,
                value,
            })
        }

        if restart_point_indexes.len() != restart_point_offsets.len() {
            return Err(ReadError::FailedToParse(format!(
                "Failed to match restart point offsets to deserialized vector indexes. There were {} raw offsets but there are {} matched block entries.",
                restart_point_offsets.len(),
                restart_point_indexes.len()
            )));
        }

        Ok((block_entries, restart_point_indexes))
    }

    /**
    Deserialize the offsets to the block's restart points.

    The restart point offsets are serialized as a sequence of fixed-length `u32` values.
    */
    fn deserialize_restart_offsets(
        buf: &[u8],
        expected_num_restart_points: u32,
    ) -> TableReadResult<Vec<u32>> {
        if (buf.len() % SIZE_OF_U32_BYTES) != 0 {
            let error_msg = format!(
                "Failed to parse the restart point offsets in the block. The buffer is not evenly divisible into {} bytes.",
                SIZE_OF_U32_BYTES
            );
            return Err(ReadError::FailedToParse(error_msg));
        }

        let mut restart_points: Vec<u32> = vec![];
        let mut current_offset = 0;
        while current_offset < buf.len() {
            let restart_point =
                u32::decode_fixed(&buf[current_offset..(current_offset + SIZE_OF_U32_BYTES)]);
            restart_points.push(restart_point);
            current_offset += SIZE_OF_U32_BYTES;
        }

        if restart_points.len() != (expected_num_restart_points as usize) {
            let error_msg = format!(
                "Failed to parse the restart point offsets in the block. Expected {} restart points but only found {}.",
                expected_num_restart_points,
                restart_points.len(),
            );
            return Err(ReadError::FailedToParse(error_msg));
        }

        Ok(restart_points)
    }
}

/// Iterator adapter used to maintain iteration state.
pub(crate) struct BlockIter<K>
where
    K: RainDbKeyType,
{
    /// The index to the current entry in the `block_entries` vector.
    current_index: usize,

    /// The deserialized entries within a block.
    block_entries: Arc<Vec<BlockEntry<K>>>,
}

impl<K> RainDbIterator for BlockIter<K>
where
    K: RainDbKeyType,
{
    type Key = K;
    type Error = ReadError;

    fn is_valid(&self) -> bool {
        self.current_index < self.block_entries.len()
    }

    fn seek(&mut self, target: &K) -> Result<(), Self::Error> {
        // Check if the cursor is already at the target
        if self.is_valid() {
            let (current_key, _) = self.current().unwrap();
            if current_key.eq(target) {
                // The cursor is already at the target
                return Ok(());
            }
        }

        // Binary search to find a key >= `target` since block entries are sorted (a.k.a. the
        // leftmost element)
        let mut left = 0;
        let mut right = self.block_entries.len();
        while left < right {
            let mid = (left + right) / 2;
            let mid_entry = &self.block_entries[mid];

            match mid_entry.key.cmp(target) {
                Ordering::Less => {
                    // The key at `mid` is smaller than `target`. So, shift the search space right
                    // to see if we can find a larger key that is smaller than `target`. Our aim is
                    // to get as close as we can to the `target`.
                    left = mid + 1;
                }
                Ordering::Greater | Ordering::Equal => {
                    // The key at `mid` is >= the `target`. So, shift the search space left to see
                    // if we can find a key that is greater than `target` in the case that the
                    // `target` does not exist.
                    right = mid;
                }
            }
        }

        self.current_index = left;

        Ok(())
    }

    fn seek_to_first(&mut self) -> Result<(), Self::Error> {
        self.current_index = 0;

        Ok(())
    }

    fn seek_to_last(&mut self) -> Result<(), Self::Error> {
        self.current_index = self.block_entries.len() - 1;

        Ok(())
    }

    fn next(&mut self) -> Option<(&K, &Vec<u8>)> {
        if self.current_index == self.block_entries.len() || !self.is_valid() {
            // Set the index to an invalid value if we are at the boundaries
            self.current_index = self.block_entries.len();

            return None;
        }

        self.current_index += 1;

        // Return the next item if the iterator is still valid
        if self.is_valid() {
            return self.current();
        }

        None
    }

    fn prev(&mut self) -> Option<(&K, &Vec<u8>)> {
        if self.current_index == 0 || !self.is_valid() {
            // Set the index to an invalid value if we are at the boundaries
            self.current_index = self.block_entries.len();

            return None;
        }

        self.current_index -= 1;

        // Return the previous item if the iterator is still valid
        if self.is_valid() {
            return self.current();
        }

        None
    }

    fn current(&self) -> Option<(&K, &Vec<u8>)> {
        if !self.is_valid() {
            return None;
        }

        let current_entry = &self.block_entries[self.current_index];
        Some((&current_entry.key, &current_entry.value))
    }
}

/**
A newtype that represents a metaindex and meta block key.

The newtype is necessary so that we can implement the `TryFrom` trait to deserialize a byte array
to the string value.
*/
#[derive(Eq, Ord, PartialEq, PartialOrd)]
pub(crate) struct MetaIndexKey(String);

/// Crate-only
impl MetaIndexKey {
    /// Create a new instance of [`MetaIndexKey`].
    pub(crate) fn new(key: String) -> Self {
        Self(key)
    }
}

impl RainDbKeyType for MetaIndexKey {
    fn as_bytes(&self) -> Vec<u8> {
        String::as_bytes(&self.0).to_vec()
    }
}

impl TryFrom<Vec<u8>> for MetaIndexKey {
    type Error = ReadError;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        let maybe_key = std::str::from_utf8(&value);
        match maybe_key {
            Err(_base_err) => Err(ReadError::FailedToParse(
                "Failed to parse the string for the metaindex key.".to_string(),
            )),
            Ok(key) => Ok(MetaIndexKey(key.to_string())),
        }
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use std::rc::Rc;

    use crate::config::PREFIX_COMPRESSION_RESTART_INTERVAL;
    use crate::tables::block_builder::BlockBuilder;
    use crate::Operation;

    use super::*;

    #[test]
    fn block_reader_can_deserialize_a_block() {
        let mut block_builder: BlockBuilder<InternalKey> =
            BlockBuilder::new(PREFIX_COMPRESSION_RESTART_INTERVAL);
        for idx in 0..2_000_usize {
            let num = idx + 100_000;
            let key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num as u64));
        }
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        assert!(
            block_builder.approximate_size() >= block_reader.size(),
            "The size of the deserialized block should not be understated by the builder's \
            approximation."
        );
        assert_eq!(
            block_reader.block_entries.len(),
            2_000,
            "There should be the same number of entries as the block builder that created the \
            block being deserialized."
        );
    }

    #[test]
    fn block_iterator_can_iterate_the_block() {
        let mut block_builder: BlockBuilder<InternalKey> =
            BlockBuilder::new(PREFIX_COMPRESSION_RESTART_INTERVAL);
        for idx in 0..2_000_usize {
            let num = idx + 100_000;
            let key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num as u64));
        }
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        let mut block_iter = block_reader.iter();
        let mut idx: usize = 0;
        while block_iter.is_valid() && idx < 2_000 {
            let num = idx + 100_000;
            let expected_key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            let expected_val = u64::encode_fixed_vec(num as u64);

            let (curr_key, curr_val) = block_iter.current().unwrap();
            assert_eq!(&expected_key, curr_key);
            assert_eq!(&expected_val, curr_val);

            // Move to the next entry
            idx += 1;
            block_iter.next();
        }

        assert!(
            block_iter.next().is_none() && idx == 2_000,
            "Arrived at the last element early (index {idx}). Expected last element at iteration \
            2,000."
        );
        assert!(
            !block_iter.is_valid(),
            "The block iterator should not be valid after moving past the end of the iterator"
        );
    }

    #[test]
    fn block_iterator_can_seek_to_last() {
        let mut block_builder: BlockBuilder<InternalKey> =
            BlockBuilder::new(PREFIX_COMPRESSION_RESTART_INTERVAL);
        for idx in 0..2_000_usize {
            let num = idx + 100_000;
            let key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num as u64));
        }
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        let mut block_iter = block_reader.iter();

        block_iter.seek_to_last().unwrap();
        let (last_key, last_val) = block_iter.current().unwrap();
        assert_eq!(
            last_key,
            &InternalKey::new(
                101_999_usize.to_string().as_bytes().to_vec(),
                1_999,
                Operation::Put,
            ),
            "Found an incorrect last key"
        );
        assert_eq!(
            last_val,
            &u64::encode_fixed_vec(101_999),
            "Found an incorrect last value"
        );
        assert!(block_iter.next().is_none());
        assert!(
            !block_iter.is_valid(),
            "The block iterator should not be valid after moving past the end of the iterator."
        );
        assert!(
            block_iter.prev().is_none(),
            "Must use one of the seek to reset an invalid iterator."
        );
    }

    #[test]
    fn block_iterator_with_one_entry_can_seek_to_targets() {
        let mut block_builder: BlockBuilder<InternalKey> =
            BlockBuilder::new(PREFIX_COMPRESSION_RESTART_INTERVAL);
        let num: u64 = 100_000;
        let key = InternalKey::new(num.to_string().as_bytes().to_vec(), 100, Operation::Put);
        block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num));
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        let mut block_iter = block_reader.iter();

        // Start the iterator at an arbitrary position
        block_iter.seek_to_last().unwrap();

        // Seek key that exists
        block_iter
            .seek(&InternalKey::new(
                100_000.to_string().as_bytes().to_vec(),
                100,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(100_000.to_string().as_bytes().to_vec(), 100, Operation::Put,),
            "Found an incorrect key"
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_000),
            "Found an incorrect value"
        );

        // Seeking a key that does not exist should end at an entry greater than the target
        block_iter
            .seek(&InternalKey::new(
                0_usize.to_string().as_bytes().to_vec(),
                // Sequence numbers are sorted in descending order so this is greater than what
                // exists in the block
                100,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(
                100_000_usize.to_string().as_bytes().to_vec(),
                100,
                Operation::Put,
            ),
            "Found an incorrect key. Should have found a key greater than the target."
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_000),
            "Found an incorrect value. Should have found the value for a key greater than the \
            target."
        );

        // Seeking a key that does not exist and there are no more files should return `None`
        block_iter
            .seek(&InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                // Sequence numbers are sorted in descending order so this is greater than what
                // exists in the block
                116,
                Operation::Put,
            ))
            .unwrap();
        assert!(block_iter.current().is_none());
    }

    #[test]
    fn block_iterator_with_multiple_entries_can_seek_to_targets() {
        let mut block_builder: BlockBuilder<InternalKey> =
            BlockBuilder::new(PREFIX_COMPRESSION_RESTART_INTERVAL);
        for idx in 0..2_000_usize {
            let num = idx + 100_000;
            let key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num as u64));
        }
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        let mut block_iter = block_reader.iter();

        // Start the iterator at an arbitrary position
        block_iter.seek_to_last().unwrap();

        // Seek key that exists
        block_iter
            .seek(&InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                117,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                117,
                Operation::Put,
            ),
            "Found an incorrect key"
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_117),
            "Found an incorrect value"
        );

        // Seeking a key that does not exist should end at an entry greater than the target
        block_iter
            .seek(&InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                // Sequence numbers are sorted in descending order so this is greater than what
                // exists in the block
                118,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                117,
                Operation::Put,
            ),
            "Found an incorrect key. Should have found a key greater than the target."
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_117),
            "Found an incorrect key. Should have found a key greater than the target."
        );

        block_iter
            .seek(&InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                // Sequence numbers are sorted in descending order so this is greater than what
                // exists in the block
                116,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(
                100_118_usize.to_string().as_bytes().to_vec(),
                118,
                Operation::Put,
            ),
            "Found an incorrect key. Should have found a key greater than the target."
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_118),
            "Found an incorrect value. Should have found a value for a key greater than the target."
        );
    }

    #[test]
    fn block_reader_given_a_compression_restart_interval_of_one_can_deserialize_a_block() {
        let mut block_builder: BlockBuilder<InternalKey> = BlockBuilder::new(1);
        for idx in 0..30_usize {
            let num = idx + 100_000;
            let key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num as u64));
        }
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        assert_eq!(
            block_reader.block_entries.len(),
            30,
            "There should be the same number of entries as the block builder that created the \
            block being deserialized."
        );
    }

    #[test]
    fn block_iterator_given_a_compression_restart_interval_of_one_can_seek_to_targets() {
        let mut block_builder: BlockBuilder<InternalKey> = BlockBuilder::new(1);
        for idx in 0..2_000_usize {
            let num = idx + 100_000;
            let key = InternalKey::new(
                num.to_string().as_bytes().to_vec(),
                idx as u64,
                Operation::Put,
            );
            block_builder.add_entry(Rc::new(key), &u64::encode_fixed_vec(num as u64));
        }
        let finalized_block = block_builder.finalize();

        let block_reader: BlockReader<InternalKey> = BlockReader::new(finalized_block).unwrap();
        let mut block_iter = block_reader.iter();

        // Start the iterator at an arbitrary position
        block_iter.seek_to_last().unwrap();

        // Seek key that exists
        block_iter
            .seek(&InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                117,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                117,
                Operation::Put,
            ),
            "Found an incorrect key"
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_117),
            "Found an incorrect value"
        );

        // Seeking a key that does not exist should end at an entry greater than the target
        block_iter
            .seek(&InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                // Sequence numbers are sorted in descending order so this is greater than what
                // exists in the block
                118,
                Operation::Put,
            ))
            .unwrap();
        let (target_key, target_val) = block_iter.current().unwrap();

        assert_eq!(
            target_key,
            &InternalKey::new(
                100_117_usize.to_string().as_bytes().to_vec(),
                117,
                Operation::Put,
            ),
            "Found an incorrect key. Should have found a key greater than the target."
        );
        assert_eq!(
            target_val,
            &u64::encode_fixed_vec(100_117),
            "Found an incorrect value. Should have found a value greater than the target."
        );
    }
}