pub trait VersionStore: Send + Sync {
// Required methods
fn get(&self, key: &[u8], read_ts: Timestamp) -> Result<Option<Arc<[u8]>>>;
fn latest_commit_ts(&self, key: &[u8]) -> Result<Option<Timestamp>>;
fn apply(&self, commit_ts: Timestamp, writes: Vec<WriteEntry>) -> Result<()>;
}Expand description
A keeper of timestamped versions, the backend a Db is built on.
This is the extension point for plugging txn-db onto a real storage
engine. The transaction layer calls these three methods and supplies all of
the isolation logic itself; an implementation only has to store versions and
answer the snapshot-read query honestly. The three methods below state the
precise contract.
Implementations must be Send + Sync: a Db shares one store
across every thread that holds a clone of it.
§Examples
Driving the shipped MemoryStore directly through the trait:
use std::sync::Arc;
use txn_db::{MemoryStore, Timestamp, VersionStore};
let store = MemoryStore::new();
let key: Arc<[u8]> = Arc::from(&b"k"[..]);
// Apply one version at commit timestamp 1.
store.apply(Timestamp::from_raw(1), vec![(key.clone(), Some(Arc::from(&b"v1"[..])))])?;
// A reader at timestamp 1 sees it; a reader at timestamp 0 does not.
assert_eq!(store.get(b"k", Timestamp::from_raw(1))?.as_deref(), Some(&b"v1"[..]));
assert_eq!(store.get(b"k", Timestamp::ZERO)?, None);Required Methods§
Sourcefn get(&self, key: &[u8], read_ts: Timestamp) -> Result<Option<Arc<[u8]>>>
fn get(&self, key: &[u8], read_ts: Timestamp) -> Result<Option<Arc<[u8]>>>
Return the value of key visible at read_ts.
The result is the value of the newest version of key whose commit
timestamp is <= read_ts, or None if there is no such version or the
newest visible version is a tombstone (the key was deleted as of
read_ts).
§Errors
Returns TxnError::Store if the backend fails
to service the read. MemoryStore never fails.
Sourcefn latest_commit_ts(&self, key: &[u8]) -> Result<Option<Timestamp>>
fn latest_commit_ts(&self, key: &[u8]) -> Result<Option<Timestamp>>
Return the commit timestamp of the most recent version of key.
Returns None if the key has never been written. The commit path uses
this to decide whether a key was modified after a transaction’s
snapshot, so it must account for every version ever applied — including
tombstones.
§Errors
Returns TxnError::Store if the backend fails.
MemoryStore never fails.
Sourcefn apply(&self, commit_ts: Timestamp, writes: Vec<WriteEntry>) -> Result<()>
fn apply(&self, commit_ts: Timestamp, writes: Vec<WriteEntry>) -> Result<()>
Install a batch of versions at commit_ts.
Each entry is a key paired with either Some(value) (a write) or None
(a tombstone marking a delete). The database guarantees that apply is
called with strictly increasing commit_ts and is never run
concurrently with another apply on the same store, so versions arrive
in commit order.
§Errors
Returns TxnError::Store if the backend fails
to persist the batch. MemoryStore never fails.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".