Skip to main content

Readable

Trait Readable 

Source
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§

Source

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.

Source

fn contains_key<K: AsRef<[u8]>>( &self, keyspace: impl AsRef<Keyspace>, key: K, ) -> Result<bool>

Returns true if the snapshot contains the specified key.

§Examples
tree.insert("a", "my_value")?;

let snapshot = db.snapshot();
assert!(snapshot.contains_key(&tree, "a")?);
§Errors

Will return Err if an IO error occurs.

Source

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());
Source

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());
Source

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.

Source

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());
Source

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());
Source

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§

Source

fn is_empty(&self, keyspace: impl AsRef<Keyspace>) -> Result<bool>

Returns true if the parkeyspacetition is empty.

This operation has O(log N) complexity.

§Examples
assert!(db.snapshot().is_empty(&tree)?);

tree.insert("a", "abc")?;
assert!(!db.snapshot().is_empty(&tree)?);
§Errors

Will return Err if an IO error occurs.

Source

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.

Implementors§