Skip to content

Commit

Permalink
Keep Cache and Env alive with Rc (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
acrrd committed Apr 18, 2021
1 parent 0b700fe commit b7af394
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 38 deletions.
15 changes: 10 additions & 5 deletions src/checkpoint.rs
Expand Up @@ -19,23 +19,25 @@

use crate::{ffi, Error, DB};
use std::ffi::CString;
use std::marker::PhantomData;
use std::path::Path;

/// Undocumented parameter for `ffi::rocksdb_checkpoint_create` function. Zero by default.
const LOG_SIZE_FOR_FLUSH: u64 = 0_u64;

/// Database's checkpoint object.
/// Used to create checkpoints of the specified DB from time to time.
pub struct Checkpoint {
pub struct Checkpoint<'db> {
inner: *mut ffi::rocksdb_checkpoint_t,
_db: PhantomData<&'db ()>,
}

impl Checkpoint {
impl<'db> Checkpoint<'db> {
/// Creates new checkpoint object for specific DB.
///
/// Does not actually produce checkpoints, call `.create_checkpoint()` method to produce
/// a DB checkpoint.
pub fn new(db: &DB) -> Result<Checkpoint, Error> {
pub fn new(db: &'db DB) -> Result<Checkpoint<'db>, Error> {
let checkpoint: *mut ffi::rocksdb_checkpoint_t;

unsafe { checkpoint = ffi_try!(ffi::rocksdb_checkpoint_object_create(db.inner)) };
Expand All @@ -44,7 +46,10 @@ impl Checkpoint {
return Err(Error::new("Could not create checkpoint object.".to_owned()));
}

Ok(Checkpoint { inner: checkpoint })
Ok(Checkpoint {
inner: checkpoint,
_db: PhantomData,
})
}

/// Creates new physical DB checkpoint in directory specified by `path`.
Expand All @@ -70,7 +75,7 @@ impl Checkpoint {
}
}

impl Drop for Checkpoint {
impl<'db> Drop for Checkpoint<'db> {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_checkpoint_object_destroy(self.inner);
Expand Down
8 changes: 8 additions & 0 deletions src/db.rs
Expand Up @@ -16,6 +16,7 @@
use crate::{
column_family::AsColumnFamilyRef,
column_family::BoundColumnFamily,
db_options::OptionsMustOutliveDB,
ffi,
ffi_util::{from_cstr, opt_bytes_to_ptr, raw_data, to_cpath},
ColumnFamily, ColumnFamilyDescriptor, CompactOptions, DBIteratorWithThreadMode,
Expand All @@ -29,6 +30,7 @@ use std::collections::BTreeMap;
use std::ffi::{CStr, CString};
use std::fmt;
use std::fs;
use std::iter;
use std::marker::PhantomData;
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -102,6 +104,7 @@ pub struct DBWithThreadMode<T: ThreadMode> {
pub(crate) inner: *mut ffi::rocksdb_t,
cfs: T, // Column families are held differently depending on thread mode
path: PathBuf,
_outlive: Vec<OptionsMustOutliveDB>,
}

/// Minimal set of DB-related methods, intended to be generic over
Expand Down Expand Up @@ -238,6 +241,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
inner: db,
cfs: T::new(BTreeMap::new()),
path: path.as_ref().to_path_buf(),
_outlive: vec![opts.outlive.clone()],
})
}

Expand Down Expand Up @@ -330,6 +334,9 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
I: IntoIterator<Item = ColumnFamilyDescriptor>,
{
let cfs: Vec<_> = cfs.into_iter().collect();
let outlive = iter::once(opts.outlive.clone())
.chain(cfs.iter().map(|cf| cf.options.outlive.clone()))
.collect();

let cpath = to_cpath(&path)?;

Expand Down Expand Up @@ -401,6 +408,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
inner: db,
path: path.as_ref().to_path_buf(),
cfs: T::new(cf_map),
_outlive: outlive,
})
}

Expand Down

0 comments on commit b7af394

Please sign in to comment.