Consistency
Snapshots provide consistent read-only views over the entire state of the key-value store.
Once created, it supports all read operations—get or scan—giving you a stable, consistent view of the data as it existed at the instant the snapshot was taken.
Creating a snapshot
Section titled “Creating a snapshot”Call snapshot() on any SlateDB handle.
The call is cheap and non-blocking; it merely pins the current state so it can’t be garbage-collected while the snapshot is alive.
use slatedb::config::Settings;use slatedb::{Db, Error};use std::sync::Arc;
#[tokio::main]async fn main() -> Result<(), Error> { // Initialize database let object_store = Arc::new(slatedb::object_store::memory::InMemory::new()); let db = Db::builder("my_db", object_store) .with_settings(Settings::default()) .build() .await?;
// Write some data to the database db.put(b"key1", b"original_value").await?; db.put(b"key2", b"data_before_snapshot").await?;
// Create a snapshot let snapshot = db.snapshot().await?; println!("Snapshot created successfully");
// After creating snapshot, make changes to the database db.put(b"key1", b"modified_value").await?;
// Read from snapshot - should see original data let snapshot_value1 = snapshot.get(b"key1").await?; println!( "Snapshot key1: {:?}", snapshot_value1.map(|v| String::from_utf8_lossy(&v).to_string()) );
// Read from database - should see updated data let db_value1 = db.get(b"key1").await?; println!( "Database key1: {:?}", db_value1.map(|v| String::from_utf8_lossy(&v).to_string()) );
Ok(())}Reading from a snapshot
Section titled “Reading from a snapshot”Use the returned snapshot handle exactly like a regular Db, but every operation sees the same, immutable state.
use slatedb::config::Settings;use slatedb::{Db, Error};use std::sync::Arc;
#[tokio::main]async fn main() -> Result<(), Error> { // Initialize database let object_store = Arc::new(slatedb::object_store::memory::InMemory::new()); let db = Db::builder("my_db", object_store) .with_settings(Settings::default()) .build() .await?;
// Write some data to the database db.put(b"apple", b"red").await?; db.put(b"banana", b"yellow").await?; db.put(b"cherry", b"red").await?;
// Create a snapshot let snapshot = db.snapshot().await?; println!("Snapshot created successfully");
// After creating snapshot, make changes to the database db.put(b"apple", b"green").await?; // Modify existing db.put(b"date", b"brown").await?; // Add new key db.delete(b"banana").await?; // Delete existing
// Scan from snapshot - should see original data println!("\nScanning snapshot:"); let mut snapshot_iter = snapshot.scan(b"a".to_vec()..=b"z".to_vec()).await?; while let Some(kv) = snapshot_iter.next().await? { println!( " {}: {}", String::from_utf8_lossy(&kv.key), String::from_utf8_lossy(&kv.value) ); }
// Scan from database - should see updated data println!("\nScanning database:"); let mut db_iter = db.scan(b"a".to_vec()..=b"z".to_vec()).await?; while let Some(kv) = db_iter.next().await? { println!( " {}: {}", String::from_utf8_lossy(&kv.key), String::from_utf8_lossy(&kv.value) ); }
Ok(())}