@ -18,10 +18,9 @@ extern crate libc;
use self ::libc ::{ c_void , size_t } ;
use self ::libc ::{ c_void , size_t } ;
use std ::collections ::BTreeMap ;
use std ::collections ::BTreeMap ;
use std ::ffi ::{ CString , CStr } ;
use std ::ffi ::{ CString , CStr } ;
use std ::fs ::{ self , PathExt } ;
use std ::fs ;
use std ::ops ::Deref ;
use std ::ops ::Deref ;
use std ::path ::Path ;
use std ::path ::Path ;
use std ::ptr ::Unique ;
use std ::slice ;
use std ::slice ;
use std ::str ::from_utf8 ;
use std ::str ::from_utf8 ;
@ -197,11 +196,9 @@ impl RocksDB {
let cpath_ptr = cpath . as_ptr ( ) ;
let cpath_ptr = cpath . as_ptr ( ) ;
let ospath = Path ::new ( path ) ;
let ospath = Path ::new ( path ) ;
if ! ospath . exists ( ) {
match fs ::create_dir_all ( & ospath ) {
match fs ::create_dir_all ( & ospath ) {
Err ( e ) = > return Err ( "Failed to create rocksdb directory." . to_string ( ) ) ,
Err ( e ) = > return Err ( "Failed to create rocksdb directory." . to_string ( ) ) ,
Ok ( _ ) = > ( ) ,
Ok ( _ ) = > ( ) ,
}
}
}
let mut err : * const i8 = 0 as * const i8 ;
let mut err : * const i8 = 0 as * const i8 ;
@ -242,14 +239,13 @@ impl RocksDB {
} ) . collect ( ) ;
} ) . collect ( ) ;
// Prepare to ship to C.
// Prepare to ship to C.
let names = cfnames . as_slice ( ) ;
let copts : * const rocksdb_ffi ::RocksDBOptions = cfopts . as_ptr ( ) ;
let copts : * const rocksdb_ffi ::RocksDBOptions = cfopts . as_ptr ( ) ;
let handles : * const rocksdb_ffi ::RocksDBCFHandle = cfhandles . as_ptr ( ) ;
let handles : * const rocksdb_ffi ::RocksDBCFHandle = cfhandles . as_ptr ( ) ;
let nfam = cfs_v . len ( ) ;
let nfam = cfs_v . len ( ) ;
unsafe {
unsafe {
db = rocksdb_ffi ::rocksdb_open_column_families ( opts . inner , cpath_ptr ,
db = rocksdb_ffi ::rocksdb_open_column_families ( opts . inner , cpath_ptr ,
nfam as libc ::c_int ,
nfam as libc ::c_int ,
names . as_ptr ( ) ,
cf names. as_ptr ( ) ,
copts , handles , err_ptr ) ;
copts , handles , err_ptr ) ;
}
}
@ -279,10 +275,6 @@ impl RocksDB {
let cpath_ptr = cpath . as_ptr ( ) ;
let cpath_ptr = cpath . as_ptr ( ) ;
let ospath = Path ::new ( path ) ;
let ospath = Path ::new ( path ) ;
if ! ospath . exists ( ) {
return Err ( "path does not exist" . to_string ( ) ) ;
}
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 ;
unsafe {
unsafe {
@ -299,10 +291,6 @@ impl RocksDB {
let cpath_ptr = cpath . as_ptr ( ) ;
let cpath_ptr = cpath . as_ptr ( ) ;
let ospath = Path ::new ( path ) ;
let ospath = Path ::new ( path ) ;
if ! ospath . exists ( ) {
return Err ( "path does not exist" . to_string ( ) ) ;
}
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 ;
unsafe {
unsafe {
@ -384,7 +372,7 @@ impl RocksDB {
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 ;
unsafe {
unsafe {
rocksdb_ffi ::rocksdb_drop_column_family ( self . inner , cf . unwrap ( ) , err_ptr ) ;
rocksdb_ffi ::rocksdb_drop_column_family ( self . inner , * cf . unwrap ( ) , err_ptr ) ;
}
}
if ! err . is_null ( ) {
if ! err . is_null ( ) {
return Err ( error_message ( err ) ) ;
return Err ( error_message ( err ) ) ;
@ -472,7 +460,12 @@ impl Drop for WriteBatch {
impl Drop for RocksDB {
impl Drop for RocksDB {
fn drop ( & mut self ) {
fn drop ( & mut self ) {
unsafe { rocksdb_ffi ::rocksdb_close ( self . inner ) ; }
unsafe {
for ( _ , cf ) in self . cfs . iter ( ) {
rocksdb_ffi ::rocksdb_column_family_handle_destroy ( * cf ) ;
}
rocksdb_ffi ::rocksdb_close ( self . inner ) ;
}
}
}
}
}
@ -535,21 +528,21 @@ impl ReadOptions {
}
}
pub struct RocksDBVector {
pub struct RocksDBVector {
base : Unique < u8 > ,
base : * mut u8 ,
len : usize ,
len : usize ,
}
}
impl Deref for RocksDBVector {
impl Deref for RocksDBVector {
type Target = [ u8 ] ;
type Target = [ u8 ] ;
fn deref ( & self ) -> & [ u8 ] {
fn deref ( & self ) -> & [ u8 ] {
unsafe { slice ::from_raw_parts ( self . base . get ( ) , self . len ) }
unsafe { slice ::from_raw_parts ( self . base , self . len ) }
}
}
}
}
impl Drop for RocksDBVector {
impl Drop for RocksDBVector {
fn drop ( & mut self ) {
fn drop ( & mut self ) {
unsafe {
unsafe {
libc ::free ( * self . base . deref ( ) as * mut libc ::c_void ) ;
libc ::free ( self . base as * mut libc ::c_void ) ;
}
}
}
}
}
}
@ -558,7 +551,7 @@ impl RocksDBVector {
pub fn from_c ( val : * mut u8 , val_len : size_t ) -> RocksDBVector {
pub fn from_c ( val : * mut u8 , val_len : size_t ) -> RocksDBVector {
unsafe {
unsafe {
RocksDBVector {
RocksDBVector {
base : Unique ::new ( val ) ,
base : val ,
len : val_len as usize ,
len : val_len as usize ,
}
}
}
}