Merge pull request #52 from siddontang/master

support snapshot get/get_cf
master
Tyler Neely 9 years ago
commit 7d5fa795aa
  1. 90
      src/rocksdb.rs

@ -201,6 +201,21 @@ impl<'a> Snapshot<'a> {
readopts.set_snapshot(self); readopts.set_snapshot(self);
DBIterator::new(self.db, &readopts, mode) DBIterator::new(self.db, &readopts, mode)
} }
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, 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<Option<DBVector>, String> {
let mut readopts = ReadOptions::new();
readopts.set_snapshot(self);
self.db.get_cf_opt(cf, key, &readopts)
}
} }
impl<'a> Drop for Snapshot<'a> { impl<'a> Drop for Snapshot<'a> {
@ -399,29 +414,30 @@ impl DB {
self.write_opt(batch, &WriteOptions::new()) self.write_opt(batch, &WriteOptions::new())
} }
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String> { pub fn get_opt(&self,
unsafe { key: &[u8],
let readopts = rocksdb_ffi::rocksdb_readoptions_create(); readopts: &ReadOptions)
if readopts.0.is_null() { -> Result<Option<DBVector>, String> {
return Err("Unable to create rocksdb read options. This is \ if readopts.inner.0.is_null() {
a fairly trivial call, and its failure may be \ return Err("Unable to create rocksdb read options. This is a \
indicative of a mis-compiled or mis-loaded \ fairly trivial call, and its failure may be \
rocksdb library." indicative of a mis-compiled or mis-loaded rocksdb \
library."
.to_string()); .to_string());
} }
unsafe {
let val_len: size_t = 0; let val_len: size_t = 0;
let val_len_ptr = &val_len as *const size_t; let val_len_ptr = &val_len as *const size_t;
let mut err: *const i8 = 0 as *const i8; let mut err: *const i8 = 0 as *const i8;
let err_ptr: *mut *const i8 = &mut err; let err_ptr: *mut *const i8 = &mut err;
let val = let val =
rocksdb_ffi::rocksdb_get(self.inner, rocksdb_ffi::rocksdb_get(self.inner,
readopts.clone(), readopts.inner,
key.as_ptr(), key.as_ptr(),
key.len() as size_t, key.len() as size_t,
val_len_ptr, val_len_ptr,
err_ptr) as *mut u8; err_ptr) as *mut u8;
rocksdb_ffi::rocksdb_readoptions_destroy(readopts);
if !err.is_null() { if !err.is_null() {
return Err(error_message(err)); return Err(error_message(err));
} }
@ -432,33 +448,36 @@ impl DB {
} }
} }
pub fn get_cf(&self, pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String> {
self.get_opt(key, &ReadOptions::new())
}
pub fn get_cf_opt(&self,
cf: DBCFHandle, cf: DBCFHandle,
key: &[u8]) key: &[u8],
readopts: &ReadOptions)
-> Result<Option<DBVector>, String> { -> Result<Option<DBVector>, String> {
unsafe { if readopts.inner.0.is_null() {
let readopts = rocksdb_ffi::rocksdb_readoptions_create(); return Err("Unable to create rocksdb read options. This is a \
if readopts.0.is_null() { fairly trivial call, and its failure may be \
return Err("Unable to create rocksdb read options. This is \ indicative of a mis-compiled or mis-loaded rocksdb \
a fairly trivial call, and its failure may be \ library."
indicative of a mis-compiled or mis-loaded \
rocksdb library."
.to_string()); .to_string());
} }
unsafe {
let val_len: size_t = 0; let val_len: size_t = 0;
let val_len_ptr = &val_len as *const size_t; let val_len_ptr = &val_len as *const size_t;
let mut err: *const i8 = 0 as *const i8; let mut err: *const i8 = 0 as *const i8;
let err_ptr: *mut *const i8 = &mut err; let err_ptr: *mut *const i8 = &mut err;
let val = let val =
rocksdb_ffi::rocksdb_get_cf(self.inner, rocksdb_ffi::rocksdb_get_cf(self.inner,
readopts.clone(), readopts.inner,
cf, cf,
key.as_ptr(), key.as_ptr(),
key.len() as size_t, key.len() as size_t,
val_len_ptr, val_len_ptr,
err_ptr) as *mut u8; err_ptr) as *mut u8;
rocksdb_ffi::rocksdb_readoptions_destroy(readopts);
if !err.is_null() { if !err.is_null() {
return Err(error_message(err)); return Err(error_message(err));
} }
@ -469,6 +488,13 @@ impl DB {
} }
} }
pub fn get_cf(&self,
cf: DBCFHandle,
key: &[u8])
-> Result<Option<DBVector>, String> {
self.get_cf_opt(cf, key, &ReadOptions::new())
}
pub fn create_cf(&mut self, pub fn create_cf(&mut self,
name: &str, name: &str,
opts: &Options) opts: &Options)
@ -949,3 +975,25 @@ fn iterator_test() {
let opts = Options::new(); let opts = Options::new();
assert!(DB::destroy(&opts, path).is_ok()); 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<Option<DBVector>, 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());
}

Loading…
Cancel
Save