electrs_rocksdb/
column_family.rs

1// Copyright 2020 Tyler Neely
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{db::MultiThreaded, ffi, Options};
16
17use std::sync::Arc;
18
19/// The name of the default column family.
20///
21/// The column family with this name is created implicitly whenever column
22/// families are used.
23pub const DEFAULT_COLUMN_FAMILY_NAME: &str = "default";
24
25/// A descriptor for a RocksDB column family.
26///
27/// A description of the column family, containing the name and `Options`.
28pub struct ColumnFamilyDescriptor {
29    pub(crate) name: String,
30    pub(crate) options: Options,
31}
32
33impl ColumnFamilyDescriptor {
34    // Create a new column family descriptor with the specified name and options.
35    pub fn new<S>(name: S, options: Options) -> Self
36    where
37        S: Into<String>,
38    {
39        Self {
40            name: name.into(),
41            options,
42        }
43    }
44}
45
46/// An opaque type used to represent a column family. Returned from some functions, and used
47/// in others
48pub struct ColumnFamily {
49    pub(crate) inner: *mut ffi::rocksdb_column_family_handle_t,
50}
51
52/// A specialized opaque type used to represent a column family by the [`MultiThreaded`]
53/// mode. Clone (and Copy) is derived to behave like `&ColumnFamily` (this is used for
54/// single-threaded mode). `Clone`/`Copy` is safe because this lifetime is bound to DB like
55/// iterators/snapshots. On top of it, this is as cheap and small as `&ColumnFamily` because
56/// this only has a single pointer-wide field.
57pub struct BoundColumnFamily<'a> {
58    pub(crate) inner: *mut ffi::rocksdb_column_family_handle_t,
59    pub(crate) multi_threaded_cfs: std::marker::PhantomData<&'a MultiThreaded>,
60}
61
62// internal struct which isn't exposed to public api.
63// but its memory will be exposed after transmute()-ing to BoundColumnFamily.
64// ColumnFamily's lifetime should be bound to DB. But, db holds cfs and cfs can't easily
65// self-reference DB as its lifetime due to rust's type system
66pub(crate) struct UnboundColumnFamily {
67    pub(crate) inner: *mut ffi::rocksdb_column_family_handle_t,
68}
69
70impl UnboundColumnFamily {
71    pub(crate) fn bound_column_family<'a>(self: Arc<Self>) -> Arc<BoundColumnFamily<'a>> {
72        // SAFETY: the new BoundColumnFamily here just adding lifetime,
73        // so that column family handle won't outlive db.
74        unsafe { Arc::from_raw(Arc::into_raw(self).cast()) }
75    }
76}
77
78fn destroy_handle(handle: *mut ffi::rocksdb_column_family_handle_t) {
79    // SAFETY: This should be called only from various Drop::drop(), strictly keeping a 1-to-1
80    // ownership to avoid double invocation to the rocksdb function with same handle.
81    unsafe {
82        ffi::rocksdb_column_family_handle_destroy(handle);
83    }
84}
85
86impl Drop for ColumnFamily {
87    fn drop(&mut self) {
88        destroy_handle(self.inner);
89    }
90}
91
92// these behaviors must be identical between BoundColumnFamily and UnboundColumnFamily
93// due to the unsafe transmute() in bound_column_family()!
94impl<'a> Drop for BoundColumnFamily<'a> {
95    fn drop(&mut self) {
96        destroy_handle(self.inner);
97    }
98}
99
100impl Drop for UnboundColumnFamily {
101    fn drop(&mut self) {
102        destroy_handle(self.inner);
103    }
104}
105
106/// Handy type alias to hide actual type difference to reference [`ColumnFamily`]
107/// depending on the `multi-threaded-cf` crate feature.
108#[cfg(not(feature = "multi-threaded-cf"))]
109pub type ColumnFamilyRef<'a> = &'a ColumnFamily;
110
111#[cfg(feature = "multi-threaded-cf")]
112pub type ColumnFamilyRef<'a> = Arc<BoundColumnFamily<'a>>;
113
114/// Utility trait to accept both supported references to `ColumnFamily`
115/// (`&ColumnFamily` and `BoundColumnFamily`)
116pub trait AsColumnFamilyRef {
117    fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t;
118}
119
120impl AsColumnFamilyRef for ColumnFamily {
121    fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t {
122        self.inner
123    }
124}
125
126impl<'a> AsColumnFamilyRef for &'a ColumnFamily {
127    fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t {
128        self.inner
129    }
130}
131
132// Only implement for Arc-ed BoundColumnFamily as this tightly coupled and
133// implementation detail, considering use of std::mem::transmute. BoundColumnFamily
134// isn't expected to be used as naked.
135// Also, ColumnFamilyRef might not be Arc<BoundColumnFamily<'a>> depending crate
136// feature flags so, we can't use the type alias here.
137impl<'a> AsColumnFamilyRef for Arc<BoundColumnFamily<'a>> {
138    fn inner(&self) -> *mut ffi::rocksdb_column_family_handle_t {
139        self.inner
140    }
141}
142
143unsafe impl Send for ColumnFamily {}
144unsafe impl Send for UnboundColumnFamily {}
145unsafe impl Sync for UnboundColumnFamily {}
146unsafe impl<'a> Send for BoundColumnFamily<'a> {}