@ -60,10 +60,12 @@ pub enum Direction {
Reverse ,
Reverse ,
}
}
pub type KVBytes = ( Box < [ u8 ] > , Box < [ u8 ] > ) ;
impl Iterator for DBIterator {
impl Iterator for DBIterator {
type Item = ( Box < [ u8 ] > , Box < [ u8 ] > ) ;
type Item = KVBytes ;
fn next ( & mut self ) -> Option < ( Box < [ u8 ] > , Box < [ u8 ] > ) > {
fn next ( & mut self ) -> Option < KVBytes > {
let native_iter = self . inner ;
let native_iter = self . inner ;
if ! self . just_seeked {
if ! self . just_seeked {
match self . direction {
match self . direction {
@ -111,8 +113,8 @@ pub enum IteratorMode<'a> {
impl DBIterator {
impl DBIterator {
fn new < ' b > ( db : & DB ,
fn new ( db : & DB ,
readopts : & ' b ReadOptions ,
readopts : & ReadOptions ,
mode : IteratorMode )
mode : IteratorMode )
-> DBIterator {
-> DBIterator {
unsafe {
unsafe {
@ -201,13 +203,13 @@ impl<'a> Snapshot<'a> {
}
}
pub fn iterator ( & self , mode : IteratorMode ) -> DBIterator {
pub fn iterator ( & self , mode : IteratorMode ) -> DBIterator {
let mut readopts = ReadOptions ::new ( ) ;
let mut readopts = ReadOptions ::default ( ) ;
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 > {
pub fn get ( & self , key : & [ u8 ] ) -> Result < Option < DBVector > , String > {
let mut readopts = ReadOptions ::new ( ) ;
let mut readopts = ReadOptions ::default ( ) ;
readopts . set_snapshot ( self ) ;
readopts . set_snapshot ( self ) ;
self . db . get_opt ( key , & readopts )
self . db . get_opt ( key , & readopts )
}
}
@ -216,7 +218,7 @@ impl<'a> Snapshot<'a> {
cf : DBCFHandle ,
cf : DBCFHandle ,
key : & [ u8 ] )
key : & [ u8 ] )
-> Result < Option < DBVector > , String > {
-> Result < Option < DBVector > , String > {
let mut readopts = ReadOptions ::new ( ) ;
let mut readopts = ReadOptions ::default ( ) ;
readopts . set_snapshot ( self ) ;
readopts . set_snapshot ( self ) ;
self . db . get_cf_opt ( cf , key , & readopts )
self . db . get_cf_opt ( cf , key , & readopts )
}
}
@ -250,7 +252,7 @@ pub trait Writable {
impl DB {
impl DB {
pub fn open_default ( path : & str ) -> Result < DB , String > {
pub fn open_default ( path : & str ) -> Result < DB , String > {
let mut opts = Options ::new ( ) ;
let mut opts = Options ::default ( ) ;
opts . create_if_missing ( true ) ;
opts . create_if_missing ( true ) ;
DB ::open ( & opts , path )
DB ::open ( & opts , path )
}
}
@ -274,13 +276,8 @@ impl DB {
let cpath_ptr = cpath . as_ptr ( ) ;
let cpath_ptr = cpath . as_ptr ( ) ;
let ospath = Path ::new ( path ) ;
let ospath = Path ::new ( path ) ;
match fs ::create_dir_all ( & ospath ) {
if let Err ( e ) = fs ::create_dir_all ( & ospath ) {
Err ( e ) = > {
return Err ( format! ( "Failed to create rocksdb directory: {:?}" , e ) )
return Err ( format! ( " Failed to create rocksdb directory : \
{ :? } " ,
e ) )
}
Ok ( _ ) = > ( ) ,
}
}
let mut err : * const i8 = 0 as * const i8 ;
let mut err : * const i8 = 0 as * const i8 ;
@ -337,7 +334,7 @@ impl DB {
copts , handles , err_ptr ) ;
copts , handles , err_ptr ) ;
}
}
for handle in cfhandles . iter ( ) {
for handle in & cfhandles {
if handle . is_null ( ) {
if handle . is_null ( ) {
return Err ( "Received null column family handle from DB."
return Err ( "Received null column family handle from DB."
. to_string ( ) ) ;
. to_string ( ) ) ;
@ -411,11 +408,11 @@ impl DB {
if ! err . is_null ( ) {
if ! err . is_null ( ) {
return Err ( error_message ( err ) ) ;
return Err ( error_message ( err ) ) ;
}
}
return Ok ( ( ) ) ;
Ok ( ( ) )
}
}
pub fn write ( & self , batch : WriteBatch ) -> Result < ( ) , String > {
pub fn write ( & self , batch : WriteBatch ) -> Result < ( ) , String > {
self . write_opt ( batch , & WriteOptions ::new ( ) )
self . write_opt ( batch , & WriteOptions ::default ( ) )
}
}
pub fn get_opt ( & self ,
pub fn get_opt ( & self ,
@ -445,15 +442,16 @@ impl DB {
if ! err . is_null ( ) {
if ! err . is_null ( ) {
return Err ( error_message ( err ) ) ;
return Err ( error_message ( err ) ) ;
}
}
match val . is_null ( ) {
if val . is_null ( ) {
true = > Ok ( None ) ,
Ok ( None )
false = > Ok ( Some ( DBVector ::from_c ( val , val_len ) ) ) ,
} else {
Ok ( Some ( DBVector ::from_c ( val , val_len ) ) )
}
}
}
}
}
}
pub fn get ( & self , key : & [ u8 ] ) -> Result < Option < DBVector > , String > {
pub fn get ( & self , key : & [ u8 ] ) -> Result < Option < DBVector > , String > {
self . get_opt ( key , & ReadOptions ::new ( ) )
self . get_opt ( key , & ReadOptions ::default ( ) )
}
}
pub fn get_cf_opt ( & self ,
pub fn get_cf_opt ( & self ,
@ -485,9 +483,10 @@ impl DB {
if ! err . is_null ( ) {
if ! err . is_null ( ) {
return Err ( error_message ( err ) ) ;
return Err ( error_message ( err ) ) ;
}
}
match val . is_null ( ) {
if val . is_null ( ) {
true = > Ok ( None ) ,
Ok ( None )
false = > Ok ( Some ( DBVector ::from_c ( val , val_len ) ) ) ,
} else {
Ok ( Some ( DBVector ::from_c ( val , val_len ) ) )
}
}
}
}
}
}
@ -496,7 +495,7 @@ impl DB {
cf : DBCFHandle ,
cf : DBCFHandle ,
key : & [ u8 ] )
key : & [ u8 ] )
-> Result < Option < DBVector > , String > {
-> Result < Option < DBVector > , String > {
self . get_cf_opt ( cf , key , & ReadOptions ::new ( ) )
self . get_cf_opt ( cf , key , & ReadOptions ::default ( ) )
}
}
pub fn create_cf ( & mut self ,
pub fn create_cf ( & mut self ,
@ -554,16 +553,16 @@ impl DB {
}
}
pub fn iterator ( & self , mode : IteratorMode ) -> DBIterator {
pub fn iterator ( & self , mode : IteratorMode ) -> DBIterator {
let opts = ReadOptions ::new ( ) ;
let opts = ReadOptions ::default ( ) ;
DBIterator ::new ( & self , & opts , mode )
DBIterator ::new ( self , & opts , mode )
}
}
pub fn iterator_cf ( & self ,
pub fn iterator_cf ( & self ,
cf_handle : DBCFHandle ,
cf_handle : DBCFHandle ,
mode : IteratorMode )
mode : IteratorMode )
-> Result < DBIterator , String > {
-> Result < DBIterator , String > {
let opts = ReadOptions ::new ( ) ;
let opts = ReadOptions ::default ( ) ;
DBIterator ::new_cf ( & self , cf_handle , & opts , mode )
DBIterator ::new_cf ( self , cf_handle , & opts , mode )
}
}
pub fn snapshot ( & self ) -> Snapshot {
pub fn snapshot ( & self ) -> Snapshot {
@ -701,7 +700,7 @@ impl DB {
impl Writable for DB {
impl Writable for DB {
fn put ( & self , key : & [ u8 ] , value : & [ u8 ] ) -> Result < ( ) , String > {
fn put ( & self , key : & [ u8 ] , value : & [ u8 ] ) -> Result < ( ) , String > {
self . put_opt ( key , value , & WriteOptions ::new ( ) )
self . put_opt ( key , value , & WriteOptions ::default ( ) )
}
}
fn put_cf ( & self ,
fn put_cf ( & self ,
@ -709,11 +708,11 @@ impl Writable for DB {
key : & [ u8 ] ,
key : & [ u8 ] ,
value : & [ u8 ] )
value : & [ u8 ] )
-> Result < ( ) , String > {
-> Result < ( ) , String > {
self . put_cf_opt ( cf , key , value , & WriteOptions ::new ( ) )
self . put_cf_opt ( cf , key , value , & WriteOptions ::default ( ) )
}
}
fn merge ( & self , key : & [ u8 ] , value : & [ u8 ] ) -> Result < ( ) , String > {
fn merge ( & self , key : & [ u8 ] , value : & [ u8 ] ) -> Result < ( ) , String > {
self . merge_opt ( key , value , & WriteOptions ::new ( ) )
self . merge_opt ( key , value , & WriteOptions ::default ( ) )
}
}
fn merge_cf ( & self ,
fn merge_cf ( & self ,
@ -721,20 +720,20 @@ impl Writable for DB {
key : & [ u8 ] ,
key : & [ u8 ] ,
value : & [ u8 ] )
value : & [ u8 ] )
-> Result < ( ) , String > {
-> Result < ( ) , String > {
self . merge_cf_opt ( cf , key , value , & WriteOptions ::new ( ) )
self . merge_cf_opt ( cf , key , value , & WriteOptions ::default ( ) )
}
}
fn delete ( & self , key : & [ u8 ] ) -> Result < ( ) , String > {
fn delete ( & self , key : & [ u8 ] ) -> Result < ( ) , String > {
self . delete_opt ( key , & WriteOptions ::new ( ) )
self . delete_opt ( key , & WriteOptions ::default ( ) )
}
}
fn delete_cf ( & self , cf : DBCFHandle , key : & [ u8 ] ) -> Result < ( ) , String > {
fn delete_cf ( & self , cf : DBCFHandle , key : & [ u8 ] ) -> Result < ( ) , String > {
self . delete_cf_opt ( cf , key , & WriteOptions ::new ( ) )
self . delete_cf_opt ( cf , key , & WriteOptions ::default ( ) )
}
}
}
}
impl WriteBatch {
impl Default for WriteBatch {
pub fn new ( ) -> WriteBatch {
fn default ( ) -> WriteBatch {
WriteBatch {
WriteBatch {
inner : unsafe { rocksdb_ffi ::rocksdb_writebatch_create ( ) } ,
inner : unsafe { rocksdb_ffi ::rocksdb_writebatch_create ( ) } ,
}
}
@ -750,7 +749,7 @@ impl Drop for WriteBatch {
impl Drop for DB {
impl Drop for DB {
fn drop ( & mut self ) {
fn drop ( & mut self ) {
unsafe {
unsafe {
for ( _ , cf ) in self . cfs . iter ( ) {
for cf in self . cfs . values ( ) {
rocksdb_ffi ::rocksdb_column_family_handle_destroy ( * cf ) ;
rocksdb_ffi ::rocksdb_column_family_handle_destroy ( * cf ) ;
}
}
rocksdb_ffi ::rocksdb_close ( self . inner ) ;
rocksdb_ffi ::rocksdb_close ( self . inner ) ;
@ -840,11 +839,6 @@ impl Drop for ReadOptions {
}
}
impl ReadOptions {
impl ReadOptions {
fn new ( ) -> ReadOptions {
unsafe {
ReadOptions { inner : rocksdb_ffi ::rocksdb_readoptions_create ( ) }
}
}
// TODO add snapshot setting here
// TODO add snapshot setting here
// TODO add snapshot wrapper structs with proper destructors;
// TODO add snapshot wrapper structs with proper destructors;
// that struct needs an "iterator" impl too.
// that struct needs an "iterator" impl too.
@ -863,6 +857,14 @@ impl ReadOptions {
}
}
}
}
impl Default for ReadOptions {
fn default ( ) -> ReadOptions {
unsafe {
ReadOptions { inner : rocksdb_ffi ::rocksdb_readoptions_create ( ) }
}
}
}
pub struct DBVector {
pub struct DBVector {
base : * mut u8 ,
base : * mut u8 ,
len : usize ,
len : usize ,
@ -891,7 +893,7 @@ impl DBVector {
}
}
}
}
pub fn to_utf8 < ' a > ( & ' a self ) -> Option < & ' a str > {
pub fn to_utf8 ( & self ) -> Option < & str > {
from_utf8 ( self . deref ( ) ) . ok ( )
from_utf8 ( self . deref ( ) ) . ok ( )
}
}
}
}
@ -908,7 +910,7 @@ fn external() {
assert! ( db . delete ( b" k1 " ) . is_ok ( ) ) ;
assert! ( db . delete ( b" k1 " ) . is_ok ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
}
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
let result = DB ::destroy ( & opts , path ) ;
let result = DB ::destroy ( & opts , path ) ;
assert! ( result . is_ok ( ) ) ;
assert! ( result . is_ok ( ) ) ;
}
}
@ -917,7 +919,7 @@ fn external() {
fn errors_do_stuff ( ) {
fn errors_do_stuff ( ) {
let path = "_rust_rocksdb_error" ;
let path = "_rust_rocksdb_error" ;
let db = DB ::open_default ( path ) . unwrap ( ) ;
let db = DB ::open_default ( path ) . unwrap ( ) ;
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
// The DB will still be open when we try to destroy and the lock should fail
// The DB will still be open when we try to destroy and the lock should fail
match DB ::destroy ( & opts , path ) {
match DB ::destroy ( & opts , path ) {
Err ( ref s ) = > {
Err ( ref s ) = > {
@ -936,7 +938,7 @@ fn writebatch_works() {
let db = DB ::open_default ( path ) . unwrap ( ) ;
let db = DB ::open_default ( path ) . unwrap ( ) ;
{
{
// test put
// test put
let batch = WriteBatch ::new ( ) ;
let batch = WriteBatch ::default ( ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
let _ = batch . put ( b" k1 " , b" v1111 " ) ;
let _ = batch . put ( b" k1 " , b" v1111 " ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
@ -947,14 +949,14 @@ fn writebatch_works() {
}
}
{
{
// test delete
// test delete
let batch = WriteBatch ::new ( ) ;
let batch = WriteBatch ::default ( ) ;
let _ = batch . delete ( b" k1 " ) ;
let _ = batch . delete ( b" k1 " ) ;
let p = db . write ( batch ) ;
let p = db . write ( batch ) ;
assert! ( p . is_ok ( ) ) ;
assert! ( p . is_ok ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
}
}
}
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
}
}
@ -976,7 +978,7 @@ fn iterator_test() {
from_utf8 ( & * v ) . unwrap ( ) ) ;
from_utf8 ( & * v ) . unwrap ( ) ) ;
}
}
}
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
}
}
@ -998,6 +1000,6 @@ fn snapshot_test() {
assert! ( db . get ( b" k2 " ) . unwrap ( ) . is_some ( ) ) ;
assert! ( db . get ( b" k2 " ) . unwrap ( ) . is_some ( ) ) ;
assert! ( snap . get ( b" k2 " ) . unwrap ( ) . is_none ( ) ) ;
assert! ( snap . get ( b" k2 " ) . unwrap ( ) . is_none ( ) ) ;
}
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
}
}