ckb_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
16use crate::ffi;
17use crate::ops::*;
18/// Implementation of bindings to RocksDB Checkpoint[1] API
19///
20/// [1]: https://github.com/facebook/rocksdb/wiki/Checkpoints
21use crate::{Error, DB};
22use std::ffi::CString;
23use std::marker::PhantomData;
24use std::path::Path;
25
26/// Undocumented parameter for `ffi::rocksdb_checkpoint_create` function. Zero by default.
27const LOG_SIZE_FOR_FLUSH: u64 = 0_u64;
28
29/// Database's checkpoint object.
30/// Used to create checkpoints of the specified DB from time to time.
31pub struct Checkpoint<'db> {
32    pub(crate) inner: *mut ffi::rocksdb_checkpoint_t,
33    pub(crate) _db: PhantomData<&'db ()>,
34}
35
36impl<'db> Checkpoint<'db> {
37    /// Creates new checkpoint object for specific DB.
38    ///
39    /// Does not actually produce checkpoints, call `.create_checkpoint()` method to produce
40    /// a DB checkpoint.
41    pub fn new(db: &'db DB) -> Result<Checkpoint<'db>, Error> {
42        db.create_checkpoint_object()
43    }
44
45    /// Creates new physical DB checkpoint in directory specified by `path`.
46    pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
47        let path = path.as_ref();
48        let cpath = match CString::new(path.to_string_lossy().as_bytes()) {
49            Ok(c) => c,
50            Err(_) => {
51                return Err(Error::new(
52                    "Failed to convert path to CString when creating DB checkpoint".to_owned(),
53                ));
54            }
55        };
56
57        unsafe {
58            ffi_try!(ffi::rocksdb_checkpoint_create(
59                self.inner,
60                cpath.as_ptr(),
61                LOG_SIZE_FOR_FLUSH,
62            ));
63
64            Ok(())
65        }
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}