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<(UserKey, UserValue)>> + '_
pub fn iter( &self ) -> impl DoubleEndedIterator<Item = Result<(UserKey, UserValue)>> + '_
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::{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());
§Errors
Will return Err
if an IO error occurs.
sourcepub fn range<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
range: R
) -> impl DoubleEndedIterator<Item = Result<(UserKey, UserValue)>> + '_
pub fn range<K: AsRef<[u8]>, R: RangeBounds<K>>( &self, range: R ) -> impl DoubleEndedIterator<Item = Result<(UserKey, UserValue)>> + '_
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::{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());
§Errors
Will return Err
if an IO error occurs.
sourcepub fn prefix<K: AsRef<[u8]>>(
&self,
prefix: K
) -> impl DoubleEndedIterator<Item = Result<(UserKey, UserValue)>> + '_
pub fn prefix<K: AsRef<[u8]>>( &self, prefix: K ) -> impl DoubleEndedIterator<Item = Result<(UserKey, UserValue)>> + '_
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::{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());
§Errors
Will return Err
if an IO error occurs.
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::{Tree, Config};
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::{Tree, Config};
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::{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::{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::{Tree, Config};
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.