Skip to content

names

names #

The names table: what references an object — a call's result or a state's current version.

all_rows(db) #

Every name and the object it points at.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def all_rows(db: sqlite3.Connection) -> dict[str, ContentHash]:
    """Every name and the object it points at."""
    rows = db.execute("SELECT name, hash FROM names").fetchall()
    return {name: ContentHash.from_hex(hash) for name, hash in rows}

call_name(key, position) #

The name holding return position of the call key.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def call_name(key: CallKey, position: int) -> str:
    """The name holding return ``position`` of the call ``key``."""
    return f"call:{key.hex}/{position}"

call_results(db, key) #

The objects a call's return positions point at, in position order; empty if ragged.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def call_results(db: sqlite3.Connection, key: CallKey) -> list[ContentHash]:
    """The objects a call's return positions point at, in position order; empty if ragged."""
    prefix = f"call:{key.hex}/"
    rows = db.execute(
        "SELECT name, hash FROM names WHERE name >= ? AND name < ?",
        (prefix, f"call:{key.hex}0"),
    ).fetchall()
    located = sorted((int(name.rsplit("/", 1)[1]), hash) for name, hash in rows)
    if any(index != position for index, (position, _) in enumerate(located)):
        return []
    return [ContentHash.from_hex(hash) for _, hash in located]

delete(db, name) #

Forget name.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def delete(db: sqlite3.Connection, name: str) -> None:
    """Forget ``name``."""
    db.execute("DELETE FROM names WHERE name = ?", (name,))

kept_hashes(db) #

Every object a kept name holds.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def kept_hashes(db: sqlite3.Connection) -> set[ContentHash]:
    """Every object a ``kept`` name holds."""
    rows = db.execute("SELECT DISTINCT hash FROM names WHERE kept != 0").fetchall()
    return {ContentHash.from_hex(hash) for (hash,) in rows}

point(db, name, hash, *, kept) #

Point name at hash; a kept name exempts its object from eviction.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def point(db: sqlite3.Connection, name: str, hash: ContentHash, *, kept: bool) -> None:
    """Point ``name`` at ``hash``; a ``kept`` name exempts its object from eviction."""
    db.execute(
        "INSERT INTO names (name, hash, kept, created_at) VALUES (?, ?, ?, ?) "
        "ON CONFLICT(name) DO UPDATE SET hash = excluded.hash, kept = excluded.kept, "
        "created_at = excluded.created_at",
        (name, hash.hex, int(kept), time.time()),
    )

resolve(db, name) #

The object name points at, if any.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def resolve(db: sqlite3.Connection, name: str) -> ContentHash | None:
    """The object ``name`` points at, if any."""
    row = db.execute("SELECT hash FROM names WHERE name = ?", (name,)).fetchone()
    return ContentHash.from_hex(row[0]) if row else None

state_name(key) #

The name holding the current version of the state key.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/names.py
def state_name(key: str) -> str:
    """The name holding the current version of the state ``key``."""
    return f"state:{key}"