added delete, improved testing

master
Tyler Neely 10 years ago
parent 1389365fde
commit 6dbde9d2e9
  1. 2
      README.md
  2. 4
      rocksdb-sys/lib.rs
  3. 24
      src/main.rs
  4. 30
      src/rocksdb.rs

@ -34,7 +34,7 @@ fn main() {
```
### status
- [x] basic open/put/get/close
- [x] basic open/put/get/delete/close
- [x] linux support
- [x] rocksdb compiled via cargo
- [x] OSX support

@ -106,13 +106,13 @@ extern {
pub fn rocksdb_get(db: RocksdbInstance, readopts: RocksdbReadOptions,
k: *const u8, kLen: size_t,
valLen: *const size_t, err: *mut i8) -> *mut c_void;
pub fn rocksdb_delete(db: RocksdbInstance, writeopts: RocksdbWriteOptions,
k: *const u8, kLen: size_t, err: *mut i8) -> *mut c_void;
pub fn rocksdb_close(db: RocksdbInstance);
pub fn rocksdb_destroy_db(
options: RocksdbOptions, path: *const i8, err: *mut i8);
pub fn rocksdb_repair_db(
options: RocksdbOptions, path: *const i8, err: *mut i8);
}
#[allow(dead_code)]

@ -7,19 +7,23 @@ use test::Bencher;
fn main() {
match rocksdb::create_or_open("/tmp/rust-rocksdb".to_string()) {
Ok(db) => {
db.put(b"my key", b"my value");
for i in range(0u, 20) {
spawn(proc() {
db.put(b"my key", b"my value");
db.get(b"my key").map( |value| {
match value.to_utf8() {
Some(v) =>
println!("retrieved utf8 value {}", v),
None =>
println!("did not read valid utf-8 out of the db"),
}});
db.get(b"my key").map( |value| {
match value.to_utf8() {
Some(v) =>
println!("retrieved utf8 value {}", v),
None =>
println!("did not read valid utf-8 out of the db"),
}});
db.get(b"NOT my key").on_absent(|| { println!("value not found") });
db.get(b"NOT my key").on_absent(|| { println!("value not found") });
db.close();
db.close();
});
}
},
Err(e) => panic!(e),
}

@ -74,6 +74,31 @@ impl Rocksdb {
}
}
pub fn delete(&self, key: &[u8]) -> Result<(),String> {
unsafe {
let writeopts = rocksdb_ffi::rocksdb_writeoptions_create();
let err = 0 as *mut i8;
rocksdb_ffi::rocksdb_delete(self.inner, writeopts, key.as_ptr(),
key.len() as size_t, err);
if err.is_not_null() {
let cs = CString::new(err as *const i8, true);
match cs.as_str() {
Some(error_string) =>
return Err(error_string.to_string()),
None => {
let ie = IoError::last_error();
return Err(format!(
"ERROR: desc:{}, details:{}",
ie.desc,
ie.detail.unwrap_or_else(
|| {"none provided by OS".to_string()})))
}
}
}
return Ok(())
}
}
pub fn close(&self) {
unsafe { rocksdb_ffi::rocksdb_close(self.inner); }
}
@ -227,7 +252,8 @@ fn external() {
let p = db.put(b"k1", b"v1111");
assert!(p.is_ok());
let r: RocksdbResult<RocksdbVector, String> = db.get(b"k1");
//assert!(r.is_some());
r.map(|v| { assert!(v.as_slice().len() == 5); } );
assert!(r.unwrap().to_utf8().unwrap() == "v1111");
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").is_none());
db.close();
}

Loading…
Cancel
Save