some scaffolding, refactoring, clearing of todo's

master
Tyler Neely 10 years ago
parent b87ca6bce8
commit 14a68671e7
  1. 94
      src/ffi.rs
  2. 1
      src/lib.rs
  3. 30
      src/rocksdb.rs

@ -33,6 +33,8 @@ pub struct RocksDBBlockBasedTableOptions(pub *const c_void);
pub struct RocksDBCache(pub *const c_void); pub struct RocksDBCache(pub *const c_void);
#[repr(C)] #[repr(C)]
pub struct RocksDBFilterPolicy(pub *const c_void); pub struct RocksDBFilterPolicy(pub *const c_void);
#[repr(C)]
pub struct RocksDBSnapshot(pub *const c_void);
impl Copy for RocksDBOptions {} impl Copy for RocksDBOptions {}
impl Copy for RocksDBInstance {} impl Copy for RocksDBInstance {}
@ -45,6 +47,7 @@ impl Copy for RocksDBFilterPolicy {}
impl Copy for RocksDBCompactionStyle {} impl Copy for RocksDBCompactionStyle {}
impl Copy for RocksDBCompressionType {} impl Copy for RocksDBCompressionType {}
impl Copy for RocksDBUniversalCompactionStyle {} impl Copy for RocksDBUniversalCompactionStyle {}
impl Copy for RocksDBSnapshot {}
pub fn new_bloom_filter(bits: c_int) -> RocksDBFilterPolicy { pub fn new_bloom_filter(bits: c_int) -> RocksDBFilterPolicy {
unsafe { unsafe {
@ -168,50 +171,73 @@ extern {
pub fn rocksdb_filterpolicy_create_bloom( pub fn rocksdb_filterpolicy_create_bloom(
bits_per_key: c_int) -> RocksDBFilterPolicy; bits_per_key: c_int) -> RocksDBFilterPolicy;
pub fn rocksdb_open(options: RocksDBOptions, pub fn rocksdb_open(options: RocksDBOptions,
path: *const i8, err: *mut i8) -> RocksDBInstance; path: *const i8,
err: *mut i8
) -> RocksDBInstance;
pub fn rocksdb_writeoptions_create() -> RocksDBWriteOptions; pub fn rocksdb_writeoptions_create() -> RocksDBWriteOptions;
pub fn rocksdb_put(db: RocksDBInstance, writeopts: RocksDBWriteOptions, pub fn rocksdb_put(db: RocksDBInstance,
k: *const u8, kLen: size_t, v: *const u8, writeopts: RocksDBWriteOptions,
vLen: size_t, err: *mut i8); k: *const u8, kLen: size_t,
v: *const u8, vLen: size_t,
err: *mut i8);
pub fn rocksdb_readoptions_create() -> RocksDBReadOptions; pub fn rocksdb_readoptions_create() -> RocksDBReadOptions;
pub fn rocksdb_get(db: RocksDBInstance, readopts: RocksDBReadOptions, pub fn rocksdb_get(db: RocksDBInstance,
k: *const u8, kLen: size_t, readopts: RocksDBReadOptions,
valLen: *const size_t, err: *mut i8) -> *mut c_void; k: *const u8, kLen: size_t,
pub fn rocksdb_delete(db: RocksDBInstance, writeopts: RocksDBWriteOptions, valLen: *const size_t,
k: *const u8, kLen: size_t, err: *mut i8) -> *mut c_void; 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_close(db: RocksDBInstance);
pub fn rocksdb_destroy_db( pub fn rocksdb_destroy_db(options: RocksDBOptions,
options: RocksDBOptions, path: *const i8, err: *mut i8); path: *const i8, err: *mut i8);
pub fn rocksdb_repair_db( pub fn rocksdb_repair_db(options: RocksDBOptions,
options: RocksDBOptions, path: *const i8, err: *mut i8); path: *const i8, err: *mut i8);
// Merge // Merge
pub fn rocksdb_merge(db: RocksDBInstance, writeopts: RocksDBWriteOptions, pub fn rocksdb_merge(db: RocksDBInstance,
k: *const u8, kLen: size_t, writeopts: RocksDBWriteOptions,
v: *const u8, vLen: size_t, err: *mut i8); k: *const u8, kLen: size_t,
v: *const u8, vLen: size_t,
err: *mut i8);
pub fn rocksdb_mergeoperator_create( pub fn rocksdb_mergeoperator_create(
state: *mut c_void, state: *mut c_void,
destroy: extern fn(*mut c_void) -> (), destroy: extern fn(*mut c_void) -> (),
full_merge: extern fn ( full_merge: extern fn (arg: *mut c_void,
arg: *mut c_void, key: *const c_char, key_len: size_t, key: *const c_char, key_len: size_t,
existing_value: *const c_char, existing_value_len: size_t, existing_value: *const c_char,
operands_list: *const *const c_char, operands_list_len: *const size_t, existing_value_len: size_t,
num_operands: c_int, operands_list: *const *const c_char,
success: *mut u8, new_value_length: *mut size_t operands_list_len: *const size_t,
) -> *const c_char, num_operands: c_int, success: *mut u8,
partial_merge: extern fn( new_value_length: *mut size_t
arg: *mut c_void, key: *const c_char, key_len: size_t, ) -> *const c_char,
operands_list: *const *const c_char, operands_list_len: *const size_t, partial_merge: extern fn(arg: *mut c_void,
num_operands: c_int, key: *const c_char, key_len: size_t,
success: *mut u8, new_value_length: *mut size_t operands_list: *const *const c_char,
) -> *const c_char, operands_list_len: *const size_t,
delete_value: Option<extern "C" fn(*mut c_void, value: *const c_char, num_operands: c_int, success: *mut u8,
value_len: *mut size_t) -> ()>, new_value_length: *mut size_t
) -> *const c_char,
delete_value: Option<extern "C" fn(*mut c_void,
value: *const c_char,
value_len: *mut size_t
) -> ()>,
name_fn: extern fn(*mut c_void) -> *const c_char, name_fn: extern fn(*mut c_void) -> *const c_char,
) -> RocksDBMergeOperator; ) -> RocksDBMergeOperator;
pub fn rocksdb_mergeoperator_destroy(mo: RocksDBMergeOperator); pub fn rocksdb_mergeoperator_destroy(mo: RocksDBMergeOperator);
pub fn rocksdb_options_set_merge_operator( pub fn rocksdb_options_set_merge_operator(options: RocksDBOptions,
options: RocksDBOptions, mo: RocksDBMergeOperator);
mo: RocksDBMergeOperator); // Snapshot
pub fn rocksdb_create_snapshot(db: RocksDBInstance) -> RocksDBSnapshot;
pub fn rocksdb_readoptions_set_snapshot(read_opts: RocksDBReadOptions,
snapshot: RocksDBSnapshot);
pub fn rocksdb_release_snapshot(db: RocksDBInstance,
snapshot: RocksDBSnapshot);
} }
#[allow(dead_code)] #[allow(dead_code)]

@ -24,6 +24,7 @@ pub use ffi::{
RocksDBUniversalCompactionStyle, RocksDBUniversalCompactionStyle,
RocksDBCompactionStyle, RocksDBCompactionStyle,
RocksDBCompressionType, RocksDBCompressionType,
RocksDBSnapshot,
}; };
pub use rocksdb::{ pub use rocksdb::{
RocksDB, RocksDB,

@ -23,8 +23,12 @@ use std::ffi::CString;
use std::str::from_utf8; use std::str::from_utf8;
use std::slice; use std::slice;
use std::str::from_c_str; use std::str::from_c_str;
use std::io::fs::PathExtensions;
use std::io;
use std::io::fs;
use rocksdb_ffi; use rocksdb_ffi;
use rocksdb_ffi::RocksDBSnapshot;
use rocksdb_options::RocksDBOptions; use rocksdb_options::RocksDBOptions;
pub struct RocksDB { pub struct RocksDB {
@ -43,8 +47,13 @@ impl RocksDB {
let cpath = CString::from_slice(path.as_bytes()); let cpath = CString::from_slice(path.as_bytes());
let cpath_ptr = cpath.as_ptr(); let cpath_ptr = cpath.as_ptr();
//TODO test path here, as if rocksdb fails it will just crash the let ospath = Path::new(path);
// process currently if !ospath.exists() {
match fs::mkdir_recursive(&ospath, io::USER_DIR) {
Err(e) => return Err(e.desc),
Ok(_) => (),
}
}
let err = 0 as *mut i8; let err = 0 as *mut i8;
let db = rocksdb_ffi::rocksdb_open(opts.inner, cpath_ptr, err); let db = rocksdb_ffi::rocksdb_open(opts.inner, cpath_ptr, err);
@ -65,8 +74,13 @@ impl RocksDB {
let cpath = CString::from_slice(path.as_bytes()); let cpath = CString::from_slice(path.as_bytes());
let cpath_ptr = cpath.as_ptr(); let cpath_ptr = cpath.as_ptr();
//TODO test path here, as if rocksdb fails it will just crash the let ospath = Path::new(path);
// process currently if !ospath.exists() {
match fs::mkdir_recursive(&ospath, io::USER_DIR) {
Err(e) => return Err(e.desc),
Ok(_) => (),
}
}
let err = 0 as *mut i8; let err = 0 as *mut i8;
let result = rocksdb_ffi::rocksdb_destroy_db( let result = rocksdb_ffi::rocksdb_destroy_db(
@ -79,6 +93,12 @@ impl RocksDB {
} }
} }
pub fn create_snapshot(self) -> RocksDBSnapshot {
unsafe {
rocksdb_ffi::rocksdb_create_snapshot(self.inner)
}
}
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), &str> { pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), &str> {
unsafe { unsafe {
let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let writeopts = rocksdb_ffi::rocksdb_writeoptions_create();
@ -132,7 +152,7 @@ impl RocksDB {
return RocksDBResult::Error(cs); return RocksDBResult::Error(cs);
} }
match val.is_null() { match val.is_null() {
true => RocksDBResult::None, true => RocksDBResult::None,
false => { false => {
RocksDBResult::Some(RocksDBVector::from_c(val, val_len)) RocksDBResult::Some(RocksDBVector::from_c(val, val_len))
} }

Loading…
Cancel
Save