refactor(state): trash-teardown drop helper; simplify deferral-record parsing

This commit is contained in:
Teknium
2026-09-02 16:31:45 -07:00
parent d86ebe66c0
commit 96508304ec
2 changed files with 15 additions and 12 deletions
+5 -6
View File
@@ -366,7 +366,7 @@ class SessionSchemaMixin:
``_FTS_HOLDER_ESCALATE_SECONDS``, provably inactive orphan Desktop
backends are reaped and the holders re-checked."""
now = time.time()
record = None
record = {}
try:
row = cursor.execute(
"SELECT value FROM state_meta WHERE key = ? LIMIT 1", (FTS_REBUILD_DEFERRAL_KEY,),
@@ -376,13 +376,12 @@ class SessionSchemaMixin:
if isinstance(parsed, dict):
record = parsed
except (sqlite3.Error, TypeError, ValueError, json.JSONDecodeError):
record = None
record = {}
try:
first_seen = float((record or {}).get("first_seen", now))
attempts = int((record or {}).get("attempts", 0)) + 1
first_seen = float(record.get("first_seen", now))
attempts = int(record.get("attempts", 0)) + 1
except (TypeError, ValueError):
first_seen = now
attempts = 1
first_seen, attempts = now, 1
if first_seen > now or first_seen < 0:
first_seen = now
diagnostic = {
+10 -6
View File
@@ -318,10 +318,7 @@ class SessionSearchMixin:
(high_water,),
).fetchall()
if not upper_rows:
conn.execute(f"DROP TABLE IF EXISTS {tbl}")
_delete_meta(conn, marker_key)
logger.info("Old FTS shadow table %s torn down.", tbl)
return True
return _drop(conn, marker_key)
upper = upper_rows[-1][0]
cur = conn.execute(f"DELETE FROM {tbl} WHERE {key} > ? AND {key} <= ?", (high_water, upper))
if cur.rowcount > 0:
@@ -332,10 +329,17 @@ class SessionSearchMixin:
f"(SELECT {key} FROM {tbl} LIMIT {self._FTS_REBUILD_CHUNK_ROWS})"
)
if cur.rowcount == 0:
conn.execute(f"DROP TABLE IF EXISTS {tbl}")
logger.info("Old FTS shadow table %s torn down.", tbl)
return _drop(conn)
return True # re-check: more trash tables / chunks may remain
def _drop(conn, marker_key: Optional[str] = None) -> bool:
"""Drained — the DROP is cheap now. True: re-check for more trash."""
conn.execute(f"DROP TABLE IF EXISTS {tbl}")
if marker_key is not None:
_delete_meta(conn, marker_key)
logger.info("Old FTS shadow table %s torn down.", tbl)
return True
try:
return bool(self._execute_write(_do))
except sqlite3.OperationalError as exc: