Skip to content

Commit 931c241

Browse files
authored
refactor: update tx types to use txunsync (#1)
* fix: update for mdbx @ 0.5.0 * refactor: use tx unsync * dep: update for 0.6.0 * feat: many iterators
1 parent 503daed commit 931c241

File tree

12 files changed

+450
-382
lines changed

12 files changed

+450
-382
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ signet-storage = { version = "0.0.1", path = "./crates/storage" }
4242
signet-storage-types = { version = "0.0.1", path = "./crates/types" }
4343

4444
# External, in-house
45-
signet-libmdbx = "0.4.0"
45+
signet-libmdbx = "0.6.0"
4646

4747
signet-zenith = "0.16.0-rc.5"
4848

crates/hot-mdbx/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bytes.workspace = true
1414
page_size.workspace = true
1515
parking_lot.workspace = true
1616
signet-hot.workspace = true
17-
signet-libmdbx = { workspace = true, features = ["read-tx-timeouts"] }
17+
signet-libmdbx.workspace = true
1818
sysinfo = "0.37.2"
1919
tempfile = { workspace = true, optional = true }
2020
thiserror.workspace = true

crates/hot-mdbx/src/cursor.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@ use signet_hot::{
55
MAX_FIXED_VAL_SIZE, MAX_KEY_SIZE,
66
model::{DualKeyTraverse, KvTraverse, KvTraverseMut, RawDualKeyValue, RawKeyValue, RawValue},
77
};
8-
use signet_libmdbx::{RO, RW, TransactionKind};
8+
use signet_libmdbx::{Ro, Rw, RwSync, TransactionKind, tx::WriteMarker};
99
use std::{
1010
borrow::Cow,
1111
ops::{Deref, DerefMut},
1212
};
1313

1414
/// Read only Cursor.
15-
pub type CursorRO<'a> = Cursor<'a, RO>;
15+
pub type CursorRo<'a> = Cursor<'a, Ro>;
1616

1717
/// Read write cursor.
18-
pub type CursorRW<'a> = Cursor<'a, RW>;
18+
pub type CursorRw<'a> = Cursor<'a, Rw>;
19+
20+
/// Synchronized read only cursor.
21+
pub type CursorRoSync<'a> = Cursor<'a, signet_libmdbx::RoSync>;
22+
23+
/// Synchronized read write cursor.
24+
pub type CursorRwSync<'a> = Cursor<'a, RwSync>;
1925

2026
/// Cursor wrapper to access KV items.
27+
///
28+
/// The inner cursor type uses `K::Inner` which is the transaction's internal
29+
/// pointer access type:
30+
/// - For `RO`: `K::Inner = RoGuard`
31+
/// - For `RW`: `K::Inner = RwUnsync`
2132
pub struct Cursor<'a, K: TransactionKind> {
2233
/// Inner `libmdbx` cursor.
2334
pub(crate) inner: signet_libmdbx::Cursor<'a, K>,
@@ -30,13 +41,9 @@ pub struct Cursor<'a, K: TransactionKind> {
3041
buf: [u8; MAX_KEY_SIZE + MAX_FIXED_VAL_SIZE],
3142
}
3243

33-
impl<K: TransactionKind + std::fmt::Debug> std::fmt::Debug for Cursor<'_, K> {
44+
impl<K: TransactionKind> std::fmt::Debug for Cursor<'_, K> {
3445
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35-
f.debug_struct("Cursor")
36-
.field("inner", &self.inner)
37-
.field("fsi", &self.fsi)
38-
.field("buf", &self.buf)
39-
.finish()
46+
f.debug_struct("Cursor").field("fsi", &self.fsi).finish_non_exhaustive()
4047
}
4148
}
4249

@@ -48,7 +55,7 @@ impl<'a, K: TransactionKind> Deref for Cursor<'a, K> {
4855
}
4956
}
5057

51-
impl<'a> DerefMut for Cursor<'a, RW> {
58+
impl<'a, K: TransactionKind> DerefMut for Cursor<'a, K> {
5259
fn deref_mut(&mut self) -> &mut Self::Target {
5360
&mut self.inner
5461
}
@@ -95,13 +102,13 @@ where
95102
}
96103
}
97104

98-
impl KvTraverseMut<MdbxError> for Cursor<'_, RW> {
105+
impl<K: TransactionKind + WriteMarker> KvTraverseMut<MdbxError> for Cursor<'_, K> {
99106
fn delete_current(&mut self) -> Result<(), MdbxError> {
100107
self.inner.del(Default::default()).map_err(MdbxError::Mdbx)
101108
}
102109
}
103110

104-
impl Cursor<'_, RW> {
111+
impl<K: TransactionKind + WriteMarker> Cursor<'_, K> {
105112
/// Stores multiple contiguous fixed-size data elements in a single request.
106113
///
107114
/// This directly calls MDBX FFI, bypassing the transaction execution wrapper

crates/hot-mdbx/src/lib.rs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
3535
use parking_lot::RwLock;
3636
use signet_libmdbx::{
37-
Environment, EnvironmentFlags, Geometry, HandleSlowReadersReturnCode,
38-
MaxReadTransactionDuration, Mode, PageSize, RO, RW, SyncMode, ffi,
37+
Environment, EnvironmentFlags, Geometry, Mode, Ro, RoSync, Rw, RwSync, SyncMode, ffi,
38+
sys::{HandleSlowReadersReturnCode, PageSize},
3939
};
4040
use std::{
4141
collections::HashMap,
@@ -45,7 +45,7 @@ use std::{
4545
};
4646

4747
mod cursor;
48-
pub use cursor::{Cursor, CursorRO, CursorRW};
48+
pub use cursor::{Cursor, CursorRo, CursorRoSync, CursorRw, CursorRwSync};
4949

5050
mod db_info;
5151
pub use db_info::{FixedSizeInfo, FsiCache};
@@ -105,8 +105,6 @@ pub struct DatabaseArguments {
105105
/// Database geometry settings.
106106
geometry: Geometry<Range<usize>>,
107107

108-
/// Maximum duration of a read transaction. If [None], the default value is used.
109-
max_read_transaction_duration: Option<MaxReadTransactionDuration>,
110108
/// Open environment in exclusive/monopolistic mode. If [None], the default value is used.
111109
///
112110
/// This can be used as a replacement for `MDB_NOLOCK`, which don't supported by MDBX. In this
@@ -165,7 +163,6 @@ impl DatabaseArguments {
165163
shrink_threshold: Some(0),
166164
page_size: Some(PageSize::Set(utils::default_page_size())),
167165
},
168-
max_read_transaction_duration: None,
169166
exclusive: None,
170167
max_readers: None,
171168
sync_mode: SyncMode::Durable,
@@ -206,23 +203,6 @@ impl DatabaseArguments {
206203
self
207204
}
208205

209-
/// Set the maximum duration of a read transaction.
210-
pub const fn max_read_transaction_duration(
211-
&mut self,
212-
max_read_transaction_duration: Option<MaxReadTransactionDuration>,
213-
) {
214-
self.max_read_transaction_duration = max_read_transaction_duration;
215-
}
216-
217-
/// Set the maximum duration of a read transaction.
218-
pub const fn with_max_read_transaction_duration(
219-
mut self,
220-
max_read_transaction_duration: Option<MaxReadTransactionDuration>,
221-
) -> Self {
222-
self.max_read_transaction_duration(max_read_transaction_duration);
223-
self
224-
}
225-
226206
/// Set the mdbx exclusive flag.
227207
pub const fn with_exclusive(mut self, exclusive: Option<bool>) -> Self {
228208
self.exclusive = exclusive;
@@ -372,28 +352,40 @@ impl DatabaseEnv {
372352
// https://github.com/paradigmxyz/reth/blob/fa2b9b685ed9787636d962f4366caf34a9186e66/crates/storage/libmdbx-rs/mdbx-sys/libmdbx/mdbx.c#L16017.
373353
inner_env.set_rp_augment_limit(256 * 1024);
374354

375-
if let Some(max_read_transaction_duration) = args.max_read_transaction_duration {
376-
inner_env.set_max_read_transaction_duration(max_read_transaction_duration);
377-
}
378-
379355
let fsi_cache = Arc::new(RwLock::new(HashMap::new()));
380356
let env = Self { inner: inner_env.open(path)?, fsi_cache, _lock_file };
381357

382358
Ok(env)
383359
}
384360

385361
/// Start a new read-only transaction.
386-
fn tx(&self) -> Result<Tx<RO>, MdbxError> {
362+
pub fn tx(&self) -> Result<Tx<Ro>, MdbxError> {
387363
self.inner
388-
.begin_ro_txn()
364+
.begin_ro_unsync()
389365
.map(|tx| Tx::new(tx, self.fsi_cache.clone()))
390366
.map_err(MdbxError::Mdbx)
391367
}
392368

393369
/// Start a new read-write transaction.
394-
fn tx_mut(&self) -> Result<Tx<RW>, MdbxError> {
370+
pub fn tx_rw(&self) -> Result<Tx<Rw>, MdbxError> {
371+
self.inner
372+
.begin_rw_unsync()
373+
.map(|tx| Tx::new(tx, self.fsi_cache.clone()))
374+
.map_err(MdbxError::Mdbx)
375+
}
376+
377+
/// Start a new read-only synchronous transaction.
378+
pub fn tx_sync(&self) -> Result<Tx<RoSync>, MdbxError> {
379+
self.inner
380+
.begin_ro_sync()
381+
.map(|tx| Tx::new(tx, self.fsi_cache.clone()))
382+
.map_err(MdbxError::Mdbx)
383+
}
384+
385+
/// Start a new read-write synchronous transaction.
386+
pub fn tx_rw_sync(&self) -> Result<Tx<RwSync>, MdbxError> {
395387
self.inner
396-
.begin_rw_txn()
388+
.begin_rw_sync()
397389
.map(|tx| Tx::new(tx, self.fsi_cache.clone()))
398390
.map_err(MdbxError::Mdbx)
399391
}
@@ -408,14 +400,14 @@ impl Deref for DatabaseEnv {
408400
}
409401

410402
impl HotKv for DatabaseEnv {
411-
type RoTx = Tx<RO>;
412-
type RwTx = Tx<RW>;
403+
type RoTx = Tx<Ro>;
404+
type RwTx = Tx<Rw>;
413405

414406
fn reader(&self) -> Result<Self::RoTx, HotKvError> {
415407
self.tx().map_err(HotKvError::from_err)
416408
}
417409

418410
fn writer(&self) -> Result<Self::RwTx, HotKvError> {
419-
self.tx_mut().map_err(HotKvError::from_err)
411+
self.tx_rw().map_err(HotKvError::from_err)
420412
}
421413
}

0 commit comments

Comments
 (0)