Skip to content

Commit ccfa004

Browse files
PR feedback fixes
1 parent a77d767 commit ccfa004

2 files changed

Lines changed: 60 additions & 24 deletions

File tree

temoa/core/config.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,48 @@ def __init__(
161161
self.output_threshold_cost = output_threshold_cost
162162
self.sqlite_inputs = sqlite or {}
163163

164-
# SQLite performance defaults
165-
self.sqlite_journal_mode = str(self.sqlite_inputs.get('journal_mode', 'WAL'))
166-
self.sqlite_synchronous = str(self.sqlite_inputs.get('synchronous', 'NORMAL'))
167-
self.sqlite_temp_store = str(self.sqlite_inputs.get('temp_store', 'MEMORY'))
168-
self.sqlite_mmap_size = int(self.sqlite_inputs.get('mmap_size', 8589934592))
169-
self.sqlite_cache_size = int(self.sqlite_inputs.get('cache_size', -512000))
164+
# SQLite performance settings
165+
# journal_mode: DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
166+
jm_allowed = {'DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'}
167+
jm = self.sqlite_inputs.get('journal_mode', 'WAL')
168+
if isinstance(jm, str) and jm.upper() in jm_allowed:
169+
self.sqlite_journal_mode: str | int = jm.upper()
170+
elif isinstance(jm, (int, float, str)) and str(jm).isdigit():
171+
self.sqlite_journal_mode = int(jm)
172+
else:
173+
self.sqlite_journal_mode = 'WAL'
174+
175+
# synchronous: OFF (0) | NORMAL (1) | FULL (2) | EXTRA (3)
176+
sync_allowed = {'OFF', 'NORMAL', 'FULL', 'EXTRA'}
177+
sync = self.sqlite_inputs.get('synchronous', 'NORMAL')
178+
if isinstance(sync, str) and sync.upper() in sync_allowed:
179+
self.sqlite_synchronous: str | int = sync.upper()
180+
elif isinstance(sync, (int, float, str)) and str(sync).isdigit():
181+
self.sqlite_synchronous = int(sync)
182+
else:
183+
self.sqlite_synchronous = 'NORMAL'
184+
185+
# temp_store: DEFAULT (0) | FILE (1) | MEMORY (2)
186+
temp_allowed = {'DEFAULT', 'FILE', 'MEMORY'}
187+
ts = self.sqlite_inputs.get('temp_store', 'MEMORY')
188+
if isinstance(ts, str) and ts.upper() in temp_allowed:
189+
self.sqlite_temp_store: str | int = ts.upper()
190+
elif isinstance(ts, (int, float, str)) and str(ts).isdigit():
191+
self.sqlite_temp_store = int(ts)
192+
else:
193+
self.sqlite_temp_store = 'MEMORY'
194+
195+
mmap_size = self.sqlite_inputs.get('mmap_size', 8589934592)
196+
if isinstance(mmap_size, (int, float, str)):
197+
self.sqlite_mmap_size = int(mmap_size)
198+
else:
199+
self.sqlite_mmap_size = 8589934592
200+
201+
cache_size = self.sqlite_inputs.get('cache_size', -512000)
202+
if isinstance(cache_size, (int, float, str)):
203+
self.sqlite_cache_size = int(cache_size)
204+
else:
205+
self.sqlite_cache_size = -512000
170206

171207
# Cycle detection limits
172208
if not isinstance(cycle_count_limit, int) or cycle_count_limit < -1:
@@ -317,8 +353,11 @@ def __repr__(self) -> str:
317353
msg += spacer
318354
msg += '{:>{}s}: {}\n'.format('SQLite journal mode', width, self.sqlite_journal_mode)
319355
msg += '{:>{}s}: {}\n'.format('SQLite synchronous', width, self.sqlite_synchronous)
356+
msg += '{:>{}s}: {}\n'.format('SQLite temp store', width, self.sqlite_temp_store)
320357
msg += '{:>{}s}: {}\n'.format('SQLite mmap size (bytes)', width, self.sqlite_mmap_size)
321-
msg += '{:>{}s}: {}\n'.format('SQLite cache size (pages)', width, self.sqlite_cache_size)
358+
msg += '{:>{}s}: {}\n'.format(
359+
'SQLite cache size (pages or KiB if negative)', width, self.sqlite_cache_size
360+
)
322361

323362
msg += spacer
324363
msg += '{:>{}s}: {}\n'.format('Time sequencing', width, self.time_sequencing)

temoa/utilities/sqlite_utils.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,17 @@ def tune_sqlite_connection(con: sqlite3.Connection, config: 'TemoaConfig | None'
3333
mmap_size = getattr(config, 'sqlite_mmap_size', mmap_size)
3434
cache_size = getattr(config, 'sqlite_cache_size', cache_size)
3535

36-
try:
37-
con.execute(f'PRAGMA journal_mode = {journal_mode}')
38-
con.execute(f'PRAGMA synchronous = {synchronous}')
39-
con.execute(f'PRAGMA temp_store = {temp_store}')
40-
con.execute(f'PRAGMA mmap_size = {mmap_size}')
41-
con.execute(f'PRAGMA cache_size = {cache_size}')
42-
logger.debug(
43-
'SQLite tuned: journal_mode=%s, synchronous=%s, temp_store=%s, '
44-
'mmap_size=%d, cache_size=%d',
45-
journal_mode,
46-
synchronous,
47-
temp_store,
48-
mmap_size,
49-
cache_size,
50-
)
51-
except sqlite3.Error as e:
52-
logger.warning('Failed to apply some SQLite performance PRAGMAs: %s', e)
36+
pragmas = [
37+
('journal_mode', journal_mode),
38+
('synchronous', synchronous),
39+
('temp_store', temp_store),
40+
('mmap_size', mmap_size),
41+
('cache_size', cache_size),
42+
]
43+
44+
for name, value in pragmas:
45+
try:
46+
con.execute(f'PRAGMA {name} = {value}')
47+
logger.debug('Applied SQLite PRAGMA: %s = %s', name, value)
48+
except sqlite3.Error as e:
49+
logger.warning('Failed to apply SQLite PRAGMA %s: %s', name, e)

0 commit comments

Comments
 (0)