Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Environment::copy #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ffi;
use error::{Result, lmdb_result};
use database::Database;
use transaction::{RoTransaction, RwTransaction, Transaction};
use flags::{DatabaseFlags, EnvironmentFlags};
use flags::{DatabaseFlags, EnvironmentCopyFlags, EnvironmentFlags};

#[cfg(windows)]
/// Adding a 'missing' trait from windows OsStrExt
Expand Down Expand Up @@ -138,6 +138,18 @@ impl Environment {
}
}

///
/// Make a consistent copy of the environment in the given destination directory.
///
/// Destination must exist, and be an empty directory.
///
pub fn copy(&self, destination: &Path, flags: EnvironmentCopyFlags) -> Result<()> {
let c_destination = CString::new(destination.as_os_str().as_bytes()).unwrap();
unsafe {
lmdb_result(ffi::mdb_env_copy2(self.env(), c_destination.as_ptr(), flags.bits()))
}
}

/// Closes the database handle. Normally unnecessary.
///
/// Closing a database handle is not necessary, but lets `Transaction::open_database` reuse the
Expand Down Expand Up @@ -462,4 +474,24 @@ mod test {
assert_eq!(stat.overflow_pages(), 0);
assert_eq!(stat.entries(), 64);
}

#[test]
fn test_copy() {
let dir = TempDir::new("test").unwrap();
let dir2 = TempDir::new("test-copy").unwrap();
{
let env = Environment::new().open(dir.path()).unwrap();
{
let db = env.open_db(None).unwrap();
let mut txn = env.begin_rw_txn().unwrap();
txn.put(db, b"key1", b"val1", WriteFlags::empty()).unwrap();
txn.commit().unwrap();
}
env.copy(dir2.path(), EnvironmentCopyFlags::empty()).unwrap()
}
let env = Environment::new().open(dir2.path()).unwrap();
let db = env.open_db(None).unwrap();
let txn = env.begin_ro_txn().unwrap();
assert_eq!(txn.get(db, b"key1").unwrap(), b"val1");
}
}
11 changes: 11 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,14 @@ bitflags! {
const APPEND_DUP = MDB_APPENDDUP;
}
}

bitflags! {
#[doc="Environment Copy Options"]
pub struct EnvironmentCopyFlags: c_uint {

#[doc="Perform compaction while copying: omit free pages and sequentially renumber all "]
#[doc="pages in output. This option consumes more CPU and runs more slowly than the "]
#[doc="default, but may produce a smaller output database."]
const COMPACT = MDB_CP_COMPACT;
}
}