Skip to content

Commit f2308d6

Browse files
committed
Address review feedback: move defaults to Python, validate in Rust
- Move LMDB default values to Python side (lmdb_max_dbs=1024, lmdb_map_size=4GiB) so fields are never None, matching the pattern used by GlobalExecutionOptions.source_max_inflight_rows - Make Rust EnvironmentSettings fields non-optional (u32/usize) with serde default functions for backward compatibility - Add validation with client_bail! for invalid values (max_dbs < 1, map_size == 0) - Remove unused u32 import from std
1 parent ff5434b commit f2308d6

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

python/cocoindex/_internal/setting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Settings:
5858

5959
db_path: os.PathLike[str] | None = None
6060
global_execution_options: GlobalExecutionOptions | None = None
61-
lmdb_max_dbs: int | None = None
62-
lmdb_map_size: int | None = None
61+
lmdb_max_dbs: int = 1024
62+
lmdb_map_size: int = 0x1_0000_0000 # 4 GiB
6363

6464
@classmethod
6565
def from_env(cls, db_path: os.PathLike[str] | None = None) -> Self:

python/cocoindex/setting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Settings:
4646

4747
db_path: os.PathLike[str] | None = None
4848
global_execution_options: GlobalExecutionOptions | None = None
49-
lmdb_max_dbs: int | None = None
50-
lmdb_map_size: int | None = None
49+
lmdb_max_dbs: int = 1024
50+
lmdb_map_size: int = 0x1_0000_0000 # 4 GiB
5151

5252
@classmethod
5353
def from_env(cls, db_path: os.PathLike[str] | None = None) -> Self:

rust/core/src/engine/environment.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ use crate::{
33
};
44

55
use serde::{Deserialize, Serialize};
6-
use std::{collections::BTreeSet, path::PathBuf, u32};
6+
use std::{collections::BTreeSet, path::PathBuf};
77

88
const DEFAULT_MAX_DBS: u32 = 1024;
99
const DEFAULT_LMDB_MAP_SIZE: usize = 0x1_0000_0000; // 4GiB
1010

11+
fn default_max_dbs() -> u32 {
12+
DEFAULT_MAX_DBS
13+
}
14+
fn default_lmdb_map_size() -> usize {
15+
DEFAULT_LMDB_MAP_SIZE
16+
}
17+
1118
#[derive(Clone, Serialize, Deserialize, Debug)]
1219
pub struct EnvironmentSettings {
1320
pub db_path: PathBuf,
14-
#[serde(default)]
15-
pub lmdb_max_dbs: Option<u32>,
16-
#[serde(default)]
17-
pub lmdb_map_size: Option<usize>,
21+
#[serde(default = "default_max_dbs")]
22+
pub lmdb_max_dbs: u32,
23+
#[serde(default = "default_lmdb_map_size")]
24+
pub lmdb_map_size: usize,
1825
}
1926

2027
struct EnvironmentInner<Prof: EngineProfile> {
@@ -40,12 +47,16 @@ impl<Prof: EngineProfile> Environment<Prof> {
4047
std::fs::create_dir_all(&db_path)?;
4148
// Backward compatibility: migrate LMDB files from old layout into mdb/.
4249
Self::migrate_legacy_db_files(&settings.db_path, &db_path)?;
43-
let max_dbs = settings.lmdb_max_dbs.unwrap_or(DEFAULT_MAX_DBS);
44-
let map_size = settings.lmdb_map_size.unwrap_or(DEFAULT_LMDB_MAP_SIZE);
50+
if settings.lmdb_max_dbs < 1 {
51+
client_bail!("lmdb_max_dbs must be >= 1, got {}", settings.lmdb_max_dbs);
52+
}
53+
if settings.lmdb_map_size == 0 {
54+
client_bail!("lmdb_map_size must be > 0, got {}", settings.lmdb_map_size);
55+
}
4556
let db_env = unsafe {
4657
heed::EnvOpenOptions::new()
47-
.max_dbs(max_dbs)
48-
.map_size(map_size)
58+
.max_dbs(settings.lmdb_max_dbs)
59+
.map_size(settings.lmdb_map_size)
4960
.open(db_path)
5061
}?;
5162
let cleared_count = db_env.clear_stale_readers()?;

0 commit comments

Comments
 (0)