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 stat() for sub-database to txn #50

Open
wants to merge 2 commits 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
50 changes: 4 additions & 46 deletions src/environment.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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};

Expand Down Expand Up @@ -158,56 +159,13 @@ impl Environment {
/// Retrieves statistics about this environment.
pub fn stat(&self) -> Result<Stat> {
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 {}

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -56,6 +57,7 @@ mod flags;
mod cursor;
mod database;
mod environment;
mod stat;
mod error;
mod transaction;

Expand Down
61 changes: 61 additions & 0 deletions src/stat.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

10 changes: 10 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Stat> {
unsafe {
let mut stat = Stat::new();
lmdb_try!(ffi::mdb_stat(self.txn(), database.dbi(), stat.stat()));
Ok(stat)
}
}
}

/// An LMDB read-only transaction.
Expand Down