mirror of
https://github.com/xpltdco/media-rip.git
synced 2026-04-03 02:53:58 -06:00
Compare commits
3 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70910d516e | ||
|
|
6682438163 | ||
|
|
3205c101c3 |
1 changed files with 70 additions and 8 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
"""SQLite database layer with WAL mode and async CRUD operations.
|
"""SQLite database layer with async CRUD operations.
|
||||||
|
|
||||||
Uses aiosqlite for async access. ``init_db`` sets critical PRAGMAs
|
Uses aiosqlite for async access. ``init_db`` sets critical PRAGMAs
|
||||||
(busy_timeout, WAL, synchronous) *before* creating any tables so that
|
(busy_timeout, journal_mode, synchronous) *before* creating any tables so
|
||||||
concurrent download workers never hit ``SQLITE_BUSY``.
|
that concurrent download workers never hit ``SQLITE_BUSY``. WAL mode is
|
||||||
|
preferred on local filesystems; DELETE mode is used automatically when a
|
||||||
|
network filesystem (CIFS, NFS) is detected.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
@ -90,19 +92,31 @@ async def init_db(db_path: str) -> aiosqlite.Connection:
|
||||||
|
|
||||||
PRAGMA order matters:
|
PRAGMA order matters:
|
||||||
1. ``busy_timeout`` — prevents immediate ``SQLITE_BUSY`` on lock contention
|
1. ``busy_timeout`` — prevents immediate ``SQLITE_BUSY`` on lock contention
|
||||||
2. ``journal_mode=WAL`` — enables concurrent readers + single writer
|
2. ``journal_mode`` — WAL for local filesystems, DELETE for network mounts
|
||||||
3. ``synchronous=NORMAL`` — safe durability level for WAL mode
|
(CIFS/NFS lack the shared-memory primitives WAL requires)
|
||||||
|
3. ``synchronous=NORMAL`` — safe durability level
|
||||||
|
|
||||||
Returns the ready-to-use connection.
|
Returns the ready-to-use connection.
|
||||||
"""
|
"""
|
||||||
|
# Detect network filesystem *before* opening the DB so we never attempt
|
||||||
|
# WAL on CIFS/NFS (which creates broken SHM files that persist).
|
||||||
|
use_wal = not _is_network_filesystem(db_path)
|
||||||
|
|
||||||
db = await aiosqlite.connect(db_path)
|
db = await aiosqlite.connect(db_path)
|
||||||
db.row_factory = aiosqlite.Row
|
db.row_factory = aiosqlite.Row
|
||||||
|
|
||||||
# --- PRAGMAs (before any DDL) ---
|
# --- PRAGMAs (before any DDL) ---
|
||||||
await db.execute("PRAGMA busy_timeout = 5000")
|
await db.execute("PRAGMA busy_timeout = 5000")
|
||||||
result = await db.execute("PRAGMA journal_mode = WAL")
|
|
||||||
row = await result.fetchone()
|
if use_wal:
|
||||||
journal_mode = row[0] if row else "unknown"
|
journal_mode = await _try_journal_mode(db, "wal")
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
"Network filesystem detected for %s — using DELETE journal mode",
|
||||||
|
db_path,
|
||||||
|
)
|
||||||
|
journal_mode = await _try_journal_mode(db, "delete")
|
||||||
|
|
||||||
logger.info("journal_mode set to %s", journal_mode)
|
logger.info("journal_mode set to %s", journal_mode)
|
||||||
|
|
||||||
await db.execute("PRAGMA synchronous = NORMAL")
|
await db.execute("PRAGMA synchronous = NORMAL")
|
||||||
|
|
@ -115,6 +129,54 @@ async def init_db(db_path: str) -> aiosqlite.Connection:
|
||||||
return db
|
return db
|
||||||
|
|
||||||
|
|
||||||
|
def _is_network_filesystem(db_path: str) -> bool:
|
||||||
|
"""Return True if *db_path* resides on a network filesystem (CIFS, NFS, etc.).
|
||||||
|
|
||||||
|
Parses ``/proc/mounts`` (Linux) to find the filesystem type of the
|
||||||
|
longest-prefix mount matching the database directory. Returns False
|
||||||
|
on non-Linux hosts or if detection fails.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
|
||||||
|
network_fs_types = {"cifs", "nfs", "nfs4", "smb", "smbfs", "9p", "fuse.sshfs"}
|
||||||
|
try:
|
||||||
|
db_dir = os.path.dirname(os.path.abspath(db_path))
|
||||||
|
with open("/proc/mounts", "r") as f:
|
||||||
|
mounts = f.readlines()
|
||||||
|
best_match = ""
|
||||||
|
best_fstype = ""
|
||||||
|
for line in mounts:
|
||||||
|
parts = line.split()
|
||||||
|
if len(parts) < 3:
|
||||||
|
continue
|
||||||
|
mountpoint, fstype = parts[1], parts[2]
|
||||||
|
if db_dir.startswith(mountpoint) and len(mountpoint) > len(best_match):
|
||||||
|
best_match = mountpoint
|
||||||
|
best_fstype = fstype
|
||||||
|
is_net = best_fstype in network_fs_types
|
||||||
|
if is_net:
|
||||||
|
logger.info(
|
||||||
|
"Detected %s filesystem at %s for database %s",
|
||||||
|
best_fstype, best_match, db_path,
|
||||||
|
)
|
||||||
|
return is_net
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _try_journal_mode(
|
||||||
|
db: aiosqlite.Connection, mode: str,
|
||||||
|
) -> str:
|
||||||
|
"""Try setting *mode* and return the actual journal mode string."""
|
||||||
|
try:
|
||||||
|
result = await db.execute(f"PRAGMA journal_mode = {mode}")
|
||||||
|
row = await result.fetchone()
|
||||||
|
return (row[0] if row else "unknown").lower()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("PRAGMA journal_mode=%s failed: %s", mode, exc)
|
||||||
|
return "error"
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# CRUD helpers
|
# CRUD helpers
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue