fix: implement soft-delete for ZipStore (issue #828)#4107
Open
mohammadZuherJaserAsad wants to merge 6 commits into
Open
fix: implement soft-delete for ZipStore (issue #828)#4107mohammadZuherJaserAsad wants to merge 6 commits into
mohammadZuherJaserAsad wants to merge 6 commits into
Conversation
Implement soft-delete via b"" overwrite. supports_deletes=True. Update _get, exists, list, delete, delete_dir.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4107 +/- ##
==========================================
- Coverage 93.53% 84.30% -9.24%
==========================================
Files 88 90 +2
Lines 11894 15415 +3521
==========================================
+ Hits 11125 12995 +1870
- Misses 769 2420 +1651
🚀 New features to boost your workflow:
|
Contributor
Hypothesis's stateful property tests caught two real bugs: - Setting a key to b"" (a legitimate zero-length value) was indistinguishable from a soft-deleted key, since the sentinel was also b"". list()/exists()/get() incorrectly treated it as missing. Switched to a long, specific sentinel that can't plausibly collide with real payload bytes. - delete() writes the sentinel to an already-existing zip entry, which zipfile.writestr() flags with a "Duplicate name" UserWarning every time. This project's pytest config turns warnings into errors, so any delete of an existing key failed the test suite. The warning is suppressed specifically in delete(), since it's expected/intentional there (unlike set() overwrites, which intentionally keep warning per test_api_integration). Also fixes the ruff lint failures (unused ZipStore import in test_stateful.py, one reformatted line) and adds filterwarnings markers to the stateful tests to account for the pre-existing, expected duplicate-name warning on ZipStore overwrites.
test: remove unused ZipStore import; expect duplicate-name warning in stateful tests
The previous soft-delete used a fixed sentinel byte string as entry data, which Slow Hypothesis CI proved can still collide with a legitimate value (Hypothesis found a falsifying example that set a key to the literal sentinel bytes). This replaces it with a ZipInfo.comment flag, which is metadata never exposed through set()/get() and therefore cannot collide with any data a caller stores, while still persisting the deletion to disk in the zip central directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #828 — implements soft-delete for
ZipStoreso thatdelete()anddelete_dir()no longer raiseNotImplementedError.The ZIP format has no native entry-removal API (a known CPython limitation: python/cpython#51067). This PR uses the soft-delete approach suggested by @dcherian in the issue: overwrite the entry with an empty byte sentinel (
b"") and filter it out in all read, exists, and list paths.Changes
src/zarr/storage/_zip.pysupports_deletes = True(wasFalse)_SOFT_DELETE_SENTINEL = b""constantdelete(key): writes sentinel to zip entry; no-op if key is absentdelete_dir(prefix): collects live keys under prefix, callsdelete()on each_get(): reads all bytes first; returnsNoneif content equals sentinelexists(): returnsFalsefor entries whose content equals sentinellist(): deduplicates entries (duplicate names can appear after overwrites) and skips sentinel entrieslist_dir(): consumes filteredlist()output for consistencytests/test_store/test_zip.pydelete_dir, andlist()filteringtest_api_integrationupdated: removed twopytest.raises(NotImplementedError)blocks that are no longer correcttests/test_store/test_stateful.pypytest.skip(reason="ZipStore does not support delete")blocksDesign notes
namelist()returns the name twice.NameToInfotracks the last-written entry, so reading always returns the sentinel.list()uses aseenset to yield each name at most once.delete_dir()holds theRLockwhile callinglist_prefix()and thendelete()(which also acquires it). This works becausethreading.RLockis re-entrant for the same thread.set_if_not_existsedge case: After a soft-delete the name is still innamelist(), soset_if_not_existswill not re-write it. This matches the documented semantics and existing zarr usage patterns.Testing
pytest tests/test_store/test_zip.py -v pytest tests/test_store/test_stateful.py -v -k "not slow_hypothesis"