|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -use crate::{ffi, Options}; |
| 15 | +use crate::{db::MultiThreaded, ffi, Options}; |
16 | 16 |
|
17 | 17 | /// The name of the default column family. |
18 | 18 | /// |
@@ -47,4 +47,42 @@ pub struct ColumnFamily { |
47 | 47 | pub(crate) inner: *mut ffi::rocksdb_column_family_handle_t, |
48 | 48 | } |
49 | 49 |
|
| 50 | +/// A specialized opaque type used to represent a column family by the [`MultiThreaded`] |
| 51 | +/// mode. Clone (and Copy) is derived to behave like `&ColumnFamily` (this is used for |
| 52 | +/// single-threaded mode). `Clone`/`Copy` is safe because this lifetime is bound to DB like |
| 53 | +/// iterators/snapshots. On top of it, this is as cheap and small as `&ColumnFamily` because |
| 54 | +/// this only has a single pointer-wide field. |
| 55 | +#[derive(Clone, Copy)] |
| 56 | +pub struct BoundColumnFamily<'a> { |
| 57 | + pub(crate) inner: *mut ffi::rocksdb_column_family_handle_t, |
| 58 | + pub(crate) multi_threaded_cfs: std::marker::PhantomData<&'a MultiThreaded>, |
| 59 | +} |
| 60 | + |
| 61 | +/// Handy type alias to hide actual type difference to reference [`ColumnFamily`] |
| 62 | +/// depending on the `multi-threaded-cf` crate feature. |
| 63 | +#[cfg(not(feature = "multi-threaded-cf"))] |
| 64 | +pub type ColumnFamilyRef<'a> = &'a ColumnFamily; |
| 65 | + |
| 66 | +#[cfg(feature = "multi-threaded-cf")] |
| 67 | +pub type ColumnFamilyRef<'a> = BoundColumnFamily<'a>; |
| 68 | + |
| 69 | +/// Utility trait to accept both supported references to `ColumnFamily` |
| 70 | +/// (`&ColumnFamily` and `BoundColumnFamily`) |
| 71 | +pub trait AsColumnFamilyRef { |
| 72 | + fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t; |
| 73 | +} |
| 74 | + |
| 75 | +impl<'a> AsColumnFamilyRef for &'a ColumnFamily { |
| 76 | + fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t { |
| 77 | + self.inner |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'a> AsColumnFamilyRef for BoundColumnFamily<'a> { |
| 82 | + fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t { |
| 83 | + self.inner |
| 84 | + } |
| 85 | +} |
| 86 | + |
50 | 87 | unsafe impl Send for ColumnFamily {} |
| 88 | +unsafe impl<'a> Send for BoundColumnFamily<'a> {} |
0 commit comments