Struct disklru::Store

source ·
pub struct Store<K, V>where
K: Serialize + DeserializeOwned + Eq,
V: Serialize + DeserializeOwned,
{ /* private fields */ }
Expand description

LRU store, backed by by sled.

The typical use case is: you want a persistent cache, with an interface that has similarities with a basic HashMap. And, you do not want it to grow too much, and prefer to drop on the floor data which has not been accessed for a long time.

LRU is normally used for in-memory caches, but here it’s implemented on something which is persistent. Implementation is inspired from work on the in-memory cache HashLRU.

In most cases this implementation tries to be as close as possible to the standard collections HashMap however there are a few differences:

  • keys and values must be (de)serialiable
  • keys must implement Eq
  • no iterator on mutables
  • as it relies on external data, any operation can fail
  • inserts take references, gets return owned values

This latest aspect is really the fundamental difference: as here all the data is serialized and stored within the store, returning a reference does not really make any sense. Conversely, when putting values in the store, you don’t need to transfer ownership: the store is going to make a serializable copy anyway.

Examples

use disklru::Store;

let mut store = Store::open_temporary(4).unwrap();
store.insert(&1, &10).unwrap();
store.insert(&2, &20).unwrap();
store.insert(&3, &30).unwrap();
store.insert(&4, &40).unwrap();
store.insert(&5, &50).unwrap();
// key1 has been dropped, size is limited to 4
assert_eq!(Some(2), store.lru().unwrap());
assert_eq!(Some(20), store.get(&2).unwrap());
// getting key2 has made key3 the least recently used item
assert_eq!(Some(3), store.lru().unwrap());
assert_eq!(Some(40), store.get(&4).unwrap());
// getting key4 makes it the most recently used item
assert_eq!("[3: 30, 5: 50, 2: 20, 4: 40]", format!("{}", store));
store.flush().unwrap(); // commit

Implementations§

source§

impl<K, V> Store<K, V>where
K: Serialize + DeserializeOwned + Eq,
V: Serialize + DeserializeOwned,

source

pub fn open_with_config(config: Config, capacity: usize) -> Result<Self>

source

pub fn open_with_path<P: AsRef<Path>>(path: P, capacity: usize) -> Result<Self>

source

pub fn open_temporary(capacity: usize) -> Result<Self>

source

pub fn capacity(&self) -> usize

source

pub fn len(&self) -> usize

source

pub fn resize(&mut self, capacity: usize) -> Result<usize>

source

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

source

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

source

pub async fn flush_async(&self) -> Result<usize>

source

pub fn mru(&self) -> Result<Option<K>>

source

pub fn lru(&self) -> Result<Option<K>>

source

pub fn insert(&mut self, k: &K, v: &V) -> Result<Option<V>>

source

pub fn push(&mut self, k: &K, v: &V) -> Result<Option<(K, V)>>

source

pub fn remove(&mut self, k: &K) -> Result<Option<V>>

source

pub fn pop(&mut self, k: &K) -> Result<Option<(K, V)>>

source

pub fn pop_lru(&mut self) -> Result<Option<(K, V)>>

source

pub fn pop_mru(&mut self) -> Result<Option<(K, V)>>

source

pub fn forget(&mut self, k: &K) -> Result<()>

source

pub fn forget_mru(&mut self) -> Result<()>

source

pub fn forget_lru(&mut self) -> Result<()>

source

pub fn contains_key(&mut self, k: &K) -> Result<bool>

source

pub fn bump(&mut self, k: &K) -> Result<()>

source

pub fn get(&mut self, k: &K) -> Result<Option<V>>

source

pub fn get_key_value(&mut self, k: &K) -> Result<Option<(K, V)>>

source

pub fn get_lru(&mut self) -> Result<Option<V>>

source

pub fn peek(&self, k: &K) -> Result<Option<V>>

source

pub fn peek_mru(&self) -> Result<Option<V>>

source

pub fn peek_lru(&self) -> Result<Option<V>>

source

pub fn peek_key_value(&self, k: &K) -> Result<Option<(K, V)>>

source

pub fn export(&self) -> Export<'_, K, V>

source

pub fn iter(&self) -> Iter<'_, K, V>

source

pub fn to_vec(&self) -> Result<Vec<(K, V)>>

source

pub fn import<I>(&mut self, iter: I) -> Result<usize>where
I: Iterator<Item = (K, V)>,

source

pub fn import_map_iter<'a, I>(&mut self, iter: I) -> Result<usize>where
K: Serialize + DeserializeOwned + Eq + 'a,
V: Serialize + DeserializeOwned + 'a,
I: Iterator<Item = (&'a K, &'a V)>,

source

pub fn import_vec_iter<'a, I>(&mut self, iter: I) -> Result<usize>where
K: Serialize + DeserializeOwned + Eq + 'a,
V: Serialize + DeserializeOwned + 'a,
I: Iterator<Item = &'a (K, V)>,

source

pub fn keys(&self) -> Keys<'_, K, V>

source

pub fn values(&self) -> Values<'_, K, V>

source

pub fn export_keys(&self) -> ExportKeys<'_, K, V>

source

pub fn export_values(&self) -> ExportValues<'_, K, V>

source

pub fn dump(&self) -> Result<Dump<K, V>>

source

pub fn restore(&mut self, dump: &Dump<K, V>) -> Result<usize>

source§

impl<K, V> Store<K, V>where
K: Serialize + DeserializeOwned + Eq + Hash,
V: Serialize + DeserializeOwned,

source

pub fn to_map(&self) -> Result<HashMap<K, V>>

source§

impl<K, V> Store<K, V>where
K: Serialize + DeserializeOwned + Eq + Clone + Debug,
V: Serialize + DeserializeOwned,

source

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

Trait Implementations§

source§

impl<K, V> Debug for Store<K, V>where
K: Serialize + DeserializeOwned + Eq + Debug,
V: Serialize + DeserializeOwned + Debug,

source§

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

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

impl<K, V> Display for Store<K, V>where
K: Serialize + DeserializeOwned + Eq + Display,
V: Serialize + DeserializeOwned + Display,

source§

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

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

impl<K, V> PartialEq<Store<K, V>> for Store<K, V>where
K: Serialize + DeserializeOwned + Eq,
V: Serialize + DeserializeOwned + Eq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V> Eq for Store<K, V>where
K: Serialize + DeserializeOwned + Eq,
V: Serialize + DeserializeOwned + Eq,

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for Store<K, V>

§

impl<K, V> Send for Store<K, V>where
K: Send,
V: Send,

§

impl<K, V> Sync for Store<K, V>where
K: Sync,
V: Sync,

§

impl<K, V> Unpin for Store<K, V>where
K: Unpin,
V: Unpin,

§

impl<K, V> !UnwindSafe for Store<K, V>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToString for Twhere
T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.