[−][src]Struct sled::Tree
A flash-sympathetic persistent lock-free B+ tree
Methods
impl Tree
[src]
impl Tree
pub fn start_default<P: AsRef<Path>>(path: P) -> Result<Tree, ()>
[src]
pub fn start_default<P: AsRef<Path>>(path: P) -> Result<Tree, ()>
Load existing or create a new Tree
with a default configuration.
pub fn start(config: Config) -> Result<Tree, ()>
[src]
pub fn start(config: Config) -> Result<Tree, ()>
Load existing or create a new Tree
.
pub fn flush(&self) -> Result<(), ()>
[src]
pub fn flush(&self) -> Result<(), ()>
Flushes any pending IO buffers to disk to ensure durability.
pub fn get<'g>(&self, key: &[u8]) -> Result<Option<PinnedValue>, ()>
[src]
pub fn get<'g>(&self, key: &[u8]) -> Result<Option<PinnedValue>, ()>
Retrieve a value from the Tree
if it exists.
pub fn cas(
&self,
key: Vec<u8>,
old: Option<&[u8]>,
new: Option<Vec<u8>>
) -> Result<(), Option<PinnedValue>>
[src]
pub fn cas(
&self,
key: Vec<u8>,
old: Option<&[u8]>,
new: Option<Vec<u8>>
) -> Result<(), Option<PinnedValue>>
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));
pub fn set(
&self,
key: Vec<u8>,
value: Vec<u8>
) -> Result<Option<PinnedValue>, ()>
[src]
pub fn set(
&self,
key: Vec<u8>,
value: Vec<u8>
) -> Result<Option<PinnedValue>, ()>
Set a key to a new value, returning the old value if it was set.
pub fn merge(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), ()>
[src]
pub fn merge(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), ()>
Merge a new value into the total state for a key.
Examples
fn concatenate_merge( _key: &[u8], // the key being merged old_value: Option<&[u8]>, // the previous value, if one existed merged_bytes: &[u8] // the new bytes being merged in ) -> Option<Vec<u8>> { // set the new value, return None to delete 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".to_vec(); tree.set(k.clone(), vec![0]); tree.merge(k.clone(), vec![1]); tree.merge(k.clone(), vec![2]); // assert_eq!(tree.get(&k).unwrap().unwrap(), vec![0, 1, 2]); // sets replace previously merged data, // bypassing the merge function. tree.set(k.clone(), vec![3]); // assert_eq!(tree.get(&k), Ok(Some(vec![3]))); // merges on non-present values will add them tree.del(&k); tree.merge(k.clone(), vec![4]); // assert_eq!(tree.get(&k).unwrap().unwrap(), vec![4]);
pub fn del(&self, key: &[u8]) -> Result<Option<PinnedValue>, ()>
[src]
pub fn del(&self, key: &[u8]) -> Result<Option<PinnedValue>, ()>
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]).unwrap().unwrap(), vec![1]); assert_eq!(t.del(&*vec![1]), Ok(None));
ⓘImportant traits for Iter<'a>pub fn scan(&self, key: &[u8]) -> Iter
[src]
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);
ⓘImportant traits for Iter<'a>pub fn iter(&self) -> Iter
[src]
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
impl Sync for Tree
[src]
impl Sync for Tree
impl Send for Tree
[src]
impl Send for Tree
impl Clone for Tree
[src]
impl Clone for Tree
fn clone(&self) -> Tree
[src]
fn clone(&self) -> Tree
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<'a> IntoIterator for &'a Tree
[src]
impl<'a> IntoIterator for &'a Tree
type Item = Result<(Vec<u8>, PinnedValue), ()>
The type of the elements being iterated over.
type IntoIter = Iter<'a>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a>fn into_iter(self) -> Iter<'a>
[src]
fn into_iter(self) -> Iter<'a>
Creates an iterator from a value. Read more
impl Debug for Tree
[src]
impl Debug for Tree
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
[src]
fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)
[src]
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more