Skip to content

Add method to retrieve specific database statistics #41

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ impl Environment {
Ok(stat)
}
}

/// Retrieves statistics about specified database of this environment.
pub fn db_stat(&self, db: Database) -> Result<Stat> {
let txn = self.begin_ro_txn()?;
unsafe {
let mut stat = Stat(mem::zeroed());
lmdb_try!(ffi::mdb_stat(txn.txn(), db.dbi(), &mut stat.0));
Ok(stat)
}
}
}

/// Environment statistics.
Expand Down Expand Up @@ -462,4 +472,64 @@ mod test {
assert_eq!(stat.overflow_pages(), 0);
assert_eq!(stat.entries(), 64);
}

#[test]
fn test_db_stat() {
let dir = TempDir::new("test").unwrap();
let env = Environment::new().set_max_dbs(2).open(dir.path()).unwrap();

// Stats should be empty initially.
let stat = env.stat().unwrap();
assert_eq!(stat.page_size(), 4096);
assert_eq!(stat.depth(), 0);
assert_eq!(stat.branch_pages(), 0);
assert_eq!(stat.leaf_pages(), 0);
assert_eq!(stat.overflow_pages(), 0);
assert_eq!(stat.entries(), 0);

let db1 = env.create_db(Some("db1"), Default::default()).unwrap();
let db2 = env.create_db(Some("db2"), Default::default()).unwrap();

// Write a few small values.
for i in 0..64 {
let mut value = [0u8; 8];
LittleEndian::write_u64(&mut value, i);
let mut tx = env.begin_rw_txn().expect("begin_rw_txn");
tx.put(db1, &value, &value, WriteFlags::default()).expect("tx.put");
tx.commit().expect("tx.commit")
}

for i in 0..32 {
let mut value = [0u8; 8];
LittleEndian::write_u64(&mut value, i);
let mut tx = env.begin_rw_txn().expect("begin_rw_txn");
tx.put(db2, &value, &value, WriteFlags::default()).expect("tx.put");
tx.commit().expect("tx.commit")
}

let stat = env.stat().unwrap();
assert_eq!(stat.page_size(), 4096);
assert_eq!(stat.depth(), 1);
assert_eq!(stat.branch_pages(), 0);
assert_eq!(stat.leaf_pages(), 1);
assert_eq!(stat.overflow_pages(), 0);
assert_eq!(stat.entries(), 2);

// Stats should now reflect inserted values.
let stat = env.db_stat(db1).unwrap();
assert_eq!(stat.page_size(), 4096);
assert_eq!(stat.depth(), 1);
assert_eq!(stat.branch_pages(), 0);
assert_eq!(stat.leaf_pages(), 1);
assert_eq!(stat.overflow_pages(), 0);
assert_eq!(stat.entries(), 64);

let stat = env.db_stat(db2).unwrap();
assert_eq!(stat.page_size(), 4096);
assert_eq!(stat.depth(), 1);
assert_eq!(stat.branch_pages(), 0);
assert_eq!(stat.leaf_pages(), 1);
assert_eq!(stat.overflow_pages(), 0);
assert_eq!(stat.entries(), 32);
}
}