pub struct Mapx<K, V> { /* private fields */ }Expand description
A disk-based, HashMap-like data structure with typed keys and values.
Mapx stores key-value pairs on disk, encoding both keys and values
for type safety and persistence.
§Key determinism
K must encode deterministically. Do not use HashMap, HashSet,
or wrappers containing them as keys; their randomized iteration order
can produce different storage bytes for logically equal keys.
Implementations§
Source§impl<K, V> Mapx<K, V>
impl<K, V> Mapx<K, V>
Sourcepub unsafe fn shadow(&self) -> Self
pub unsafe fn shadow(&self) -> Self
§Safety
Creates a second handle to the same underlying storage, bypassing Rust’s aliasing guarantees. The caller must ensure no concurrent writes to the same key through any handle. Multiple writers on disjoint keys are safe. Concurrent reads alongside writes are safe (the engine provides snapshot isolation).
Sourcepub unsafe fn from_bytes(s: impl AsRef<[u8]>) -> Self
pub unsafe fn from_bytes(s: impl AsRef<[u8]>) -> Self
§Safety
Reconstructs a handle from a raw byte slice that was previously
produced by as_bytes on a valid instance of
the same type. The caller must ensure s encodes a prefix
they have unique ownership of. Passing arbitrary bytes
(corrupted, truncated, or from a different type) is undefined
behavior and may cause panics or silent data corruption on
subsequent operations.
pub fn as_bytes(&self) -> &[u8] ⓘ
pub fn new() -> Self
pub fn clear(&mut self)
pub fn is_the_same_instance(&self, other_hdr: &Self) -> bool
Sourcepub fn instance_id(&self) -> u64
pub fn instance_id(&self) -> u64
Returns the unique instance ID of this data structure.
Sourcepub fn save_meta(&self) -> Result<u64>
pub fn save_meta(&self) -> Result<u64>
Persists this instance’s metadata to disk so that it can be
recovered later via from_meta.
Returns the instance_id that should be passed to from_meta.
Sourcepub fn from_meta(instance_id: u64) -> Result<Self>
pub fn from_meta(instance_id: u64) -> Result<Self>
Recovers an instance from previously saved metadata.
The caller must ensure that the underlying VSDB database still contains the data referenced by this instance ID.
§Aliasing warning
The returned handle is a full alias of the original
instance, not an independent copy — it addresses the exact
same underlying key range (this is how
instance_id is recovered: it is
the raw prefix). If the original handle that produced
this instance_id (or another from_meta/shadow
restore of it) is still alive in-process, the same
no-concurrent-writes-to-the-same-key discipline
documented on shadow applies across
every live alias: no concurrent writes to the same
key through any handle.
from_meta is intended to restore a handle after the
original has gone out of scope (e.g. across a process
restart); calling it while the original is still live
requires the same care as shadow(), even though this
function is safe Rust.
Source§impl<K, V> Mapx<K, V>
impl<K, V> Mapx<K, V>
Sourcepub fn get_mut(&mut self, key: &K) -> Option<ValueMut<'_, V>>
pub fn get_mut(&mut self, key: &K) -> Option<ValueMut<'_, V>>
Retrieves a mutable reference to a value in the map.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Checks if the map contains a value for the specified key.
Sourcepub fn insert(&mut self, key: &K, value: &V)
pub fn insert(&mut self, key: &K, value: &V)
Inserts a key-value pair into the map.
Does not return the old value for performance reasons.
Sourcepub fn entry(&mut self, key: &K) -> Entry<'_, V>
pub fn entry(&mut self, key: &K) -> Entry<'_, V>
Gets an entry for a given key, allowing for in-place modification.
Sourcepub fn iter_mut(&mut self) -> MapxIterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> MapxIterMut<'_, K, V> ⓘ
Returns a mutable iterator over the map’s entries.
Sourcepub fn values(&self) -> MapxOrdValues<'_, V> ⓘ
pub fn values(&self) -> MapxOrdValues<'_, V> ⓘ
Returns an iterator over the map’s values.
Sourcepub fn values_mut(&mut self) -> MapxOrdValuesMut<'_, V> ⓘ
pub fn values_mut(&mut self) -> MapxOrdValuesMut<'_, V> ⓘ
Returns a mutable iterator over the map’s values.
Sourcepub fn remove(&mut self, key: &K)
pub fn remove(&mut self, key: &K)
Removes a key from the map.
Does not return the old value for performance reasons.
Sourcepub fn batch_entry(&mut self) -> MapxBatchEntry<'_, K, V>
pub fn batch_entry(&mut self) -> MapxBatchEntry<'_, K, V>
Start a batch operation.
This method allows you to perform multiple insert/remove operations and commit them atomically.
A failed commit consumes
the buffered operations (none are applied) and is not retryable —
re-stage the operations on a fresh batch instead.
§Examples
use vsdb::{Mapx, vsdb_set_base_dir};
vsdb_set_base_dir("/tmp/vsdb_mapx_batch_entry").unwrap();
let mut map = Mapx::new();
let mut batch = map.batch_entry();
batch.insert(&1, &"one".to_string());
batch.insert(&2, &"two".to_string());
batch.commit().unwrap();
assert_eq!(map.get(&1), Some("one".to_string()));
assert_eq!(map.get(&2), Some("two".to_string()));Sourcepub fn keys(&self) -> impl DoubleEndedIterator<Item = K> + '_
pub fn keys(&self) -> impl DoubleEndedIterator<Item = K> + '_
Returns an iterator over the map’s keys.
Decodes only K — unlike iter().map(|(k, _)| k), the value
bytes are never decoded and discarded (see
MapxOrdRawKey::keys’s doc comment for why this matters for
nested-VSDB-collection value types).
Trait Implementations§
Source§impl<'de, K, V> Deserialize<'de> for Mapx<K, V>
impl<'de, K, V> Deserialize<'de> for Mapx<K, V>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl<K: Eq, V: Eq> Eq for Mapx<K, V>
Source§impl<'a, K, V> IntoIterator for &'a Mapx<K, V>
impl<'a, K, V> IntoIterator for &'a Mapx<K, V>
Source§impl<'a, K, V> IntoIterator for &'a mut Mapx<K, V>
impl<'a, K, V> IntoIterator for &'a mut Mapx<K, V>
Source§impl<K: PartialEq, V: PartialEq> PartialEq for Mapx<K, V>
impl<K: PartialEq, V: PartialEq> PartialEq for Mapx<K, V>
impl<K: PartialEq, V: PartialEq> StructuralPartialEq for Mapx<K, V>
Auto Trait Implementations§
impl<K, V> !Freeze for Mapx<K, V>
impl<K, V> RefUnwindSafe for Mapx<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for Mapx<K, V>
impl<K, V> Sync for Mapx<K, V>
impl<K, V> Unpin for Mapx<K, V>
impl<K, V> UnsafeUnpin for Mapx<K, V>
impl<K, V> UnwindSafe for Mapx<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> KeyDe for Twhere
T: DeserializeOwned,
impl<T> KeyDe for Twhere
T: DeserializeOwned,
Source§impl<T> KeyEn for Twhere
T: Serialize,
impl<T> KeyEn for Twhere
T: Serialize,
Source§fn try_encode_key(&self) -> Result<Vec<u8>, VsdbError>
fn try_encode_key(&self) -> Result<Vec<u8>, VsdbError>
Err only if the
Serialize implementation is broken — see module-level trust
model for details.