Skip to content

Commit

Permalink
Fix freelist counting, remove byteorder dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 committed Jan 18, 2024
1 parent 3166fa8 commit 10c9d72
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ arrayref = { version = "0.3", optional = true }
arrayvec = { version = "0.7", optional = true }
bytes = { version = "1", optional = true }
bitflags = "2"
byteorder = "1"
ciborium = { version = "0.2", optional = true }
derive_more = "0.99"
impls = { version = "1", optional = true }
Expand Down
15 changes: 6 additions & 9 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
transaction::{RO, RW},
Mode, ReadWriteOptions, SyncMode, Transaction, TransactionKind,
};
use byteorder::{ByteOrder, NativeEndian};
use libc::c_uint;
use mem::size_of;
use sealed::sealed;
Expand All @@ -14,6 +13,7 @@ use std::{
fmt::Debug,
marker::PhantomData,
mem,
ops::Deref,
os::unix::ffi::OsStrExt,
path::Path,
ptr, result,
Expand Down Expand Up @@ -378,7 +378,7 @@ where
/// Note:
///
/// * MDBX stores all the freelists in the designated table 0 in each database,
/// and the freelist count is stored at the beginning of the value as `libc::size_t`
/// and the freelist count is stored at the beginning of the value as 32-bit integer
/// in the native byte order.
///
/// * It will create a read transaction to traverse the freelist table.
Expand All @@ -390,16 +390,13 @@ where

for result in cursor {
let (_key, value) = result?;
if value.len() < mem::size_of::<usize>() {
if value.len() < mem::size_of::<u32>() {
return Err(Error::Corrupted);
}

let s = &value[..mem::size_of::<usize>()];
if cfg!(target_pointer_width = "64") {
freelist += NativeEndian::read_u64(s) as usize;
} else {
freelist += NativeEndian::read_u32(s) as usize;
}
freelist +=
u32::from_ne_bytes(value.deref()[..mem::size_of::<u32>()].try_into().unwrap())
as usize;
}

Ok(freelist)
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub use orm_uses::*;
#[cfg(test)]
mod test_utils {
use super::*;
use byteorder::{ByteOrder, LittleEndian};
use tempfile::tempdir;

type Database = crate::Database<NoWriteMap>;
Expand All @@ -69,9 +68,8 @@ mod test_utils {
)
.unwrap();

for height in 0..1000 {
let mut value = [0u8; 8];
LittleEndian::write_u64(&mut value, height);
for height in 0..1000_u64 {
let value = height.to_le_bytes();
let tx = db.begin_rw_txn().unwrap();
let index = tx.create_table(None, TableFlags::DUP_SORT).unwrap();
tx.put(&index, HEIGHT_KEY, value, WriteFlags::empty())
Expand Down
11 changes: 4 additions & 7 deletions tests/environment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use byteorder::{ByteOrder, LittleEndian};
use libmdbx::*;
use tempfile::tempdir;

Expand Down Expand Up @@ -146,9 +145,8 @@ fn test_stat() {
assert_eq!(stat.entries(), 0);

// Write a few small values.
for i in 0..64 {
let mut value = [0u8; 8];
LittleEndian::write_u64(&mut value, i);
for i in 0..64_u64 {
let value = i.to_le_bytes();
let tx = db.begin_rw_txn().unwrap();
tx.put(
&tx.open_table(None).unwrap(),
Expand Down Expand Up @@ -190,9 +188,8 @@ fn test_freelist() {
assert_eq!(freelist, 0);

// Write a few small values.
for i in 0..64 {
let mut value = [0u8; 8];
LittleEndian::write_u64(&mut value, i);
for i in 0..64_u64 {
let value = i.to_le_bytes();
let tx = db.begin_rw_txn().unwrap();
tx.put(
&tx.open_table(None).unwrap(),
Expand Down

0 comments on commit 10c9d72

Please sign in to comment.