Skip to content

hold

hold #

Hold — the object one name holds, pinned for as long as the hold lives.

Hold #

One name and the object it points at, pinned so eviction spares it.

Source code in capturegraph-lib/capturegraph/recipes/scratch/store/hold.py
class Hold:
    """One name and the object it points at, pinned so eviction spares it."""

    def __init__(self, scratch: Scratch, name: str) -> None:
        """Hold whatever ``name`` points at right now, if it is still on disk."""
        self.name = name
        self._scratch = scratch
        self._pin = _named(scratch, name)

    @property
    def hash(self) -> ContentHash | None:
        """The object held, or ``None`` if the name holds nothing that is still there."""
        pin = self._pin
        return pin.hash if pin is not None and pin.path.is_dir() else None

    def point(self, hash: ContentHash) -> None:
        """Make ``hash`` what the name holds, letting go of the object it replaces."""
        if self._pin is not None and self._pin.hash == hash:
            return
        taken = Pinned(self._scratch, hash)
        with self._scratch.database.transaction() as db:
            names.point(db, self.name, hash, kept=False)
        self.release()
        self._pin = taken

    def release(self) -> None:
        """Let go of the object; the name goes on pointing at it."""
        if self._pin is not None:
            self._pin.release()
            self._pin = None

    def __repr__(self) -> str:
        """``Hold(<name>)``."""
        return f"Hold({self.name!r})"

hash property #

The object held, or None if the name holds nothing that is still there.

__init__(scratch, name) #

Hold whatever name points at right now, if it is still on disk.

Source code in capturegraph-lib/capturegraph/recipes/scratch/store/hold.py
def __init__(self, scratch: Scratch, name: str) -> None:
    """Hold whatever ``name`` points at right now, if it is still on disk."""
    self.name = name
    self._scratch = scratch
    self._pin = _named(scratch, name)

__repr__() #

Hold(<name>).

Source code in capturegraph-lib/capturegraph/recipes/scratch/store/hold.py
def __repr__(self) -> str:
    """``Hold(<name>)``."""
    return f"Hold({self.name!r})"

point(hash) #

Make hash what the name holds, letting go of the object it replaces.

Source code in capturegraph-lib/capturegraph/recipes/scratch/store/hold.py
def point(self, hash: ContentHash) -> None:
    """Make ``hash`` what the name holds, letting go of the object it replaces."""
    if self._pin is not None and self._pin.hash == hash:
        return
    taken = Pinned(self._scratch, hash)
    with self._scratch.database.transaction() as db:
        names.point(db, self.name, hash, kept=False)
    self.release()
    self._pin = taken

release() #

Let go of the object; the name goes on pointing at it.

Source code in capturegraph-lib/capturegraph/recipes/scratch/store/hold.py
def release(self) -> None:
    """Let go of the object; the name goes on pointing at it."""
    if self._pin is not None:
        self._pin.release()
        self._pin = None