Skip to main content

MemoryStore

Struct MemoryStore 

Source
pub struct MemoryStore { /* private fields */ }
Expand description

In-memory storage backend.

All data is stored in BTreeMaps — nothing touches disk. Ideal for testing and prototyping.

§Example

use crdt_store::{MemoryStore, StateStore};

let mut store = MemoryStore::new();
store.put("sensors", "s1", b"temp=22.5").unwrap();

let data = store.get("sensors", "s1").unwrap().unwrap();
assert_eq!(data, b"temp=22.5");

Implementations§

Source§

impl MemoryStore

Source

pub fn new() -> Self

Create a new empty in-memory store.

Source

pub fn state_count(&self) -> usize

Returns the total number of state entries across all namespaces.

Source

pub fn total_event_count(&self) -> usize

Returns the total number of events across all entities.

Trait Implementations§

Source§

impl Default for MemoryStore

Source§

fn default() -> Self

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

impl EventStore for MemoryStore

Source§

fn append_event( &mut self, namespace: &str, entity_id: &str, data: &[u8], timestamp: u64, node_id: &str, ) -> Result<u64, Self::Error>

Append an event to the log for a given entity. Returns the assigned sequence number.
Source§

fn events_since( &self, namespace: &str, entity_id: &str, since_sequence: u64, ) -> Result<Vec<StoredEvent>, Self::Error>

Read events for an entity since a given sequence number (exclusive).
Source§

fn event_count( &self, namespace: &str, entity_id: &str, ) -> Result<u64, Self::Error>

Count total events for an entity (useful for compaction decisions).
Source§

fn save_snapshot( &mut self, namespace: &str, entity_id: &str, state: &[u8], at_sequence: u64, version: u8, ) -> Result<(), Self::Error>

Save a snapshot of an entity’s state at a given event sequence.
Source§

fn load_snapshot( &self, namespace: &str, entity_id: &str, ) -> Result<Option<Snapshot>, Self::Error>

Load the latest snapshot for an entity.
Source§

fn truncate_events_before( &mut self, namespace: &str, entity_id: &str, before_sequence: u64, ) -> Result<u64, Self::Error>

Delete events before a given sequence (for compaction). The snapshot at before_sequence must exist first.
Source§

impl StateStore for MemoryStore

Source§

type Error = MemoryError

Error type for this backend.
Source§

fn put( &mut self, namespace: &str, key: &str, value: &[u8], ) -> Result<(), Self::Error>

Store a value under (namespace, key).
Source§

fn get( &self, namespace: &str, key: &str, ) -> Result<Option<Vec<u8>>, Self::Error>

Retrieve a value by (namespace, key). Returns None if the key does not exist.
Source§

fn delete(&mut self, namespace: &str, key: &str) -> Result<(), Self::Error>

Delete a value by (namespace, key).
Source§

fn list_keys(&self, namespace: &str) -> Result<Vec<String>, Self::Error>

List all keys in a namespace.
Source§

fn exists(&self, namespace: &str, key: &str) -> Result<bool, Self::Error>

Check if a key exists in a namespace.

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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