grenad 0.5.0

Tools to sort, merge, write, and read immutable key-value pairs.
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
use std::convert::TryInto;
use std::io;
use std::io::SeekFrom;
use std::ops::Deref;

use crate::reader::{Block, BlockCursor};
use crate::{CompressionType, Error, Reader};

/// A cursor that can move forward backward and move on a specified key.
#[derive(Clone)]
pub struct ReaderCursor<R> {
    index_block_cursor: IndexBlockCursor,
    current_cursor: Option<BlockCursor<Block>>,
    reader: Reader<R>,
}

impl<R> ReaderCursor<R> {
    /// The currently pointed key and value pair.
    pub fn current(&self) -> Option<(&[u8], &[u8])> {
        self.current_cursor.as_ref().and_then(BlockCursor::current)
    }

    /// Consumes the [`ReaderCursor`] and returns the underlying [`Reader`] type.
    pub fn into_reader(self) -> Reader<R> {
        self.reader
    }

    /// Consumes the [`ReaderCursor`] and returns the underlying [`io::Read`] type.
    ///
    /// The returned [`io::Read`] type has been [`io::Seek`]ed which means that
    /// you must seek it back to the front to be read from the start.
    pub fn into_inner(self) -> R {
        self.reader.into_inner()
    }

    /// Gets a reference to the underlying reader.
    pub fn get_ref(&self) -> &R {
        self.reader.get_ref()
    }

    /// Resets the position of the cursor.
    ///
    /// Useful when you want to be able to call `move_on_next` or `move_on_prev` in a loop
    /// and ensure that it will start from the first or the last value of the cursor.
    pub fn reset(&mut self) {
        self.current_cursor = None;
        self.index_block_cursor.reset();
    }
}

impl<R: io::Read + io::Seek> ReaderCursor<R> {
    /// Creates a new [`ReaderCursor`] by consumming a [`Reader`].
    pub(crate) fn new(reader: Reader<R>) -> Result<ReaderCursor<R>, Error> {
        Ok(ReaderCursor {
            index_block_cursor: IndexBlockCursor::new(
                reader.index_block_offset(),
                reader.compression_type(),
                reader.index_levels(),
            ),
            current_cursor: None,
            reader,
        })
    }

    /// Returns the block containing the entries that is following the current one.
    pub(crate) fn next_block_from_index(&mut self) -> Result<Option<Block>, Error> {
        match self.index_block_cursor.move_on_next(&mut self.reader.reader)? {
            Some((_, offset_bytes)) => {
                let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                self.reader.reader.seek(SeekFrom::Start(offset))?;
                let compression_type = self.reader.compression_type();
                Block::new(&mut self.reader.reader, compression_type).map(Some)
            }
            None => Ok(None),
        }
    }

    /// Returns the block containing the entries that is preceding the current one.
    pub(crate) fn prev_block_from_index(&mut self) -> Result<Option<Block>, Error> {
        match self.index_block_cursor.move_on_prev(&mut self.reader.reader)? {
            Some((_, offset_bytes)) => {
                let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                self.reader.reader.seek(SeekFrom::Start(offset))?;
                let compression_type = self.reader.compression_type();
                Block::new(&mut self.reader.reader, compression_type).map(Some)
            }
            None => Ok(None),
        }
    }

    /// Moves the cursor on the first entry and returns it.
    pub fn move_on_first(&mut self) -> crate::Result<Option<(&[u8], &[u8])>> {
        match self.index_block_cursor.move_on_first(&mut self.reader.reader)? {
            Some((_, offset_bytes)) => {
                let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                self.reader.reader.seek(SeekFrom::Start(offset))?;
                let compression_type = self.reader.compression_type();
                let current_cursor = self.current_cursor.insert(
                    Block::new(&mut self.reader.reader, compression_type)
                        .map(Block::into_cursor)?,
                );
                Ok(current_cursor.move_on_first())
            }
            None => {
                self.current_cursor = None;
                Ok(None)
            }
        }
    }

    /// Moves the cursor on the last entry and returns it.
    pub fn move_on_last(&mut self) -> crate::Result<Option<(&[u8], &[u8])>> {
        match self.index_block_cursor.move_on_last(&mut self.reader.reader)? {
            Some((_, offset_bytes)) => {
                let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                self.reader.reader.seek(SeekFrom::Start(offset))?;
                let compression_type = self.reader.compression_type();
                let current_cursor = self.current_cursor.insert(
                    Block::new(&mut self.reader.reader, compression_type)
                        .map(Block::into_cursor)?,
                );
                Ok(current_cursor.move_on_last())
            }
            None => {
                self.current_cursor = None;
                Ok(None)
            }
        }
    }

    /// Moves the cursor on the entry following the current one and returns it.
    pub fn move_on_next(&mut self) -> crate::Result<Option<(&[u8], &[u8])>> {
        match self.current_cursor.as_mut().map(BlockCursor::move_on_next) {
            Some(Some((key, val))) => {
                let (key, val) = unsafe { crate::transmute_entry_to_static(key, val) };
                Ok(Some((key, val)))
            }
            Some(None) => match self.next_block_from_index()?.map(Block::into_cursor) {
                Some(current_cursor) => {
                    let current_cursor = self.current_cursor.insert(current_cursor);
                    Ok(current_cursor.move_on_first())
                }
                None => Ok(None),
            },
            None => self.move_on_first(),
        }
    }

    /// Moves the cursor on the entry preceding the current one and returns it.
    pub fn move_on_prev(&mut self) -> crate::Result<Option<(&[u8], &[u8])>> {
        match self.current_cursor.as_mut().map(BlockCursor::move_on_prev) {
            Some(Some((key, val))) => {
                let (key, val) = unsafe { crate::transmute_entry_to_static(key, val) };
                Ok(Some((key, val)))
            }
            Some(None) => match self.prev_block_from_index()?.map(Block::into_cursor) {
                Some(current_cursor) => {
                    let current_cursor = self.current_cursor.insert(current_cursor);
                    Ok(current_cursor.move_on_last())
                }
                None => Ok(None),
            },
            None => self.move_on_last(),
        }
    }

    /// Moves the cursor on the entry with a key lower than or equal to the
    /// specified one and returns the corresponding entry.
    pub fn move_on_key_lower_than_or_equal_to<A: AsRef<[u8]>>(
        &mut self,
        target_key: A,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        let target_key = target_key.as_ref();
        match self.move_on_key_greater_than_or_equal_to(target_key)? {
            Some((key, val)) if key == target_key => {
                let (key, val) = unsafe { crate::transmute_entry_to_static(key, val) };
                Ok(Some((key, val)))
            }
            Some(_) => self.move_on_prev(),
            None => self.move_on_last().map(|opt| opt.filter(|(key, _)| *key <= target_key)),
        }
    }

    /// Moves the cursor on the entry with a key greater than or equal to the
    /// specified one and returns the corresponding entry.
    pub fn move_on_key_greater_than_or_equal_to<A: AsRef<[u8]>>(
        &mut self,
        key: A,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        // We move on the block which has a key greater than or equal to the key we are
        // searching for as the key stored in the index block is the last key of the block.
        let key = key.as_ref();
        match self
            .index_block_cursor
            .move_on_key_greater_than_or_equal_to(key, &mut self.reader.reader)?
        {
            Some((_, offset_bytes)) => {
                let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                self.reader.reader.seek(SeekFrom::Start(offset))?;
                let compression_type = self.reader.compression_type();
                let current_cursor = self.current_cursor.insert(
                    Block::new(&mut self.reader.reader, compression_type)
                        .map(Block::into_cursor)?,
                );
                Ok(current_cursor.move_on_key_greater_than_or_equal_to(key))
            }
            None => Ok(None),
        }
    }

    /// Moves the cursor on the entry with a key equal to the key specified and
    /// returns the corresponding entry.
    pub fn move_on_key_equal_to<A: AsRef<[u8]>>(
        &mut self,
        key: A,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        let key = key.as_ref();
        self.move_on_key_greater_than_or_equal_to(key).map(|opt| opt.filter(|(k, _)| *k == key))
    }
}

impl<R> Deref for ReaderCursor<R> {
    type Target = Reader<R>;

    fn deref(&self) -> &Self::Target {
        &self.reader
    }
}

/// Represent an n-depth index block cursor.
#[derive(Clone)]
struct IndexBlockCursor {
    base_block_offset: u64,
    compression_type: CompressionType,
    index_levels: u8,
    /// Defines the different index block cursor for each depth,
    /// associated with the offset at which it has been retrieved.
    /// The length of it must be index_levels + 1 once initialized.
    inner: Option<Vec<(u64, BlockCursor<Block>)>>,
}

impl IndexBlockCursor {
    fn new(
        base_block_offset: u64,
        compression_type: CompressionType,
        index_levels: u8,
    ) -> IndexBlockCursor {
        IndexBlockCursor { base_block_offset, compression_type, index_levels, inner: None }
    }

    fn reset(&mut self) {
        self.inner = None;
    }

    fn move_on_first<R: io::Read + io::Seek>(
        &mut self,
        reader: R,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        self.iter_index_blocks(reader, |c| c.move_on_first())
    }

    fn move_on_last<R: io::Read + io::Seek>(
        &mut self,
        reader: R,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        self.iter_index_blocks(reader, |c| c.move_on_last())
    }

    fn move_on_next<R: io::Read + io::Seek>(
        &mut self,
        reader: R,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        self.recursive_index_block(reader, |c| c.move_on_next())
    }

    fn move_on_prev<R: io::Read + io::Seek>(
        &mut self,
        reader: R,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        self.recursive_index_block(reader, |c| c.move_on_prev())
    }

    fn move_on_key_greater_than_or_equal_to<R: io::Read + io::Seek>(
        &mut self,
        key: &[u8],
        reader: R,
    ) -> crate::Result<Option<(&[u8], &[u8])>> {
        self.iter_index_blocks(reader, |c| c.move_on_key_greater_than_or_equal_to(key))
    }

    fn iter_index_blocks<R, F>(
        &mut self,
        mut reader: R,
        mut mov: F,
    ) -> crate::Result<Option<(&[u8], &[u8])>>
    where
        R: io::Read + io::Seek,
        F: FnMut(&mut BlockCursor<Block>) -> Option<(&[u8], &[u8])>,
    {
        match self.inner.as_mut() {
            Some(inner) => {
                let mut jump_to_offset = self.base_block_offset;
                for (offset, cursor) in inner {
                    // Only seek and load the Block if it is not already the one that we
                    // have in memory, we know that by checking the offset of the blocks.
                    if jump_to_offset != *offset {
                        reader.seek(SeekFrom::Start(jump_to_offset))?;
                        *cursor = Block::new(&mut reader, self.compression_type)
                            .map(Block::into_cursor)?;
                        *offset = jump_to_offset;
                    }

                    match (mov)(cursor) {
                        Some((_, offset_bytes)) => {
                            let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                            jump_to_offset = offset;
                        }
                        None => return Ok(None),
                    }
                }
            }
            None => self.inner = self.initial_index_blocks(reader, mov)?,
        }

        // We return the position pointed by the last index block level.
        // The last index blocks exposes the offsets of the data blocks.
        match self.inner.as_ref().and_then(|inner| inner.last()) {
            Some((_, cursor)) => Ok(cursor.current()),
            None => Ok(None),
        }
    }

    fn recursive_index_block<R, FM>(
        &mut self,
        mut reader: R,
        mut mov: FM,
    ) -> crate::Result<Option<(&[u8], &[u8])>>
    where
        R: io::Read + io::Seek,
        FM: FnMut(&mut BlockCursor<Block>) -> Option<(&[u8], &[u8])>,
    {
        fn recursive<'a, S, FN>(
            reader: &mut S,
            compression_type: CompressionType,
            blocks: &'a mut [(u64, BlockCursor<Block>)],
            mov: &mut FN,
        ) -> crate::Result<Option<(&'a [u8], &'a [u8])>>
        where
            S: io::Read + io::Seek,
            FN: FnMut(&mut BlockCursor<Block>) -> Option<(&[u8], &[u8])>,
        {
            match blocks.split_last_mut() {
                Some(((_offset, cursor), head)) => {
                    match (mov)(cursor) {
                        Some((_key, _offset)) => Ok(cursor.current()),
                        None => {
                            // We reached the end of the current index block, so we ask for the
                            // parent index block to execute the exact same user function and
                            // return the offset where is located the block on which we must be
                            // able to try again.
                            match recursive(reader, compression_type, head, mov)? {
                                Some((_, offset_bytes)) => {
                                    let offset =
                                        offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                                    reader.seek(SeekFrom::Start(offset))?;
                                    *cursor = Block::new(reader, compression_type)
                                        .map(Block::into_cursor)?;

                                    // We return the result of the call has is. If it returns None
                                    // it means we are not able to execute the `mov` function.
                                    Ok((mov)(cursor))
                                }
                                None => Ok(None),
                            }
                        }
                    }
                }
                // If we reach this branch it means that the base index block was
                // not able to execute the `mov` function and that we reached the
                // end of everything! We must STOP!
                None => Ok(None),
            }
        }

        if self.inner.is_none() {
            self.inner = self.initial_index_blocks(&mut reader, &mut mov)?;
        }

        match &mut self.inner {
            Some(inner) => recursive(&mut reader, self.compression_type, inner, &mut mov),
            None => Ok(None),
        }
    }

    /// Returns the index block cursors by calling the user function to load the blocks.
    #[allow(clippy::type_complexity)] // Return type is not THAT complex
    fn initial_index_blocks<R, FM>(
        &mut self,
        mut reader: R,
        mut mov: FM,
    ) -> crate::Result<Option<Vec<(u64, BlockCursor<Block>)>>>
    where
        R: io::Read + io::Seek,
        FM: FnMut(&mut BlockCursor<Block>) -> Option<(&[u8], &[u8])>,
    {
        let depth = self.index_levels as usize + 1;
        let mut inner = Vec::with_capacity(depth);

        let mut jump_to_offset = self.base_block_offset;
        for _ in 0..depth {
            reader.seek(SeekFrom::Start(jump_to_offset))?;
            let mut cursor =
                Block::new(&mut reader, self.compression_type).map(Block::into_cursor)?;

            match (mov)(&mut cursor) {
                Some((_key, offset_bytes)) => {
                    let offset = offset_bytes.try_into().map(u64::from_be_bytes).unwrap();
                    jump_to_offset = offset;
                    inner.push((jump_to_offset, cursor));
                }
                None => return Ok(None),
            }
        }

        Ok(Some(inner))
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryInto;
    use std::io::Cursor;

    use super::*;
    use crate::compression::CompressionType;
    use crate::writer::Writer;

    #[test]
    fn simple_empty() {
        let writer = Writer::builder().index_levels(2).memory();
        let bytes = writer.into_inner().unwrap();
        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();

        let mut cursor = reader.into_cursor().unwrap();
        let result = cursor.move_on_key_greater_than_or_equal_to([0, 0, 0, 0]).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn no_compression() {
        let wb = Writer::builder();
        let mut writer = wb.build(Vec::new());

        for x in 0..2000u32 {
            let x = x.to_be_bytes();
            writer.insert(x, x).unwrap();
        }

        let bytes = writer.into_inner().unwrap();
        assert_ne!(bytes.len(), 0);

        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
        let mut cursor = reader.into_cursor().unwrap();
        let mut x: u32 = 0;

        while let Some((k, v)) = cursor.move_on_next().unwrap() {
            assert_eq!(k, x.to_be_bytes());
            assert_eq!(v, x.to_be_bytes());
            x += 1;
        }

        assert_eq!(x, 2000);
    }

    #[test]
    fn empty() {
        let buffer = Writer::memory().into_inner().unwrap();
        let reader = Reader::new(Cursor::new(buffer)).unwrap();
        let mut cursor = reader.into_cursor().unwrap();
        assert_eq!(cursor.move_on_next().unwrap(), None);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    #[cfg(feature = "snappy")]
    fn snappy_compression() {
        let mut wb = Writer::builder();
        wb.compression_type(CompressionType::Snappy);
        let mut writer = wb.build(Vec::new());

        for x in 0..2000u32 {
            let x = x.to_be_bytes();
            writer.insert(x, x).unwrap();
        }

        let bytes = writer.into_inner().unwrap();
        assert_ne!(bytes.len(), 0);

        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
        let mut cursor = reader.into_cursor().unwrap();
        let mut x: u32 = 0;

        while let Some((k, v)) = cursor.move_on_next().unwrap() {
            assert_eq!(k, x.to_be_bytes());
            assert_eq!(v, x.to_be_bytes());
            x += 1;
        }

        assert_eq!(x, 2000);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn easy_move_on_key_greater_than_or_equal() {
        let mut writer = Writer::memory();
        let mut nums = Vec::new();
        for x in (10..24000i32).step_by(3) {
            nums.push(x);
            let x = x.to_be_bytes();
            writer.insert(x, x).unwrap();
        }

        let bytes = writer.into_inner().unwrap();
        assert_ne!(bytes.len(), 0);

        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
        let mut cursor = reader.into_cursor().unwrap();

        for n in 0..24020i32 {
            match nums.binary_search(&n) {
                Ok(i) => {
                    let n = nums[i];
                    let (k, _) = cursor
                        .move_on_key_greater_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .unwrap();
                    let k = k.try_into().map(i32::from_be_bytes).unwrap();
                    assert_eq!(k, n);
                }
                Err(i) => {
                    let k = cursor
                        .move_on_key_greater_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .map(|(k, _)| k.try_into().map(i32::from_be_bytes).unwrap());
                    assert_eq!(k, nums.get(i).copied());
                }
            }
        }
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn easy_move_on_key_lower_than_or_equal() {
        let mut writer = Writer::memory();
        let mut nums = Vec::new();
        for x in (10..24000i32).step_by(3) {
            nums.push(x);
            let x = x.to_be_bytes();
            writer.insert(x, x).unwrap();
        }

        let bytes = writer.into_inner().unwrap();
        assert_ne!(bytes.len(), 0);

        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
        let mut cursor = reader.into_cursor().unwrap();
        for n in 0..24020i32 {
            match nums.binary_search(&n) {
                Ok(i) => {
                    let n = nums[i];
                    let (k, _) = cursor
                        .move_on_key_lower_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .unwrap();
                    let k = k.try_into().map(i32::from_be_bytes).unwrap();
                    assert_eq!(k, n);
                }
                Err(i) => {
                    let k = cursor
                        .move_on_key_lower_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .map(|(k, _)| k.try_into().map(i32::from_be_bytes).unwrap());
                    let expected = i.checked_sub(1).and_then(|i| nums.get(i)).copied();
                    assert_eq!(k, expected, "queried value {}", n);
                }
            }
        }
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn easy_move_on_key_greater_than_or_equal_index_levels_2() {
        let mut wb = Writer::builder();
        wb.index_levels(2);
        let mut writer = wb.memory();
        let mut nums = Vec::new();
        for x in (10..24000i32).step_by(3) {
            nums.push(x);
            let x = x.to_be_bytes();
            writer.insert(x, x).unwrap();
        }

        let bytes = writer.into_inner().unwrap();
        assert_ne!(bytes.len(), 0);

        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
        let mut cursor = reader.into_cursor().unwrap();

        for n in 0..24020i32 {
            match nums.binary_search(&n) {
                Ok(i) => {
                    let n = nums[i];
                    let (k, _) = cursor
                        .move_on_key_greater_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .unwrap();
                    let k = k.try_into().map(i32::from_be_bytes).unwrap();
                    assert_eq!(k, n);
                }
                Err(i) => {
                    let k = cursor
                        .move_on_key_greater_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .map(|(k, _)| k.try_into().map(i32::from_be_bytes).unwrap());
                    assert_eq!(k, nums.get(i).copied());
                }
            }
        }
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn easy_move_on_key_lower_than_or_equal_index_levels_2() {
        let mut wb = Writer::builder();
        wb.index_levels(2);
        let mut writer = wb.memory();
        let mut nums = Vec::new();
        for x in (10..24000i32).step_by(3) {
            nums.push(x);
            let x = x.to_be_bytes();
            writer.insert(x, x).unwrap();
        }

        let bytes = writer.into_inner().unwrap();
        assert_ne!(bytes.len(), 0);

        let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
        let mut cursor = reader.into_cursor().unwrap();
        for n in 0..24020i32 {
            match nums.binary_search(&n) {
                Ok(i) => {
                    let n = nums[i];
                    let (k, _) = cursor
                        .move_on_key_lower_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .unwrap();
                    let k = k.try_into().map(i32::from_be_bytes).unwrap();
                    assert_eq!(k, n);
                }
                Err(i) => {
                    let k = cursor
                        .move_on_key_lower_than_or_equal_to(n.to_be_bytes())
                        .unwrap()
                        .map(|(k, _)| k.try_into().map(i32::from_be_bytes).unwrap());
                    let expected = i.checked_sub(1).and_then(|i| nums.get(i)).copied();
                    assert_eq!(k, expected, "queried value {}", n);
                }
            }
        }
    }

    quickcheck! {
        #[cfg_attr(miri, ignore)]
        fn qc_compare_to_binary_search(nums: Vec<u32>, queries: Vec<u32>) -> bool {
            let mut nums = nums;
            nums.sort_unstable();
            nums.dedup();

            let mut writer = Writer::builder().index_levels(2).memory();
            for &x in &nums {
                let x = x.to_be_bytes();
                writer.insert(x, x).unwrap();
            }

            let bytes = writer.into_inner().unwrap();
            let reader = Reader::new(Cursor::new(bytes.as_slice())).unwrap();
            let mut cursor = reader.into_cursor().unwrap();

            for q in queries {
                match nums.binary_search(&q) {
                    Ok(i) => {
                        let q = nums[i];
                        let (k, _) = cursor
                            .move_on_key_lower_than_or_equal_to(q.to_be_bytes())
                            .unwrap()
                            .unwrap();
                        let k = k.try_into().map(u32::from_be_bytes).unwrap();
                        if k != q {
                            return false;
                        }
                    }
                    Err(i) => {
                        let k = cursor
                            .move_on_key_lower_than_or_equal_to(q.to_be_bytes())
                            .unwrap()
                            .map(|(k, _)| k.try_into().map(u32::from_be_bytes).unwrap());
                        let expected = i.checked_sub(1).and_then(|i| nums.get(i)).copied();
                        if k != expected {
                            return false;
                        }
                    }
                }
            }

            return true;
        }
    }
}