@ -60,10 +60,12 @@ pub enum Direction {
Reverse ,
}
pub type KVBytes = ( Box < [ u8 ] > , Box < [ u8 ] > ) ;
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 ;
if ! self . just_seeked {
match self . direction {
@ -201,13 +203,13 @@ impl<'a> Snapshot<'a> {
}
pub fn iterator ( & self , mode : IteratorMode ) -> DBIterator {
let mut readopts = ReadOptions ::new ( ) ;
let mut readopts = ReadOptions ::default ( ) ;
readopts . set_snapshot ( self ) ;
DBIterator ::new ( self . db , & readopts , mode )
}
pub fn get ( & self , key : & [ u8 ] ) -> Result < Option < DBVector > , String > {
let mut readopts = ReadOptions ::new ( ) ;
let mut readopts = ReadOptions ::default ( ) ;
readopts . set_snapshot ( self ) ;
self . db . get_opt ( key , & readopts )
}
@ -216,7 +218,7 @@ impl<'a> Snapshot<'a> {
cf : DBCFHandle ,
key : & [ u8 ] )
-> Result < Option < DBVector > , String > {
let mut readopts = ReadOptions ::new ( ) ;
let mut readopts = ReadOptions ::default ( ) ;
readopts . set_snapshot ( self ) ;
self . db . get_cf_opt ( cf , key , & readopts )
}
@ -250,7 +252,7 @@ pub trait Writable {
impl DB {
pub fn open_default ( path : & str ) -> Result < DB , String > {
let mut opts = Options ::new ( ) ;
let mut opts = Options ::default ( ) ;
opts . create_if_missing ( true ) ;
DB ::open ( & opts , path )
}
@ -410,7 +412,7 @@ impl DB {
}
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 ,
@ -449,7 +451,7 @@ impl DB {
}
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 ,
@ -493,7 +495,7 @@ impl DB {
cf : DBCFHandle ,
key : & [ u8 ] )
-> 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 ,
@ -551,16 +553,16 @@ impl DB {
}
pub fn iterator ( & self , mode : IteratorMode ) -> DBIterator {
let opts = ReadOptions ::new ( ) ;
DBIterator ::new ( & self , & opts , mode )
let opts = ReadOptions ::default ( ) ;
DBIterator ::new ( self , & opts , mode )
}
pub fn iterator_cf ( & self ,
cf_handle : DBCFHandle ,
mode : IteratorMode )
-> Result < DBIterator , String > {
let opts = ReadOptions ::new ( ) ;
DBIterator ::new_cf ( & self , cf_handle , & opts , mode )
let opts = ReadOptions ::default ( ) ;
DBIterator ::new_cf ( self , cf_handle , & opts , mode )
}
pub fn snapshot ( & self ) -> Snapshot {
@ -698,7 +700,7 @@ impl DB {
impl Writable for DB {
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 ,
@ -706,11 +708,11 @@ impl Writable for DB {
key : & [ u8 ] ,
value : & [ u8 ] )
-> 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 > {
self . merge_opt ( key , value , & WriteOptions ::new ( ) )
self . merge_opt ( key , value , & WriteOptions ::default ( ) )
}
fn merge_cf ( & self ,
@ -718,20 +720,20 @@ impl Writable for DB {
key : & [ u8 ] ,
value : & [ u8 ] )
-> 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 > {
self . delete_opt ( key , & WriteOptions ::new ( ) )
self . delete_opt ( key , & WriteOptions ::default ( ) )
}
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 {
pub fn new ( ) -> WriteBatch {
impl Default for WriteBatch {
fn default ( ) -> WriteBatch {
WriteBatch {
inner : unsafe { rocksdb_ffi ::rocksdb_writebatch_create ( ) } ,
}
@ -837,11 +839,6 @@ impl Drop for ReadOptions {
}
impl ReadOptions {
fn new ( ) -> ReadOptions {
unsafe {
ReadOptions { inner : rocksdb_ffi ::rocksdb_readoptions_create ( ) }
}
}
// TODO add snapshot setting here
// TODO add snapshot wrapper structs with proper destructors;
// that struct needs an "iterator" impl too.
@ -860,6 +857,14 @@ impl ReadOptions {
}
}
impl Default for ReadOptions {
fn default ( ) -> ReadOptions {
unsafe {
ReadOptions { inner : rocksdb_ffi ::rocksdb_readoptions_create ( ) }
}
}
}
pub struct DBVector {
base : * mut u8 ,
len : usize ,
@ -905,7 +910,7 @@ fn external() {
assert! ( db . delete ( b" k1 " ) . is_ok ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
let result = DB ::destroy ( & opts , path ) ;
assert! ( result . is_ok ( ) ) ;
}
@ -914,7 +919,7 @@ fn external() {
fn errors_do_stuff ( ) {
let path = "_rust_rocksdb_error" ;
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
match DB ::destroy ( & opts , path ) {
Err ( ref s ) = > {
@ -933,7 +938,7 @@ fn writebatch_works() {
let db = DB ::open_default ( path ) . unwrap ( ) ;
{
// test put
let batch = WriteBatch ::new ( ) ;
let batch = WriteBatch ::default ( ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
let _ = batch . put ( b" k1 " , b" v1111 " ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
@ -944,14 +949,14 @@ fn writebatch_works() {
}
{
// test delete
let batch = WriteBatch ::new ( ) ;
let batch = WriteBatch ::default ( ) ;
let _ = batch . delete ( b" k1 " ) ;
let p = db . write ( batch ) ;
assert! ( p . is_ok ( ) ) ;
assert! ( db . get ( b" k1 " ) . unwrap ( ) . is_none ( ) ) ;
}
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
}
@ -973,7 +978,7 @@ fn iterator_test() {
from_utf8 ( & * v ) . unwrap ( ) ) ;
}
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
}
@ -995,6 +1000,6 @@ fn snapshot_test() {
assert! ( db . get ( b" k2 " ) . unwrap ( ) . is_some ( ) ) ;
assert! ( snap . get ( b" k2 " ) . unwrap ( ) . is_none ( ) ) ;
}
let opts = Options ::new ( ) ;
let opts = Options ::default ( ) ;
assert! ( DB ::destroy ( & opts , path ) . is_ok ( ) ) ;
}