ckb_rocksdb/
snapshot.rs

1// Copyright 2019 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//
15
16use crate::{
17    handle::ConstHandle, ops::*, ColumnFamily, DBRawIterator, DBVector, Error, ReadOptions, DB,
18};
19
20/// A consistent view of the database at the point of creation.
21///
22/// ```
23/// use ckb_rocksdb::{prelude::*, IteratorMode};
24/// # use ckb_rocksdb::TemporaryDBPath;
25///
26/// let path = "_path_for_rocksdb_storage3";
27/// # let path = TemporaryDBPath::new();
28/// # {
29///
30///     let db = DB::open_default(&path).unwrap();
31///     let snapshot = db.snapshot(); // Creates a longer-term snapshot of the DB, but closed when goes out of scope
32///     let mut iter = snapshot.iterator(IteratorMode::Start); // Make as many iterators as you'd like from one snapshot
33
34/// # }
35/// ```
36///
37pub struct Snapshot<'a> {
38    pub(crate) db: &'a DB,
39    pub(crate) inner: *const ffi::rocksdb_snapshot_t,
40}
41
42impl<'a> ConstHandle<ffi::rocksdb_snapshot_t> for Snapshot<'a> {
43    fn const_handle(&self) -> *const ffi::rocksdb_snapshot_t {
44        self.inner
45    }
46}
47
48impl<'a> Read for Snapshot<'a> {}
49
50impl<'a> GetCF<ReadOptions> for Snapshot<'a> {
51    fn get_cf_full<K: AsRef<[u8]>>(
52        &self,
53        cf: Option<&ColumnFamily>,
54        key: K,
55        readopts: Option<&ReadOptions>,
56    ) -> Result<Option<DBVector>, Error> {
57        let mut ro = readopts.cloned().unwrap_or_default();
58        ro.set_snapshot(self);
59
60        self.db.get_cf_full(cf, key, Some(&ro))
61    }
62}
63
64impl<'a> MultiGet<ReadOptions> for Snapshot<'a> {
65    fn multi_get_full<K, I>(
66        &self,
67        keys: I,
68        readopts: Option<&ReadOptions>,
69    ) -> Vec<Result<Option<DBVector>, Error>>
70    where
71        K: AsRef<[u8]>,
72        I: IntoIterator<Item = K>,
73    {
74        let mut ro = readopts.cloned().unwrap_or_default();
75        ro.set_snapshot(self);
76
77        self.db.multi_get_full(keys, Some(&ro))
78    }
79}
80
81impl<'a> MultiGetCF<ReadOptions> for Snapshot<'a> {
82    fn multi_get_cf_full<'m, K, I>(
83        &self,
84        keys: I,
85        readopts: Option<&ReadOptions>,
86    ) -> Vec<Result<Option<DBVector>, Error>>
87    where
88        K: AsRef<[u8]>,
89        I: IntoIterator<Item = (&'m ColumnFamily, K)>,
90    {
91        let mut ro = readopts.cloned().unwrap_or_default();
92        ro.set_snapshot(self);
93
94        self.db.multi_get_cf_full(keys, Some(&ro))
95    }
96}
97
98impl<'a> Drop for Snapshot<'a> {
99    fn drop(&mut self) {
100        unsafe {
101            ffi::rocksdb_release_snapshot(self.db.inner, self.inner);
102        }
103    }
104}
105
106impl Iterate for Snapshot<'_> {
107    fn get_raw_iter<'a: 'b, 'b>(&'a self, readopts: &ReadOptions) -> DBRawIterator<'b> {
108        let mut ro = readopts.to_owned();
109        ro.set_snapshot(self);
110        self.db.get_raw_iter(&ro)
111    }
112}
113
114impl IterateCF for Snapshot<'_> {
115    fn get_raw_iter_cf<'a: 'b, 'b>(
116        &'a self,
117        cf_handle: &ColumnFamily,
118        readopts: &ReadOptions,
119    ) -> Result<DBRawIterator<'b>, Error> {
120        let mut ro = readopts.to_owned();
121        ro.set_snapshot(self);
122        self.db.get_raw_iter_cf(cf_handle, &ro)
123    }
124}