pub struct Snapshot { /* private fields */ }
Expand description
A snapshot captures a read-only point-in-time view of the tree at the time the snapshot was created
As long as the snapshot is open, old versions of objects will not be evicted as to keep the snapshot consistent. Thus, snapshots should only be kept around for as little as possible.
Snapshots do not persist across restarts.
Implementations§
source§impl Snapshot
impl Snapshot
sourcepub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<KvPair>> + 'static
pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<KvPair>> + 'static
Returns an iterator that scans through the entire snapshot.
Avoid using this function, or limit it as otherwise it may scan a lot of items.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("a", "abc", 0);
tree.insert("f", "abc", 1);
let snapshot = tree.snapshot(2);
tree.insert("g", "abc", 2);
assert_eq!(2, snapshot.iter().count());
sourcepub fn keys(&self) -> impl DoubleEndedIterator<Item = Result<UserKey>> + 'static
pub fn keys(&self) -> impl DoubleEndedIterator<Item = Result<UserKey>> + 'static
Returns an iterator that scans through the entire snapshot, returning keys only.
Avoid using this function, or limit it as otherwise it may scan a lot of items.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("a", "abc", 0);
tree.insert("f", "abc", 1);
let snapshot = tree.snapshot(2);
tree.insert("g", "abc", 2);
assert_eq!(2, snapshot.keys().count());
sourcepub fn values(
&self,
) -> impl DoubleEndedIterator<Item = Result<UserValue>> + 'static
pub fn values( &self, ) -> impl DoubleEndedIterator<Item = Result<UserValue>> + 'static
Returns an iterator that scans through the entire snapshot, returning values only.
Avoid using this function, or limit it as otherwise it may scan a lot of items.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("a", "abc", 0);
tree.insert("f", "abc", 1);
let snapshot = tree.snapshot(2);
tree.insert("g", "abc", 2);
assert_eq!(2, snapshot.values().count());
sourcepub fn range<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
range: R,
) -> impl DoubleEndedIterator<Item = Result<KvPair>> + 'static
pub fn range<K: AsRef<[u8]>, R: RangeBounds<K>>( &self, range: R, ) -> impl DoubleEndedIterator<Item = Result<KvPair>> + 'static
Returns an iterator over a range of items in the snapshot.
Avoid using full or unbounded ranges as they may scan a lot of items (unless limited).
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("a", "abc", 0);
let snapshot = tree.snapshot(1);
tree.insert("f", "abc", 1);
tree.insert("g", "abc", 2);
assert_eq!(1, snapshot.range("a"..="f").count());
sourcepub fn prefix<K: AsRef<[u8]>>(
&self,
prefix: K,
) -> impl DoubleEndedIterator<Item = Result<KvPair>> + 'static
pub fn prefix<K: AsRef<[u8]>>( &self, prefix: K, ) -> impl DoubleEndedIterator<Item = Result<KvPair>> + 'static
Returns an iterator over a prefixed set of items in the snapshot.
Avoid using an empty prefix as it may scan a lot of items (unless limited).
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("a", "abc", 0);
tree.insert("ab", "abc", 1);
let snapshot = tree.snapshot(2);
tree.insert("abc", "abc", 2);
assert_eq!(2, snapshot.prefix("a").count());
sourcepub fn first_key_value(&self) -> Result<Option<(UserKey, UserValue)>>
pub fn first_key_value(&self) -> Result<Option<(UserKey, UserValue)>>
Returns the first key-value pair in the snapshot. The key in this pair is the minimum key in the snapshot.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("5", "abc", 0);
tree.insert("3", "abc", 1);
let snapshot = tree.snapshot(2);
tree.insert("1", "abc", 2);
let (key, _) = snapshot.first_key_value()?.expect("item should exist");
assert_eq!(&*key, "3".as_bytes());
§Errors
Will return Err
if an IO error occurs.
sourcepub fn last_key_value(&self) -> Result<Option<(UserKey, UserValue)>>
pub fn last_key_value(&self) -> Result<Option<(UserKey, UserValue)>>
Returns the las key-value pair in the snapshot. The key in this pair is the maximum key in the snapshot.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
tree.insert("1", "abc", 0);
tree.insert("3", "abc", 1);
let snapshot = tree.snapshot(2);
tree.insert("5", "abc", 2);
let (key, _) = snapshot.last_key_value()?.expect("item should exist");
assert_eq!(&*key, "3".as_bytes());
§Errors
Will return Err
if an IO error occurs.
sourcepub fn contains_key<K: AsRef<[u8]>>(&self, key: K) -> Result<bool>
pub fn contains_key<K: AsRef<[u8]>>(&self, key: K) -> Result<bool>
Returns true
if the snapshot contains the specified key.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
let snapshot = tree.snapshot(0);
assert!(!snapshot.contains_key("a")?);
tree.insert("a", "abc", 0);
assert!(!snapshot.contains_key("a")?);
§Errors
Will return Err
if an IO error occurs.
sourcepub fn is_empty(&self) -> Result<bool>
pub fn is_empty(&self) -> Result<bool>
Returns true
if the snapshot is empty.
This operation has O(1) complexity.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
let snapshot = tree.snapshot(0);
assert!(snapshot.is_empty()?);
tree.insert("a", "abc", 0);
assert!(snapshot.is_empty()?);
§Errors
Will return Err
if an IO error occurs.
sourcepub fn len(&self) -> Result<usize>
pub fn len(&self) -> Result<usize>
Scans the entire snapshot, returning the amount of items.
§Caution
This operation scans the entire tree: O(n) complexity!
Never, under any circumstances, use .len()
== 0 to check
if the snapshot is empty, use Snapshot::is_empty
instead.
§Examples
use lsm_tree::{AbstractTree, Config, Tree};
let tree = Config::new(folder).open()?;
let snapshot = tree.snapshot(0);
assert_eq!(snapshot.len()?, 0);
tree.insert("1", "abc", 0);
tree.insert("3", "abc", 1);
tree.insert("5", "abc", 2);
assert_eq!(snapshot.len()?, 0);
§Errors
Will return Err
if an IO error occurs.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Snapshot
impl RefUnwindSafe for Snapshot
impl Send for Snapshot
impl Sync for Snapshot
impl Unpin for Snapshot
impl UnwindSafe for Snapshot
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)