fasttier_defs.py¶
clew/fasttier_defs.py
The fast tier's definition reconciliation: added, removed and renamed functions (PR1; 2.0.0).
add_definitions
list[str] clew.fasttier_defs.add_definitions(sqlite3.Connection conn, int path_rowid, str rel, bool js, list[Any] added)
Insert added functions with the full build's own writer.
Source · 27 lines
def add_definitions(
conn: sqlite3.Connection, path_rowid: int, rel: str, js: bool, added: list[Any]
) -> list[str]:
"""THE SAME WRITERS THE FULL BUILD USES, so a provisional row is shaped as the full build's
row will be. A C-family or Python function is written as an `ast`-sourced row, which is what
the full build writes for a function doxygen has not seen; its doxygen row replaces it then.
@brief Add functions.
@return Names.
"""
from .ast_symbols import ParsedFunction, _insert_recovered
from .synth import _Writer, module_of
writer = _Writer(conn) if js else None
for record in added:
if writer is not None:
owner = _owner(conn, path_rowid, str(record.get("cls") or ""))
writer._function(path_rowid, module_of(rel), record, owner, "js")
else:
r = record
fn = ParsedFunction(
name=r[0], qualified=r[1], args=r[2], static=bool(r[3]), start=r[4], end=r[5]
)
_insert_recovered(conn, path_rowid, fn)
return [str(r["name"] if js else r[0]) for r in added]
Activity · 12 steps
Requirements
end_module_row
None clew.fasttier_defs.end_module_row(sqlite3.Connection conn, int path_rowid, int|None lines)
End a JS/TS file's __module__ row at the file's new last line.
Source · 17 lines
def end_module_row(conn: sqlite3.Connection, path_rowid: int, lines: int | None) -> None:
"""FOUND BY `incremental_probe fastcompare` on mattermost-mobile: the module row spans the
whole file, and the end of the file sits past every anchor, so the line map left it at the old
length (8 where a full build has 12). Any length-changing edit did this, not only an add.
@brief Re-end the module row.
@return None.
"""
from .jsast import MODULE_CALLER
if lines:
conn.execute(
"UPDATE memberdef SET bodyend = ? WHERE name = ? AND bodyfile_id = ?",
(lines, MODULE_CALLER, path_rowid),
)
Activity · 3 steps
Requirements
reconcile
tuple[list[int], list[int], DefinitionChanges] | None clew.fasttier_defs.reconcile(bool js, Any old_payload, Any new_payload, list old, list new)
Line map and function changes between a file's old and new anchors.
Source · 31 lines
def reconcile(
js: bool, old_payload: Any, new_payload: Any, old: list, new: list
) -> tuple[list[int], list[int], DefinitionChanges] | None:
"""ONLY FUNCTIONS MAY DIFFER, and the matched anchors must stay in order: a reshuffled set of
same-name overloads would map one body onto another's lines.
@brief Reconcile one file.
@return The plan, or None.
"""
old_map, new_map = dict(old), dict(new)
differing = set(old_map) ^ set(new_map)
pairs = sorted({(old_map[k], new_map[k]) for k in set(old_map) & set(new_map)})
ordered = all(b[1] >= a[1] for a, b in zip(pairs, pairs[1:]))
if any(k[0] != _FUNCTIONS for k in differing) or not ordered:
return None
gone = {(k[1], k[3]) for k in set(old_map) - set(new_map)}
came = {(k[1], k[3]) for k in set(new_map) - set(old_map)}
old_records, new_records = (
_function_records(js, old_payload),
_function_records(js, new_payload),
)
changes = DefinitionChanges(
added=[new_records[i] for i in sorted(came) if i in new_records],
removed=[old_records[i] for i in sorted(gone) if i in old_records],
module_lines=(int(new_payload.get("lines") or 0) or None)
if js and isinstance(new_payload, dict)
else None,
)
return [o for o, _n in pairs], [n for _o, n in pairs], changes
Activity · 11 steps
Requirements
remove_definitions
int clew.fasttier_defs.remove_definitions(sqlite3.Connection conn, int path_rowid, bool js, list[Any] removed)
Delete removed functions and every row referencing them.
Source · 41 lines
def remove_definitions(
conn: sqlite3.Connection, path_rowid: int, js: bool, removed: list[Any]
) -> int:
"""BEFORE THE SHIFT, while the rows still sit at the lines the old payload names.
@brief Remove functions.
@return Count.
"""
ids: list[int] = []
for record in removed:
found = _rows_of(conn, path_rowid, js, record)
if not found:
raise FastRefusal(f"no row for removed function {record}")
ids += found
if not ids:
return 0
marks = ",".join("?" * len(ids))
tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")}
if (
"threads" in tables
and conn.execute(
f"SELECT 1 FROM threads WHERE entry_memberdef_rowid IN ({marks}) LIMIT 1",
ids, # noqa: S608
).fetchone()
):
raise FastRefusal("a removed function is a thread's entry point")
if {"lock_acquisitions", "critical_section_calls"} <= tables:
conn.execute(
"DELETE FROM critical_section_calls WHERE acquisition_id IN " # noqa: S608
f"(SELECT id FROM lock_acquisitions WHERE holder_rowid IN ({marks}))",
ids,
)
for table, columns in MEMBERDEF_REFERENCES.items():
if table in tables:
for column in columns:
conn.execute(f"DELETE FROM {table} WHERE {column} IN ({marks})", ids) # noqa: S608
conn.execute(f"DELETE FROM memberdef WHERE rowid IN ({marks})", ids) # noqa: S608
conn.execute(f"DELETE FROM refid WHERE rowid IN ({marks})", ids) # noqa: S608
return len(ids)