Struct Tree

Source
pub struct Tree<V, E, F> { /* private fields */ }
Expand description

A flash-sympathetic persistent lock-free B+ tree

This tree keeps track of insert and update times if requested

Implementations§

Source§

impl<V, E, F> ExpiringTree<V, E, F>
where E: Encoding<HashSet<IVec>> + Encoding<DateTime<Utc>> + 'static, F: Encoding<V> + 'static,

Source

pub fn cloned(&self) -> Self

Clone for structures where V, E, and F aren’t Clone

Source

pub fn transaction<G, R>(&self, g: G) -> TransactionResult<Result<R>>

Perform a multi-key serializable transaction.

Transactions also work on tuples of Trees, preserving serializable ACID semantics! In this example, we treat two trees like a work queue, atomically apply updates to data and move them from the unprocessed Tree to the processed Tree.

Source

pub fn apply_batch(&self, batch: ExpiringBatch<V, F>) -> Result<()>

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.

Source

pub fn compare_and_swap<K>( &self, key: K, old: Option<V>, new: Option<V>, ) -> Result<Result<(), CompareAndSwapError<V>>>
where K: AsRef<[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.

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.
Source

pub fn get<K>(&self, key: K) -> Result<Option<V>>
where K: AsRef<[u8]>,

Retrieve a value from the Tree if it exists.

Source

pub fn insert<K>(&self, key: K, value: V) -> Result<Option<V>>
where IVec: From<K>, K: AsRef<[u8]>,

Insert a key to a new value, returning the last value if it was set.

Source

pub fn remove<K>(&self, key: K) -> Result<Option<V>>
where K: AsRef<[u8]>,

Delete a value, returning the old value if it existed.

Source

pub fn update_and_fetch<K>( &self, key: K, f: impl Fn(Option<V>) -> Option<V>, ) -> Result<Option<V>>
where K: AsRef<[u8]>,

Fetch the value, apply a function to it and return the result.

§Note

This may call the function multiple times if the value has been changed from other threads in the meantime.

Source

pub fn fetch_and_update<K>( &self, key: K, f: impl Fn(Option<V>) -> Option<V>, ) -> Result<Option<V>>
where K: AsRef<[u8]>,

Fetch the value, apply a function to it and return the previous value.

§Note

This may call the function multiple times if the value has been changed from other threads in the meantime.

Source

pub fn flush(&self) -> Result<()>

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.

Source

pub fn contains_key<K>(&self, key: K) -> Result<bool>
where K: AsRef<[u8]>,

Returns true if the Tree contains a value for the specified key.

Source

pub fn iter<'a>(&'a self) -> ExpiringIter<'a, V, E, F>

Create a double-ended iterator over the tuples of keys and values in this tree.

Source

pub fn range<'a, K, R>(&'a self, range: R) -> ExpiringIter<'a, V, E, F>
where K: AsRef<[u8]>, R: RangeBounds<K>,

Create a double-ended iterator over tuples of keys and values, where the keys fall within the specified range.

Source

pub fn get_lt<K>(&self, key: K) -> Result<Option<(IVec, V)>>
where K: AsRef<[u8]>,

Retrieve the key and value before the provided key, if one exists.

Source

pub fn get_gt<K>(&self, key: K) -> Result<Option<(IVec, V)>>
where K: AsRef<[u8]>,

Retrieve the next key and value from the Tree after the provided key.

§Note

The order follows the Ord implementation for Vec:

[] < [0] < [255] < [255, 0] < [255, 255] ...

To retain the ordering of numerical types use big endian reprensentation

Source

pub fn scan_prefix<'a, P>(&'a self, prefix: P) -> ExpiringIter<'a, V, E, F>
where P: AsRef<[u8]>,

Create an iterator over tuples of keys and values, where the all the keys starts with the given prefix.

Source

pub fn pop_max(&self) -> Result<Option<(IVec, V)>>

Atomically removes the maximum item in the Tree instance.

Source

pub fn pop_min(&self) -> Result<Option<(IVec, V)>>

Atomically removes the minimum item in the Tree instance.

Source

pub fn len(&self) -> usize

Returns the number of elements in this tree.

Beware: performs a full O(n) scan under the hood.

Source

pub fn is_empty(&self) -> bool

Returns true if the Tree contains no elements.

Source

pub fn clear(&self) -> Result<()>

Clears the Tree, removing all values.

Note that this is not atomic.

Source

pub fn name(&self) -> String

Returns the name of the tree.

Source

pub fn expired<'a>(&'a self) -> impl 'a + Iterator<Item = IVec>

Create an iterator over the keys of expired records

Trait Implementations§

Source§

impl<V: Clone, E: Clone, F: Clone> Clone for ExpiringTree<V, E, F>

Source§

fn clone(&self) -> ExpiringTree<V, E, F>

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

Auto Trait Implementations§

§

impl<V, E, F> Freeze for ExpiringTree<V, E, F>

§

impl<V, E, F> !RefUnwindSafe for ExpiringTree<V, E, F>

§

impl<V, E, F> Send for ExpiringTree<V, E, F>
where V: Send, F: Send, E: Send,

§

impl<V, E, F> Sync for ExpiringTree<V, E, F>
where V: Sync, F: Sync, E: Sync,

§

impl<V, E, F> Unpin for ExpiringTree<V, E, F>
where V: Unpin, F: Unpin, E: Unpin,

§

impl<V, E, F> !UnwindSafe for ExpiringTree<V, E, F>

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.