Skip to content

Commit

Permalink
Introduce an example to check the PREV_SNAPSHOT feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Feb 28, 2025
1 parent 3a143a3 commit 05ac156
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
85 changes: 85 additions & 0 deletions examples/prev-snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::error::Error;

use heed::types::*;
use heed::{Database, EnvFlags, EnvOpenOptions};

// In this test we are checking that we can move to a previous environement snapshot.
fn main() -> Result<(), Box<dyn Error>> {
let env_path = tempfile::tempdir()?;

let env = unsafe {
EnvOpenOptions::new()
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3)
.open(&env_path)?
};

let mut wtxn = env.write_txn()?;
let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;

// We fill the db database with entries.
db.put(&mut wtxn, "I am here", "to test things")?;
db.put(&mut wtxn, "I am here too", "for the same purpose")?;

wtxn.commit()?;

env.prepare_for_closing().wait();

// We can get the env state from before the last commit
// and therefore see an empty env.
let env = unsafe {
EnvOpenOptions::new()
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3)
.flags(EnvFlags::PREV_SNAPSHOT)
.open(&env_path)?
};

let mut wtxn = env.write_txn()?;
let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;

assert!(db.is_empty(&wtxn)?);

wtxn.abort();
env.prepare_for_closing().wait();

// However, if we don't commit we can still get
// back the latest version of the env.
let env = unsafe {
EnvOpenOptions::new()
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3)
.open(&env_path)?
};

let mut wtxn = env.write_txn()?;
let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;

assert_eq!(db.get(&wtxn, "I am here")?, Some("to test things"));
assert_eq!(db.get(&wtxn, "I am here too")?, Some("for the same purpose"));

// And write new stuff in the env.
db.put(&mut wtxn, "I will fade away", "I am so sad")?;

wtxn.commit()?;
env.prepare_for_closing().wait();

// Once again we can get back the previous version
// of the env and see some entries disappear.
let env = unsafe {
EnvOpenOptions::new()
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3)
.flags(EnvFlags::PREV_SNAPSHOT)
.open(&env_path)?
};

let rtxn = env.read_txn()?;
let db: Database<Str, Str> = env.open_database(&rtxn, None)?.unwrap();

assert_eq!(db.get(&rtxn, "I am here")?, Some("to test things"));
assert_eq!(db.get(&rtxn, "I am here too")?, Some("for the same purpose"));
assert_eq!(db.get(&rtxn, "I will fade away")?, None);

Ok(())
}
4 changes: 4 additions & 0 deletions heed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ path = "../examples/multi-env.rs"
name = "nested"
path = "../examples/nested.rs"

[[example]]
name = "prev-snapshot"
path = "../examples/prev-snapshot.rs"

[[example]]
name = "rmp-serde"
path = "../examples/rmp-serde.rs"
Expand Down
4 changes: 4 additions & 0 deletions heed3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ longer-keys = ["lmdb-master3-sys/longer-keys"]

# Examples are located outside the standard heed/examples directory to prevent
# conflicts between heed3 and heed examples when working on both crates.
[[example]]
name = "prev-snapshot"
path = "../examples/prev-snapshot.rs"

[[example]]
name = "heed3-encrypted"
path = "../examples/heed3-encrypted.rs"
Expand Down

0 comments on commit 05ac156

Please sign in to comment.