Skip to main content

MemoryStore

Struct MemoryStore 

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

An in-memory VersionStore that shards the keyspace for concurrency.

Each key is hashed to one of a fixed number of shards; each shard holds its keys’ version chains behind its own reader-writer lock. Reads lock a single shard; a commit locks only the shards its keys fall in. Commits to disjoint shards therefore run in parallel, and the snapshot read of a key is a binary search within its shard for the newest version at or below the snapshot timestamp.

This is the default store of Db::new and suits caches, tests, and workloads that fit in memory. Versions accumulate until garbage collection lands (a later roadmap phase), so a long-lived store under heavy overwrite grows without bound for now.

§Examples

use txn_db::{Db, MemoryStore};

// `Db::new()` uses a `MemoryStore`; this is the explicit form.
let db = Db::with_store(MemoryStore::new());
let mut tx = db.begin();
tx.put(b"hello".to_vec(), b"world".to_vec());
tx.commit()?;

Implementations§

Source§

impl MemoryStore

Source

pub fn new() -> Self

Create an empty in-memory store with the default shard count.

§Examples
use txn_db::MemoryStore;

let store = MemoryStore::new();
Source

pub fn with_shards(shards: usize) -> Self

Create an empty store with a specific number of shards.

shards is rounded up to a power of two (and at least one). More shards reduce contention between commits that touch unrelated keys, at the cost of a larger fixed footprint. The default of MemoryStore::new suits most workloads; tune this only with a benchmark in hand.

§Examples
use txn_db::MemoryStore;

let store = MemoryStore::with_shards(64);
Source

pub fn key_count(&self) -> usize

Number of distinct keys that have ever been written.

Counts keys, not versions, and includes keys whose latest version is a tombstone. Primarily useful in tests and diagnostics.

§Examples
use txn_db::MemoryStore;

let store = MemoryStore::new();
assert_eq!(store.key_count(), 0);

Trait Implementations§

Source§

impl Default for MemoryStore

Source§

fn default() -> Self

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

impl VersionStore for MemoryStore

Source§

fn get(&self, key: &[u8], read_ts: Timestamp) -> Result<Option<Arc<[u8]>>>

Return the value of key visible at read_ts. Read more
Source§

fn try_commit( &self, read_ts: Timestamp, commit_ts: Timestamp, writes: Vec<WriteEntry>, reads: &[Arc<[u8]>], ) -> Result<()>

Validate a transaction and, if it does not conflict, apply its writes. Read more

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

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error