From 53392b0d419d495a613a4a6802e1a261600e0a69 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Fri, 21 Feb 2025 13:10:11 +0100 Subject: [PATCH] Apply the Env::copy_to_file changes to heed3 --- heed/src/envs/encrypted_env.rs | 46 ++++++++++++++++++++++++++++++++-- heed3/Cargo.toml | 1 + 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/heed/src/envs/encrypted_env.rs b/heed/src/envs/encrypted_env.rs index 742280c8..4de94431 100644 --- a/heed/src/envs/encrypted_env.rs +++ b/heed/src/envs/encrypted_env.rs @@ -253,8 +253,50 @@ impl EncryptedEnv { /// /// 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>(&self, path: P, option: CompactionOption) -> Result { - 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> { + /// # 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 = 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. diff --git a/heed3/Cargo.toml b/heed3/Cargo.toml index eabc5926..e0b1b3e2 100644 --- a/heed3/Cargo.toml +++ b/heed3/Cargo.toml @@ -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"