diff --git a/src/lib.rs b/src/lib.rs index 5a760e4..e423b02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,7 @@ pub use rocksdb::{ RocksDB, RocksDBResult, RocksDBVector, + Writable, }; pub use rocksdb_options::{ RocksDBOptions, diff --git a/src/main.rs b/src/main.rs index dddeabe..7e18a46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ extern crate rocksdb; extern crate test; -use rocksdb::{RocksDBOptions, RocksDB, MergeOperands, new_bloom_filter}; +use rocksdb::{RocksDBOptions, RocksDB, MergeOperands, new_bloom_filter, Writable}; use rocksdb::RocksDBCompactionStyle::RocksDBUniversalCompaction; fn main() { @@ -88,7 +88,7 @@ mod tests { use test::Bencher; use std::thread::sleep_ms; - use rocksdb::{RocksDBOptions, RocksDB, MergeOperands, new_bloom_filter}; + use rocksdb::{RocksDBOptions, RocksDB, MergeOperands, new_bloom_filter, Writable}; use rocksdb::RocksDBCompactionStyle::RocksDBUniversalCompaction; fn tuned_for_somebody_elses_disk() -> RocksDB { diff --git a/src/merge_operator.rs b/src/merge_operator.rs index 35006d6..f9f3741 100644 --- a/src/merge_operator.rs +++ b/src/merge_operator.rs @@ -21,7 +21,7 @@ use std::ptr; use std::slice; use rocksdb_options::{RocksDBOptions}; -use rocksdb::{RocksDB, RocksDBResult, RocksDBVector}; +use rocksdb::{RocksDB, RocksDBResult, RocksDBVector, Writable}; pub struct MergeOperatorCallback { pub name: CString, diff --git a/src/rocksdb.rs b/src/rocksdb.rs index f4e84f7..a504db7 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -32,6 +32,17 @@ pub struct RocksDB { inner: rocksdb_ffi::RocksDBInstance, } +pub struct WriteBatch { + inner: rocksdb_ffi::RocksDBWriteBatch +} + +// This is for the RocksDB and write batches to share the same API +pub trait Writable { + fn put(&self, key: &[u8], value: &[u8]) -> Result<(), &str>; + fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), &str>; + fn delete(&self, key: &[u8]) -> Result<(),&str>; +} + fn error_message<'a>(ptr: *const i8) -> &'a str { unsafe { return from_utf8(CStr::from_ptr(ptr).to_bytes()).unwrap(); @@ -117,33 +128,11 @@ impl RocksDB { } } - pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), &str> { - unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); - let err = 0 as *mut i8; - rocksdb_ffi::rocksdb_put(self.inner, writeopts, key.as_ptr(), - key.len() as size_t, value.as_ptr(), - value.len() as size_t, err); - if !err.is_null() { - return Err(error_message(err)); - } - return Ok(()) - } - } - - pub fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), &str> { - unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); - let err = 0 as *mut i8; - rocksdb_ffi::rocksdb_merge(self.inner, writeopts, key.as_ptr(), - key.len() as size_t, value.as_ptr(), - value.len() as size_t, err); - if !err.is_null() { - return Err(error_message(err)); - } - return Ok(()) - } - } + ////pub fn create_writebatch(self) -> WriteBatch { + //// WriteBatch(unsafe { + //// rocksdb_ffi::rocksdb_writebatch_create() + //// }) + ////} pub fn get(&self, key: &[u8]) -> RocksDBResult { unsafe { @@ -172,12 +161,33 @@ impl RocksDB { } } - pub fn delete(&self, key: &[u8]) -> Result<(),&str> { + pub fn close(&self) { + unsafe { rocksdb_ffi::rocksdb_close(self.inner); } + } +} + +impl Writable for RocksDB { + fn put(&self, key: &[u8], value: &[u8]) -> Result<(), &str> { + unsafe { + let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); + let err = 0 as *mut i8; + rocksdb_ffi::rocksdb_put(self.inner, writeopts, key.as_ptr(), + key.len() as size_t, value.as_ptr(), + value.len() as size_t, err); + if !err.is_null() { + return Err(error_message(err)); + } + return Ok(()) + } + } + + fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), &str> { 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); + rocksdb_ffi::rocksdb_merge(self.inner, writeopts, key.as_ptr(), + key.len() as size_t, value.as_ptr(), + value.len() as size_t, err); if !err.is_null() { return Err(error_message(err)); } @@ -185,8 +195,17 @@ impl RocksDB { } } - pub fn close(&self) { - unsafe { rocksdb_ffi::rocksdb_close(self.inner); } + fn delete(&self, key: &[u8]) -> Result<(),&str> { + 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_null() { + return Err(error_message(err)); + } + return Ok(()) + } } }