Skip to content

Commit ba079e2

Browse files
KeatsKerollmops
authored andcommitted
Use mdb_stat for db.len()
Closes #56
1 parent 28c4b56 commit ba079e2

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

heed/src/db/polymorph.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -596,24 +596,22 @@ impl PolyDatabase {
596596
/// assert_eq!(ret, 3);
597597
///
598598
/// wtxn.commit()?;
599+
///
599600
/// # Ok(()) }
600601
/// ```
601-
pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result<usize> {
602+
pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result<u64> {
602603
assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize);
603604

604-
let mut cursor = RoCursor::new(txn, self.dbi)?;
605-
let mut count = 0;
605+
let mut db_stat = mem::MaybeUninit::uninit();
606+
let result = unsafe { mdb_result(ffi::mdb_stat(txn.txn, self.dbi, db_stat.as_mut_ptr())) };
606607

607-
match cursor.move_on_first()? {
608-
Some(_) => count += 1,
609-
None => return Ok(0),
610-
}
611-
612-
while let Some(_) = cursor.move_on_next()? {
613-
count += 1;
608+
match result {
609+
Ok(()) => {
610+
let stats = unsafe { db_stat.assume_init() };
611+
Ok(stats.ms_entries as u64)
612+
}
613+
Err(e) => Err(e.into()),
614614
}
615-
616-
Ok(count)
617615
}
618616

619617
/// Returns `true` if and only if this database is empty.

heed/src/db/uniform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl<KC, DC> Database<KC, DC> {
509509
/// wtxn.commit()?;
510510
/// # Ok(()) }
511511
/// ```
512-
pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result<usize> {
512+
pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result<u64> {
513513
self.dyndb.len(txn)
514514
}
515515

heed/src/mdb/lmdb_ffi.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub use ffi::{
88
};
99
use lmdb_master3_sys as ffi;
1010

11+
pub use ffi::mdb_env_stat as mdb_env_stat;
12+
pub use ffi::mdb_stat as mdb_stat;
13+
pub use ffi::MDB_stat as MDB_Stat;
14+
15+
1116
pub mod cursor_op {
1217
use super::ffi::{self, MDB_cursor_op};
1318

0 commit comments

Comments
 (0)