1use crate::{
17 handle::ConstHandle, ops::*, ColumnFamily, DBRawIterator, DBVector, Error, ReadOptions, DB,
18};
19
20pub 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}