|
|
@ -13,13 +13,13 @@ |
|
|
|
// limitations under the License.
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use {DB, Error, Options, WriteOptions, ColumnFamily, ColumnFamilyDescriptor}; |
|
|
|
|
|
|
|
use ffi; |
|
|
|
use ffi; |
|
|
|
use ffi_util::opt_bytes_to_ptr; |
|
|
|
use ffi_util::opt_bytes_to_ptr; |
|
|
|
|
|
|
|
use {ColumnFamily, ColumnFamilyDescriptor, Error, Options, WriteOptions, DB}; |
|
|
|
|
|
|
|
|
|
|
|
use libc::{self, c_char, c_int, c_uchar, c_void, size_t}; |
|
|
|
use libc::{self, c_char, c_int, c_uchar, c_void, size_t}; |
|
|
|
use std::collections::BTreeMap; |
|
|
|
use std::collections::BTreeMap; |
|
|
|
|
|
|
|
use std::ffi::CStr; |
|
|
|
use std::ffi::CString; |
|
|
|
use std::ffi::CString; |
|
|
|
use std::fmt; |
|
|
|
use std::fmt; |
|
|
|
use std::fs; |
|
|
|
use std::fs; |
|
|
@ -28,7 +28,6 @@ use std::path::Path; |
|
|
|
use std::ptr; |
|
|
|
use std::ptr; |
|
|
|
use std::slice; |
|
|
|
use std::slice; |
|
|
|
use std::str; |
|
|
|
use std::str; |
|
|
|
use std::ffi::CStr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new_bloom_filter(bits: c_int) -> *mut ffi::rocksdb_filterpolicy_t { |
|
|
|
pub fn new_bloom_filter(bits: c_int) -> *mut ffi::rocksdb_filterpolicy_t { |
|
|
|
unsafe { ffi::rocksdb_filterpolicy_create_bloom(bits) } |
|
|
|
unsafe { ffi::rocksdb_filterpolicy_create_bloom(bits) } |
|
|
@ -150,7 +149,6 @@ pub struct DBRawIterator { |
|
|
|
inner: *mut ffi::rocksdb_iterator_t, |
|
|
|
inner: *mut ffi::rocksdb_iterator_t, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// An iterator over a database or column family, with specifiable
|
|
|
|
/// An iterator over a database or column family, with specifiable
|
|
|
|
/// ranges and direction.
|
|
|
|
/// ranges and direction.
|
|
|
|
///
|
|
|
|
///
|
|
|
@ -201,7 +199,11 @@ pub enum IteratorMode<'a> { |
|
|
|
|
|
|
|
|
|
|
|
impl DBRawIterator { |
|
|
|
impl DBRawIterator { |
|
|
|
fn new(db: &DB, readopts: &ReadOptions) -> DBRawIterator { |
|
|
|
fn new(db: &DB, readopts: &ReadOptions) -> DBRawIterator { |
|
|
|
unsafe { DBRawIterator { inner: ffi::rocksdb_create_iterator(db.inner, readopts.inner) } } |
|
|
|
unsafe { |
|
|
|
|
|
|
|
DBRawIterator { |
|
|
|
|
|
|
|
inner: ffi::rocksdb_create_iterator(db.inner, readopts.inner), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn new_cf( |
|
|
|
fn new_cf( |
|
|
@ -581,10 +583,13 @@ impl<'a> Drop for Snapshot<'a> { |
|
|
|
|
|
|
|
|
|
|
|
impl ColumnFamilyDescriptor { |
|
|
|
impl ColumnFamilyDescriptor { |
|
|
|
// Create a new column family descriptor with the specified name and options.
|
|
|
|
// Create a new column family descriptor with the specified name and options.
|
|
|
|
pub fn new<S>(name: S, options: Options) -> Self where S: Into<String> { |
|
|
|
pub fn new<S>(name: S, options: Options) -> Self |
|
|
|
|
|
|
|
where |
|
|
|
|
|
|
|
S: Into<String>, |
|
|
|
|
|
|
|
{ |
|
|
|
ColumnFamilyDescriptor { |
|
|
|
ColumnFamilyDescriptor { |
|
|
|
name: name.into(), |
|
|
|
name: name.into(), |
|
|
|
options |
|
|
|
options, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -606,13 +611,21 @@ impl DB { |
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Column families opened using this function will be created with default `Options`.
|
|
|
|
/// Column families opened using this function will be created with default `Options`.
|
|
|
|
pub fn open_cf<P: AsRef<Path>>(opts: &Options, path: P, cfs: &[&str]) -> Result<DB, Error> { |
|
|
|
pub fn open_cf<P: AsRef<Path>>(opts: &Options, path: P, cfs: &[&str]) -> Result<DB, Error> { |
|
|
|
let cfs_v = cfs.to_vec().iter().map(|name| ColumnFamilyDescriptor::new(*name, Options::default())).collect(); |
|
|
|
let cfs_v = cfs |
|
|
|
|
|
|
|
.to_vec() |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|name| ColumnFamilyDescriptor::new(*name, Options::default())) |
|
|
|
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
|
|
DB::open_cf_descriptors(opts, path, cfs_v) |
|
|
|
DB::open_cf_descriptors(opts, path, cfs_v) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Open a database with the given database options and column family names/options.
|
|
|
|
/// Open a database with the given database options and column family names/options.
|
|
|
|
pub fn open_cf_descriptors<P: AsRef<Path>>(opts: &Options, path: P, cfs: Vec<ColumnFamilyDescriptor>) -> Result<DB, Error> { |
|
|
|
pub fn open_cf_descriptors<P: AsRef<Path>>( |
|
|
|
|
|
|
|
opts: &Options, |
|
|
|
|
|
|
|
path: P, |
|
|
|
|
|
|
|
cfs: Vec<ColumnFamilyDescriptor>, |
|
|
|
|
|
|
|
) -> Result<DB, Error> { |
|
|
|
let path = path.as_ref(); |
|
|
|
let path = path.as_ref(); |
|
|
|
let cpath = match CString::new(path.to_string_lossy().as_bytes()) { |
|
|
|
let cpath = match CString::new(path.to_string_lossy().as_bytes()) { |
|
|
|
Ok(c) => c, |
|
|
|
Ok(c) => c, |
|
|
@ -646,7 +659,7 @@ impl DB { |
|
|
|
if !cfs_v.iter().any(|cf| cf.name == "default") { |
|
|
|
if !cfs_v.iter().any(|cf| cf.name == "default") { |
|
|
|
cfs_v.push(ColumnFamilyDescriptor { |
|
|
|
cfs_v.push(ColumnFamilyDescriptor { |
|
|
|
name: String::from("default"), |
|
|
|
name: String::from("default"), |
|
|
|
options: Options::default() |
|
|
|
options: Options::default(), |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
// We need to store our CStrings in an intermediate vector
|
|
|
|
// We need to store our CStrings in an intermediate vector
|
|
|
@ -661,7 +674,8 @@ impl DB { |
|
|
|
// These handles will be populated by DB.
|
|
|
|
// These handles will be populated by DB.
|
|
|
|
let mut cfhandles: Vec<_> = cfs_v.iter().map(|_| ptr::null_mut()).collect(); |
|
|
|
let mut cfhandles: Vec<_> = cfs_v.iter().map(|_| ptr::null_mut()).collect(); |
|
|
|
|
|
|
|
|
|
|
|
let mut cfopts: Vec<_> = cfs_v.iter() |
|
|
|
let mut cfopts: Vec<_> = cfs_v |
|
|
|
|
|
|
|
.iter() |
|
|
|
.map(|cf| cf.options.inner as *const _) |
|
|
|
.map(|cf| cf.options.inner as *const _) |
|
|
|
.collect(); |
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
|
@ -672,7 +686,8 @@ impl DB { |
|
|
|
cfs_v.len() as c_int, |
|
|
|
cfs_v.len() as c_int, |
|
|
|
cfnames.as_mut_ptr(), |
|
|
|
cfnames.as_mut_ptr(), |
|
|
|
cfopts.as_mut_ptr(), |
|
|
|
cfopts.as_mut_ptr(), |
|
|
|
cfhandles.as_mut_ptr(),)); |
|
|
|
cfhandles.as_mut_ptr(), |
|
|
|
|
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for handle in &cfhandles { |
|
|
|
for handle in &cfhandles { |
|
|
@ -731,7 +746,6 @@ impl DB { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn destroy<P: AsRef<Path>>(opts: &Options, path: P) -> Result<(), Error> { |
|
|
|
pub fn destroy<P: AsRef<Path>>(opts: &Options, path: P) -> Result<(), Error> { |
|
|
|
let cpath = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap(); |
|
|
|
let cpath = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap(); |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
@ -929,11 +943,16 @@ impl DB { |
|
|
|
pub fn prefix_iterator_cf<'a>( |
|
|
|
pub fn prefix_iterator_cf<'a>( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
cf_handle: ColumnFamily, |
|
|
|
cf_handle: ColumnFamily, |
|
|
|
prefix: &'a [u8] |
|
|
|
prefix: &'a [u8], |
|
|
|
) -> Result<DBIterator, Error> { |
|
|
|
) -> Result<DBIterator, Error> { |
|
|
|
let mut opts = ReadOptions::default(); |
|
|
|
let mut opts = ReadOptions::default(); |
|
|
|
opts.set_prefix_same_as_start(true); |
|
|
|
opts.set_prefix_same_as_start(true); |
|
|
|
DBIterator::new_cf(self, cf_handle, &opts, IteratorMode::From(prefix, Direction::Forward)) |
|
|
|
DBIterator::new_cf( |
|
|
|
|
|
|
|
self, |
|
|
|
|
|
|
|
cf_handle, |
|
|
|
|
|
|
|
&opts, |
|
|
|
|
|
|
|
IteratorMode::From(prefix, Direction::Forward), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn raw_iterator(&self) -> DBRawIterator { |
|
|
|
pub fn raw_iterator(&self) -> DBRawIterator { |
|
|
@ -1207,7 +1226,9 @@ impl WriteBatch { |
|
|
|
|
|
|
|
|
|
|
|
impl Default for WriteBatch { |
|
|
|
impl Default for WriteBatch { |
|
|
|
fn default() -> WriteBatch { |
|
|
|
fn default() -> WriteBatch { |
|
|
|
WriteBatch { inner: unsafe { ffi::rocksdb_writebatch_create() } } |
|
|
|
WriteBatch { |
|
|
|
|
|
|
|
inner: unsafe { ffi::rocksdb_writebatch_create() }, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1268,21 +1289,21 @@ impl ReadOptions { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_prefix_same_as_start(&mut self, v: bool) { |
|
|
|
pub fn set_prefix_same_as_start(&mut self, v: bool) { |
|
|
|
unsafe { |
|
|
|
unsafe { ffi::rocksdb_readoptions_set_prefix_same_as_start(self.inner, v as c_uchar) } |
|
|
|
ffi::rocksdb_readoptions_set_prefix_same_as_start(self.inner, v as c_uchar) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_total_order_seek(&mut self, v: bool) { |
|
|
|
pub fn set_total_order_seek(&mut self, v: bool) { |
|
|
|
unsafe { |
|
|
|
unsafe { ffi::rocksdb_readoptions_set_total_order_seek(self.inner, v as c_uchar) } |
|
|
|
ffi::rocksdb_readoptions_set_total_order_seek(self.inner, v as c_uchar) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Default for ReadOptions { |
|
|
|
impl Default for ReadOptions { |
|
|
|
fn default() -> ReadOptions { |
|
|
|
fn default() -> ReadOptions { |
|
|
|
unsafe { ReadOptions { inner: ffi::rocksdb_readoptions_create() } } |
|
|
|
unsafe { |
|
|
|
|
|
|
|
ReadOptions { |
|
|
|
|
|
|
|
inner: ffi::rocksdb_readoptions_create(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1352,7 +1373,6 @@ fn test_db_vector() { |
|
|
|
assert_eq!(&*v, &ctrl[..]); |
|
|
|
assert_eq!(&*v, &ctrl[..]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn external() { |
|
|
|
fn external() { |
|
|
|
let path = "_rust_rocksdb_externaltest"; |
|
|
|
let path = "_rust_rocksdb_externaltest"; |
|
|
|