Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep Cache and Env alive with Rc #497

Merged
merged 6 commits into from Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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