Skip to content

Commit 1b106c6

Browse files
committed
lmdb: add max_readers and additional_dbs fields to NostrLmdbBuilder
1 parent dd8328d commit 1b106c6

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

database/nostr-lmdb/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
### Added
3333

3434
- Add NostrLmdbBuilder and allow setting a custom map size (https://github.com/rust-nostr/nostr/pull/970)
35+
- Add `max_readers` and `additional_dbs` fields to `NostrLmdbBuilder`
3536

3637
## v0.42.0 - 2025/05/20
3738

database/nostr-lmdb/src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ pub struct NostrLmdbBuilder {
3737
/// - 32GB for 64-bit arch
3838
/// - 4GB for 32-bit arch
3939
pub map_size: Option<usize>,
40+
/// Maximum number of reader threads
41+
///
42+
/// Defaults to 126 if not set
43+
pub max_readers: Option<u32>,
44+
/// Number of additional databases to allocate beyond the 9 internal ones
45+
///
46+
/// Defaults to 0 if not set
47+
pub additional_dbs: Option<u32>,
4048
}
4149

4250
impl NostrLmdbBuilder {
@@ -48,6 +56,8 @@ impl NostrLmdbBuilder {
4856
Self {
4957
path: path.as_ref().to_path_buf(),
5058
map_size: None,
59+
max_readers: None,
60+
additional_dbs: None,
5161
}
5262
}
5363

@@ -61,10 +71,29 @@ impl NostrLmdbBuilder {
6171
self
6272
}
6373

74+
/// Maximum number of reader threads
75+
///
76+
/// Defaults to 126 if not set
77+
pub fn max_readers(mut self, max_readers: u32) -> Self {
78+
self.max_readers = Some(max_readers);
79+
self
80+
}
81+
82+
/// Number of additional databases to allocate beyond the 9 internal ones
83+
///
84+
/// Defaults to 0 if not set
85+
pub fn additional_dbs(mut self, additional_dbs: u32) -> Self {
86+
self.additional_dbs = Some(additional_dbs);
87+
self
88+
}
89+
6490
/// Build
6591
pub fn build(self) -> Result<NostrLMDB, DatabaseError> {
6692
let map_size: usize = self.map_size.unwrap_or(MAP_SIZE);
67-
let db: Store = Store::open(self.path, map_size).map_err(DatabaseError::backend)?;
93+
let max_readers: u32 = self.max_readers.unwrap_or(126);
94+
let additional_dbs: u32 = self.additional_dbs.unwrap_or(0);
95+
let db: Store = Store::open(self.path, map_size, max_readers, additional_dbs)
96+
.map_err(DatabaseError::backend)?;
6897
Ok(NostrLMDB { db })
6998
}
7099
}

database/nostr-lmdb/src/store/lmdb/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,21 @@ pub(crate) struct Lmdb {
4848
}
4949

5050
impl Lmdb {
51-
pub(super) fn new<P>(path: P, map_size: usize) -> Result<Self, Error>
51+
pub(super) fn new<P>(
52+
path: P,
53+
map_size: usize,
54+
max_readers: u32,
55+
additional_dbs: u32,
56+
) -> Result<Self, Error>
5257
where
5358
P: AsRef<Path>,
5459
{
5560
// Construct LMDB env
5661
let env: Env = unsafe {
5762
EnvOpenOptions::new()
5863
.flags(EnvFlags::NO_TLS)
59-
.max_dbs(9)
64+
.max_dbs(9 + additional_dbs)
65+
.max_readers(max_readers)
6066
.map_size(map_size)
6167
.open(path)?
6268
};

database/nostr-lmdb/src/store/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ pub struct Store {
2727
}
2828

2929
impl Store {
30-
pub(super) fn open<P>(path: P, map_size: usize) -> Result<Store, Error>
30+
pub(super) fn open<P>(
31+
path: P,
32+
map_size: usize,
33+
max_readers: u32,
34+
additional_dbs: u32,
35+
) -> Result<Store, Error>
3136
where
3237
P: AsRef<Path>,
3338
{
@@ -36,7 +41,7 @@ impl Store {
3641
// Create the directory if it doesn't exist
3742
fs::create_dir_all(path)?;
3843

39-
let db: Lmdb = Lmdb::new(path, map_size)?;
44+
let db: Lmdb = Lmdb::new(path, map_size, max_readers, additional_dbs)?;
4045
let ingester: Sender<IngesterItem> = Ingester::run(db.clone());
4146

4247
Ok(Self { db, ingester })

0 commit comments

Comments
 (0)