|
|
|
@ -154,7 +154,7 @@ pub struct Snapshot<'a> { |
|
|
|
|
/// ```
|
|
|
|
|
pub struct DBRawIterator<'a> { |
|
|
|
|
inner: *mut ffi::rocksdb_iterator_t, |
|
|
|
|
db: PhantomData<&'a DB> |
|
|
|
|
db: PhantomData<&'a DB>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// An iterator over a database or column family, with specifiable
|
|
|
|
@ -214,7 +214,7 @@ impl<'a> DBRawIterator<'a> { |
|
|
|
|
unsafe { |
|
|
|
|
DBRawIterator { |
|
|
|
|
inner: ffi::rocksdb_create_iterator(db.inner, readopts.inner), |
|
|
|
|
db: PhantomData |
|
|
|
|
db: PhantomData, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -227,7 +227,7 @@ impl<'a> DBRawIterator<'a> { |
|
|
|
|
unsafe { |
|
|
|
|
Ok(DBRawIterator { |
|
|
|
|
inner: ffi::rocksdb_create_iterator_cf(db.inner, readopts.inner, cf_handle.inner), |
|
|
|
|
db: PhantomData |
|
|
|
|
db: PhantomData, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -603,7 +603,11 @@ impl<'a> Snapshot<'a> { |
|
|
|
|
DBRawIterator::new(self.db, &readopts) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn raw_iterator_cf_opt(&self, cf_handle: ColumnFamily, mut readopts: ReadOptions) -> Result<DBRawIterator, Error> { |
|
|
|
|
pub fn raw_iterator_cf_opt( |
|
|
|
|
&self, |
|
|
|
|
cf_handle: ColumnFamily, |
|
|
|
|
mut readopts: ReadOptions, |
|
|
|
|
) -> Result<DBRawIterator, Error> { |
|
|
|
|
readopts.set_snapshot(self); |
|
|
|
|
DBRawIterator::new_cf(self.db, cf_handle, &readopts) |
|
|
|
|
} |
|
|
|
@ -613,17 +617,30 @@ impl<'a> Snapshot<'a> { |
|
|
|
|
self.get_opt(key, readopts) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_cf<K: AsRef<[u8]>>(&self, cf: ColumnFamily, key: K) -> Result<Option<DBVector>, Error> { |
|
|
|
|
pub fn get_cf<K: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
cf: ColumnFamily, |
|
|
|
|
key: K, |
|
|
|
|
) -> Result<Option<DBVector>, Error> { |
|
|
|
|
let readopts = ReadOptions::default(); |
|
|
|
|
self.get_cf_opt(cf, key.as_ref(), readopts) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_opt<K: AsRef<[u8]>>(&self, key: K, mut readopts: ReadOptions) -> Result<Option<DBVector>, Error> { |
|
|
|
|
pub fn get_opt<K: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
key: K, |
|
|
|
|
mut readopts: ReadOptions, |
|
|
|
|
) -> Result<Option<DBVector>, Error> { |
|
|
|
|
readopts.set_snapshot(self); |
|
|
|
|
self.db.get_opt(key.as_ref(), &readopts) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_cf_opt<K: AsRef<[u8]>>(&self, cf: ColumnFamily, key: K, mut readopts: ReadOptions) -> Result<Option<DBVector>, Error> { |
|
|
|
|
pub fn get_cf_opt<K: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
cf: ColumnFamily, |
|
|
|
|
key: K, |
|
|
|
|
mut readopts: ReadOptions, |
|
|
|
|
) -> Result<Option<DBVector>, Error> { |
|
|
|
|
readopts.set_snapshot(self); |
|
|
|
|
self.db.get_cf_opt(cf, key.as_ref(), &readopts) |
|
|
|
|
} |
|
|
|
@ -690,13 +707,14 @@ impl DB { |
|
|
|
|
"Failed to convert path to CString \ |
|
|
|
|
when opening DB." |
|
|
|
|
.to_owned(), |
|
|
|
|
)) |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if let Err(e) = fs::create_dir_all(&path) { |
|
|
|
|
return Err(Error::new(format!( |
|
|
|
|
"Failed to create RocksDB directory: `{:?}`.", e |
|
|
|
|
"Failed to create RocksDB directory: `{:?}`.", |
|
|
|
|
e |
|
|
|
|
))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -755,7 +773,8 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (n, h) in cfs_v.iter().zip(cfhandles) { |
|
|
|
|
cf_map.write() |
|
|
|
|
cf_map |
|
|
|
|
.write() |
|
|
|
|
.map_err(|e| Error::new(e.to_string()))? |
|
|
|
|
.insert(n.name.clone(), h); |
|
|
|
|
} |
|
|
|
@ -829,7 +848,11 @@ impl DB { |
|
|
|
|
self.write_opt(batch, &wo) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_opt<K: AsRef<[u8]>>(&self, key: K, readopts: &ReadOptions) -> Result<Option<DBVector>, Error> { |
|
|
|
|
pub fn get_opt<K: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
key: K, |
|
|
|
|
readopts: &ReadOptions, |
|
|
|
|
) -> Result<Option<DBVector>, Error> { |
|
|
|
|
if readopts.inner.is_null() { |
|
|
|
|
return Err(Error::new( |
|
|
|
|
"Unable to create RocksDB read options. \ |
|
|
|
@ -902,7 +925,11 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_cf<K: AsRef<[u8]>>(&self, cf: ColumnFamily, key: K) -> Result<Option<DBVector>, Error> { |
|
|
|
|
pub fn get_cf<K: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
cf: ColumnFamily, |
|
|
|
|
key: K, |
|
|
|
|
) -> Result<Option<DBVector>, Error> { |
|
|
|
|
self.get_cf_opt(cf, key.as_ref(), &ReadOptions::default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -914,7 +941,7 @@ impl DB { |
|
|
|
|
"Failed to convert path to CString \ |
|
|
|
|
when opening rocksdb" |
|
|
|
|
.to_owned(), |
|
|
|
|
)) |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
let cf = unsafe { |
|
|
|
@ -924,10 +951,12 @@ impl DB { |
|
|
|
|
cname.as_ptr(), |
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
self.cfs.write().map_err(|e| Error::new(e.to_string()))? |
|
|
|
|
self.cfs |
|
|
|
|
.write() |
|
|
|
|
.map_err(|e| Error::new(e.to_string()))? |
|
|
|
|
.insert(name.to_string(), cf_handle); |
|
|
|
|
|
|
|
|
|
ColumnFamily {
|
|
|
|
|
|
|
|
|
|
ColumnFamily { |
|
|
|
|
inner: cf_handle, |
|
|
|
|
db: PhantomData, |
|
|
|
|
} |
|
|
|
@ -936,29 +965,29 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn drop_cf(&self, name: &str) -> Result<(), Error> { |
|
|
|
|
if let Some(cf) = self.cfs.write().map_err(|e| Error::new(e.to_string()))? |
|
|
|
|
.remove(name) { |
|
|
|
|
if let Some(cf) = self |
|
|
|
|
.cfs |
|
|
|
|
.write() |
|
|
|
|
.map_err(|e| Error::new(e.to_string()))? |
|
|
|
|
.remove(name) |
|
|
|
|
{ |
|
|
|
|
unsafe { |
|
|
|
|
ffi_try!(ffi::rocksdb_drop_column_family(self.inner, cf,)); |
|
|
|
|
} |
|
|
|
|
Ok(()) |
|
|
|
|
} else { |
|
|
|
|
Err(Error::new( |
|
|
|
|
format!("Invalid column family: {}", name).to_owned() |
|
|
|
|
format!("Invalid column family: {}", name).to_owned(), |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Return the underlying column family handle.
|
|
|
|
|
pub fn cf_handle(&self, name: &str) -> Option<ColumnFamily> { |
|
|
|
|
self.cfs |
|
|
|
|
.read() |
|
|
|
|
.ok()? |
|
|
|
|
.get(name) |
|
|
|
|
.map(|h| ColumnFamily { |
|
|
|
|
inner: *h, |
|
|
|
|
db: PhantomData |
|
|
|
|
}) |
|
|
|
|
self.cfs.read().ok()?.get(name).map(|h| ColumnFamily { |
|
|
|
|
inner: *h, |
|
|
|
|
db: PhantomData, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn iterator(&self, mode: IteratorMode) -> DBIterator { |
|
|
|
@ -982,7 +1011,11 @@ impl DB { |
|
|
|
|
pub fn prefix_iterator<P: AsRef<[u8]>>(&self, prefix: P) -> DBIterator { |
|
|
|
|
let mut opts = ReadOptions::default(); |
|
|
|
|
opts.set_prefix_same_as_start(true); |
|
|
|
|
DBIterator::new(self, &opts, IteratorMode::From(prefix.as_ref(), Direction::Forward)) |
|
|
|
|
DBIterator::new( |
|
|
|
|
self, |
|
|
|
|
&opts, |
|
|
|
|
IteratorMode::From(prefix.as_ref(), Direction::Forward), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn iterator_cf( |
|
|
|
@ -1007,7 +1040,7 @@ impl DB { |
|
|
|
|
pub fn prefix_iterator_cf<P: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
cf_handle: ColumnFamily, |
|
|
|
|
prefix: P |
|
|
|
|
prefix: P, |
|
|
|
|
) -> Result<DBIterator, Error> { |
|
|
|
|
let mut opts = ReadOptions::default(); |
|
|
|
|
opts.set_prefix_same_as_start(true); |
|
|
|
@ -1034,14 +1067,14 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn put_opt<K, V>(&self, key: K, value: V, writeopts: &WriteOptions) -> Result<(), Error> |
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
|
|
ffi_try!(ffi::rocksdb_put( |
|
|
|
|
self.inner, |
|
|
|
|
writeopts.inner, |
|
|
|
@ -1060,15 +1093,15 @@ impl DB { |
|
|
|
|
key: K, |
|
|
|
|
value: V, |
|
|
|
|
writeopts: &WriteOptions, |
|
|
|
|
) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
|
|
ffi_try!(ffi::rocksdb_put_cf( |
|
|
|
|
self.inner, |
|
|
|
|
writeopts.inner, |
|
|
|
@ -1082,15 +1115,11 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn merge_opt<K, V>( |
|
|
|
|
&self, |
|
|
|
|
key: K, |
|
|
|
|
value: V, |
|
|
|
|
writeopts: &WriteOptions, |
|
|
|
|
) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn merge_opt<K, V>(&self, key: K, value: V, writeopts: &WriteOptions) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
@ -1114,14 +1143,14 @@ impl DB { |
|
|
|
|
value: V, |
|
|
|
|
writeopts: &WriteOptions, |
|
|
|
|
) -> Result<(), Error> |
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
|
|
ffi_try!(ffi::rocksdb_merge_cf( |
|
|
|
|
self.inner, |
|
|
|
|
writeopts.inner, |
|
|
|
@ -1135,9 +1164,13 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn delete_opt<K: AsRef<[u8]>>(&self, key: K, writeopts: &WriteOptions) -> Result<(), Error> { |
|
|
|
|
pub fn delete_opt<K: AsRef<[u8]>>( |
|
|
|
|
&self, |
|
|
|
|
key: K, |
|
|
|
|
writeopts: &WriteOptions, |
|
|
|
|
) -> Result<(), Error> { |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
ffi_try!(ffi::rocksdb_delete( |
|
|
|
|
self.inner, |
|
|
|
@ -1155,7 +1188,6 @@ impl DB { |
|
|
|
|
key: K, |
|
|
|
|
writeopts: &WriteOptions, |
|
|
|
|
) -> Result<(), Error> { |
|
|
|
|
|
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
|
@ -1171,30 +1203,34 @@ impl DB { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn put<K, V>(&self, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
self.put_opt(key.as_ref(), value.as_ref(), &WriteOptions::default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn put_cf<K, V>(&self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn put_cf<K, V>(&self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
self.put_cf_opt(cf, key.as_ref(), value.as_ref(), &WriteOptions::default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn merge<K, V>(&self, key: K, value: V) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn merge<K, V>(&self, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
self.merge_opt(key.as_ref(), value.as_ref(), &WriteOptions::default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn merge_cf<K, V>(&self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn merge_cf<K, V>(&self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
self.merge_cf_opt(cf, key.as_ref(), value.as_ref(), &WriteOptions::default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1284,9 +1320,10 @@ impl WriteBatch { |
|
|
|
|
|
|
|
|
|
/// Insert a value into the database under the given key.
|
|
|
|
|
pub fn put<K, V>(&mut self, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
@ -1302,10 +1339,11 @@ impl WriteBatch { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn put_cf<K, V>(&mut self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn put_cf<K, V>(&mut self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
@ -1322,10 +1360,11 @@ impl WriteBatch { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn merge<K, V>(&mut self, key: K, value: V) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn merge<K, V>(&mut self, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
@ -1341,10 +1380,11 @@ impl WriteBatch { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn merge_cf<K, V>(&mut self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error>
|
|
|
|
|
where K: AsRef<[u8]>,
|
|
|
|
|
V: AsRef<[u8]> { |
|
|
|
|
|
|
|
|
|
pub fn merge_cf<K, V>(&mut self, cf: ColumnFamily, key: K, value: V) -> Result<(), Error> |
|
|
|
|
where |
|
|
|
|
K: AsRef<[u8]>, |
|
|
|
|
V: AsRef<[u8]>, |
|
|
|
|
{ |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
let value = value.as_ref(); |
|
|
|
|
|
|
|
|
@ -1458,7 +1498,7 @@ impl ReadOptions { |
|
|
|
|
|
|
|
|
|
pub fn set_iterate_upper_bound<K: AsRef<[u8]>>(&mut self, key: K) { |
|
|
|
|
let key = key.as_ref(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
ffi::rocksdb_readoptions_set_iterate_upper_bound( |
|
|
|
|
self.inner, |
|
|
|
|