Code review fixes

master
Xuejie Xiao 6 years ago
parent 81091a05ba
commit a651b19aa5
  1. 58
      src/db.rs
  2. 4
      src/lib.rs
  3. 10
      tests/test_pinnable_slice.rs

@ -938,14 +938,20 @@ impl DB {
self.get_cf_opt(cf, key.as_ref(), &ReadOptions::default()) self.get_cf_opt(cf, key.as_ref(), &ReadOptions::default())
} }
pub fn get_pinned_opt<T: AsRef<[u8]>>(&self, key: T, readopts: &ReadOptions) -> Result<Option<DBPinnableSlice>, Error> { /// Return the value associated with a key using RocksDB's PinnableSlice
/// so as to avoid unnecessary memory copy.
pub fn get_pinned_opt<K: AsRef<[u8]>>(
&self,
key: K,
readopts: &ReadOptions,
) -> Result<Option<DBPinnableSlice>, Error> {
if readopts.inner.is_null() { if readopts.inner.is_null() {
return Err(Error::new( return Err(Error::new(
"Unable to create RocksDB read options. \ "Unable to create RocksDB read options. \
This is a fairly trivial call, and its \ This is a fairly trivial call, and its \
failure may be indicative of a \ failure may be indicative of a \
mis-compiled or mis-loaded RocksDB \ mis-compiled or mis-loaded RocksDB \
library." library."
.to_owned(), .to_owned(),
)); ));
} }
@ -966,23 +972,29 @@ impl DB {
} }
} }
pub fn get_pinned<T: AsRef<[u8]>>(&self, key: T) -> Result<Option<DBPinnableSlice>, Error> { /// Return the value associated with a key using RocksDB's PinnableSlice
/// so as to avoid unnecessary memory copy. Similar to get_pinned_opt but
/// leverages default options.
pub fn get_pinned<K: AsRef<[u8]>>(&self, key: K) -> Result<Option<DBPinnableSlice>, Error> {
self.get_pinned_opt(key, &ReadOptions::default()) self.get_pinned_opt(key, &ReadOptions::default())
} }
pub fn get_pinned_cf_opt<T: AsRef<[u8]>>( /// Return the value associated with a key using RocksDB's PinnableSlice
/// so as to avoid unnecessary memory copy. Similar to get_pinned_opt but
/// allows specifying ColumnFamily
pub fn get_pinned_cf_opt<K: AsRef<[u8]>>(
&self, &self,
cf: ColumnFamily, cf: ColumnFamily,
key: T, key: K,
readopts: &ReadOptions, readopts: &ReadOptions,
) -> Result<Option<DBPinnableSlice>, Error> { ) -> Result<Option<DBPinnableSlice>, Error> {
if readopts.inner.is_null() { if readopts.inner.is_null() {
return Err(Error::new( return Err(Error::new(
"Unable to create RocksDB read options. \ "Unable to create RocksDB read options. \
This is a fairly trivial call, and its \ This is a fairly trivial call, and its \
failure may be indicative of a \ failure may be indicative of a \
mis-compiled or mis-loaded RocksDB \ mis-compiled or mis-loaded RocksDB \
library." library."
.to_owned(), .to_owned(),
)); ));
} }
@ -1004,7 +1016,14 @@ impl DB {
} }
} }
pub fn get_pinned_cf<T: AsRef<[u8]>>(&self, cf: ColumnFamily, key: T) -> Result<Option<DBPinnableSlice>, Error> { /// Return the value associated with a key using RocksDB's PinnableSlice
/// so as to avoid unnecessary memory copy. Similar to get_pinned_cf_opt but
/// leverages default options.
pub fn get_pinned_cf<K: AsRef<[u8]>>(
&self,
cf: ColumnFamily,
key: K,
) -> Result<Option<DBPinnableSlice>, Error> {
self.get_pinned_cf_opt(cf, key, &ReadOptions::default()) self.get_pinned_cf_opt(cf, key, &ReadOptions::default())
} }
@ -1667,9 +1686,14 @@ fn to_cpath<P: AsRef<Path>>(path: P) -> Result<CString, Error> {
} }
} }
/// Wrapper around RocksDB PinnableSlice struct.
///
/// With a pinnable slice, we can directly leverage in-memory data within
/// RocksDB toa void unnecessary memory copies. The struct here wraps the
/// returned raw pointer and ensures proper finalization work.
pub struct DBPinnableSlice<'a> { pub struct DBPinnableSlice<'a> {
ptr: *mut ffi::rocksdb_pinnableslice_t, ptr: *mut ffi::rocksdb_pinnableslice_t,
db: PhantomData<&'a DB> db: PhantomData<&'a DB>,
} }
impl<'a> AsRef<[u8]> for DBPinnableSlice<'a> { impl<'a> AsRef<[u8]> for DBPinnableSlice<'a> {
@ -1685,8 +1709,8 @@ impl<'a> Deref for DBPinnableSlice<'a> {
fn deref(&self) -> &[u8] { fn deref(&self) -> &[u8] {
unsafe { unsafe {
let mut val_len: size_t = 0; let mut val_len: size_t = 0;
let val = ffi::rocksdb_pinnableslice_value(self.ptr, &mut val_len,) as *mut u8; let val = ffi::rocksdb_pinnableslice_value(self.ptr, &mut val_len) as *mut u8;
slice::from_raw_parts(val, val_len,) slice::from_raw_parts(val, val_len)
} }
} }
} }
@ -1694,7 +1718,7 @@ impl<'a> Deref for DBPinnableSlice<'a> {
impl<'a> Drop for DBPinnableSlice<'a> { impl<'a> Drop for DBPinnableSlice<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
ffi::rocksdb_pinnableslice_destroy(self.ptr,); ffi::rocksdb_pinnableslice_destroy(self.ptr);
} }
} }
} }

@ -71,8 +71,8 @@ mod slice_transform;
pub use compaction_filter::Decision as CompactionDecision; pub use compaction_filter::Decision as CompactionDecision;
pub use db::{ pub use db::{
new_bloom_filter, DBCompactionStyle, DBCompressionType, DBIterator, DBRawIterator, DBPinnableSlice, DBRecoveryMode, DBVector, DBCompactionStyle, DBCompressionType, DBIterator, DBPinnableSlice, DBRawIterator,
Direction, IteratorMode, ReadOptions, Snapshot, WriteBatch, DBRecoveryMode, DBVector, Direction, IteratorMode, ReadOptions, Snapshot, WriteBatch,
}; };
pub use slice_transform::SliceTransform; pub use slice_transform::SliceTransform;

@ -3,16 +3,16 @@ extern crate rocksdb;
use rocksdb::{Options, DB}; use rocksdb::{Options, DB};
#[test] #[test]
pub fn test_pinnable_slice() { fn test_pinnable_slice() {
let path = "_rust_rocksdb_pinnable_slice_test"; let path = "_rust_rocksdb_pinnable_slice_test";
let mut opts = Options::default(); let mut opts = Options::default();
opts.create_if_missing(true,); opts.create_if_missing(true);
let db = DB::open(&opts, path,).unwrap(); let db = DB::open(&opts, path).unwrap();
db.put(b"k1", b"value12345",).unwrap(); db.put(b"k1", b"value12345").unwrap();
let result = db.get_pinned(b"k1",); let result = db.get_pinned(b"k1");
assert!(result.is_ok()); assert!(result.is_ok());
let value = result.unwrap(); let value = result.unwrap();

Loading…
Cancel
Save