use crate::{
handle::ConstHandle, ops::*, ColumnFamily, DBRawIterator, DBVector, Error, ReadOptions, DB,
};
pub struct Snapshot<'a> {
pub(crate) db: &'a DB,
pub(crate) inner: *const ffi::rocksdb_snapshot_t,
}
impl<'a> ConstHandle<ffi::rocksdb_snapshot_t> for Snapshot<'a> {
fn const_handle(&self) -> *const ffi::rocksdb_snapshot_t {
self.inner
}
}
impl<'a> Read for Snapshot<'a> {}
impl<'a> GetCF<ReadOptions> for Snapshot<'a> {
fn get_cf_full<K: AsRef<[u8]>>(
&self,
cf: Option<&ColumnFamily>,
key: K,
readopts: Option<&ReadOptions>,
) -> Result<Option<DBVector>, Error> {
let mut ro = readopts.cloned().unwrap_or_default();
ro.set_snapshot(self);
self.db.get_cf_full(cf, key, Some(&ro))
}
}
impl<'a> MultiGet<ReadOptions> for Snapshot<'a> {
fn multi_get_full<K, I>(
&self,
keys: I,
readopts: Option<&ReadOptions>,
) -> Vec<Result<Option<DBVector>, Error>>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = K>,
{
let mut ro = readopts.cloned().unwrap_or_default();
ro.set_snapshot(self);
self.db.multi_get_full(keys, Some(&ro))
}
}
impl<'a> MultiGetCF<ReadOptions> for Snapshot<'a> {
fn multi_get_cf_full<'m, K, I>(
&self,
keys: I,
readopts: Option<&ReadOptions>,
) -> Vec<Result<Option<DBVector>, Error>>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = (&'m ColumnFamily, K)>,
{
let mut ro = readopts.cloned().unwrap_or_default();
ro.set_snapshot(self);
self.db.multi_get_cf_full(keys, Some(&ro))
}
}
impl<'a> Drop for Snapshot<'a> {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_release_snapshot(self.db.inner, self.inner);
}
}
}
impl Iterate for Snapshot<'_> {
fn get_raw_iter<'a: 'b, 'b>(&'a self, readopts: &ReadOptions) -> DBRawIterator<'b> {
let mut ro = readopts.to_owned();
ro.set_snapshot(self);
self.db.get_raw_iter(&ro)
}
}
impl IterateCF for Snapshot<'_> {
fn get_raw_iter_cf<'a: 'b, 'b>(
&'a self,
cf_handle: &ColumnFamily,
readopts: &ReadOptions,
) -> Result<DBRawIterator<'b>, Error> {
let mut ro = readopts.to_owned();
ro.set_snapshot(self);
self.db.get_raw_iter_cf(cf_handle, &ro)
}
}