From b87bb3f255f1b3aad5c00a8e9c91d04a4c5c5e9d Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 23 Mar 2016 16:36:04 +0800 Subject: [PATCH] support snapshot get/get_cf --- src/rocksdb.rs | 102 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 66b5b08..c455188 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -201,6 +201,21 @@ impl<'a> Snapshot<'a> { readopts.set_snapshot(self); DBIterator::new(self.db, &readopts, mode) } + + pub fn get(&self, key: &[u8]) -> Result, String> { + let mut readopts = ReadOptions::new(); + readopts.set_snapshot(self); + self.db.get_opt(key, &readopts) + } + + pub fn get_cf(&self, + cf: DBCFHandle, + key: &[u8]) + -> Result, String> { + let mut readopts = ReadOptions::new(); + readopts.set_snapshot(self); + self.db.get_cf_opt(cf, key, &readopts) + } } impl<'a> Drop for Snapshot<'a> { @@ -399,29 +414,30 @@ impl DB { self.write_opt(batch, &WriteOptions::new()) } - pub fn get(&self, key: &[u8]) -> Result, String> { - unsafe { - let readopts = rocksdb_ffi::rocksdb_readoptions_create(); - if readopts.0.is_null() { - return Err("Unable to create rocksdb read options. This is \ - a fairly trivial call, and its failure may be \ - indicative of a mis-compiled or mis-loaded \ - rocksdb library." - .to_string()); - } + pub fn get_opt(&self, + key: &[u8], + readopts: &ReadOptions) + -> Result, String> { + if readopts.inner.0.is_null() { + return Err("Unable to create rocksdb read options. This is a \ + fairly trivial call, and its failure may be \ + indicative of a mis-compiled or mis-loaded rocksdb \ + library." + .to_string()); + } + unsafe { let val_len: size_t = 0; let val_len_ptr = &val_len as *const size_t; let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; let val = rocksdb_ffi::rocksdb_get(self.inner, - readopts.clone(), + readopts.inner, key.as_ptr(), key.len() as size_t, val_len_ptr, err_ptr) as *mut u8; - rocksdb_ffi::rocksdb_readoptions_destroy(readopts); if !err.is_null() { return Err(error_message(err)); } @@ -432,33 +448,36 @@ impl DB { } } - pub fn get_cf(&self, - cf: DBCFHandle, - key: &[u8]) - -> Result, String> { - unsafe { - let readopts = rocksdb_ffi::rocksdb_readoptions_create(); - if readopts.0.is_null() { - return Err("Unable to create rocksdb read options. This is \ - a fairly trivial call, and its failure may be \ - indicative of a mis-compiled or mis-loaded \ - rocksdb library." - .to_string()); - } + pub fn get(&self, key: &[u8]) -> Result, String> { + self.get_opt(key, &ReadOptions::new()) + } + pub fn get_cf_opt(&self, + cf: DBCFHandle, + key: &[u8], + readopts: &ReadOptions) + -> Result, String> { + if readopts.inner.0.is_null() { + return Err("Unable to create rocksdb read options. This is a \ + fairly trivial call, and its failure may be \ + indicative of a mis-compiled or mis-loaded rocksdb \ + library." + .to_string()); + } + + unsafe { let val_len: size_t = 0; let val_len_ptr = &val_len as *const size_t; let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; let val = rocksdb_ffi::rocksdb_get_cf(self.inner, - readopts.clone(), + readopts.inner, cf, key.as_ptr(), key.len() as size_t, val_len_ptr, err_ptr) as *mut u8; - rocksdb_ffi::rocksdb_readoptions_destroy(readopts); if !err.is_null() { return Err(error_message(err)); } @@ -469,6 +488,13 @@ impl DB { } } + pub fn get_cf(&self, + cf: DBCFHandle, + key: &[u8]) + -> Result, String> { + self.get_cf_opt(cf, key, &ReadOptions::new()) + } + pub fn create_cf(&mut self, name: &str, opts: &Options) @@ -949,3 +975,25 @@ fn iterator_test() { let opts = Options::new(); assert!(DB::destroy(&opts, path).is_ok()); } + +#[test] +fn snapshot_test() { + let path = "_rust_rocksdb_snapshottest"; + { + let db = DB::open_default(path).unwrap(); + let p = db.put(b"k1", b"v1111"); + assert!(p.is_ok()); + + let snap = db.snapshot(); + let r: Result, String> = snap.get(b"k1"); + assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); + + let p = db.put(b"k2", b"v2222"); + assert!(p.is_ok()); + + assert!(db.get(b"k2").unwrap().is_some()); + assert!(snap.get(b"k2").unwrap().is_none()); + } + let opts = Options::new(); + assert!(DB::destroy(&opts, path).is_ok()); +}