Skip to content

Commit

Permalink
Merge pull request #117 from DataONEorg/feature-114-existing-hashstor…
Browse files Browse the repository at this point in the history
…efiles

Feature-114: `tag_object` Update for Existing HashStore Files
  • Loading branch information
doulikecookiedough authored Jun 25, 2024
2 parents 627a63b + 04f7379 commit 373a870
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
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

0 comments on commit 373a870

Please sign in to comment.