From 21b9a9e98aa5f2d6d3a04cab2cc2dedfa3011fb6 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Fri, 1 Feb 2019 20:38:07 -0600 Subject: [PATCH] Key/value arguments are now generic types that impl AsRef<[u8]>... --- src/db.rs | 199 ++++++++++++++++++++++++---------- src/ffi_util.rs | 4 +- src/merge_operator.rs | 4 +- tests/test_compationfilter.rs | 2 +- 4 files changed, 147 insertions(+), 62 deletions(-) diff --git a/src/db.rs b/src/db.rs index 84395fb..347788d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -318,7 +318,9 @@ impl<'a> DBRawIterator<'a> { /// // There are no keys in the database /// } /// ``` - pub fn seek(&mut self, key: &[u8]) { + pub fn seek>(&mut self, key: K) { + let key = key.as_ref(); + unsafe { ffi::rocksdb_iter_seek( self.inner, @@ -351,7 +353,9 @@ impl<'a> DBRawIterator<'a> { /// } else { /// // There are no keys in the database /// } - pub fn seek_for_prev(&mut self, key: &[u8]) { + pub fn seek_for_prev>(&mut self, key: K) { + let key = key.as_ref(); + unsafe { ffi::rocksdb_iter_seek_for_prev( self.inner, @@ -581,24 +585,24 @@ impl<'a> Snapshot<'a> { DBRawIterator::new_cf(self.db, cf_handle, &readopts) } - pub fn get(&self, key: &[u8]) -> Result, Error> { + pub fn get>(&self, key: K) -> Result, Error> { let readopts = ReadOptions::default(); self.get_opt(key, readopts) } - pub fn get_cf(&self, cf: ColumnFamily, key: &[u8]) -> Result, Error> { + pub fn get_cf>(&self, cf: ColumnFamily, key: K) -> Result, Error> { let readopts = ReadOptions::default(); - self.get_cf_opt(cf, key, readopts) + self.get_cf_opt(cf, key.as_ref(), readopts) } - pub fn get_opt(&self, key: &[u8], mut readopts: ReadOptions) -> Result, Error> { + pub fn get_opt>(&self, key: K, mut readopts: ReadOptions) -> Result, Error> { readopts.set_snapshot(self); - self.db.get_opt(key, &readopts) + self.db.get_opt(key.as_ref(), &readopts) } - pub fn get_cf_opt(&self, cf: ColumnFamily, key: &[u8], mut readopts: ReadOptions) -> Result, Error> { + pub fn get_cf_opt>(&self, cf: ColumnFamily, key: K, mut readopts: ReadOptions) -> Result, Error> { readopts.set_snapshot(self); - self.db.get_cf_opt(cf, key, &readopts) + self.db.get_cf_opt(cf, key.as_ref(), &readopts) } } @@ -814,7 +818,7 @@ impl DB { self.write_opt(batch, &wo) } - pub fn get_opt(&self, key: &[u8], readopts: &ReadOptions) -> Result, Error> { + pub fn get_opt>(&self, key: K, readopts: &ReadOptions) -> Result, Error> { if readopts.inner.is_null() { return Err(Error::new( "Unable to create RocksDB read options. \ @@ -826,6 +830,8 @@ impl DB { )); } + let key = key.as_ref(); + unsafe { let mut val_len: size_t = 0; let val = ffi_try!(ffi::rocksdb_get( @@ -844,14 +850,14 @@ impl DB { } /// Return the bytes associated with a key value - pub fn get(&self, key: &[u8]) -> Result, Error> { - self.get_opt(key, &ReadOptions::default()) + pub fn get>(&self, key: K) -> Result, Error> { + self.get_opt(key.as_ref(), &ReadOptions::default()) } - pub fn get_cf_opt( + pub fn get_cf_opt>( &self, cf: ColumnFamily, - key: &[u8], + key: K, readopts: &ReadOptions, ) -> Result, Error> { if readopts.inner.is_null() { @@ -865,6 +871,8 @@ impl DB { )); } + let key = key.as_ref(); + unsafe { let mut val_len: size_t = 0; let val = ffi_try!(ffi::rocksdb_get_cf( @@ -883,8 +891,8 @@ impl DB { } } - pub fn get_cf(&self, cf: ColumnFamily, key: &[u8]) -> Result, Error> { - self.get_cf_opt(cf, key, &ReadOptions::default()) + pub fn get_cf>(&self, cf: ColumnFamily, key: K) -> Result, Error> { + self.get_cf_opt(cf, key.as_ref(), &ReadOptions::default()) } pub fn create_cf(&self, name: &str, opts: &Options) -> Result { @@ -960,10 +968,10 @@ impl DB { DBIterator::new(self, &opts, mode) } - pub fn prefix_iterator(&self, prefix: &[u8]) -> DBIterator { + pub fn prefix_iterator>(&self, prefix: P) -> DBIterator { let mut opts = ReadOptions::default(); opts.set_prefix_same_as_start(true); - DBIterator::new(self, &opts, IteratorMode::From(prefix, Direction::Forward)) + DBIterator::new(self, &opts, IteratorMode::From(prefix.as_ref(), Direction::Forward)) } pub fn iterator_cf( @@ -985,10 +993,10 @@ impl DB { DBIterator::new_cf(self, cf_handle, &opts, mode) } - pub fn prefix_iterator_cf( + pub fn prefix_iterator_cf>( &self, cf_handle: ColumnFamily, - prefix: &[u8] + prefix: P ) -> Result { let mut opts = ReadOptions::default(); opts.set_prefix_same_as_start(true); @@ -996,7 +1004,7 @@ impl DB { self, cf_handle, &opts, - IteratorMode::From(prefix, Direction::Forward), + IteratorMode::From(prefix.as_ref(), Direction::Forward), ) } @@ -1014,8 +1022,15 @@ impl DB { Snapshot::new(self) } - pub fn put_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), Error> { + pub fn put_opt(&self, key: K, value: V, writeopts: &WriteOptions) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { + ffi_try!(ffi::rocksdb_put( self.inner, writeopts.inner, @@ -1028,14 +1043,21 @@ impl DB { } } - pub fn put_cf_opt( + pub fn put_cf_opt( &self, cf: ColumnFamily, - key: &[u8], - value: &[u8], + key: K, + value: V, writeopts: &WriteOptions, - ) -> Result<(), Error> { + ) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { + ffi_try!(ffi::rocksdb_put_cf( self.inner, writeopts.inner, @@ -1049,12 +1071,18 @@ impl DB { } } - pub fn merge_opt( + pub fn merge_opt( &self, - key: &[u8], - value: &[u8], + key: K, + value: V, writeopts: &WriteOptions, - ) -> Result<(), Error> { + ) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { ffi_try!(ffi::rocksdb_merge( self.inner, @@ -1068,14 +1096,21 @@ impl DB { } } - pub fn merge_cf_opt( + pub fn merge_cf_opt( &self, cf: ColumnFamily, - key: &[u8], - value: &[u8], + key: K, + value: V, writeopts: &WriteOptions, - ) -> Result<(), Error> { + ) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { + ffi_try!(ffi::rocksdb_merge_cf( self.inner, writeopts.inner, @@ -1089,7 +1124,9 @@ impl DB { } } - pub fn delete_opt(&self, key: &[u8], writeopts: &WriteOptions) -> Result<(), Error> { + pub fn delete_opt>(&self, key: K, writeopts: &WriteOptions) -> Result<(), Error> { + let key = key.as_ref(); + unsafe { ffi_try!(ffi::rocksdb_delete( self.inner, @@ -1101,12 +1138,15 @@ impl DB { } } - pub fn delete_cf_opt( + pub fn delete_cf_opt>( &self, cf: ColumnFamily, - key: &[u8], + key: K, writeopts: &WriteOptions, ) -> Result<(), Error> { + + let key = key.as_ref(); + unsafe { ffi_try!(ffi::rocksdb_delete_cf( self.inner, @@ -1119,32 +1159,47 @@ impl DB { } } - pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), Error> { - self.put_opt(key, value, &WriteOptions::default()) + pub fn put(&self, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + self.put_opt(key.as_ref(), value.as_ref(), &WriteOptions::default()) } - pub fn put_cf(&self, cf: ColumnFamily, key: &[u8], value: &[u8]) -> Result<(), Error> { - self.put_cf_opt(cf, key, value, &WriteOptions::default()) + pub fn put_cf(&self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + self.put_cf_opt(cf, key.as_ref(), value.as_ref(), &WriteOptions::default()) } - pub fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), Error> { - self.merge_opt(key, value, &WriteOptions::default()) + pub fn merge(&self, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + self.merge_opt(key.as_ref(), value.as_ref(), &WriteOptions::default()) } - pub fn merge_cf(&self, cf: ColumnFamily, key: &[u8], value: &[u8]) -> Result<(), Error> { - self.merge_cf_opt(cf, key, value, &WriteOptions::default()) + pub fn merge_cf(&self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + self.merge_cf_opt(cf, key.as_ref(), value.as_ref(), &WriteOptions::default()) } - pub fn delete(&self, key: &[u8]) -> Result<(), Error> { - self.delete_opt(key, &WriteOptions::default()) + pub fn delete>(&self, key: K) -> Result<(), Error> { + self.delete_opt(key.as_ref(), &WriteOptions::default()) } - pub fn delete_cf(&self, cf: ColumnFamily, key: &[u8]) -> Result<(), Error> { - self.delete_cf_opt(cf, key, &WriteOptions::default()) + pub fn delete_cf>(&self, cf: ColumnFamily, key: K) -> Result<(), Error> { + self.delete_cf_opt(cf, key.as_ref(), &WriteOptions::default()) } - pub fn compact_range(&self, start: Option<&[u8]>, end: Option<&[u8]>) { + pub fn compact_range, E: AsRef<[u8]>>(&self, start: Option, end: Option) { unsafe { + let start = start.as_ref().map(|s| s.as_ref()); + let end = end.as_ref().map(|e| e.as_ref()); + ffi::rocksdb_compact_range( self.inner, opt_bytes_to_ptr(start), @@ -1217,7 +1272,13 @@ impl WriteBatch { } /// Insert a value into the database under the given key. - pub fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), Error> { + pub fn put(&mut self, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { ffi::rocksdb_writebatch_put( self.inner, @@ -1230,7 +1291,13 @@ impl WriteBatch { } } - pub fn put_cf(&mut self, cf: ColumnFamily, key: &[u8], value: &[u8]) -> Result<(), Error> { + pub fn put_cf(&mut self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { ffi::rocksdb_writebatch_put_cf( self.inner, @@ -1244,7 +1311,13 @@ impl WriteBatch { } } - pub fn merge(&mut self, key: &[u8], value: &[u8]) -> Result<(), Error> { + pub fn merge(&mut self, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { ffi::rocksdb_writebatch_merge( self.inner, @@ -1257,7 +1330,13 @@ impl WriteBatch { } } - pub fn merge_cf(&mut self, cf: ColumnFamily, key: &[u8], value: &[u8]) -> Result<(), Error> { + pub fn merge_cf(&mut self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> + where K: AsRef<[u8]>, + V: AsRef<[u8]> { + + let key = key.as_ref(); + let value = value.as_ref(); + unsafe { ffi::rocksdb_writebatch_merge_cf( self.inner, @@ -1274,7 +1353,9 @@ impl WriteBatch { /// Remove the database entry for key. /// /// Returns an error if the key was not found. - pub fn delete(&mut self, key: &[u8]) -> Result<(), Error> { + pub fn delete>(&mut self, key: K) -> Result<(), Error> { + let key = key.as_ref(); + unsafe { ffi::rocksdb_writebatch_delete( self.inner, @@ -1285,7 +1366,9 @@ impl WriteBatch { } } - pub fn delete_cf(&mut self, cf: ColumnFamily, key: &[u8]) -> Result<(), Error> { + pub fn delete_cf>(&mut self, cf: ColumnFamily, key: K) -> Result<(), Error> { + let key = key.as_ref(); + unsafe { ffi::rocksdb_writebatch_delete_cf( self.inner, @@ -1354,7 +1437,9 @@ impl ReadOptions { } } - pub fn set_iterate_upper_bound(&mut self, key: &[u8]) { + pub fn set_iterate_upper_bound>(&mut self, key: K) { + let key = key.as_ref(); + unsafe { ffi::rocksdb_readoptions_set_iterate_upper_bound( self.inner, diff --git a/src/ffi_util.rs b/src/ffi_util.rs index 49c298e..cce3b83 100644 --- a/src/ffi_util.rs +++ b/src/ffi_util.rs @@ -26,9 +26,9 @@ pub fn error_message(ptr: *const c_char) -> String { s } -pub fn opt_bytes_to_ptr(opt: Option<&[u8]>) -> *const c_char { +pub fn opt_bytes_to_ptr>(opt: Option) -> *const c_char { match opt { - Some(v) => v.as_ptr() as *const c_char, + Some(v) => v.as_ref().as_ptr() as *const c_char, None => ptr::null(), } } diff --git a/src/merge_operator.rs b/src/merge_operator.rs index 69551d8..fb3d1c7 100644 --- a/src/merge_operator.rs +++ b/src/merge_operator.rs @@ -368,7 +368,7 @@ mod test { let _ = db.get(b"k2"); } } - db.compact_range(None, None); + db.compact_range(None::<&[u8]>, None::<&[u8]>); let d1 = db.clone(); let d2 = db.clone(); let d3 = db.clone(); @@ -399,7 +399,7 @@ mod test { let _ = d2.get(b"k2"); } } - d2.compact_range(None, None); + d2.compact_range(None::<&[u8]>, None::<&[u8]>); }); h2.join().unwrap(); let h3 = thread::spawn(move || { diff --git a/tests/test_compationfilter.rs b/tests/test_compationfilter.rs index 5cd4dc0..c5fc8a2 100644 --- a/tests/test_compationfilter.rs +++ b/tests/test_compationfilter.rs @@ -43,7 +43,7 @@ fn compaction_filter_test() { let _ = db.put(b"k1", b"a"); let _ = db.put(b"_k", b"b"); let _ = db.put(b"%k", b"c"); - db.compact_range(None, None); + db.compact_range(None::<&[u8]>, None::<&[u8]>); assert_eq!(&*db.get(b"k1").unwrap().unwrap(), b"a"); assert!(db.get(b"_k").unwrap().is_none()); assert_eq!(&*db.get(b"%k").unwrap().unwrap(), b"secret");