diff --git a/src/environment.rs b/src/environment.rs index 9ac51c01..2061089c 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,5 +1,5 @@ use libc::{c_uint, size_t}; -use std::{fmt, ptr, result, mem}; +use std::{fmt, ptr, result}; use std::ffi::CString; #[cfg(unix)] use std::os::unix::ffi::OsStrExt; @@ -12,6 +12,7 @@ use ffi; use error::{Result, lmdb_result}; use database::Database; +use stat::Stat; use transaction::{RoTransaction, RwTransaction, Transaction}; use flags::{DatabaseFlags, EnvironmentFlags}; @@ -158,56 +159,13 @@ impl Environment { /// Retrieves statistics about this environment. pub fn stat(&self) -> Result { unsafe { - let mut stat = Stat(mem::zeroed()); - lmdb_try!(ffi::mdb_env_stat(self.env(), &mut stat.0)); + let mut stat = Stat::new(); + lmdb_try!(ffi::mdb_env_stat(self.env(), stat.stat())); Ok(stat) } } } -/// Environment statistics. -/// -/// Contains information about the size and layout of an LMDB environment. -pub struct Stat(ffi::MDB_stat); - -impl Stat { - /// Size of a database page. This is the same for all databases in the environment. - #[inline] - pub fn page_size(&self) -> u32 { - self.0.ms_psize - } - - /// Depth (height) of the B-tree. - #[inline] - pub fn depth(&self) -> u32 { - self.0.ms_depth - } - - /// Number of internal (non-leaf) pages. - #[inline] - pub fn branch_pages(&self) -> usize { - self.0.ms_branch_pages - } - - /// Number of leaf pages. - #[inline] - pub fn leaf_pages(&self) -> usize { - self.0.ms_leaf_pages - } - - /// Number of overflow pages. - #[inline] - pub fn overflow_pages(&self) -> usize { - self.0.ms_overflow_pages - } - - /// Number of data items. - #[inline] - pub fn entries(&self) -> usize { - self.0.ms_entries - } -} - unsafe impl Send for Environment {} unsafe impl Sync for Environment {} diff --git a/src/lib.rs b/src/lib.rs index bee6bfe4..7a39f001 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,8 @@ pub use cursor::{ IterDup, }; pub use database::Database; -pub use environment::{Environment, Stat, EnvironmentBuilder}; +pub use environment::{Environment, EnvironmentBuilder}; +pub use stat::Stat; pub use error::{Error, Result}; pub use flags::*; pub use transaction::{ @@ -56,6 +57,7 @@ mod flags; mod cursor; mod database; mod environment; +mod stat; mod error; mod transaction; diff --git a/src/stat.rs b/src/stat.rs new file mode 100644 index 00000000..81f2f633 --- /dev/null +++ b/src/stat.rs @@ -0,0 +1,61 @@ +use ffi; +use std::mem; + +/// Environment statistics. +/// +/// Contains information about the size and layout of an LMDB environment. +pub struct Stat(ffi::MDB_stat); + +impl Stat { + /// Create new zero'd LMDB statistics. + pub fn new() -> Stat { + unsafe { + Stat(mem::zeroed()) + } + } + + /// Returns a raw pointer to the underlying LMDB statistics. + /// + /// The caller **must** ensure that the pointer is not dereferenced after the lifetime of the + /// stat. + pub fn stat(&mut self) -> *mut ffi::MDB_stat { + &mut self.0 + } + + /// Size of a database page. This is the same for all databases in the environment. + #[inline] + pub fn page_size(&self) -> u32 { + self.0.ms_psize + } + + /// Depth (height) of the B-tree. + #[inline] + pub fn depth(&self) -> u32 { + self.0.ms_depth + } + + /// Number of internal (non-leaf) pages. + #[inline] + pub fn branch_pages(&self) -> usize { + self.0.ms_branch_pages + } + + /// Number of leaf pages. + #[inline] + pub fn leaf_pages(&self) -> usize { + self.0.ms_leaf_pages + } + + /// Number of overflow pages. + #[inline] + pub fn overflow_pages(&self) -> usize { + self.0.ms_overflow_pages + } + + /// Number of data items. + #[inline] + pub fn entries(&self) -> usize { + self.0.ms_entries + } +} + diff --git a/src/transaction.rs b/src/transaction.rs index 590fd9fe..540ffd2a 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -7,6 +7,7 @@ use ffi; use cursor::{RoCursor, RwCursor}; use environment::Environment; use database::Database; +use stat::Stat; use error::{Error, Result, lmdb_result}; use flags::{DatabaseFlags, EnvironmentFlags, WriteFlags}; @@ -102,6 +103,15 @@ pub trait Transaction : Sized { } Ok(DatabaseFlags::from_bits_truncate(flags)) } + + /// Retrieves statistics about a database. + fn stat<'txn>(&'txn self, database: Database) -> Result { + unsafe { + let mut stat = Stat::new(); + lmdb_try!(ffi::mdb_stat(self.txn(), database.dbi(), stat.stat())); + Ok(stat) + } + } } /// An LMDB read-only transaction.