From 4c0d66243589ef9fb78450dee7161613c332723d Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Sun, 3 Feb 2019 07:10:24 -0600 Subject: [PATCH] Adding test to validate correct behavior of prefix_iterator... --- tests/test_iterator.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_iterator.rs b/tests/test_iterator.rs index b96fe42..b64ab05 100644 --- a/tests/test_iterator.rs +++ b/tests/test_iterator.rs @@ -207,6 +207,50 @@ pub fn test_prefix_iterator() { } } +#[test] +pub fn test_prefix_iterator_uses_full_prefix() { + // Test scenario derived from GitHub issue #221 + + // Explanation: `db.prefix_iterator` sets the underlying + // options to seek to the first key that matches the *entire* + // prefix. From there, the iterator will continue to read pairs + // as long as the prefix extracted from `key` matches the + // prefix extracted from `prefix`. + + let path = DBPath::new("_rust_rocksdb_prefixiteratorusesfullprefixtest"); + { + let data = [ + ([0,0,0,0], b"111"), + ([0,0,0,1], b"222"), + ([0,1,0,1], b"333"), + ([0,1,1,1], b"444"), + ([0,1,2,1], b"555"), + ([0,2,0,0], b"666"), + ([2,0,0,0], b"777"), + ([2,2,2,2], b"888") + ]; + + let prefix_extractor = rocksdb::SliceTransform::create_fixed_prefix(1); + + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.set_prefix_extractor(prefix_extractor); + + let db = DB::open(&opts, &path).unwrap(); + + for (key, value) in &data { + assert!(db.put(key, *value).is_ok()); + } + + let prefix = [0,1,1]; + let results: Vec<_> = db.prefix_iterator(&prefix) + .map(|(_,v)| std::str::from_utf8(&v).unwrap().to_string()) + .collect(); + + assert_eq!(results, vec!("444", "555", "666")); + } +} + #[test] pub fn test_full_iterator() { let path = "_rust_rocksdb_fulliteratortest";