Skip to content

Commit

Permalink
new known files
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Oct 29, 2024
1 parent 988e67f commit b642e16
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
10 changes: 5 additions & 5 deletions cacholote/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ def stop_cleaning(self, maxsize: int) -> bool:
@property
def known_files(self) -> dict[str, int]:
known_files: dict[str, int] = {}
filters = [database.CacheFile.name.startswith(self.urldir)]
with config.get().instantiated_sessionmaker() as session:
for cache_entry in session.scalars(sa.select(database.CacheEntry)):
files = _get_files_from_cache_entry(cache_entry, key="file:size")
known_files.update(
{k: v for k, v in files.items() if k.startswith(self.urldir)}
)
for cache_file in session.scalars(
sa.select(database.CacheFile).filter(*filters)
):
known_files[cache_file.name] = cache_file.size
return known_files

def get_unknown_files(self, lock_validity_period: float | None) -> set[str]:
Expand Down
48 changes: 42 additions & 6 deletions cacholote/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import sqlalchemy.orm
import sqlalchemy_utils

from . import utils
from . import clean, utils

_DATETIME_MAX = datetime.datetime(
datetime.MAXYEAR, 12, 31, tzinfo=datetime.timezone.utc
Expand All @@ -55,8 +55,10 @@ class CacheEntry(Base):
updated_at = sa.Column(sa.DateTime, default=utils.utcnow, onupdate=utils.utcnow)
counter = sa.Column(sa.Integer)
tag = sa.Column(sa.String)
cache_files: sa.orm.Mapped[list[CacheFile]] = sa.orm.relationship(
secondary=association_table, back_populates="cache_entries"
cache_files: sa.orm.Mapped[set[CacheFile]] = sa.orm.relationship(
secondary=association_table,
back_populates="cache_entries",
cascade="all, save-update", # TODO
)

@property
Expand All @@ -82,11 +84,33 @@ def __repr__(self) -> str:
class CacheFile(Base):
__tablename__ = "cache_files"

name = sa.Column(sa.String(), primary_key=True)
cache_entries: sa.orm.Mapped[list[CacheEntry]] = sa.orm.relationship(
secondary=association_table, back_populates="cache_files"
name: str = sa.Column(sa.String(), primary_key=True)
size: int = sa.Column(sa.Integer())
cache_entries: sa.orm.Mapped[set[CacheEntry]] = sa.orm.relationship(
secondary=association_table,
back_populates="cache_files",
)

@property
def updated_at(self) -> datetime.datetime:
return max(
[
cache_entry.updated_at
for cache_entry in self.cache_entries
if cache_entry.updated_at
]
)

@property
def count(self) -> int:
return sum(
[
cache_entry.counter
for cache_entry in self.cache_entries
if cache_entry.counter
]
)


@sa.event.listens_for(CacheEntry, "before_insert")
def set_expiration_to_max(
Expand All @@ -99,6 +123,18 @@ def set_expiration_to_max(
warnings.warn(f"Expiration date has passed. {target.expiration=}", UserWarning)


@sa.event.listens_for(CacheEntry, "after_insert")
def add_cache_files(
mapper: sa.orm.Mapper[CacheEntry],
connection: sa.Connection,
target: CacheEntry,
) -> None:
for name, size in clean._get_files_from_cache_entry(
target, key="file:size"
).items():
target.cache_files.add(CacheFile(name=name, size=size))


def _commit_or_rollback(session: sa.orm.Session) -> None:
try:
session.commit()
Expand Down

0 comments on commit b642e16

Please sign in to comment.