Skip to content

Commit

Permalink
Apply the Env::copy_to_file changes to heed3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Feb 25, 2025
1 parent 881d5ad commit 53392b0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
46 changes: 44 additions & 2 deletions heed/src/envs/encrypted_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,50 @@ impl<T> EncryptedEnv<T> {
///
/// This function may be used to make a backup of an existing environment.
/// No lockfile is created, since it gets recreated at need.
pub fn copy_to_file<P: AsRef<Path>>(&self, path: P, option: CompactionOption) -> Result<File> {
self.inner.copy_to_file(path, option)
///
/// Note that the file must be seek to the beginning after the copy is complete.
///
/// ```
/// use std::fs;
/// use std::io::{Read, Seek, SeekFrom};
/// use std::path::Path;
/// use heed3::{EnvOpenOptions, Database, EnvFlags, FlagSetMode, CompactionOption};
/// use heed3::types::*;
/// use memchr::memmem::find_iter;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let dir = tempfile::tempdir()?;
/// # let env = unsafe { EnvOpenOptions::new()
/// # .map_size(10 * 1024 * 1024) // 10MB
/// # .max_dbs(3000)
/// # .open(dir.path())?
/// # };
///
/// let mut wtxn = env.write_txn()?;
/// let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;
///
/// db.put(&mut wtxn, &"hello0", &"world0")?;
/// db.put(&mut wtxn, &"hello1", &"world1")?;
/// db.put(&mut wtxn, &"hello2", &"world2")?;
/// db.put(&mut wtxn, &"hello3", &"world3")?;
///
/// wtxn.commit()?;
///
/// let mut tmp_file = tempfile::tempfile()?;
/// env.copy_to_file(&mut tmp_file, CompactionOption::Enabled)?;
/// let offset = tmp_file.seek(SeekFrom::Current(0))?;
/// assert_ne!(offset, 0);
///
/// let offset = tmp_file.seek(SeekFrom::Start(0))?;
/// assert_eq!(offset, 0);
///
/// let mut content = Vec::new();
/// tmp_file.read_to_end(&mut content)?;
/// assert!(content.len() > 8 * 6); // more than 8 times hellox + worldx
/// # Ok(()) }
/// ```
pub fn copy_to_file(&self, file: &mut File, option: CompactionOption) -> Result<()> {
self.inner.copy_to_file(file, option)
}

/// Copy an LMDB environment to the specified file descriptor, with compaction option.
Expand Down
1 change: 1 addition & 0 deletions heed3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ synchronoise = "1.0.1"

[dev-dependencies]
argon2 = { version = "0.5.3", features = ["std"] }
memchr = "2.7.4"
serde = { version = "1.0.217", features = ["derive"] }
chacha20poly1305 = "0.10.1"
tempfile = "3.15.0"
Expand Down

0 comments on commit 53392b0

Please sign in to comment.