changeset 95:da4beef6e417

Add `current_key()` function to SSIterator. In case we are not interested in the actual value, but just want to check that an entry exists, we only need access to the key and can save the time to copy the (possible large) value. In addition, returning the key as a reference to an already existing field allows even more optimized acccess.
author Thomas Krause <krauseto@hu-berlin.de>
date Sat, 04 Apr 2020 18:56:28 +0200
parents 7b43cb6b9666
children e93d0baf758a
files src/block.rs src/table_reader.rs src/test_util.rs src/types.rs
diffstat 4 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/block.rs	Mon Feb 24 13:17:14 2020 +0100
+++ b/src/block.rs	Sat Apr 04 18:56:28 2020 +0200
@@ -297,6 +297,14 @@
             false
         }
     }
+
+    fn current_key(&self) -> Option<&[u8]> {
+        if self.valid() {
+            Some(&self.key)
+        } else {
+            None
+        }
+    }
 }
 
 #[cfg(test)]
--- a/src/table_reader.rs	Mon Feb 24 13:17:14 2020 +0100
+++ b/src/table_reader.rs	Sat Apr 04 18:56:28 2020 +0200
@@ -335,6 +335,14 @@
             false
         }
     }
+
+    fn current_key(&self) -> Option<&[u8]> {
+        if let Some(ref cb) = self.current_block {
+            cb.current_key()
+        } else {
+            None
+        }
+    }
 }
 
 #[cfg(test)]
--- a/src/test_util.rs	Mon Feb 24 13:17:14 2020 +0100
+++ b/src/test_util.rs	Sat Apr 04 18:56:28 2020 +0200
@@ -48,6 +48,15 @@
             false
         }
     }
+
+    fn current_key(&self) -> Option<&[u8]> {
+        if self.init && self.ix < self.v.len() {
+            Some(self.v[self.ix].0)
+        } else {
+            None
+        }
+    }
+
     fn valid(&self) -> bool {
         self.init && self.ix < self.v.len()
     }
@@ -95,14 +104,18 @@
     assert!(it.advance());
     assert!(it.valid());
     let first = current_key_val(&it);
+    assert_eq!(first.as_ref().unwrap().0, it.current_key().unwrap());
     assert!(it.advance());
     let second = current_key_val(&it);
+    assert_eq!(second.as_ref().unwrap().0, it.current_key().unwrap());
     assert!(it.advance());
     let third = current_key_val(&it);
+    assert_eq!(third.as_ref().unwrap().0, it.current_key().unwrap());
     // fourth (last) element
     assert!(it.advance());
     assert!(it.valid());
     let fourth = current_key_val(&it);
+    assert_eq!(fourth.as_ref().unwrap().0, it.current_key().unwrap());
     // past end is invalid
     assert!(!it.advance());
     assert!(!it.valid());
--- a/src/types.rs	Mon Feb 24 13:17:14 2020 +0100
+++ b/src/types.rs	Sat Apr 04 18:56:28 2020 +0200
@@ -69,6 +69,8 @@
     fn advance(&mut self) -> bool;
     /// Return the current item (i.e. the item most recently returned by `next()`).
     fn current(&self, key: &mut Vec<u8>, val: &mut Vec<u8>) -> bool;
+    /// Return a reference to the key of the current item (i.e. the item most recently returned by `next()`).
+    fn current_key(&self) -> Option<&[u8]>;
     /// Seek the iterator to `key` or the next bigger key. If the seek is invalid (past last
     /// element, or before first element), the iterator is `reset()` and not valid.
     fn seek(&mut self, key: &[u8]);
@@ -124,6 +126,9 @@
     fn current(&self, key: &mut Vec<u8>, val: &mut Vec<u8>) -> bool {
         self.as_ref().current(key, val)
     }
+    fn current_key(&self) -> Option<&[u8]> {
+        self.as_ref().current_key()
+    }
     fn seek(&mut self, key: &[u8]) {
         self.as_mut().seek(key)
     }