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

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

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

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

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

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

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

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());
});

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

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.

Returns the number of additive keys

See VersionedMap::clear for example use.

Returns the number of all keys, including deletions

See VersionedMap::clear for example use.

Return the size of all allocated items

See VersionedMap::clear for example use.

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more