Skip to content

objects

objects #

The objects table: every sealed tree, and the facts eviction ranks it by.

ObjectRow dataclass #

One sealed object and the facts eviction ranks it by.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
@dataclass(frozen=True, slots=True)
class ObjectRow:
    """One sealed object and the facts eviction ranks it by."""

    hash: ContentHash
    size_bytes: int
    compute_seconds: float
    score: float
    last_access: float
    created_at: float

all_rows(db) #

Every indexed object.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def all_rows(db: sqlite3.Connection) -> list[ObjectRow]:
    """Every indexed object."""
    return [_row(row) for row in db.execute(f"SELECT {_COLUMNS} FROM objects").fetchall()]

delete(db, hash) #

Forget hash entirely: its row, its names, and every pin on it.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def delete(db: sqlite3.Connection, hash: ContentHash) -> None:
    """Forget ``hash`` entirely: its row, its names, and every pin on it."""
    db.execute("DELETE FROM names WHERE hash = ?", (hash.hex,))
    db.execute("DELETE FROM pins WHERE hash = ?", (hash.hex,))
    db.execute("DELETE FROM objects WHERE hash = ?", (hash.hex,))

get(db, hash) #

The row for hash, if indexed.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def get(db: sqlite3.Connection, hash: ContentHash) -> ObjectRow | None:
    """The row for ``hash``, if indexed."""
    row = db.execute(f"SELECT {_COLUMNS} FROM objects WHERE hash = ?", (hash.hex,)).fetchone()
    return _row(row) if row else None

raise_compute(db, hash, seconds) #

Price hash at the costliest computation that ever produced it.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def raise_compute(db: sqlite3.Connection, hash: ContentHash, seconds: float) -> None:
    """Price ``hash`` at the costliest computation that ever produced it."""
    db.execute(
        "UPDATE objects SET compute_seconds = MAX(compute_seconds, ?) WHERE hash = ?",
        (seconds, hash.hex),
    )

seed(db, hash, size_bytes) #

Record hash as an object of size_bytes, unless already recorded.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def seed(db: sqlite3.Connection, hash: ContentHash, size_bytes: int) -> None:
    """Record ``hash`` as an object of ``size_bytes``, unless already recorded."""
    db.execute(
        f"INSERT INTO objects ({_COLUMNS}) VALUES (?, ?, 0.0, 1.0, ?, ?) "
        "ON CONFLICT(hash) DO NOTHING",
        (hash.hex, size_bytes, time.time(), time.time()),
    )

total_size(db) #

The bytes every indexed object adds up to.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def total_size(db: sqlite3.Connection) -> int:
    """The bytes every indexed object adds up to."""
    (total,) = db.execute("SELECT COALESCE(SUM(size_bytes), 0) FROM objects").fetchone()
    return int(total)

touch(db, hash, score, now) #

Record an access to hash with its updated use score.

Source code in capturegraph-lib/capturegraph/recipes/scratch/index/objects.py
def touch(db: sqlite3.Connection, hash: ContentHash, score: float, now: float) -> None:
    """Record an access to ``hash`` with its updated use ``score``."""
    db.execute(
        "UPDATE objects SET score = ?, last_access = ? WHERE hash = ?",
        (score, now, hash.hex),
    )