From 342e50d387dcb46f398dbf44f965f6ff49a303df Mon Sep 17 00:00:00 2001 From: Myk Melez Date: Thu, 24 May 2018 19:54:30 -0700 Subject: [PATCH] ignore NotFound error when seeking to key in iter_from/iter_dup_from --- src/cursor.rs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/cursor.rs b/src/cursor.rs index 11c189a..1f0dc63 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -61,9 +61,13 @@ pub trait Cursor<'txn> { /// For databases with duplicate data items (`DatabaseFlags::DUP_SORT`), the /// duplicate data items of each key will be returned before moving on to /// the next key. - fn iter_from(&mut self, key: K) -> Result> where K: AsRef<[u8]> { - try!(self.get(Some(key.as_ref()), None, ffi::MDB_SET_RANGE)); - Ok(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT)) + fn iter_from(&mut self, key: K) -> Iter<'txn> where K: AsRef<[u8]> { + match self.get(Some(key.as_ref()), None, ffi::MDB_SET_RANGE) { + Err(Error::NotFound) => Ok(()), + Err(error) => Err(error), + Ok(_) => Ok(()), + }.unwrap(); + Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT) } /// Iterate over duplicate database items. The iterator will begin with the @@ -82,9 +86,13 @@ pub trait Cursor<'txn> { /// Iterate over duplicate items in the database starting from the given /// key. Each item will be returned as an iterator of its duplicates. - fn iter_dup_from(&mut self, key: &K) -> Result> where K: AsRef<[u8]> { - try!(self.get(Some(key.as_ref()), None, ffi::MDB_SET_RANGE)); - Ok(IterDup::new(self.cursor(), ffi::MDB_GET_CURRENT)) + fn iter_dup_from(&mut self, key: &K) -> IterDup<'txn> where K: AsRef<[u8]> { + match self.get(Some(key.as_ref()), None, ffi::MDB_SET_RANGE) { + Err(Error::NotFound) => Ok(()), + Err(error) => Err(error), + Ok(_) => Ok(()), + }.unwrap(); + IterDup::new(self.cursor(), ffi::MDB_GET_CURRENT) } /// Iterate over the duplicates of the item in the database with the given @@ -446,12 +454,13 @@ mod test { assert_eq!(items, cursor.iter_start().collect::>()); assert_eq!(items.clone().into_iter().skip(1).collect::>(), - cursor.iter_from(b"key2").unwrap().collect::>()); + cursor.iter_from(b"key2").collect::>()); assert_eq!(items.clone().into_iter().skip(3).collect::>(), - cursor.iter_from(b"key4").unwrap().collect::>()); + cursor.iter_from(b"key4").collect::>()); - assert!(cursor.iter_from(b"key6").is_err()); + assert_eq!(vec!().into_iter().collect::>(), + cursor.iter_from(b"key6").collect::>()); } #[test] @@ -493,18 +502,20 @@ mod test { cursor.iter_dup_start().flat_map(|x| x).collect::>()); assert_eq!(items.clone().into_iter().skip(3).collect::>(), - cursor.iter_dup_from(b"b").unwrap().flat_map(|x| x).collect::>()); + cursor.iter_dup_from(b"b").flat_map(|x| x).collect::>()); assert_eq!(items.clone().into_iter().skip(3).collect::>(), - cursor.iter_dup_from(b"ab").unwrap().flat_map(|x| x).collect::>()); + cursor.iter_dup_from(b"ab").flat_map(|x| x).collect::>()); assert_eq!(items.clone().into_iter().skip(9).collect::>(), - cursor.iter_dup_from(b"d").unwrap().flat_map(|x| x).collect::>()); + cursor.iter_dup_from(b"d").flat_map(|x| x).collect::>()); + + assert_eq!(vec!().into_iter().collect::>(), + cursor.iter_dup_from(b"f").flat_map(|x| x).collect::>()); assert_eq!(items.clone().into_iter().skip(3).take(3).collect::>(), cursor.iter_dup_of(b"b").unwrap().collect::>()); - assert!(cursor.iter_dup_from(b"foo").is_err()); assert!(cursor.iter_dup_of(b"foo").is_err()); }