Adding lifetime to ColumnFamily...

master
Jordan Terrell 6 years ago
parent e2949d094c
commit 248b40b465
  1. 28
      src/db.rs
  2. 8
      src/lib.rs

@ -22,6 +22,7 @@ use std::collections::BTreeMap;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::fmt; use std::fmt;
use std::fs; use std::fs;
use std::marker::PhantomData;
use std::ops::Deref; use std::ops::Deref;
use std::path::Path; use std::path::Path;
use std::ptr; use std::ptr;
@ -699,7 +700,7 @@ impl DB {
for (n, h) in cfs_v.iter().zip(cfhandles) { for (n, h) in cfs_v.iter().zip(cfhandles) {
cf_map.write() cf_map.write()
.map_err(|e| Error::new(e.to_string()))? .map_err(|e| Error::new(e.to_string()))?
.insert(n.name.clone(), ColumnFamily { inner: h }); .insert(n.name.clone(), h);
} }
} }
@ -866,15 +867,19 @@ impl DB {
} }
}; };
let cf = unsafe { let cf = unsafe {
let cf_handler = ffi_try!(ffi::rocksdb_create_column_family( let cf_handle = ffi_try!(ffi::rocksdb_create_column_family(
self.inner, self.inner,
opts.inner, opts.inner,
cname.as_ptr(), cname.as_ptr(),
)); ));
let cf = ColumnFamily { inner: cf_handler };
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); .insert(name.to_string(), cf_handle);
cf
ColumnFamily {
inner: cf_handle,
db: PhantomData,
}
}; };
Ok(cf) Ok(cf)
} }
@ -883,7 +888,7 @@ impl DB {
if let Some(cf) = self.cfs.write().map_err(|e| Error::new(e.to_string()))? if let Some(cf) = self.cfs.write().map_err(|e| Error::new(e.to_string()))?
.remove(name) { .remove(name) {
unsafe { unsafe {
ffi_try!(ffi::rocksdb_drop_column_family(self.inner, cf.inner,)); ffi_try!(ffi::rocksdb_drop_column_family(self.inner, cf,));
} }
Ok(()) Ok(())
} else { } else {
@ -895,7 +900,14 @@ impl DB {
/// Return the underlying column family handle. /// Return the underlying column family handle.
pub fn cf_handle(&self, name: &str) -> Option<ColumnFamily> { pub fn cf_handle(&self, name: &str) -> Option<ColumnFamily> {
self.cfs.read().ok()?.get(name).cloned() self.cfs
.read()
.ok()?
.get(name)
.map(|h| ColumnFamily {
inner: *h,
db: PhantomData
})
} }
pub fn iterator(&self, mode: IteratorMode) -> DBIterator { pub fn iterator(&self, mode: IteratorMode) -> DBIterator {
@ -1269,7 +1281,7 @@ impl Drop for DB {
unsafe { unsafe {
if let Ok(cfs) = self.cfs.read() { if let Ok(cfs) = self.cfs.read() {
for cf in cfs.values() { for cf in cfs.values() {
ffi::rocksdb_column_family_handle_destroy(cf.inner); ffi::rocksdb_column_family_handle_destroy(*cf);
} }
} }
ffi::rocksdb_close(self.inner); ffi::rocksdb_close(self.inner);

@ -73,6 +73,7 @@ pub use merge_operator::MergeOperands;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::marker::PhantomData;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -81,7 +82,7 @@ use std::sync::{Arc, RwLock};
/// See crate level documentation for a simple usage example. /// See crate level documentation for a simple usage example.
pub struct DB { pub struct DB {
inner: *mut ffi::rocksdb_t, inner: *mut ffi::rocksdb_t,
cfs: Arc<RwLock<BTreeMap<String, ColumnFamily>>>, cfs: Arc<RwLock<BTreeMap<String, *mut ffi::rocksdb_column_family_handle_t>>>,
path: PathBuf, path: PathBuf,
} }
@ -233,8 +234,9 @@ pub struct WriteOptions {
/// An opaque type used to represent a column family. Returned from some functions, and used /// An opaque type used to represent a column family. Returned from some functions, and used
/// in others /// in others
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct ColumnFamily { pub struct ColumnFamily<'a> {
inner: *mut ffi::rocksdb_column_family_handle_t, inner: *mut ffi::rocksdb_column_family_handle_t,
db: PhantomData<&'a DB>,
} }
unsafe impl Send for ColumnFamily {} unsafe impl<'a> Send for ColumnFamily<'a> {}

Loading…
Cancel
Save