pub struct Snapshot<S: VersionStore = MemoryStore> { /* private fields */ }Expand description
A read-only, point-in-time view of the database.
A snapshot is created by Db::snapshot and reads as
of the moment it was taken. It has no write buffer and nothing to commit, so
it is cheaper than a transaction when all you need is to read several keys at
one consistent instant. Multiple snapshots and transactions coexist without
blocking each other.
§Examples
use txn_db::Db;
let db = Db::new();
let mut tx = db.begin();
tx.put(b"k".to_vec(), b"v1".to_vec());
tx.commit()?;
// Capture a snapshot, then change the database.
let snap = db.snapshot();
let mut tx = db.begin();
tx.put(b"k".to_vec(), b"v2".to_vec());
tx.commit()?;
// The snapshot still sees the value as of when it was taken.
assert_eq!(snap.get(b"k")?.as_deref(), Some(&b"v1"[..]));
assert_eq!(db.snapshot().get(b"k")?.as_deref(), Some(&b"v2"[..]));Implementations§
Source§impl<S: VersionStore> Snapshot<S>
impl<S: VersionStore> Snapshot<S>
Sourcepub fn read_timestamp(&self) -> Timestamp
pub fn read_timestamp(&self) -> Timestamp
The timestamp this snapshot reads at.
§Examples
use txn_db::Db;
let db = Db::new();
assert_eq!(db.snapshot().read_timestamp(), txn_db::Timestamp::ZERO);Sourcepub fn get(&self, key: &[u8]) -> Result<Option<Arc<[u8]>>>
pub fn get(&self, key: &[u8]) -> Result<Option<Arc<[u8]>>>
Read the value of key as of this snapshot.
Returns the newest version committed at or before the snapshot
timestamp, or None if the key does not exist as of that instant.
§Errors
Returns TxnError::Store if the backing store
fails the read. The default in-memory store never fails.
§Examples
use txn_db::Db;
let db = Db::new();
assert_eq!(db.snapshot().get(b"missing")?, None);Auto Trait Implementations§
impl<S> Freeze for Snapshot<S>
impl<S> RefUnwindSafe for Snapshot<S>where
S: RefUnwindSafe,
impl<S> Send for Snapshot<S>
impl<S> Sync for Snapshot<S>
impl<S> Unpin for Snapshot<S>
impl<S> UnsafeUnpin for Snapshot<S>
impl<S> UnwindSafe for Snapshot<S>where
S: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more