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

Feature-114: tag_object Update for Existing HashStore Files #117

Merged
merged 3 commits into from
Jun 25, 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
8 changes: 7 additions & 1 deletion src/hashstore/filehashstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CidRefsContentError,
CidRefsDoesNotExist,
CidRefsFileNotFound,
HashStoreRefsAlreadyExists,
NonMatchingChecksum,
NonMatchingObjSize,
PidAlreadyExistsError,
Expand Down Expand Up @@ -639,7 +640,12 @@ def tag_object(self, pid, cid):
cid_refs_path,
"Refs file already exists, verifying.",
)
return True
error_msg = (
f"FileHashStore - tag_object: Object with cid: {cid}"
+ f" already exists and is tagged with pid: {pid}"
)
logging.error(error_msg)
raise HashStoreRefsAlreadyExists(error_msg)
elif os.path.exists(pid_refs_path) and not os.path.exists(cid_refs_path):
debug_msg = (
f"FileHashStore - tag_object: pid refs file exists ({pid_refs_path})"
Expand Down
8 changes: 8 additions & 0 deletions src/hashstore/filehashstore_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def __init__(self, message, errors=None):
self.errors = errors


class HashStoreRefsAlreadyExists(Exception):
"""Custom exception thrown when called to tag an object that is already tagged appropriately."""

def __init__(self, message, errors=None):
super().__init__(message)
self.errors = errors


class UnsupportedAlgorithm(Exception):
"""Custom exception thrown when a given algorithm is not supported in HashStore for
calculating hashes/checksums, as the default store algo and/or other operations."""
Expand Down
6 changes: 5 additions & 1 deletion tests/test_filehashstore_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,11 @@ def test_store_object_duplicates_threads(pids, store):
entity = "objects"

def store_object_wrapper(obj_pid, obj_path):
store.store_object(obj_pid, obj_path) # Call store_object inside the thread
try:
store.store_object(obj_pid, obj_path) # Call store_object inside the thread
# pylint: disable=W0718
except Exception as e:
assert type(e).__name__ == "HashStoreRefsAlreadyExists"

thread1 = Thread(target=store_object_wrapper, args=(pid, path))
thread2 = Thread(target=store_object_wrapper, args=(pid, path))
Expand Down
5 changes: 4 additions & 1 deletion tests/test_filehashstore_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from hashstore.filehashstore_exceptions import (
CidRefsContentError,
CidRefsFileNotFound,
HashStoreRefsAlreadyExists,
NonMatchingChecksum,
NonMatchingObjSize,
PidAlreadyExistsError,
Expand Down Expand Up @@ -86,7 +87,9 @@ def test_tag_object_pid_refs_found_cid_refs_found(pids, store):
object_metadata = store.store_object(None, path)
cid = object_metadata.cid
store.tag_object(pid, cid)
store.tag_object(pid, cid)

with pytest.raises(HashStoreRefsAlreadyExists):
store.tag_object(pid, cid)

cid_refs_file_path = store._resolve_path("cid", object_metadata.cid)
line_count = 0
Expand Down
Loading