pub struct Tree { /* private fields */ }
Expand description
atomic lock-free tree A flash-sympathetic persistent lock-free B+ tree
Implementations§
Source§impl Tree
impl Tree
Sourcepub fn flush(&self) -> CacheResult<(), ()>
pub fn flush(&self) -> CacheResult<(), ()>
Flushes any pending IO buffers to disk to ensure durability.
Sourcepub fn get(&self, key: &[u8]) -> DbResult<Option<Vec<u8>>, ()>
pub fn get(&self, key: &[u8]) -> DbResult<Option<Vec<u8>>, ()>
Retrieve a value from the Tree
if it exists.
Sourcepub fn cas(
&self,
key: Vec<u8>,
old: Option<Vec<u8>>,
new: Option<Vec<u8>>,
) -> DbResult<(), Option<Vec<u8>>>
pub fn cas( &self, key: Vec<u8>, old: Option<Vec<u8>>, new: Option<Vec<u8>>, ) -> DbResult<(), Option<Vec<u8>>>
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.
§Examples
use sled::{ConfigBuilder, Error};
let config = ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
// unique creation
assert_eq!(t.cas(vec![1], None, Some(vec![1])), Ok(()));
assert_eq!(t.cas(vec![1], None, Some(vec![1])), Err(Error::CasFailed(Some(vec![1]))));
// conditional modification
assert_eq!(t.cas(vec![1], Some(vec![1]), Some(vec![2])), Ok(()));
assert_eq!(t.cas(vec![1], Some(vec![1]), Some(vec![2])), Err(Error::CasFailed(Some(vec![2]))));
// conditional deletion
assert_eq!(t.cas(vec![1], Some(vec![2]), None), Ok(()));
assert_eq!(t.get(&*vec![1]), Ok(None));
Sourcepub fn del(&self, key: &[u8]) -> DbResult<Option<Vec<u8>>, ()>
pub fn del(&self, key: &[u8]) -> DbResult<Option<Vec<u8>>, ()>
Delete a value, returning the last result if it existed.
§Examples
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(vec![1], vec![1]);
assert_eq!(t.del(&*vec![1]), Ok(Some(vec![1])));
assert_eq!(t.del(&*vec![1]), Ok(None));
Sourcepub fn scan(&self, key: &[u8]) -> Iter<'_> ⓘ
pub fn scan(&self, key: &[u8]) -> Iter<'_> ⓘ
Iterate over tuples of keys and values, starting at the provided key.
§Examples
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(vec![1], vec![10]);
t.set(vec![2], vec![20]);
t.set(vec![3], vec![30]);
let mut iter = t.scan(&*vec![2]);
assert_eq!(iter.next(), Some(Ok((vec![2], vec![20]))));
assert_eq!(iter.next(), Some(Ok((vec![3], vec![30]))));
assert_eq!(iter.next(), None);
Sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Iterate over the tuples of keys and values in this tree.
§Examples
let config = sled::ConfigBuilder::new().temporary(true).build();
let t = sled::Tree::start(config).unwrap();
t.set(vec![1], vec![10]);
t.set(vec![2], vec![20]);
t.set(vec![3], vec![30]);
let mut iter = t.iter();
assert_eq!(iter.next(), Some(Ok((vec![1], vec![10]))));
assert_eq!(iter.next(), Some(Ok((vec![2], vec![20]))));
assert_eq!(iter.next(), Some(Ok((vec![3], vec![30]))));
assert_eq!(iter.next(), None);
Trait Implementations§
Source§impl<'a> IntoIterator for &'a Tree
impl<'a> IntoIterator for &'a Tree
impl Send for Tree
impl Sync for Tree
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more