Skip to main content

Mapx

Struct Mapx 

Source
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>
where K: KeyEnDe, V: ValueEnDe,

Source

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

Source

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.

Source

pub fn as_bytes(&self) -> &[u8]

Source

pub fn new() -> Self

Source

pub fn clear(&mut self)

Source

pub fn is_the_same_instance(&self, other_hdr: &Self) -> bool

Source

pub fn instance_id(&self) -> u64

Returns the unique instance ID of this data structure.

Source

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.

Source

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>
where K: KeyEnDe, V: ValueEnDe,

Source

pub fn get(&self, key: &K) -> Option<V>

Retrieves a value from the map for a given key.

Source

pub fn get_mut(&mut self, key: &K) -> Option<ValueMut<'_, V>>

Retrieves a mutable reference to a value in the map.

Source

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

Checks if the map contains a value for the specified key.

Source

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.

Source

pub fn entry(&mut self, key: &K) -> Entry<'_, V>

Gets an entry for a given key, allowing for in-place modification.

Source

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

Returns an iterator over the map’s entries.

Source

pub fn iter_mut(&mut self) -> MapxIterMut<'_, K, V>

Returns a mutable iterator over the map’s entries.

Source

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

Returns an iterator over the map’s values.

Source

pub fn values_mut(&mut self) -> MapxOrdValuesMut<'_, V>

Returns a mutable iterator over the map’s values.

Source

pub fn remove(&mut self, key: &K)

Removes a key from the map.

Does not return the old value for performance reasons.

Source

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

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<K, V> Clone for Mapx<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for Mapx<K, V>

Source§

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

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

impl<K, V> Default for Mapx<K, V>
where K: KeyEnDe, V: ValueEnDe,

Source§

fn default() -> Self

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

impl<'de, K, V> Deserialize<'de> for Mapx<K, V>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: Eq, V: Eq> Eq for Mapx<K, V>

Source§

impl<'a, K, V> IntoIterator for &'a Mapx<K, V>
where K: KeyEnDe, V: ValueEnDe,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = MapxIter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V> IntoIterator for &'a mut Mapx<K, V>
where K: KeyEnDe, V: ValueEnDe,

Source§

type Item = (K, ValueIterMut<'a, V>)

The type of the elements being iterated over.
Source§

type IntoIter = MapxIterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: PartialEq, V: PartialEq> PartialEq for Mapx<K, V>

Source§

fn eq(&self, other: &Mapx<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> Serialize for Mapx<K, V>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

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>

§

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

§

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

§

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

§

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> KeyDe for T

Source§

fn decode_key(bytes: &[u8]) -> Result<T, VsdbError>

Decodes a key from a byte slice. Read more
Source§

impl<T> KeyEn for T
where T: Serialize,

Source§

fn try_encode_key(&self) -> Result<Vec<u8>, VsdbError>

Attempts to encode the key. Returns Err only if the Serialize implementation is broken — see module-level trust model for details.
Source§

fn encode_key(&self) -> RawBytes

Encodes the key, panicking on failure. Read more
Source§

impl<T> KeyEnDe for T
where T: KeyEn + KeyDe,

Source§

fn try_encode(&self) -> Result<Vec<u8>, VsdbError>

Attempts to encode the key. Prefer encode for internal VSDB paths; use this at trust boundaries where a Result is needed.
Source§

fn encode(&self) -> Vec<u8>

Encodes the key, panicking on failure. Read more
Source§

fn decode(bytes: &[u8]) -> Result<T, VsdbError>

Decodes a key from a byte slice. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> ValueDe for T

Source§

fn decode_value(bytes: &[u8]) -> Result<T, VsdbError>

Decodes a value from a byte slice. Read more
Source§

impl<T> ValueEn for T
where T: Serialize,

Source§

fn try_encode_value(&self) -> Result<Vec<u8>, VsdbError>

Attempts to encode the value. Returns Err only if the Serialize implementation is broken.
Source§

fn encode_value(&self) -> RawBytes

Encodes the value, panicking on failure. Read more
Source§

impl<T> ValueEnDe for T
where T: ValueEn + ValueDe,

Source§

fn try_encode(&self) -> Result<Vec<u8>, VsdbError>

Attempts to encode the value. Prefer encode for internal VSDB paths; use this at trust boundaries where a Result is needed.
Source§

fn encode(&self) -> Vec<u8>

Encodes the value, panicking on failure. Read more
Source§

fn decode(bytes: &[u8]) -> Result<T, VsdbError>

Decodes a value from a byte slice. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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