Struct Tree

Source
pub struct Tree { /* private fields */ }
Expand description

atomic lock-free tree A flash-sympathetic persistent lock-free B+ tree

Implementations§

Source§

impl Tree

Source

pub fn start(config: Config) -> DbResult<Tree, ()>

Load existing or create a new Tree.

Source

pub fn flush(&self) -> CacheResult<(), ()>

Flushes any pending IO buffers to disk to ensure durability.

Source

pub fn get(&self, key: &[u8]) -> DbResult<Option<Vec<u8>>, ()>

Retrieve a value from the Tree if it exists.

Source

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

pub fn set(&self, key: Vec<u8>, value: Vec<u8>) -> DbResult<(), ()>

Set a key to a new value.

Source

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

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

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 Clone for Tree

Source§

fn clone(&self) -> Tree

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> IntoIterator for &'a Tree

Source§

type Item = Result<(Vec<u8>, Vec<u8>), Error<()>>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a>

Creates an iterator from a value. Read more
Source§

impl Send for Tree

Source§

impl Sync for Tree

Auto Trait Implementations§

§

impl Freeze for Tree

§

impl !RefUnwindSafe for Tree

§

impl Unpin for Tree

§

impl !UnwindSafe for Tree

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.