Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copds 1939 cleaner #125

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cacholote/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ def __init__(self) -> None:
self.logger = config.get().logger
self.fs, self.dirname = utils.get_cache_files_fs_dirname()

urldir = self.fs.unstrip_protocol(self.dirname)
self.urldir = self.fs.unstrip_protocol(self.dirname)

self.logger.info("getting disk usage")
self.file_sizes: dict[str, int] = collections.defaultdict(int)
for path, size in self.fs.du(self.dirname, total=False).items():
# Group dirs
urlpath = self.fs.unstrip_protocol(path)
basename, *_ = urlpath.replace(urldir, "", 1).strip("/").split("/")
basename, *_ = urlpath.replace(self.urldir, "", 1).strip("/").split("/")
if basename:
self.file_sizes[posixpath.join(urldir, basename)] += size
self.file_sizes[posixpath.join(self.urldir, basename)] += size

self.disk_usage = sum(self.file_sizes.values())
self.log_disk_usage()
Expand Down Expand Up @@ -254,16 +254,16 @@ def delete_cache_files(
sa.select(database.CacheEntry).filter(*filters).order_by(*sorters)
):
files = _get_files_from_cache_entry(cache_entry)
if files:
if any(file.startswith(self.urldir) for file in files):
n_entries_to_delete += 1
session.delete(cache_entry)

for file, file_type in files.items():
self.pop_file_size(file)
if file_type == "application/vnd+zarr":
dirs_to_delete.append(file)
else:
files_to_delete.append(file)
for file, file_type in files.items():
self.pop_file_size(file)
if file_type == "application/vnd+zarr":
dirs_to_delete.append(file)
else:
files_to_delete.append(file)

if self.stop_cleaning(maxsize):
break
Expand Down
19 changes: 19 additions & 0 deletions tests/test_60_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,22 @@ def test_expire_cache_entries_created_at() -> None:
assert clean.expire_cache_entries(after=toc) == 0
assert clean.expire_cache_entries(before=toc) == 1
assert clean.expire_cache_entries(after=tic) == 1


def test_multiple(tmp_path: pathlib.Path) -> None:
oldpath = tmp_path / "old.txt"
oldpath.write_bytes(ONE_BYTE)
with config.set(cache_files_urlpath=str(tmp_path / "old")):
cached_oldpath = pathlib.Path(open_url(oldpath).path)
assert cached_oldpath.exists()

newpath = tmp_path / "new.txt"
newpath.write_bytes(ONE_BYTE)
with config.set(cache_files_urlpath=str(tmp_path / "new")):
cached_newpath = pathlib.Path(open_url(newpath).path)
assert cached_newpath.exists()

with config.set(cache_files_urlpath=str(tmp_path / "new")):
clean.clean_cache_files(0)
assert not cached_newpath.exists()
assert cached_oldpath.exists()