Skip to content

Commit

Permalink
Edition 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 committed Feb 23, 2025
1 parent 0e2b2d0 commit c0c4edf
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libmdbx"
version = "0.5.3"
edition = "2021"
edition = "2024"
license = "MPL-2.0"
description = "Idiomatic and safe MDBX wrapper."
documentation = "https://docs.rs/libmdbx"
Expand Down
2 changes: 1 addition & 1 deletion benches/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utils;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use ffi::*;
use libmdbx::*;
use std::ptr;
Expand Down
4 changes: 2 additions & 2 deletions benches/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod utils;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use ffi::*;
use libc::size_t;
use libmdbx::{ObjectLength, WriteFlags};
use rand::{prelude::SliceRandom, SeedableRng};
use rand::{SeedableRng, prelude::SliceRandom};
use rand_xorshift::XorShiftRng;
use std::ptr;
use utils::*;
Expand Down
2 changes: 1 addition & 1 deletion benches/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use libmdbx::{Database, NoWriteMap, WriteFlags};
use tempfile::{tempdir, TempDir};
use tempfile::{TempDir, tempdir};

pub fn get_key(n: u32) -> String {
format!("key{n}")
Expand Down
2 changes: 1 addition & 1 deletion mdbx-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mdbx-sys"
version = "12.12.0"
edition = "2021"
edition = "2024"
license = "MPL-2.0"
description = "Rust bindings for libmdbx."
documentation = "https://docs.rs/mdbx-sys"
Expand Down
2 changes: 1 addition & 1 deletion mdbx-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bindgen::{
callbacks::{IntKind, ParseCallbacks},
Formatter,
callbacks::{IntKind, ParseCallbacks},
};
use std::{env, path::PathBuf};

Expand Down
11 changes: 6 additions & 5 deletions src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{error::mdbx_result, Error, TransactionKind};
use crate::{Error, TransactionKind, error::mdbx_result};
use derive_more::{Deref, DerefMut, Display};
use std::{borrow::Cow, slice};
use thiserror::Error;
Expand All @@ -17,7 +17,7 @@ pub trait Decodable<'tx> {
where
Self: Sized,
{
let s = slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len);
let s = unsafe { slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len) };

Decodable::decode(s)
}
Expand All @@ -33,9 +33,10 @@ impl<'tx> Decodable<'tx> for Cow<'tx, [u8]> {
txn: *const ffi::MDBX_txn,
data_val: &ffi::MDBX_val,
) -> Result<Self, Error> {
let is_dirty = (!K::ONLY_CLEAN) && mdbx_result(ffi::mdbx_is_dirty(txn, data_val.iov_base))?;
let is_dirty =
(!K::ONLY_CLEAN) && mdbx_result(unsafe { ffi::mdbx_is_dirty(txn, data_val.iov_base) })?;

let s = slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len);
let s = unsafe { slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len) };

Ok(if is_dirty {
Cow::Owned(s.to_vec())
Expand All @@ -56,7 +57,7 @@ impl<'tx> Decodable<'tx> for lifetimed_bytes::Bytes<'tx> {
txn: *const ffi::MDBX_txn,
data_val: &ffi::MDBX_val,
) -> Result<Self, Error> {
Cow::<'tx, [u8]>::decode_val::<K>(txn, data_val).map(From::from)
unsafe { Cow::<'tx, [u8]>::decode_val::<K>(txn, data_val).map(From::from) }
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::{
DatabaseKind, Decodable, Transaction,
database::TxnPtr,
error::{mdbx_result, Error, Result},
error::{Error, Result, mdbx_result},
flags::*,
mdbx_try_optional,
table::Table,
transaction::{txn_execute, TransactionKind, RW},
DatabaseKind, Decodable, Transaction,
transaction::{RW, TransactionKind, txn_execute},
};
use ffi::{
MDBX_cursor_op, MDBX_FIRST, MDBX_FIRST_DUP, MDBX_GET_BOTH, MDBX_GET_BOTH_RANGE,
MDBX_GET_CURRENT, MDBX_GET_MULTIPLE, MDBX_LAST, MDBX_LAST_DUP, MDBX_NEXT, MDBX_NEXT_DUP,
MDBX_NEXT_MULTIPLE, MDBX_NEXT_NODUP, MDBX_PREV, MDBX_PREV_DUP, MDBX_PREV_MULTIPLE,
MDBX_PREV_NODUP, MDBX_SET, MDBX_SET_KEY, MDBX_SET_LOWERBOUND, MDBX_SET_RANGE,
MDBX_FIRST, MDBX_FIRST_DUP, MDBX_GET_BOTH, MDBX_GET_BOTH_RANGE, MDBX_GET_CURRENT,
MDBX_GET_MULTIPLE, MDBX_LAST, MDBX_LAST_DUP, MDBX_NEXT, MDBX_NEXT_DUP, MDBX_NEXT_MULTIPLE,
MDBX_NEXT_NODUP, MDBX_PREV, MDBX_PREV_DUP, MDBX_PREV_MULTIPLE, MDBX_PREV_NODUP, MDBX_SET,
MDBX_SET_KEY, MDBX_SET_LOWERBOUND, MDBX_SET_RANGE, MDBX_cursor_op,
};
use libc::c_void;
use parking_lot::Mutex;
Expand Down
86 changes: 44 additions & 42 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
error::{mdbx_result, Error, Result},
Mode, ReadWriteOptions, SyncMode, Transaction, TransactionKind,
error::{Error, Result, mdbx_result},
table::Table,
transaction::{RO, RW},
Mode, ReadWriteOptions, SyncMode, Transaction, TransactionKind,
};
use libc::c_uint;
use mem::size_of;
Expand All @@ -20,7 +20,7 @@ use std::{
ops::Deref,
path::Path,
ptr, result,
sync::mpsc::{sync_channel, SyncSender},
sync::mpsc::{SyncSender, sync_channel},
thread::sleep,
time::Duration,
};
Expand Down Expand Up @@ -253,45 +253,47 @@ where
if let Mode::ReadWrite { .. } = options.mode {
let (tx, rx) = std::sync::mpsc::sync_channel(0);
let e = db.inner;
std::thread::spawn(move || loop {
match rx.recv() {
Ok(msg) => match msg {
TxnManagerMessage::Begin {
parent,
flags,
sender,
} => {
let e = e;
let mut txn: *mut ffi::MDBX_txn = ptr::null_mut();
sender
.send(
mdbx_result(unsafe {
ffi::mdbx_txn_begin_ex(
e.0,
parent.0,
flags,
&mut txn,
ptr::null_mut(),
)
})
.map(|_| TxnPtr(txn)),
)
.unwrap()
}
TxnManagerMessage::Abort { tx, sender } => {
sender
.send(mdbx_result(unsafe { ffi::mdbx_txn_abort(tx.0) }))
.unwrap();
}
TxnManagerMessage::Commit { tx, sender } => {
sender
.send(mdbx_result(unsafe {
ffi::mdbx_txn_commit_ex(tx.0, ptr::null_mut())
}))
.unwrap();
}
},
Err(_) => return,
std::thread::spawn(move || {
loop {
match rx.recv() {
Ok(msg) => match msg {
TxnManagerMessage::Begin {
parent,
flags,
sender,
} => {
let e = e;
let mut txn: *mut ffi::MDBX_txn = ptr::null_mut();
sender
.send(
mdbx_result(unsafe {
ffi::mdbx_txn_begin_ex(
e.0,
parent.0,
flags,
&mut txn,
ptr::null_mut(),
)
})
.map(|_| TxnPtr(txn)),
)
.unwrap()
}
TxnManagerMessage::Abort { tx, sender } => {
sender
.send(mdbx_result(unsafe { ffi::mdbx_txn_abort(tx.0) }))
.unwrap();
}
TxnManagerMessage::Commit { tx, sender } => {
sender
.send(mdbx_result(unsafe {
ffi::mdbx_txn_commit_ex(tx.0, ptr::null_mut())
}))
.unwrap();
}
},
Err(_) => return,
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use crate::{
error::{Error, Result},
flags::*,
table::Table,
transaction::{Transaction, TransactionKind, RO, RW},
transaction::{RO, RW, Transaction, TransactionKind},
};

mod codec;
Expand Down
2 changes: 1 addition & 1 deletion src/orm/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::traits::*;
use crate::{TransactionKind, WriteFlags, RW};
use crate::{RW, TransactionKind, WriteFlags};
use std::marker::PhantomData;

#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/orm/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{traits::*, transaction::Transaction};
use crate::{DatabaseOptions, Mode, TableFlags, WriteMap, RO, RW};
use crate::{DatabaseOptions, Mode, RO, RW, TableFlags, WriteMap};
use anyhow::Context;
use std::{
collections::BTreeMap,
Expand Down
4 changes: 2 additions & 2 deletions src/orm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ mod transaction;

pub use self::{cursor::*, database::*, impls::*, traits::*, transaction::*};
pub use crate::{
dupsort, table, table_info, DatabaseKind, DatabaseOptions, Mode, NoWriteMap, ReadWriteOptions,
SyncMode, TransactionKind, WriteMap, RO, RW,
DatabaseKind, DatabaseOptions, Mode, NoWriteMap, RO, RW, ReadWriteOptions, SyncMode,
TransactionKind, WriteMap, dupsort, table, table_info,
};
2 changes: 1 addition & 1 deletion src/orm/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{cursor::*, traits::*};
use crate::{Stat, TransactionKind, WriteFlags, WriteMap, RO, RW};
use crate::{RO, RW, Stat, TransactionKind, WriteFlags, WriteMap};
use anyhow::Context;
use std::{collections::HashMap, marker::PhantomData};

Expand Down
6 changes: 3 additions & 3 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
Transaction,
database::DatabaseKind,
error::{mdbx_result, Result},
error::{Result, mdbx_result},
flags::c_enum,
transaction::{txn_execute, TransactionKind},
Transaction,
transaction::{TransactionKind, txn_execute},
};
use libc::c_uint;
use std::{ffi::CString, marker::PhantomData, ptr};
Expand Down
14 changes: 7 additions & 7 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
Cursor, Decodable, Error, Stat,
database::{Database, DatabaseKind, NoWriteMap, TxnManagerMessage, TxnPtr},
error::{mdbx_result, Result},
flags::{c_enum, TableFlags, WriteFlags},
error::{Result, mdbx_result},
flags::{TableFlags, WriteFlags, c_enum},
table::Table,
Cursor, Decodable, Error, Stat,
};
use ffi::{MDBX_txn_flags_t, MDBX_TXN_RDONLY, MDBX_TXN_READWRITE};
use ffi::{MDBX_TXN_RDONLY, MDBX_TXN_READWRITE, MDBX_txn_flags_t};
use indexmap::IndexSet;
use libc::{c_uint, c_void};
use parking_lot::Mutex;
Expand All @@ -16,7 +16,7 @@ use std::{
marker::PhantomData,
mem::size_of,
ptr, result, slice,
sync::{mpsc::sync_channel, Arc},
sync::{Arc, mpsc::sync_channel},
};

#[sealed]
Expand Down Expand Up @@ -389,7 +389,7 @@ where
/// # Safety
/// Caller must close ALL other [Table] and [Cursor] instances pointing to the same dbi BEFORE calling this function.
pub unsafe fn drop_table<'txn>(&'txn self, table: Table<'txn>) -> Result<()> {
mdbx_result(txn_execute(&self.txn, |txn| {
mdbx_result(txn_execute(&self.txn, |txn| unsafe {
ffi::mdbx_drop(txn, table.dbi(), true)
}))?;

Expand All @@ -406,7 +406,7 @@ where
/// # Safety
/// Caller must close ALL other [Table] and [Cursor] instances pointing to the same dbi BEFORE calling this function.
pub unsafe fn close_table(&self, table: Table<'_>) -> Result<()> {
mdbx_result(ffi::mdbx_dbi_close(self.db.ptr().0, table.dbi()))?;
mdbx_result(unsafe { ffi::mdbx_dbi_close(self.db.ptr().0, table.dbi()) })?;

Ok(())
}
Expand Down
12 changes: 7 additions & 5 deletions tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ fn test_iter_empty_dup_database() {
assert!(cursor.iter_from::<(), ()>(b"foo").next().is_none());
assert!(cursor.iter_dup::<(), ()>().flatten().next().is_none());
assert!(cursor.iter_dup_start::<(), ()>().flatten().next().is_none());
assert!(cursor
.iter_dup_from::<(), ()>(b"foo")
.flatten()
.next()
.is_none());
assert!(
cursor
.iter_dup_from::<(), ()>(b"foo")
.flatten()
.next()
.is_none()
);
assert!(cursor.iter_dup_of::<(), ()>(b"foo").next().is_none());
}

Expand Down
36 changes: 20 additions & 16 deletions tests/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ fn test_open() {
let dir = tempdir().unwrap();

// opening non-existent database with read-only should fail
assert!(Database::open_with_options(
&dir,
DatabaseOptions {
mode: Mode::ReadOnly,
..Default::default()
}
)
.is_err());
assert!(
Database::open_with_options(
&dir,
DatabaseOptions {
mode: Mode::ReadOnly,
..Default::default()
}
)
.is_err()
);

// opening non-existent database should succeed
assert!(Database::open(&dir).is_ok());

// opening database with read-only should succeed
assert!(Database::open_with_options(
&dir,
DatabaseOptions {
mode: Mode::ReadOnly,
..Default::default()
}
)
.is_ok());
assert!(
Database::open_with_options(
&dir,
DatabaseOptions {
mode: Mode::ReadOnly,
..Default::default()
}
)
.is_ok()
);
}

#[test]
Expand Down

0 comments on commit c0c4edf

Please sign in to comment.