|
|
@ -14,7 +14,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
use std::ffi::CStr; |
|
|
|
use std::ffi::CStr; |
|
|
|
use std::path::Path; |
|
|
|
use std::path::Path; |
|
|
|
use std::ptr::null_mut; |
|
|
|
use std::ptr::{null_mut, NonNull}; |
|
|
|
use std::slice; |
|
|
|
use std::slice; |
|
|
|
use std::sync::Arc; |
|
|
|
use std::sync::Arc; |
|
|
|
|
|
|
|
|
|
|
@ -35,18 +35,14 @@ use crate::{ |
|
|
|
ColumnFamilyDescriptor, Error, SnapshotWithThreadMode, |
|
|
|
ColumnFamilyDescriptor, Error, SnapshotWithThreadMode, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
fn new_cache(capacity: size_t) -> *mut ffi::rocksdb_cache_t { |
|
|
|
|
|
|
|
unsafe { ffi::rocksdb_cache_create_lru(capacity) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) struct CacheWrapper { |
|
|
|
pub(crate) struct CacheWrapper { |
|
|
|
pub(crate) inner: *mut ffi::rocksdb_cache_t, |
|
|
|
pub(crate) inner: NonNull<ffi::rocksdb_cache_t>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Drop for CacheWrapper { |
|
|
|
impl Drop for CacheWrapper { |
|
|
|
fn drop(&mut self) { |
|
|
|
fn drop(&mut self) { |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
ffi::rocksdb_cache_destroy(self.inner); |
|
|
|
ffi::rocksdb_cache_destroy(self.inner.as_ptr()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -55,30 +51,26 @@ impl Drop for CacheWrapper { |
|
|
|
pub struct Cache(pub(crate) Arc<CacheWrapper>); |
|
|
|
pub struct Cache(pub(crate) Arc<CacheWrapper>); |
|
|
|
|
|
|
|
|
|
|
|
impl Cache { |
|
|
|
impl Cache { |
|
|
|
/// Create a lru cache with capacity
|
|
|
|
/// Creates an LRU cache with capacity in bytes.
|
|
|
|
pub fn new_lru_cache(capacity: size_t) -> Result<Cache, Error> { |
|
|
|
pub fn new_lru_cache(capacity: size_t) -> Cache { |
|
|
|
let cache = new_cache(capacity); |
|
|
|
let inner = NonNull::new(unsafe { ffi::rocksdb_cache_create_lru(capacity) }).unwrap(); |
|
|
|
if cache.is_null() { |
|
|
|
Cache(Arc::new(CacheWrapper { inner })) |
|
|
|
Err(Error::new("Could not create Cache".to_owned())) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Ok(Cache(Arc::new(CacheWrapper { inner: cache }))) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns the Cache memory usage
|
|
|
|
/// Returns the cache memory usage in bytes.
|
|
|
|
pub fn get_usage(&self) -> usize { |
|
|
|
pub fn get_usage(&self) -> usize { |
|
|
|
unsafe { ffi::rocksdb_cache_get_usage(self.0.inner) } |
|
|
|
unsafe { ffi::rocksdb_cache_get_usage(self.0.inner.as_ptr()) } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns pinned memory usage
|
|
|
|
/// Returns the pinned memory usage in bytes.
|
|
|
|
pub fn get_pinned_usage(&self) -> usize { |
|
|
|
pub fn get_pinned_usage(&self) -> usize { |
|
|
|
unsafe { ffi::rocksdb_cache_get_pinned_usage(self.0.inner) } |
|
|
|
unsafe { ffi::rocksdb_cache_get_pinned_usage(self.0.inner.as_ptr()) } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Sets cache capacity
|
|
|
|
/// Sets cache capacity in bytes.
|
|
|
|
pub fn set_capacity(&mut self, capacity: size_t) { |
|
|
|
pub fn set_capacity(&mut self, capacity: size_t) { |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
ffi::rocksdb_cache_set_capacity(self.0.inner, capacity); |
|
|
|
ffi::rocksdb_cache_set_capacity(self.0.inner.as_ptr(), capacity); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -376,21 +368,6 @@ impl BlockBasedOptions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// When provided: use the specified cache for blocks.
|
|
|
|
|
|
|
|
/// Otherwise rocksdb will automatically create and use an 8MB internal cache.
|
|
|
|
|
|
|
|
#[deprecated(
|
|
|
|
|
|
|
|
since = "0.15.0", |
|
|
|
|
|
|
|
note = "This function will be removed in next release. Use set_block_cache instead" |
|
|
|
|
|
|
|
)] |
|
|
|
|
|
|
|
pub fn set_lru_cache(&mut self, size: size_t) { |
|
|
|
|
|
|
|
let cache = new_cache(size); |
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
// Since cache is wrapped in shared_ptr, we don't need to
|
|
|
|
|
|
|
|
// call rocksdb_cache_destroy explicitly.
|
|
|
|
|
|
|
|
ffi::rocksdb_block_based_options_set_block_cache(self.inner, cache); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets global cache for blocks (user data is stored in a set of blocks, and
|
|
|
|
/// Sets global cache for blocks (user data is stored in a set of blocks, and
|
|
|
|
/// a block is the unit of reading from disk). Cache must outlive DB instance which uses it.
|
|
|
|
/// a block is the unit of reading from disk). Cache must outlive DB instance which uses it.
|
|
|
|
///
|
|
|
|
///
|
|
|
@ -398,7 +375,7 @@ impl BlockBasedOptions { |
|
|
|
/// By default, rocksdb will automatically create and use an 8MB internal cache.
|
|
|
|
/// By default, rocksdb will automatically create and use an 8MB internal cache.
|
|
|
|
pub fn set_block_cache(&mut self, cache: &Cache) { |
|
|
|
pub fn set_block_cache(&mut self, cache: &Cache) { |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
ffi::rocksdb_block_based_options_set_block_cache(self.inner, cache.0.inner); |
|
|
|
ffi::rocksdb_block_based_options_set_block_cache(self.inner, cache.0.inner.as_ptr()); |
|
|
|
} |
|
|
|
} |
|
|
|
self.outlive.block_cache = Some(cache.clone()); |
|
|
|
self.outlive.block_cache = Some(cache.clone()); |
|
|
|
} |
|
|
|
} |
|
|
@ -750,7 +727,7 @@ impl Options { |
|
|
|
path.as_ptr(), |
|
|
|
path.as_ptr(), |
|
|
|
env.0.inner, |
|
|
|
env.0.inner, |
|
|
|
ignore_unknown_options, |
|
|
|
ignore_unknown_options, |
|
|
|
cache.0.inner, |
|
|
|
cache.0.inner.as_ptr(), |
|
|
|
&mut db_options, |
|
|
|
&mut db_options, |
|
|
|
&mut num_column_families, |
|
|
|
&mut num_column_families, |
|
|
|
&mut column_family_names, |
|
|
|
&mut column_family_names, |
|
|
@ -2828,7 +2805,7 @@ impl Options { |
|
|
|
/// Not supported in ROCKSDB_LITE mode!
|
|
|
|
/// Not supported in ROCKSDB_LITE mode!
|
|
|
|
pub fn set_row_cache(&mut self, cache: &Cache) { |
|
|
|
pub fn set_row_cache(&mut self, cache: &Cache) { |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
ffi::rocksdb_options_set_row_cache(self.inner, cache.0.inner); |
|
|
|
ffi::rocksdb_options_set_row_cache(self.inner, cache.0.inner.as_ptr()); |
|
|
|
} |
|
|
|
} |
|
|
|
self.outlive.row_cache = Some(cache.clone()); |
|
|
|
self.outlive.row_cache = Some(cache.clone()); |
|
|
|
} |
|
|
|
} |
|
|
|