pub trait Readable {
// Required methods
fn get<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
key: K,
) -> Result<Option<UserValue>>;
fn contains_key<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
key: K,
) -> Result<bool>;
fn first_key_value(&self, keyspace: impl AsRef<Keyspace>) -> Option<Guard>;
fn last_key_value(&self, keyspace: impl AsRef<Keyspace>) -> Option<Guard>;
fn size_of<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
key: K,
) -> Result<Option<u32>>;
fn iter(&self, keyspace: impl AsRef<Keyspace>) -> Iter ⓘ;
fn range<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
keyspace: impl AsRef<Keyspace>,
range: R,
) -> Iter ⓘ;
fn prefix<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
prefix: K,
) -> Iter ⓘ;
// Provided methods
fn is_empty(&self, keyspace: impl AsRef<Keyspace>) -> Result<bool> { ... }
fn len(&self, keyspace: impl AsRef<Keyspace>) -> Result<usize> { ... }
}Expand description
Readable snapshot
Can be used to pass a write transaction into a function that expects a snapshot.
Required Methods§
Sourcefn get<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
key: K,
) -> Result<Option<UserValue>>
fn get<K: AsRef<[u8]>>( &self, keyspace: impl AsRef<Keyspace>, key: K, ) -> Result<Option<UserValue>>
Retrieves an item from the snapshot.
§Examples
tree.insert("a", "my_value")?;
let snapshot = db.snapshot();
let item = snapshot.get(&tree, "a")?;
assert_eq!(Some("my_value".as_bytes().into()), item);
tree.insert("b", "my_updated_value")?;
// Repeatable read
let item = snapshot.get(&tree, "a")?;
assert_eq!(Some("my_value".as_bytes().into()), item);§Errors
Will return Err if an IO error occurs.
Sourcefn contains_key<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
key: K,
) -> Result<bool>
fn contains_key<K: AsRef<[u8]>>( &self, keyspace: impl AsRef<Keyspace>, key: K, ) -> Result<bool>
Sourcefn first_key_value(&self, keyspace: impl AsRef<Keyspace>) -> Option<Guard>
fn first_key_value(&self, keyspace: impl AsRef<Keyspace>) -> Option<Guard>
Returns the first key-value pair in the snapshot. The key in this pair is the minimum key in the snapshot.
§Examples
tree.insert("1", "abc")?;
tree.insert("3", "abc")?;
tree.insert("5", "abc")?;
let key = db.snapshot().first_key_value(&tree).expect("item should exist").key()?;
assert_eq!(&*key, "1".as_bytes());Sourcefn last_key_value(&self, keyspace: impl AsRef<Keyspace>) -> Option<Guard>
fn last_key_value(&self, keyspace: impl AsRef<Keyspace>) -> Option<Guard>
Returns the last key-value pair in the snapshot. The key in this pair is the maximum key in the snapshot.
§Examples
tree.insert("1", "abc")?;
tree.insert("3", "abc")?;
tree.insert("5", "abc")?;
let key = db.snapshot().last_key_value(&tree).expect("item should exist").key()?;
assert_eq!(&*key, "5".as_bytes());Sourcefn size_of<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
key: K,
) -> Result<Option<u32>>
fn size_of<K: AsRef<[u8]>>( &self, keyspace: impl AsRef<Keyspace>, key: K, ) -> Result<Option<u32>>
Retrieves the size of an item from the snapshot.
§Examples
tree.insert("a", "my_value")?;
let snapshot = db.snapshot();
let item = snapshot.size_of(&tree, "a")?.unwrap_or_default();
assert_eq!("my_value".len() as u32, item);
tree.insert("b", "my_updated_value")?;
// Repeatable read
let item = snapshot.size_of(&tree, "a")?.unwrap_or_default();
assert_eq!("my_value".len() as u32, item);§Errors
Will return Err if an IO error occurs.
Sourcefn iter(&self, keyspace: impl AsRef<Keyspace>) -> Iter ⓘ
fn iter(&self, keyspace: impl AsRef<Keyspace>) -> Iter ⓘ
Iterates over the snapshot.
Avoid using this function, or limit it as otherwise it may scan a lot of items.
§Examples
tree.insert("a", "abc")?;
tree.insert("f", "abc")?;
tree.insert("g", "abc")?;
assert_eq!(3, db.snapshot().iter(&tree).count());Sourcefn range<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
keyspace: impl AsRef<Keyspace>,
range: R,
) -> Iter ⓘ
fn range<K: AsRef<[u8]>, R: RangeBounds<K>>( &self, keyspace: impl AsRef<Keyspace>, range: R, ) -> Iter ⓘ
Iterates over a range of the snapshot.
Avoid using full or unbounded ranges as they may scan a lot of items (unless limited).
§Examples
tree.insert("a", "abc")?;
tree.insert("f", "abc")?;
tree.insert("g", "abc")?;
assert_eq!(2, db.snapshot().range(&tree, "a"..="f").count());Sourcefn prefix<K: AsRef<[u8]>>(
&self,
keyspace: impl AsRef<Keyspace>,
prefix: K,
) -> Iter ⓘ
fn prefix<K: AsRef<[u8]>>( &self, keyspace: impl AsRef<Keyspace>, prefix: K, ) -> Iter ⓘ
Iterates over a prefixed set of the snapshot.
Avoid using an empty prefix as it may scan a lot of items (unless limited).
§Examples
tree.insert("a", "abc")?;
tree.insert("ab", "abc")?;
tree.insert("abc", "abc")?;
assert_eq!(2, db.snapshot().prefix(&tree, "ab").count());Provided Methods§
Sourcefn len(&self, keyspace: impl AsRef<Keyspace>) -> Result<usize>
fn len(&self, keyspace: impl AsRef<Keyspace>) -> Result<usize>
Scans the entire keyspace, returning the number of items.
§Caution
This operation scans the entire keyspace: O(n) complexity!
Never, under any circumstances, use .len() == 0 to check
if the keyspace is empty, use Readable::is_empty instead.
§Examples
tree.insert("a", "my_value")?;
tree.insert("b", "my_value2")?;
let snapshot = db.snapshot();
assert_eq!(2, snapshot.len(&tree)?);
tree.insert("c", "my_value3")?;
// Repeatable read
assert_eq!(2, snapshot.len(&tree)?);
// Start new snapshot
let snapshot = db.snapshot();
assert_eq!(3, snapshot.len(&tree)?);§Errors
Will return Err if an IO error occurs.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.