changeset 316:3b4c55889370

skipmap: Fix bug in iterator and add regression test.
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 29 Sep 2017 19:39:11 +0200
parents 678ab5ac55d5
children 13a35941c2ff
files src/skipmap.rs
diffstat 1 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/skipmap.rs	Fri Sep 29 19:38:44 2017 +0200
+++ b/src/skipmap.rs	Fri Sep 29 19:39:11 2017 +0200
@@ -74,10 +74,11 @@
     pub fn contains(&self, key: &[u8]) -> bool {
         self.map.borrow().contains(key)
     }
+
+    /// inserts a key into the table. key may not be empty.
     pub fn insert(&mut self, key: Vec<u8>, val: Vec<u8>) {
-        // TODO: Possibly wrap into Mutex.
+        assert!(!key.is_empty());
         self.map.borrow_mut().insert(key, val);
-        // Rc::get_mut(&mut self.map).unwrap().insert(key, val);
     }
 
     pub fn iter(&self) -> SkipMapIter {
@@ -140,7 +141,7 @@
         }
 
         unsafe {
-            if current.is_null() {
+            if current.is_null() || (*current).key.is_empty() {
                 return None;
             } else if self.cmp.cmp(&(*current).key, key) == Ordering::Less {
                 return None;
@@ -349,6 +350,7 @@
 #[cfg(test)]
 pub mod tests {
     use super::*;
+    use cmp::MemtableKeyCmp;
     use test_util::{test_iterator_properties, LdbIteratorIter};
     use types::current_key_val;
     use options;
@@ -418,6 +420,17 @@
     }
 
     #[test]
+    fn test_empty_skipmap_find_memtable_cmp() {
+        // Regression test: Make sure comparator isn't called with empty key.
+        let cmp: Rc<Box<Cmp>> = Rc::new(Box::new(MemtableKeyCmp(options::for_test().cmp)));
+        let skm = SkipMap::new(cmp);
+
+        let mut it = skm.iter();
+        it.seek("abc".as_bytes());
+        assert!(!it.valid());
+    }
+
+    #[test]
     fn test_skipmap_iterator_0() {
         let skm = SkipMap::new(options::for_test().cmp);
         let mut i = 0;