pub struct VersionedMap<K, V>where
    K: Key + 'static,
    V: Value + 'static,{ /* private fields */ }
Expand description

HashMap that tracks incremental changes between commits

Calling clone() will create a reference to the same instance, and can be easily shared between threads.

To write to disk the entire content of a hash map on every commit, see Map

Implementations§

source§

impl<K, V> VersionedMap<K, V>where K: Key + Clone, V: Value,

source

pub fn insert(&self, key: K, value: impl Into<Arc<V>>) -> Arc<V>

Set key to value.

insert never overwrites existing values.

Returns either the existing value, or the newly inserted value.

It is equivalent to calling map.insert_with(key, move || value).

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();
assert_eq!(m.insert(1, "first".to_owned()), "first".to_owned().into());
assert_eq!(m.insert(1, "second".to_owned()), "first".to_owned().into());
source

pub fn insert_with<T: Into<Arc<V>>, F: FnOnce() -> T>( &self, key: K, new: F ) -> Arc<V>

Set key to the value returned by new.

insert never overwrites existing values.

Returns either the existing value, or the newly inserted value.

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();
assert_eq!(m.insert_with(1, || "first".to_owned()), "first".to_owned().into());
assert_eq!(m.insert_with(1, || "second".to_owned()), "first".to_owned().into());
source

pub fn update_with<T: Into<Arc<V>>>( &self, key: K, update: impl FnOnce(Arc<V>) -> T ) -> Option<Arc<V>>

Update the value in key to the one returned by the update closure.

update_with will never insert a new value to the map.

Returns the update value, or None if key does not exist in the map.

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();

assert_eq!(m.update_with(1, |_| "first".to_owned()), None);

m.insert(1, "first".to_owned());

assert_eq!(m.update_with(1, |_| "second".to_owned()), Some("second".to_owned().into()));
source

pub fn get<Q>(&self, key: &Q) -> Option<Arc<V>>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the stored value for a key, or None

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();

assert_eq!(m.get(&1), None);

m.insert(1, "first".to_owned());
assert_eq!(m.get(&1), Some("first".to_owned().into()));
source

pub fn remove(&self, key: K)

Sets the key as removed in the map

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();

m.insert(1, "first".to_owned());
assert_eq!(m.get(&1), Some("first".to_owned().into()));

m.remove(1);
assert_eq!(m.get(&1), None);
source

pub fn contains(&self, key: &K) -> bool

Returns true if there’s an addition for the specified key

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();

assert_eq!(m.contains(&1), false);
m.insert(1, "first".to_owned());

assert_eq!(m.contains(&1), true);
source

pub fn for_each(&self, callback: impl FnMut(&K, &V))

Call the function for all additive keys

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();

m.insert(1, "first".to_owned());

m.for_each(|k, v| {
    assert_eq!(v, &"first".to_owned());
});
source

pub fn retain(&self, callback: impl FnMut(&K, &V) -> bool)

Mark values as deleted where callback returns false

Examples
use infinitree::fields::VersionedMap;

let m = VersionedMap::<usize, String>::default();

m.insert(1, "first".to_owned());

m.retain(|k, v| false);
assert_eq!(m.contains(&1), false);
source

pub fn commit(&self)

Clear out the current changeset, and commit all changes to history.

This operation potentially helps free some memory, but more importantly any subsequent Store calls are going to be empty until further additions or removals.

source

pub fn len(&self) -> usize

Returns the number of additive keys

See VersionedMap::clear for example use.

source

pub fn size(&self) -> usize

Returns the number of all keys, including deletions

See VersionedMap::clear for example use.

source

pub fn capacity(&self) -> usize

Return the size of all allocated items

See VersionedMap::clear for example use.

source

pub fn clear(&self) -> usize

Free all items in the VersionedMap, without tracking changes

Returns the number of elements freed.

Examples
use infinitree::fields::VersionedMap;

let value = "first".to_owned();
let m = VersionedMap::<usize, String>::default();

assert_eq!(m.is_empty(), true);

let _ = m.insert(1, value.clone());

assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);

m.commit();

assert_eq!(m.contains(&1), true);

assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);

m.remove(1);

assert_eq!(m.contains(&1), false);

assert_eq!(m.len(), 0);
assert_eq!(m.size(), 2);
assert_eq!(m.is_empty(), true);

// Call `clear()`
assert_eq!(m.clear(), 2);

assert_eq!(m.len(), 0);
assert_eq!(m.size(), 0);
assert_eq!(m.is_empty(), true);
source

pub fn rollback(&self) -> usize

Roll back all modification since the last commit

Calling rollback also frees up memory dynamically.

Examples
use infinitree::fields::VersionedMap;

let value = "first".to_owned();
let m = VersionedMap::<usize, String>::default();

assert_eq!(m.is_empty(), true);

let _ = m.insert(1, value.clone());

assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);

m.commit();

assert_eq!(m.contains(&1), true);

assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);

m.remove(1);

assert_eq!(m.contains(&1), false);

assert_eq!(m.len(), 0);
assert_eq!(m.size(), 2);
assert_eq!(m.is_empty(), true);

// Call `rollback()`
assert_eq!(m.rollback(), 1);

assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);
source

pub fn is_empty(&self) -> bool

True if the number of additions to the map is zero

Since VersionedMap is tracking changes, is_empty() may return true even if a non-zero amount of memory is being used.

Trait Implementations§

source§

impl<K, V> Clone for VersionedMap<K, V>where K: Key + 'static, V: Value + 'static,

source§

fn clone(&self) -> Self

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<K, V> Collection for VersionedMap<K, V>where K: Key, V: Value,

§

type Depth = Incremental

Use this strategy to load the collection. Read more
§

type Key = K

The key that the predicate will use to decide whether to pull more data into memory.
§

type Serialized = (K, Option<Arc<V>>)

The serialized record format. This type will typically implement serde::Serialize
§

type Item = (K, Option<Arc<V>>)

This is equivalent to Iterator::Item, and should contain a full record that can be inserted into the in-memory store.
source§

fn key(from: &Self::Serialized) -> &Self::Key

Get the key based on the deserialized data. You want this to be a reference that’s easy to derive from the serialized data.
source§

fn load(from: Self::Serialized, _object: &mut dyn Reader) -> Self::Item

Load the full record, and return it
source§

fn insert(&mut self, record: Self::Item)

Store the deserialized record in the collection
source§

impl<K, V> Default for VersionedMap<K, V>where K: Key + 'static, V: Value + 'static,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<K, V> Index for VersionedMap<K, V>where K: Key + Clone, V: Value,

source§

fn store_all(&mut self) -> Result<Vec<Intent<Box<dyn Store>>>>

Generate an Intent wrapper for each field in the Index. Read more
source§

fn load_all(&mut self) -> Result<Vec<Intent<Box<dyn Load>>>>

Generate an Intent wrapper for each field in the Index. Read more
source§

impl<K, V> Store for VersionedMap<K, V>where K: Key + Clone, V: Value,

source§

fn store(&mut self, transaction: &mut dyn Transaction, _object: &mut dyn Writer)

Store the contents of the field into the index. The field itself needs to track whether this should be a complete rewrite or an upsert. Read more

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for VersionedMap<K, V>

§

impl<K, V> Send for VersionedMap<K, V>

§

impl<K, V> Sync for VersionedMap<K, V>

§

impl<K, V> Unpin for VersionedMap<K, V>

§

impl<K, V> UnwindSafe for VersionedMap<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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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<K, T> Load for Twhere T: Query<Key = K>,

source§

fn load( &mut self, pool: Pool<AEADReader>, transaction_list: Vec<(Id, String, Stream), Global> )

Execute a load action. Read more
source§

impl<T> Query for Twhere T: Collection,

§

type Key = <T as Collection>::Key

The key that the predicate will use to decide whether to pull more data into memory.
source§

fn select( &mut self, pool: Pool<AEADReader>, transaction_list: Vec<(Id, String, Stream), Global>, predicate: impl Fn(&<T as Query>::Key) -> QueryAction )

Load items into memory based on a predicate Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V