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.

clew/fasttier_defs.py:297

Show on the map →

Called by _apply

Calls _insert_recovered, _owner, module_of

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
lines 307–307from .ast_symbols import ParsedFunction, _insert_recoveredlines 308–308from .synth import _Writer, module_oflines 310–310writer = _Writer(conn) if js else Nonelines 311–311◇ for record in addedlines 312–312◇ if writer is not Nonelines 313–313owner = _owner(conn, path_rowid, str(record.get("cls") or ""))lines 314–314writer._function(path_rowid, module_of(rel), record, owner, "js")lines 315–315elselines 316–316r = recordlines 317–319fn = ParsedFunction( name=r[0], qualified=r[1], args=r[2], static=bool(r[3]), start=r[4], end=r[5] )lines 320–320_insert_recovered(conn, path_rowid, fn)lines 321–321return [str(r["name"] if js else r[0]) for r in added]

Requirements

REQ-DDB-INDEX-006

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.

clew/fasttier_defs.py:201

Show on the map →

Called by _apply

Calls none

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
lines 209–209from .jsast import MODULE_CALLERlines 211–211◇ if lineslines 212–215conn.execute( "UPDATE memberdef SET bodyend = ? WHERE name = ? AND bodyfile_id = ?", (lines, MODULE_CALLER, path_rowid), )

Requirements

REQ-DDB-INDEX-006

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.

clew/fasttier_defs.py:163

Show on the map →

Called by _line_anchors

Calls _function_records, all

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
lines 172–172old_map, new_map = dict(old), dict(new)lines 173–173differing = set(old_map) ^ set(new_map)lines 174–174pairs = sorted({(old_map[k], new_map[k]) for k in set(old_map) & set(new_map)})lines 175–175ordered = all(b[1] >= a[1] for a, b in zip(pairs, pairs[1:]))lines 176–176◇ if any(k[0] != _FUNCTIONS for k in differing) or not orderedlines 177–177return Nonelines 178–178gone = {(k[1], k[3]) for k in set(old_map) - set(new_map)}lines 179–179came = {(k[1], k[3]) for k in set(new_map) - set(old_map)}lines 180–183old_records, new_records = ( _function_records(js, old_payload), _function_records(js, new_payload), )lines 184–190changes = 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, )lines 191–191return [o for o, _n in pairs], [n for _o, n in pairs], changes

Requirements

REQ-DDB-INDEX-006

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.

clew/fasttier_defs.py:247

Show on the map →

Called by _apply

Calls _rows_of

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)

Activity · 21 steps
lines 255–255ids: list[int] = []lines 256–256◇ for record in removedlines 257–257found = _rows_of(conn, path_rowid, js, record)lines 258–258◇ if not foundlines 259–259raise FastRefusal(f"no row for removed function {record}")lines 260–260ids += foundlines 261–261◇ if not idslines 262–262return 0lines 263–263marks = ",".join("?" * len(ids))lines 264–264tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")}lines 265–270◇ if (lines 272–272raise FastRefusal("a removed function is a thread's entry point")lines 273–273◇ if {"lock_acquisitions", "critical_section_calls"} <= tableslines 274–278conn.execute( "DELETE FROM critical_section_calls WHERE acquisition_id IN " # noqa: S608 f"(SELECT id FROM lock_acquisitions WHERE holder_rowid IN ({marks}))", ids, )lines 279–279◇ for table, columns in MEMBERDEF_REFERENCES.items()lines 280–280◇ if table in tableslines 281–281◇ for column in columnslines 282–282conn.execute(f"DELETE FROM {table} WHERE {column} IN ({marks})", ids)lines 283–283conn.execute(f"DELETE FROM memberdef WHERE rowid IN ({marks})", ids)lines 284–284conn.execute(f"DELETE FROM refid WHERE rowid IN ({marks})", ids)lines 285–285return len(ids)

Requirements

REQ-DDB-INDEX-006