From 4c0d66243589ef9fb78450dee7161613c332723d Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Sun, 3 Feb 2019 07:10:24 -0600 Subject: [PATCH 1/3] 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"; From f09fbaea71417ed79bacab7c3f07fd4bd7e7fda5 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Sun, 3 Feb 2019 07:47:25 -0600 Subject: [PATCH 2/3] Minor tweak to test comment... --- tests/test_iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_iterator.rs b/tests/test_iterator.rs index b64ab05..8fe0582 100644 --- a/tests/test_iterator.rs +++ b/tests/test_iterator.rs @@ -213,7 +213,7 @@ pub fn test_prefix_iterator_uses_full_prefix() { // 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 + // `prefix`. From there, the iterator will continue to read pairs // as long as the prefix extracted from `key` matches the // prefix extracted from `prefix`. From e1a534ca5ea2c3778e33ec8b79850b9341945026 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Mon, 4 Feb 2019 04:23:12 -0600 Subject: [PATCH 3/3] Removing unnecessary pub modifier on test functions... --- tests/test_iterator.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_iterator.rs b/tests/test_iterator.rs index 8fe0582..578b398 100644 --- a/tests/test_iterator.rs +++ b/tests/test_iterator.rs @@ -23,7 +23,7 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> { } #[test] -pub fn test_iterator() { +fn test_iterator() { let n = DBPath::new("_rust_rocksdb_iteratortest"); { let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice(); @@ -172,7 +172,7 @@ fn key(k: &[u8]) -> Box<[u8]> { } #[test] -pub fn test_prefix_iterator() { +fn test_prefix_iterator() { let n = DBPath::new("_rust_rocksdb_prefixiteratortest"); { let a1: Box<[u8]> = key(b"aaa1"); @@ -208,7 +208,7 @@ pub fn test_prefix_iterator() { } #[test] -pub fn test_prefix_iterator_uses_full_prefix() { +fn test_prefix_iterator_uses_full_prefix() { // Test scenario derived from GitHub issue #221 // Explanation: `db.prefix_iterator` sets the underlying @@ -252,7 +252,7 @@ pub fn test_prefix_iterator_uses_full_prefix() { } #[test] -pub fn test_full_iterator() { +fn test_full_iterator() { let path = "_rust_rocksdb_fulliteratortest"; { let a1: Box<[u8]> = key(b"aaa1");