From 1b2dbfc6e76bf0240e7f954313efe171be6ca4e0 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Mon, 5 Jan 2015 21:24:42 -0500 Subject: [PATCH] add duplicate item iterator --- src/cursor.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cursor.rs b/src/cursor.rs index 9946d90..451dac9 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -89,6 +89,12 @@ pub trait CursorExt<'txn> : Cursor<'txn> + Sized { self.get(Some(key), None, ffi::MDB_SET_RANGE).unwrap(); IterDup::new(self.cursor(), ffi::MDB_GET_CURRENT) } + + /// Iterate over the duplicates of the item in the database with the given key. + fn iter_dup_of(&mut self, key: &[u8]) -> LmdbResult> { + try!(self.get(Some(key), None, ffi::MDB_SET)); + Ok(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT_DUP)) + } } impl<'txn, T> CursorExt<'txn> for T where T: Cursor<'txn> {} @@ -473,8 +479,12 @@ mod test { assert_eq!(items.clone().into_iter().skip(3).collect::>(), cursor.iter_dup_from(b"ab").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_of(b"foo").is_err()); + } #[test] fn test_put_del() {