rocksdb/
checkpoint.rs

1// Copyright 2018 Eugene P.
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//
15
16//! Implementation of bindings to RocksDB Checkpoint[1] API
17//!
18//! [1]: https://github.com/facebook/rocksdb/wiki/Checkpoints
19
20use crate::{db::DBInner, ffi, ffi_util::to_cpath, DBCommon, Error, ThreadMode};
21use std::{marker::PhantomData, path::Path};
22
23/// Undocumented parameter for `ffi::rocksdb_checkpoint_create` function. Zero by default.
24const LOG_SIZE_FOR_FLUSH: u64 = 0_u64;
25
26/// Database's checkpoint object.
27/// Used to create checkpoints of the specified DB from time to time.
28pub struct Checkpoint<'db> {
29    inner: *mut ffi::rocksdb_checkpoint_t,
30    _db: PhantomData<&'db ()>,
31}
32
33impl<'db> Checkpoint<'db> {
34    /// Creates new checkpoint object for specific DB.
35    ///
36    /// Does not actually produce checkpoints, call `.create_checkpoint()` method to produce
37    /// a DB checkpoint.
38    pub fn new<T: ThreadMode, I: DBInner>(db: &'db DBCommon<T, I>) -> Result<Self, Error> {
39        let checkpoint: *mut ffi::rocksdb_checkpoint_t;
40
41        unsafe {
42            checkpoint = ffi_try!(ffi::rocksdb_checkpoint_object_create(db.inner.inner()));
43        }
44
45        if checkpoint.is_null() {
46            return Err(Error::new("Could not create checkpoint object.".to_owned()));
47        }
48
49        Ok(Self {
50            inner: checkpoint,
51            _db: PhantomData,
52        })
53    }
54
55    /// Creates new physical DB checkpoint in directory specified by `path`.
56    pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
57        let cpath = to_cpath(path)?;
58        unsafe {
59            ffi_try!(ffi::rocksdb_checkpoint_create(
60                self.inner,
61                cpath.as_ptr(),
62                LOG_SIZE_FOR_FLUSH,
63            ));
64        }
65        Ok(())
66    }
67}
68
69impl<'db> Drop for Checkpoint<'db> {
70    fn drop(&mut self) {
71        unsafe {
72            ffi::rocksdb_checkpoint_object_destroy(self.inner);
73        }
74    }
75}