Skip to main content

rust_rocksdb/
db_iterator.rs

1// Copyright 2020 Tyler Neely
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    Error, ReadOptions, WriteBatch,
17    db::{DB, DBAccess},
18    ffi,
19};
20use libc::{c_char, c_uchar, size_t};
21use std::mem::ManuallyDrop;
22use std::{marker::PhantomData, slice};
23
24/// A type alias to keep compatibility. See [`DBRawIteratorWithThreadMode`] for details
25pub type DBRawIterator<'a> = DBRawIteratorWithThreadMode<'a, DB>;
26
27/// A low-level iterator over a database or column family, created by [`DB::raw_iterator`]
28/// and other `raw_iterator_*` methods.
29///
30/// This iterator replicates RocksDB's API. It should provide better
31/// performance and more features than [`DBIteratorWithThreadMode`], which is a standard
32/// Rust [`std::iter::Iterator`].
33///
34/// ```
35/// use rust_rocksdb::{DB, Options};
36///
37/// let tempdir = tempfile::Builder::new()
38///     .prefix("_path_for_rocksdb_storage4")
39///     .tempdir()
40///     .expect("Failed to create temporary path for the _path_for_rocksdb_storage4.");
41/// let path = tempdir.path();
42/// {
43///     let db = DB::open_default(path).unwrap();
44///     let mut iter = db.raw_iterator();
45///
46///     // Forwards iteration
47///     iter.seek_to_first();
48///     while iter.valid() {
49///         println!("Saw {:?} {:?}", iter.key(), iter.value());
50///         iter.next();
51///     }
52///
53///     // Reverse iteration
54///     iter.seek_to_last();
55///     while iter.valid() {
56///         println!("Saw {:?} {:?}", iter.key(), iter.value());
57///         iter.prev();
58///     }
59///
60///     // Seeking
61///     iter.seek(b"my key");
62///     while iter.valid() {
63///         println!("Saw {:?} {:?}", iter.key(), iter.value());
64///         iter.next();
65///     }
66///
67///     // Reverse iteration from key
68///     // Note, use seek_for_prev when reversing because if this key doesn't exist,
69///     // this will make the iterator start from the previous key rather than the next.
70///     iter.seek_for_prev(b"my key");
71///     while iter.valid() {
72///         println!("Saw {:?} {:?}", iter.key(), iter.value());
73///         iter.prev();
74///     }
75/// }
76/// let _ = DB::destroy(&Options::default(), path);
77/// ```
78pub struct DBRawIteratorWithThreadMode<'a, D: DBAccess> {
79    inner: std::ptr::NonNull<ffi::rocksdb_iterator_t>,
80
81    /// When iterate_lower_bound or iterate_upper_bound are set, the inner
82    /// C iterator keeps a pointer to the upper bound inside `_readopts`.
83    /// Storing this makes sure the upper bound is always alive when the
84    /// iterator is being used.
85    ///
86    /// And yes, we need to store the entire ReadOptions structure since C++
87    /// ReadOptions keep reference to C rocksdb_readoptions_t wrapper which
88    /// point to vectors we own.  See issue #660.
89    readopts: ReadOptions,
90
91    db: PhantomData<&'a D>,
92}
93
94impl<'a, D: DBAccess> DBRawIteratorWithThreadMode<'a, D> {
95    pub(crate) fn new(db: &D, readopts: ReadOptions) -> Self {
96        let inner = unsafe { db.create_iterator(&readopts) };
97        Self::from_inner(inner, readopts)
98    }
99
100    pub(crate) fn new_cf(
101        db: &'a D,
102        cf_handle: *mut ffi::rocksdb_column_family_handle_t,
103        readopts: ReadOptions,
104    ) -> Self {
105        let inner = unsafe { db.create_iterator_cf(cf_handle, &readopts) };
106        Self::from_inner(inner, readopts)
107    }
108
109    pub(crate) fn from_inner(inner: *mut ffi::rocksdb_iterator_t, readopts: ReadOptions) -> Self {
110        // This unwrap will never fail since rocksdb_create_iterator and
111        // rocksdb_create_iterator_cf functions always return non-null. They
112        // use new and deference the result so any nulls would end up with SIGSEGV
113        // there and we would have a bigger issue.
114        let inner = std::ptr::NonNull::new(inner).unwrap();
115        Self {
116            inner,
117            readopts,
118            db: PhantomData,
119        }
120    }
121
122    pub(crate) fn into_inner(self) -> (std::ptr::NonNull<ffi::rocksdb_iterator_t>, ReadOptions) {
123        let value = ManuallyDrop::new(self);
124        // SAFETY: value won't be used beyond this point
125        let inner = unsafe { std::ptr::read(&raw const value.inner) };
126        let readopts = unsafe { std::ptr::read(&raw const value.readopts) };
127
128        (inner, readopts)
129    }
130
131    /// Returns `true` if the iterator is valid. An iterator is invalidated when
132    /// it reaches the end of its defined range, or when it encounters an error.
133    ///
134    /// To check whether the iterator encountered an error after `valid` has
135    /// returned `false`, use the [`status`](DBRawIteratorWithThreadMode::status) method. `status` will never
136    /// return an error when `valid` is `true`.
137    pub fn valid(&self) -> bool {
138        unsafe { ffi::rocksdb_iter_valid(self.inner.as_ptr()) != 0 }
139    }
140
141    /// Returns an error `Result` if the iterator has encountered an error
142    /// during operation. When an error is encountered, the iterator is
143    /// invalidated and [`valid`](DBRawIteratorWithThreadMode::valid) will return `false` when called.
144    ///
145    /// Performing a seek will discard the current status.
146    pub fn status(&self) -> Result<(), Error> {
147        unsafe {
148            ffi_try!(ffi::rocksdb_iter_get_error(self.inner.as_ptr()));
149        }
150        Ok(())
151    }
152
153    /// Refreshes the iterator to represent the latest state of the DB.
154    /// The iterator is invalidated after this call and must be re-sought
155    /// before use.
156    ///
157    /// If the iterator was created with a snapshot, the refreshed iterator
158    /// will no longer use that snapshot and will instead read the latest
159    /// DB state. The snapshot itself is not released; it remains valid and
160    /// will be released when the owning [`crate::SnapshotWithThreadMode`] is dropped.
161    pub fn refresh(&mut self) -> Result<(), Error> {
162        unsafe {
163            ffi_try!(ffi::rocksdb_iter_refresh(self.inner.as_ptr()));
164        }
165        Ok(())
166    }
167
168    /// Seeks to the first key in the database.
169    ///
170    /// # Examples
171    ///
172    /// ```rust
173    /// use rust_rocksdb::{DB, Options};
174    ///
175    /// let tempdir = tempfile::Builder::new()
176    ///     .prefix("_path_for_rocksdb_storage5")
177    ///     .tempdir()
178    ///     .expect("Failed to create temporary path for the _path_for_rocksdb_storage5.");
179    /// let path = tempdir.path();
180    /// {
181    ///     let db = DB::open_default(path).unwrap();
182    ///     let mut iter = db.raw_iterator();
183    ///
184    ///     // Iterate all keys from the start in lexicographic order
185    ///     iter.seek_to_first();
186    ///
187    ///     while iter.valid() {
188    ///         println!("{:?} {:?}", iter.key(), iter.value());
189    ///         iter.next();
190    ///     }
191    ///
192    ///     // Read just the first key
193    ///     iter.seek_to_first();
194    ///
195    ///     if iter.valid() {
196    ///         println!("{:?} {:?}", iter.key(), iter.value());
197    ///     } else {
198    ///         // There are no keys in the database
199    ///     }
200    /// }
201    /// let _ = DB::destroy(&Options::default(), path);
202    /// ```
203    pub fn seek_to_first(&mut self) {
204        unsafe {
205            ffi::rocksdb_iter_seek_to_first(self.inner.as_ptr());
206        }
207    }
208
209    /// Seeks to the last key in the database.
210    ///
211    /// # Examples
212    ///
213    /// ```rust
214    /// use rust_rocksdb::{DB, Options};
215    ///
216    /// let tempdir = tempfile::Builder::new()
217    ///     .prefix("_path_for_rocksdb_storage6")
218    ///     .tempdir()
219    ///     .expect("Failed to create temporary path for the _path_for_rocksdb_storage6.");
220    /// let path = tempdir.path();
221    /// {
222    ///     let db = DB::open_default(path).unwrap();
223    ///     let mut iter = db.raw_iterator();
224    ///
225    ///     // Iterate all keys from the end in reverse lexicographic order
226    ///     iter.seek_to_last();
227    ///
228    ///     while iter.valid() {
229    ///         println!("{:?} {:?}", iter.key(), iter.value());
230    ///         iter.prev();
231    ///     }
232    ///
233    ///     // Read just the last key
234    ///     iter.seek_to_last();
235    ///
236    ///     if iter.valid() {
237    ///         println!("{:?} {:?}", iter.key(), iter.value());
238    ///     } else {
239    ///         // There are no keys in the database
240    ///     }
241    /// }
242    /// let _ = DB::destroy(&Options::default(), path);
243    /// ```
244    pub fn seek_to_last(&mut self) {
245        unsafe {
246            ffi::rocksdb_iter_seek_to_last(self.inner.as_ptr());
247        }
248    }
249
250    /// Seeks to the specified key or the first key that lexicographically follows it.
251    ///
252    /// This method will attempt to seek to the specified key. If that key does not exist, it will
253    /// find and seek to the key that lexicographically follows it instead.
254    ///
255    /// # Examples
256    ///
257    /// ```rust
258    /// use rust_rocksdb::{DB, Options};
259    ///
260    /// let tempdir = tempfile::Builder::new()
261    ///     .prefix("_path_for_rocksdb_storage7")
262    ///     .tempdir()
263    ///     .expect("Failed to create temporary path for the _path_for_rocksdb_storage7.");
264    /// let path = tempdir.path();
265    /// {
266    ///     let db = DB::open_default(path).unwrap();
267    ///     let mut iter = db.raw_iterator();
268    ///
269    ///     // Read the first key that starts with 'a'
270    ///     iter.seek(b"a");
271    ///
272    ///     if iter.valid() {
273    ///         println!("{:?} {:?}", iter.key(), iter.value());
274    ///     } else {
275    ///         // There are no keys in the database
276    ///     }
277    /// }
278    /// let _ = DB::destroy(&Options::default(), path);
279    /// ```
280    pub fn seek<K: AsRef<[u8]>>(&mut self, key: K) {
281        let key = key.as_ref();
282
283        unsafe {
284            ffi::rocksdb_iter_seek(
285                self.inner.as_ptr(),
286                key.as_ptr() as *const c_char,
287                key.len() as size_t,
288            );
289        }
290    }
291
292    /// Seeks to the specified key, or the first key that lexicographically precedes it.
293    ///
294    /// Like ``.seek()`` this method will attempt to seek to the specified key.
295    /// The difference with ``.seek()`` is that if the specified key do not exist, this method will
296    /// seek to key that lexicographically precedes it instead.
297    ///
298    /// # Examples
299    ///
300    /// ```rust
301    /// use rust_rocksdb::{DB, Options};
302    ///
303    /// let tempdir = tempfile::Builder::new()
304    ///     .prefix("_path_for_rocksdb_storage8")
305    ///     .tempdir()
306    ///     .expect("Failed to create temporary path for the _path_for_rocksdb_storage8.");
307    /// let path = tempdir.path();
308    /// {
309    ///     let db = DB::open_default(path).unwrap();
310    ///     let mut iter = db.raw_iterator();
311    ///
312    ///     // Read the last key that starts with 'a'
313    ///     iter.seek_for_prev(b"b");
314    ///
315    ///     if iter.valid() {
316    ///         println!("{:?} {:?}", iter.key(), iter.value());
317    ///     } else {
318    ///         // There are no keys in the database
319    ///     }
320    /// }
321    /// let _ = DB::destroy(&Options::default(), path);
322    /// ```
323    pub fn seek_for_prev<K: AsRef<[u8]>>(&mut self, key: K) {
324        let key = key.as_ref();
325
326        unsafe {
327            ffi::rocksdb_iter_seek_for_prev(
328                self.inner.as_ptr(),
329                key.as_ptr() as *const c_char,
330                key.len() as size_t,
331            );
332        }
333    }
334
335    /// Seeks to the next key.
336    pub fn next(&mut self) {
337        if self.valid() {
338            unsafe {
339                ffi::rocksdb_iter_next(self.inner.as_ptr());
340            }
341        }
342    }
343
344    /// Seeks to the previous key.
345    pub fn prev(&mut self) {
346        if self.valid() {
347            unsafe {
348                ffi::rocksdb_iter_prev(self.inner.as_ptr());
349            }
350        }
351    }
352
353    /// Returns a slice of the current key.
354    pub fn key(&self) -> Option<&[u8]> {
355        if self.valid() {
356            // SAFETY: We just checked that the iterator is valid.
357            Some(unsafe { self.key_impl() })
358        } else {
359            None
360        }
361    }
362
363    /// Returns a slice of the current value.
364    pub fn value(&self) -> Option<&[u8]> {
365        if self.valid() {
366            // SAFETY: We just checked that the iterator is valid.
367            Some(unsafe { self.value_impl() })
368        } else {
369            None
370        }
371    }
372
373    /// Returns pair with slice of the current key and current value.
374    pub fn item(&self) -> Option<(&[u8], &[u8])> {
375        if self.valid() {
376            // SAFETY: We just checked that the iterator is valid.
377            Some(unsafe { (self.key_impl(), self.value_impl()) })
378        } else {
379            None
380        }
381    }
382
383    /// Returns a slice of the current key.
384    ///
385    /// # Safety
386    ///
387    /// The iterator must be valid (i.e., `valid()` returns true). Calling this
388    /// method when the iterator is invalid is undefined behavior, as RocksDB
389    /// may return an invalid pointer.
390    ///
391    /// Uses `rocksdb_iter_key_slice` which returns a `rocksdb_slice_t` by value,
392    /// avoiding the overhead of output parameters compared to `rocksdb_iter_key`.
393    unsafe fn key_impl(&self) -> &[u8] {
394        unsafe {
395            let slice = ffi::rocksdb_iter_key_slice(self.inner.as_ptr());
396            slice::from_raw_parts(slice.data as *const c_uchar, slice.size)
397        }
398    }
399
400    /// Returns a slice of the current value.
401    ///
402    /// # Safety
403    ///
404    /// The iterator must be valid (i.e., `valid()` returns true). Calling this
405    /// method when the iterator is invalid is undefined behavior, as RocksDB
406    /// may return an invalid pointer.
407    ///
408    /// Uses `rocksdb_iter_value_slice` which returns a `rocksdb_slice_t` by value,
409    /// avoiding the overhead of output parameters compared to `rocksdb_iter_value`.
410    unsafe fn value_impl(&self) -> &[u8] {
411        unsafe {
412            let slice = ffi::rocksdb_iter_value_slice(self.inner.as_ptr());
413            slice::from_raw_parts(slice.data as *const c_uchar, slice.size)
414        }
415    }
416
417    /// Returns the timestamp of the current entry.
418    ///
419    /// # Safety
420    ///
421    /// The iterator must be valid (i.e., `valid()` returns true). Calling this
422    /// method when the iterator is invalid is undefined behavior, as RocksDB
423    /// may return an invalid pointer.
424    ///
425    /// Additionally, the column family must have been configured with user defined timestamps.
426    /// Calling this method on iteration of a column family without user defined timestamps configured
427    /// will result in assertion failures in debug builds and undefined behaviour in release builds.
428    ///
429    /// Uses `rocksdb_iter_timestamp_slice` which returns a `rocksdb_slice_t` by value,
430    /// avoiding the overhead of output parameters compared to `rocksdb_iter_timestamp`.
431    pub unsafe fn timestamp(&self) -> &[u8] {
432        unsafe {
433            let slice = ffi::rocksdb_iter_timestamp_slice(self.inner.as_ptr());
434            slice::from_raw_parts(slice.data as *const c_uchar, slice.size)
435        }
436    }
437}
438
439impl<D: DBAccess> Drop for DBRawIteratorWithThreadMode<'_, D> {
440    fn drop(&mut self) {
441        unsafe {
442            ffi::rocksdb_iter_destroy(self.inner.as_ptr());
443        }
444    }
445}
446
447unsafe impl<D: DBAccess> Send for DBRawIteratorWithThreadMode<'_, D> {}
448unsafe impl<D: DBAccess> Sync for DBRawIteratorWithThreadMode<'_, D> {}
449
450/// A type alias to keep compatibility. See [`DBIteratorWithThreadMode`] for details
451pub type DBIterator<'a> = DBIteratorWithThreadMode<'a, DB>;
452
453/// A standard Rust [`Iterator`] over a database or column family.
454///
455/// As an alternative, [`DBRawIteratorWithThreadMode`] is a low level wrapper around
456/// RocksDB's API, which can provide better performance and more features.
457///
458/// ```
459/// use rust_rocksdb::{DB, Direction, IteratorMode, Options};
460///
461/// let tempdir = tempfile::Builder::new()
462///     .prefix("_path_for_rocksdb_storage2")
463///     .tempdir()
464///     .expect("Failed to create temporary path for the _path_for_rocksdb_storage2.");
465/// let path = tempdir.path();
466/// {
467///     let db = DB::open_default(path).unwrap();
468///     let mut iter = db.iterator(IteratorMode::Start); // Always iterates forward
469///     for item in iter {
470///         let (key, value) = item.unwrap();
471///         println!("Saw {:?} {:?}", key, value);
472///     }
473///     iter = db.iterator(IteratorMode::End);  // Always iterates backward
474///     for item in iter {
475///         let (key, value) = item.unwrap();
476///         println!("Saw {:?} {:?}", key, value);
477///     }
478///     iter = db.iterator(IteratorMode::From(b"my key", Direction::Forward)); // From a key in Direction::{forward,reverse}
479///     for item in iter {
480///         let (key, value) = item.unwrap();
481///         println!("Saw {:?} {:?}", key, value);
482///     }
483///
484///     // You can seek with an existing Iterator instance, too
485///     iter = db.iterator(IteratorMode::Start);
486///     iter.set_mode(IteratorMode::From(b"another key", Direction::Reverse));
487///     for item in iter {
488///         let (key, value) = item.unwrap();
489///         println!("Saw {:?} {:?}", key, value);
490///     }
491/// }
492/// let _ = DB::destroy(&Options::default(), path);
493/// ```
494///
495/// An iterator must not outlive the `DB` it iterates over:
496///
497/// ```compile_fail,E0597
498/// use rust_rocksdb::{IteratorMode, DB};
499///
500/// let _iter = {
501///     let db = DB::open_default("foo").unwrap();
502///     db.iterator(IteratorMode::Start)
503/// };
504/// ```
505pub struct DBIteratorWithThreadMode<'a, D: DBAccess> {
506    raw: DBRawIteratorWithThreadMode<'a, D>,
507    direction: Direction,
508    done: bool,
509}
510
511#[derive(Copy, Clone)]
512pub enum Direction {
513    Forward,
514    Reverse,
515}
516
517pub type KVBytes = (Box<[u8]>, Box<[u8]>);
518
519#[derive(Copy, Clone)]
520pub enum IteratorMode<'a> {
521    Start,
522    End,
523    From(&'a [u8], Direction),
524}
525
526impl<'a, D: DBAccess> DBIteratorWithThreadMode<'a, D> {
527    pub(crate) fn new(db: &D, readopts: ReadOptions, mode: IteratorMode) -> Self {
528        Self::from_raw(DBRawIteratorWithThreadMode::new(db, readopts), mode)
529    }
530
531    pub(crate) fn new_cf(
532        db: &'a D,
533        cf_handle: *mut ffi::rocksdb_column_family_handle_t,
534        readopts: ReadOptions,
535        mode: IteratorMode,
536    ) -> Self {
537        Self::from_raw(
538            DBRawIteratorWithThreadMode::new_cf(db, cf_handle, readopts),
539            mode,
540        )
541    }
542
543    fn from_raw(raw: DBRawIteratorWithThreadMode<'a, D>, mode: IteratorMode) -> Self {
544        let mut rv = DBIteratorWithThreadMode {
545            raw,
546            direction: Direction::Forward, // blown away by set_mode()
547            done: false,
548        };
549        rv.set_mode(mode);
550        rv
551    }
552
553    pub fn set_mode(&mut self, mode: IteratorMode) {
554        self.done = false;
555        self.direction = match mode {
556            IteratorMode::Start => {
557                self.raw.seek_to_first();
558                Direction::Forward
559            }
560            IteratorMode::End => {
561                self.raw.seek_to_last();
562                Direction::Reverse
563            }
564            IteratorMode::From(key, Direction::Forward) => {
565                self.raw.seek(key);
566                Direction::Forward
567            }
568            IteratorMode::From(key, Direction::Reverse) => {
569                self.raw.seek_for_prev(key);
570                Direction::Reverse
571            }
572        };
573    }
574
575    /// Refreshes the iterator, then re-seeks using the given mode.
576    ///
577    /// After a refresh the underlying iterator is invalidated, so a mode
578    /// must be provided to reposition it.
579    pub fn refresh(&mut self, mode: IteratorMode) -> Result<(), Error> {
580        self.raw.refresh()?;
581        self.set_mode(mode);
582        Ok(())
583    }
584}
585
586impl<D: DBAccess> Iterator for DBIteratorWithThreadMode<'_, D> {
587    type Item = Result<KVBytes, Error>;
588
589    fn next(&mut self) -> Option<Result<KVBytes, Error>> {
590        if self.done {
591            None
592        } else if let Some((key, value)) = self.raw.item() {
593            let item = (Box::from(key), Box::from(value));
594            match self.direction {
595                Direction::Forward => self.raw.next(),
596                Direction::Reverse => self.raw.prev(),
597            }
598            Some(Ok(item))
599        } else {
600            self.done = true;
601            self.raw.status().err().map(Result::Err)
602        }
603    }
604}
605
606impl<D: DBAccess> std::iter::FusedIterator for DBIteratorWithThreadMode<'_, D> {}
607
608impl<'a, D: DBAccess> Into<DBRawIteratorWithThreadMode<'a, D>> for DBIteratorWithThreadMode<'a, D> {
609    fn into(self) -> DBRawIteratorWithThreadMode<'a, D> {
610        self.raw
611    }
612}
613
614/// Iterates the batches of writes since a given sequence number.
615///
616/// `DBWALIterator` is returned by `DB::get_updates_since()` and will return the
617/// batches of write operations that have occurred since a given sequence number
618/// (see `DB::latest_sequence_number()`). This iterator cannot be constructed by
619/// the application.
620///
621/// The iterator item type is a tuple of (`u64`, `WriteBatch`) where the first
622/// value is the sequence number of the associated write batch.
623///
624pub struct DBWALIterator {
625    pub(crate) inner: *mut ffi::rocksdb_wal_iterator_t,
626    pub(crate) start_seq_number: u64,
627}
628
629impl DBWALIterator {
630    /// Returns `true` if the iterator is valid. An iterator is invalidated when
631    /// it reaches the end of its defined range, or when it encounters an error.
632    ///
633    /// To check whether the iterator encountered an error after `valid` has
634    /// returned `false`, use the [`status`](DBWALIterator::status) method.
635    /// `status` will never return an error when `valid` is `true`.
636    pub fn valid(&self) -> bool {
637        unsafe { ffi::rocksdb_wal_iter_valid(self.inner) != 0 }
638    }
639
640    /// Returns an error `Result` if the iterator has encountered an error
641    /// during operation. When an error is encountered, the iterator is
642    /// invalidated and [`valid`](DBWALIterator::valid) will return `false` when
643    /// called.
644    pub fn status(&self) -> Result<(), Error> {
645        unsafe {
646            ffi_try!(ffi::rocksdb_wal_iter_status(self.inner));
647        }
648        Ok(())
649    }
650}
651
652impl Iterator for DBWALIterator {
653    type Item = Result<(u64, WriteBatch), Error>;
654
655    fn next(&mut self) -> Option<Self::Item> {
656        if !self.valid() {
657            return None;
658        }
659
660        let mut seq: u64 = 0;
661        let mut batch = WriteBatch {
662            inner: unsafe { ffi::rocksdb_wal_iter_get_batch(self.inner, &raw mut seq) },
663        };
664
665        // if the initial sequence number is what was requested we skip it to
666        // only provide changes *after* it
667        while seq <= self.start_seq_number {
668            unsafe {
669                ffi::rocksdb_wal_iter_next(self.inner);
670            }
671
672            if !self.valid() {
673                return None;
674            }
675
676            // this drops which in turn frees the skipped batch
677            batch = WriteBatch {
678                inner: unsafe { ffi::rocksdb_wal_iter_get_batch(self.inner, &raw mut seq) },
679            };
680        }
681
682        if !self.valid() {
683            return self.status().err().map(Result::Err);
684        }
685
686        // Seek to the next write batch.
687        // Note that WriteBatches live independently of the WAL iterator so this is safe to do
688        unsafe {
689            ffi::rocksdb_wal_iter_next(self.inner);
690        }
691
692        Some(Ok((seq, batch)))
693    }
694}
695
696impl Drop for DBWALIterator {
697    fn drop(&mut self) {
698        unsafe {
699            ffi::rocksdb_wal_iter_destroy(self.inner);
700        }
701    }
702}