1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::ffi;
use crate::ffi_util::to_cstring;
use crate::ops::GetColumnFamilys;
use crate::{
    db_iterator::DBRawIterator,
    db_options::{OptionsMustOutliveDB, ReadOptions},
    handle::Handle,
    open_raw::{OpenRaw, OpenRawFFI},
    ops, ColumnFamily, Error, Options,
};
use std::collections::BTreeMap;
use std::fmt;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};

pub struct DBWithTTL {
    pub(crate) inner: *mut ffi::rocksdb_t,
    cfs: BTreeMap<String, ColumnFamily>,
    path: PathBuf,
    _outlive: Vec<OptionsMustOutliveDB>,
}

impl DBWithTTL {
    pub fn path(&self) -> &Path {
        self.path.as_path()
    }

    pub fn create_cf_with_ttl<N: AsRef<str>>(
        &mut self,
        name: N,
        opts: &Options,
        ttl: i32,
    ) -> Result<(), Error> {
        let cname = to_cstring(
            name.as_ref(),
            "Failed to convert path to CString when opening rocksdb",
        )?;
        unsafe {
            let cf_handle = ffi_try!(ffi::rocksdb_create_column_family_with_ttl(
                self.handle(),
                opts.inner,
                cname.as_ptr(),
                ttl as libc::c_int,
            ));

            self.get_mut_cfs()
                .insert(name.as_ref().to_string(), ColumnFamily::new(cf_handle));
        };
        Ok(())
    }
}

impl Default for TTLOpenDescriptor {
    fn default() -> Self {
        TTLOpenDescriptor {
            ttls: TTLs::Default(-1),
        }
    }
}

pub enum TTLs {
    Default(i32),
    Columns(Vec<i32>),
}

// TTL is accepted in seconds
// If TTL is non positive or not provided, the behaviour is TTL = infinity
// (int32_t)Timestamp(creation) is suffixed to values in Put internally
// Expired TTL values are deleted in compaction only:(Timestamp+ttl<time_now)
// Get/Iterator may return expired entries(compaction not run on them yet)
// Different TTL may be used during different Opens
// Example: Open1 at t=0 with ttl=4 and insert k1,k2, close at t=2. Open2 at t=3 with ttl=5. Now k1,k2 should be deleted at t>=5
// read_only=true opens in the usual read-only mode. Compactions will not be triggered(neither manual nor automatic), so no expired entries removed
pub struct TTLOpenDescriptor {
    ttls: TTLs,
}

impl TTLOpenDescriptor {
    pub fn by_columns(ttls: Vec<i32>) -> Self {
        TTLOpenDescriptor {
            ttls: TTLs::Columns(ttls),
        }
    }

    pub fn by_default(ttl: i32) -> Self {
        TTLOpenDescriptor {
            ttls: TTLs::Default(ttl),
        }
    }
}

impl ops::Open for DBWithTTL {}
impl ops::OpenCF for DBWithTTL {}

impl OpenRaw for DBWithTTL {
    type Pointer = ffi::rocksdb_t;
    type Descriptor = TTLOpenDescriptor;

    fn open_ffi(input: OpenRawFFI<'_, Self::Descriptor>) -> Result<*mut Self::Pointer, Error> {
        let pointer = unsafe {
            if input.num_column_families <= 0 {
                let ttl = match input.open_descriptor.ttls {
                    TTLs::Default(ttl) => ttl as libc::c_int,
                    TTLs::Columns(_) => {
                        return Err(Error::new(
                            "Ttls size has to be the same as number of column families".to_owned(),
                        ));
                    }
                };

                ffi_try!(ffi::rocksdb_open_with_ttl(input.options, input.path, ttl,))
            } else {
                let ttls = match input.open_descriptor.ttls {
                    TTLs::Default(ttl) => (0..input.num_column_families)
                        .map(|_| ttl as libc::c_int)
                        .collect::<Vec<_>>(),
                    TTLs::Columns(ref ttls) => {
                        let ttls: Vec<_> = ttls.iter().map(|t| *t as libc::c_int).collect();

                        let is_ttls_match = if input.num_column_families <= 0 {
                            ttls.len() as i32 == 1
                        } else {
                            ttls.len() as i32 == input.num_column_families
                        };

                        if !is_ttls_match {
                            return Err(Error::new(
                                "Ttls size has to be the same as number of column families"
                                    .to_owned(),
                            ));
                        }

                        ttls
                    }
                };

                ffi_try!(ffi::rocksdb_open_column_families_with_ttl(
                    input.options,
                    input.path,
                    input.num_column_families,
                    input.column_family_names,
                    input.column_family_options,
                    input.column_family_handles,
                    ttls.as_ptr(),
                ))
            }
        };

        Ok(pointer)
    }

    fn build<I>(
        path: PathBuf,
        _open_descriptor: Self::Descriptor,
        pointer: *mut Self::Pointer,
        column_families: I,
        outlive: Vec<OptionsMustOutliveDB>,
    ) -> Result<Self, Error>
    where
        I: IntoIterator<Item = (String, *mut ffi::rocksdb_column_family_handle_t)>,
    {
        let cfs: BTreeMap<_, _> = column_families
            .into_iter()
            .map(|(k, h)| (k, ColumnFamily::new(h)))
            .collect();
        Ok(DBWithTTL {
            inner: pointer,
            cfs,
            path,
            _outlive: outlive,
        })
    }
}

impl Handle<ffi::rocksdb_t> for DBWithTTL {
    fn handle(&self) -> *mut ffi::rocksdb_t {
        self.inner
    }
}

impl ops::Iterate for DBWithTTL {
    fn get_raw_iter<'a: 'b, 'b>(&'a self, readopts: &ReadOptions) -> DBRawIterator<'b> {
        unsafe {
            DBRawIterator {
                inner: ffi::rocksdb_create_iterator(self.inner, readopts.handle()),
                db: PhantomData,
            }
        }
    }
}

impl ops::IterateCF for DBWithTTL {
    fn get_raw_iter_cf<'a: 'b, 'b>(
        &'a self,
        cf_handle: &ColumnFamily,
        readopts: &ReadOptions,
    ) -> Result<DBRawIterator<'b>, Error> {
        unsafe {
            Ok(DBRawIterator {
                inner: ffi::rocksdb_create_iterator_cf(
                    self.inner,
                    readopts.handle(),
                    cf_handle.inner,
                ),
                db: PhantomData,
            })
        }
    }
}

impl ops::GetColumnFamilys for DBWithTTL {
    fn get_cfs(&self) -> &BTreeMap<String, ColumnFamily> {
        &self.cfs
    }
    fn get_mut_cfs(&mut self) -> &mut BTreeMap<String, ColumnFamily> {
        &mut self.cfs
    }
}

impl ops::Read for DBWithTTL {}
impl ops::Write for DBWithTTL {}

unsafe impl Send for DBWithTTL {}
unsafe impl Sync for DBWithTTL {}

impl Drop for DBWithTTL {
    fn drop(&mut self) {
        unsafe {
            for cf in self.cfs.values() {
                ffi::rocksdb_column_family_handle_destroy(cf.inner);
            }
            ffi::rocksdb_close(self.inner);
        }
    }
}

impl fmt::Debug for DBWithTTL {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Read-only RocksDB {{ path: {:?} }}", self.path())
    }
}