Skip to content

Commit

Permalink
Refactor 'unTagObject' by extracting method 'removePidFromCidRefsAndD…
Browse files Browse the repository at this point in the history
…etermineDeletion'
  • Loading branch information
doulikecookiedough committed Sep 5, 2024
1 parent fe772b5 commit 8264ed6
Showing 1 changed file with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1753,20 +1753,7 @@ protected void unTagObject(String pid, String cid)
// Begin deletion process
addAndRenamePidRefsFileToDeleteList(pid, deleteList, absPidRefsPath);

try {
updateRefsFile(pid, absCidRefsPath, HashStoreRefUpdateTypes.remove);
if (Files.size(absCidRefsPath) == 0) {
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absCidRefsPath));
} else {
String warnMsg = "Cid referenced by pid: " + pid
+ " is not empty (refs exist for cid). Skipping object " + "deletion.";
logFileHashStore.warn(warnMsg);
}
} catch (Exception e) {
logFileHashStore.warn(
"Unable to remove pid: " + pid + " from cid refs file: " + absCidRefsPath +
". " + e.getMessage());
}
removePidFromCidRefsAndDetermineDeletion(pid, deleteList, absCidRefsPath);

deleteListOfFilesRenamedForDeletion(pid, cid, deleteList);
logFileHashStore.info("Untagged pid: " + pid + " with cid: " + cid);
Expand Down Expand Up @@ -1816,23 +1803,8 @@ protected void unTagObject(String pid, String cid)

addAndRenamePidRefsFileToDeleteList(pid, deleteList, absPidRefsPath);

try {
Path absCidRefsPath = getHashStoreRefsPath(cidRead, HashStoreIdTypes.cid);
try {
updateRefsFile(pid, absCidRefsPath, HashStoreRefUpdateTypes.remove);
if (Files.size(absCidRefsPath) == 0) {
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absCidRefsPath));
}
} catch (Exception e) {
logFileHashStore.warn(
"Unable to remove pid: " + pid + " from cid refs file: " + absCidRefsPath
+ ". " + e.getMessage());
}
} catch (Exception e ) {
logFileHashStore.warn(
"Unexpected exception when attempting to remove pid: " + pid + " from cid "
+ "refs file for cid: " + cidRead + ". " + e.getMessage());
}
Path absCidRefsPath = getHashStoreRefsPath(cidRead, HashStoreIdTypes.cid);
removePidFromCidRefsAndDetermineDeletion(pid, deleteList, absCidRefsPath);

deleteListOfFilesRenamedForDeletion(pid, cid, deleteList);
String warnMsg = "Object with cid: " + cidRead
Expand Down Expand Up @@ -1885,6 +1857,32 @@ protected void unTagObject(String pid, String cid)
}
}

/**
* Removes a pid from a given cid refs file (if it's found) and checks to see if the cid refs is
* empty before renaming this file for deletion.
*
* @param pid Persistent identifier
* @param deleteList If cid refs file needs to be deleted, list to add to
* @param absCidRefsPath Path of the cid refs file
*/
private void removePidFromCidRefsAndDetermineDeletion(
String pid, Collection<Path> deleteList, Path absCidRefsPath) {
try {
updateRefsFile(pid, absCidRefsPath, HashStoreRefUpdateTypes.remove);
if (Files.size(absCidRefsPath) == 0) {
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absCidRefsPath));
} else {
String warnMsg = "Cid referenced by pid: " + pid
+ " is not empty (refs exist for cid). Skipping object " + "deletion.";
logFileHashStore.warn(warnMsg);
}
} catch (Exception e) {
logFileHashStore.warn(
"Unable to remove pid: " + pid + " from cid refs file: " + absCidRefsPath + ". "
+ e.getMessage());
}
}

/**
* Deletes all the file paths contained in a given 'deleteList'
*
Expand Down

0 comments on commit 8264ed6

Please sign in to comment.