pub struct Tree { /* fields omitted */ }
A flash-sympathetic persistent lock-free B+ tree
let t = sled::Tree::start_default("path_to_my_database").unwrap();
t.set(b"yo!", b"v1".to_vec());
assert!(t.get(b"yo!").unwrap().unwrap() == &*b"v1".to_vec());
t.cas(
b"yo!",
Some(b"v1"),
Some(b"v2".to_vec()),
).unwrap();
let mut iter = t.scan(b"a non-present key before yo!");
t.del(b"yo!");
assert_eq!(t.get(b"yo!"), Ok(None));
Load existing or create a new Tree
with a default configuration.
Load existing or create a new Tree
.
Generate a monotonic ID. Not guaranteed to be
contiguous. Written to disk every idgen_persist_interval
operations, followed by a blocking flush. During recovery, we
take the last recovered generated ID and add 2x
the idgen_persist_interval
to it. While persisting, if the
previous persisted counter wasn't synced to disk yet, we will do
a blocking flush to fsync the latest counter, ensuring
that we will never give out the same counter twice.
Clears the Tree
, removing all values.
Note that this is not atomic.
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 true
if the Tree
contains a value for
the specified key.
Retrieve a value from the Tree
if it exists.
Retrieve the key and value before the provided key,
if one exists.
use sled::{ConfigBuilder, Error};
let config = ConfigBuilder::new().temporary(true).build();
let tree = sled::Tree::start(config).unwrap();
for i in 0..10 {
tree.set(vec![i], vec![i]).expect("should write successfully");
}
assert!(tree.get_lt(vec![]).unwrap().is_none());
assert!(tree.get_lt(vec![0]).unwrap().is_none());
assert!(tree.get_lt(vec![1]).unwrap().unwrap().1 == vec![0]);
assert!(tree.get_lt(vec![9]).unwrap().unwrap().1 == vec![8]);
assert!(tree.get_lt(vec![10]).unwrap().unwrap().1 == vec![9]);
assert!(tree.get_lt(vec![255]).unwrap().unwrap().1 == vec![9]);
Retrieve the next key and value from the Tree
after the
provided key.
use sled::{ConfigBuilder, Error};
let config = ConfigBuilder::new().temporary(true).build();
let tree = sled::Tree::start(config).unwrap();
for i in 0..10 {
tree.set(vec![i], vec![i]).expect("should write successfully");
}
assert!(tree.get_gt(vec![]).unwrap().unwrap().1 == vec![0]);
assert!(tree.get_gt(vec![0]).unwrap().unwrap().1 == vec![1]);
assert!(tree.get_gt(vec![1]).unwrap().unwrap().1 == vec![2]);
assert!(tree.get_gt(vec![8]).unwrap().unwrap().1 == vec![9]);
assert!(tree.get_gt(vec![9]).unwrap().is_none());
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.
If Tree is read-only, will do nothing.
use sled::{ConfigBuilder, Error};
let config = ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
assert_eq!(t.cas(&[1], None, Some(vec![1])), Ok(()));
assert_eq!(t.cas(&[1], Some(&*vec![1]), Some(vec![2])), Ok(()));
assert_eq!(t.cas(&[1], Some(&[2]), None), Ok(()));
assert_eq!(t.get(&[1]), Ok(None));
Set a key to a new value, returning the old value if it
was set.
Delete a value, returning the last result if it existed.
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(&[1], vec![1]);
assert_eq!(t.del(&*vec![1]).unwrap().unwrap(), vec![1]);
assert_eq!(t.del(&*vec![1]), Ok(None));
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 panic if no merge operator has been
configured.
fn concatenate_merge(
_key: &[u8],
old_value: Option<&[u8]>,
merged_bytes: &[u8]
) -> Option<Vec<u8>> {
let mut ret = old_value
.map(|ov| ov.to_vec())
.unwrap_or_else(|| vec![]);
ret.extend_from_slice(merged_bytes);
Some(ret)
}
let config = sled::ConfigBuilder::new()
.temporary(true)
.merge_operator(concatenate_merge)
.build();
let tree = sled::Tree::start(config).unwrap();
let k = b"k1";
tree.set(k, vec![0]);
tree.merge(k, vec![1]);
tree.merge(k, vec![2]);
tree.set(k, vec![3]);
tree.del(k);
tree.merge(k, vec![4]);
Iterate over the tuples of keys and values in this tree.
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(&[1], vec![10]);
t.set(&[2], vec![20]);
t.set(&[3], vec![30]);
let mut iter = t.iter();
Iterate over tuples of keys and values, starting at the provided key.
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(&[1], vec![10]);
t.set(&[2], vec![20]);
t.set(&[3], vec![30]);
let mut iter = t.scan(&*vec![2]);
Iterate over keys, starting at the provided key.
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(&[1], vec![10]);
t.set(&[2], vec![20]);
t.set(&[3], vec![30]);
let mut iter = t.scan(&*vec![2]);
Iterate over values, starting at the provided key.
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(&[1], vec![10]);
t.set(&[2], vec![20]);
t.set(&[3], vec![30]);
let mut iter = t.scan(&*vec![2]);
Returns the number of elements in this tree.
Beware: performs a full O(n) scan under the hood.
Returns true
if the Tree
contains no elements.
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
type Owned = T
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more