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
impl MemoryStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty in-memory store with the default shard count.
§Examples
use txn_db::MemoryStore;
let store = MemoryStore::new();Sourcepub fn with_shards(shards: usize) -> Self
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);Sourcepub fn key_count(&self) -> usize
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);