Skip to content

pins

pins #

The pins table: which living process holds which object, and how many times.

PinRow dataclass #

One process's hold on one object.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/pins.py
@dataclass(frozen=True, slots=True)
class PinRow:
    """One process's hold on one object."""

    hash: ContentHash
    owner_pid: int
    owner_boot: int
    count: int

acquire(db, hash, pid, boot) #

Count one more hold on hash by the owner (pid, boot).

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/pins.py
def acquire(db: sqlite3.Connection, hash: ContentHash, pid: int, boot: int) -> None:
    """Count one more hold on ``hash`` by the owner ``(pid, boot)``."""
    db.execute(
        "INSERT INTO pins (hash, owner_pid, owner_boot, count) VALUES (?, ?, ?, 1) "
        "ON CONFLICT(hash, owner_boot) DO UPDATE SET count = count + 1",
        (hash.hex, pid, boot),
    )

all_rows(db) #

Every pin.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/pins.py
def all_rows(db: sqlite3.Connection) -> list[PinRow]:
    """Every pin."""
    rows = db.execute("SELECT hash, owner_pid, owner_boot, count FROM pins").fetchall()
    return [_row(row) for row in rows]

delete(db, hash, boot) #

Forget the owner boot's holds on hash.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/pins.py
def delete(db: sqlite3.Connection, hash: ContentHash, boot: int) -> None:
    """Forget the owner ``boot``'s holds on ``hash``."""
    db.execute("DELETE FROM pins WHERE hash = ? AND owner_boot = ?", (hash.hex, boot))

on_object(db, hash) #

Every pin on hash.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/pins.py
def on_object(db: sqlite3.Connection, hash: ContentHash) -> list[PinRow]:
    """Every pin on ``hash``."""
    rows = db.execute(
        "SELECT hash, owner_pid, owner_boot, count FROM pins WHERE hash = ?", (hash.hex,)
    ).fetchall()
    return [_row(row) for row in rows]

release(db, hash, boot) #

Count one hold fewer on hash by the owner boot, dropping the row at zero.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/pins.py
def release(db: sqlite3.Connection, hash: ContentHash, boot: int) -> None:
    """Count one hold fewer on ``hash`` by the owner ``boot``, dropping the row at zero."""
    db.execute(
        "UPDATE pins SET count = count - 1 WHERE hash = ? AND owner_boot = ?",
        (hash.hex, boot),
    )
    db.execute(
        "DELETE FROM pins WHERE hash = ? AND owner_boot = ? AND count <= 0", (hash.hex, boot)
    )