|
|
@ -40,6 +40,11 @@ pub struct ReadOptions { |
|
|
|
inner: rocksdb_ffi::RocksDBReadOptions, |
|
|
|
inner: rocksdb_ffi::RocksDBReadOptions, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Snapshot<'a> { |
|
|
|
|
|
|
|
db: &'a RocksDB, |
|
|
|
|
|
|
|
inner: rocksdb_ffi::RocksDBSnapshot, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub struct DBIterator { |
|
|
|
pub struct DBIterator { |
|
|
|
//TODO: should have a reference to DB to enforce scope, but it's trickier than I thought to add
|
|
|
|
//TODO: should have a reference to DB to enforce scope, but it's trickier than I thought to add
|
|
|
|
inner: rocksdb_ffi::RocksDBIterator, |
|
|
|
inner: rocksdb_ffi::RocksDBIterator, |
|
|
@ -87,7 +92,7 @@ impl <'a> Iterator for SubDBIterator<'a> { |
|
|
|
|
|
|
|
|
|
|
|
impl DBIterator { |
|
|
|
impl DBIterator { |
|
|
|
//TODO alias db & opts to different lifetimes, and DBIterator to the db's lifetime
|
|
|
|
//TODO alias db & opts to different lifetimes, and DBIterator to the db's lifetime
|
|
|
|
pub fn new(db: &RocksDB, readopts: &ReadOptions) -> DBIterator { |
|
|
|
fn new(db: &RocksDB, readopts: &ReadOptions) -> DBIterator { |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner, readopts.inner); |
|
|
|
let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner, readopts.inner); |
|
|
|
rocksdb_ffi::rocksdb_iter_seek_to_first(iterator); |
|
|
|
rocksdb_ffi::rocksdb_iter_seek_to_first(iterator); |
|
|
@ -122,13 +127,33 @@ impl DBIterator { |
|
|
|
|
|
|
|
|
|
|
|
impl Drop for DBIterator { |
|
|
|
impl Drop for DBIterator { |
|
|
|
fn drop(&mut self) { |
|
|
|
fn drop(&mut self) { |
|
|
|
println!("Dropped iter"); |
|
|
|
|
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
rocksdb_ffi::rocksdb_iter_destroy(self.inner); |
|
|
|
rocksdb_ffi::rocksdb_iter_destroy(self.inner); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl <'a> Snapshot<'a> { |
|
|
|
|
|
|
|
pub fn new(db: &RocksDB) -> Snapshot { |
|
|
|
|
|
|
|
let snapshot = unsafe { rocksdb_ffi::rocksdb_create_snapshot(db.inner) }; |
|
|
|
|
|
|
|
Snapshot{db: db, inner: snapshot} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn iterator(&self) -> DBIterator { |
|
|
|
|
|
|
|
let mut readopts = ReadOptions::new(); |
|
|
|
|
|
|
|
readopts.set_snapshot(self); |
|
|
|
|
|
|
|
DBIterator::new(self.db, &readopts) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl <'a> Drop for Snapshot<'a> { |
|
|
|
|
|
|
|
fn drop(&mut self) { |
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
rocksdb_ffi::rocksdb_release_snapshot(self.db.inner, self.inner); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// This is for the RocksDB and write batches to share the same API
|
|
|
|
// This is for the RocksDB and write batches to share the same API
|
|
|
|
pub trait Writable { |
|
|
|
pub trait Writable { |
|
|
|
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), String>; |
|
|
|
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), String>; |
|
|
@ -269,6 +294,10 @@ impl RocksDB { |
|
|
|
let opts = ReadOptions::new(); |
|
|
|
let opts = ReadOptions::new(); |
|
|
|
DBIterator::new(&self, &opts) |
|
|
|
DBIterator::new(&self, &opts) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn snapshot(&self) -> Snapshot { |
|
|
|
|
|
|
|
Snapshot::new(self) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Writable for RocksDB { |
|
|
|
impl Writable for RocksDB { |
|
|
@ -394,6 +423,11 @@ impl ReadOptions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn set_snapshot(&mut self, snapshot: &Snapshot) { |
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
rocksdb_ffi::rocksdb_readoptions_set_snapshot(self.inner, snapshot.inner); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub struct RocksDBVector { |
|
|
|
pub struct RocksDBVector { |
|
|
|