Struct typed_sled::Tree [−][src]
pub struct Tree<K, V> { /* fields omitted */ }Expand description
A flash-sympathetic persistent lock-free B+ tree.
A Tree represents a single logical keyspace / namespace / bucket.
Example
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct SomeValue(u32);
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Creating a temporary sled database.
// If you want to persist the data use sled::open instead.
let db = sled::Config::new().temporary(true).open().unwrap();
// The id is used by sled to identify which Tree in the database (db) to open.
let tree = typed_sled::Tree::<String, SomeValue>::open(&db, "unique_id");
tree.insert(&"some_key".to_owned(), &SomeValue(10))?;
assert_eq!(tree.get(&"some_key".to_owned())?, Some(SomeValue(10)));
Ok(())
}Implementations
Initialize a typed tree. The id identifies the tree to be opened from the db.
Example
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct SomeValue(u32);
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Creating a temporary sled database.
// If you want to persist the data use sled::open instead.
let db = sled::Config::new().temporary(true).open().unwrap();
// The id is used by sled to identify which Tree in the database (db) to open.
let tree = typed_sled::Tree::<String, SomeValue>::open(&db, "unique_id");
tree.insert(&"some_key".to_owned(), &SomeValue(10))?;
assert_eq!(tree.get(&"some_key".to_owned())?, Some(SomeValue(10)));
Ok(())
}Insert a key to a new value, returning the last value if it was set.
pub fn transaction<F, A, E>(&self, f: F) -> TransactionResult<A, E> where
F: Fn(&TransactionalTree<'_, K, V>) -> ConflictableTransactionResult<A, E>,
pub fn transaction<F, A, E>(&self, f: F) -> TransactionResult<A, E> where
F: Fn(&TransactionalTree<'_, K, V>) -> ConflictableTransactionResult<A, E>,
Perform a multi-key serializable transaction.
Create a new batched update that can be atomically applied.
It is possible to apply a Batch in a transaction as well, which is the way you can apply a Batch to multiple Trees atomically.
Retrieve a value from the Tree if it exists.
Retrieve a value from the Tree if it exists. The key must be in serialized form.
Deserialize a key and retrieve it’s value from the Tree if it exists. The deserialization is only done if a value was retrieved successfully.
Delete a value, returning the old value if it existed.
Compare and swap. Capable of unique creation, conditional modification, or deletion. If old is None, this will only set the value if it doesn’t exist yet. If new is None, will delete the value if old is correct. If both old and new are Some, will modify the value if old is correct.
It returns Ok(Ok(())) if operation finishes successfully.
If it fails it returns: - Ok(Err(CompareAndSwapError(current, proposed))) if operation failed to setup a new value. CompareAndSwapError contains current and proposed values. - Err(Error::Unsupported) if the database is opened in read-only mode.
Fetch the value, apply a function to it and return the result.
Fetch the value, apply a function to it and return the previous value.
pub fn watch_prefix(&self, prefix: &K) -> Subscriber<K, V>ⓘ where
K: KV,
pub fn watch_prefix(&self, prefix: &K) -> Subscriber<K, V>ⓘ where
K: KV,
Subscribe to Events that happen to keys that have
the specified prefix. Events for particular keys are
guaranteed to be witnessed in the same order by all
threads, but threads may witness different interleavings
of Events across different keys. If subscribers don’t
keep up with new writes, they will cause new writes
to block. There is a buffer of 1024 items per
Subscriber. This can be used to build reactive
and replicated systems.
pub fn watch_all(&self) -> Subscriber<K, V>ⓘ where
K: KV,
pub fn watch_all(&self) -> Subscriber<K, V>ⓘ where
K: KV,
Subscribe to allEvents. Events for particular keys are
guaranteed to be witnessed in the same order by all
threads, but threads may witness different interleavings
of Events across different keys. If subscribers don’t
keep up with new writes, they will cause new writes
to block. There is a buffer of 1024 items per
Subscriber. This can be used to build reactive
and replicated systems.
Synchronously flushes all dirty IO buffers and calls fsync. If this succeeds, it is guaranteed that all previous writes will be recovered if the system crashes. Returns the number of bytes flushed during this call.
Flushing can take quite a lot of time, and you should measure the performance impact of using it on realistic sustained workloads running on realistic hardware.
Asynchronously flushes all dirty IO buffers and calls fsync. If this succeeds, it is guaranteed that all previous writes will be recovered if the system crashes. Returns the number of bytes flushed during this call.
Flushing can take quite a lot of time, and you should measure the performance impact of using it on realistic sustained workloads running on realistic hardware.
Returns true if the Tree contains a value for
the specified key.
Retrieve the key and value before the provided key, if one exists.
Retrieve the next key and value from the Tree after the
provided key.
Merge state directly into a given key’s value using the configured merge operator. This allows state to be written into a value directly, without any read-modify-write steps. Merge operators can be used to implement arbitrary data structures.
Calling merge will return an Unsupported error if it
is called without first setting a merge operator function.
Merge operators are shared by all instances of a particular
Tree. Different merge operators may be set on different
Trees.
pub fn set_merge_operator(
&self,
merge_operator: impl MergeOperator<K, V> + 'static
) where
K: KV,
V: KV,
pub fn set_merge_operator(
&self,
merge_operator: impl MergeOperator<K, V> + 'static
) where
K: KV,
V: KV,
Sets a merge operator for use with the merge function.
Merge state directly into a given key’s value using the configured merge operator. This allows state to be written into a value directly, without any read-modify-write steps. Merge operators can be used to implement arbitrary data structures.
Panics
Calling merge will panic if no merge operator has been
configured.
Create a double-ended iterator over the tuples of keys and values in this tree.
Create a double-ended iterator over tuples of keys and values, where the keys fall within the specified range.
Create an iterator over tuples of keys and values, where the all the keys starts with the given prefix.
Returns the first key and value in the Tree, or
None if the Tree is empty.
Returns the last key and value in the Tree, or
None if the Tree is empty.
Atomically removes the maximum item in the Tree instance.
Atomically removes the minimum item in the Tree instance.
Clears the Tree, removing all values.
Note that this is not atomic.
Trait Implementations
Auto Trait Implementations
impl<K, V> !RefUnwindSafe for Tree<K, V>
impl<K, V> !UnwindSafe for Tree<K, V>
Blanket Implementations
Mutably borrows from an owned value. Read more
