Skip to main content

Snapshot

Struct Snapshot 

Source
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. While it is alive it pins that instant: garbage collection will not reclaim a version the snapshot can still observe.

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

Source

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);
Source

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