From 188d482e67b087c3835fe72e41b955cb59c65041 Mon Sep 17 00:00:00 2001 From: Dou Mok Date: Thu, 1 Feb 2024 13:25:53 -0800 Subject: [PATCH] Move 'deleteObject(String pid)' deletion of metadata process to 'deleteObject(String idType, String id)' --- .../filehashstore/FileHashStore.java | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/dataone/hashstore/filehashstore/FileHashStore.java b/src/main/java/org/dataone/hashstore/filehashstore/FileHashStore.java index ed8426e4..522f534e 100644 --- a/src/main/java/org/dataone/hashstore/filehashstore/FileHashStore.java +++ b/src/main/java/org/dataone/hashstore/filehashstore/FileHashStore.java @@ -1154,6 +1154,31 @@ public void deleteObject(String idType, String id) throws IllegalArgumentExcepti } try { + // First, remove all metadata + String pidHexDigest = FileHashStoreUtility.getPidHexDigest( + pid, OBJECT_STORE_ALGORITHM + ); + String pidRelativePath = FileHashStoreUtility.getHierarchicalPathString( + DIRECTORY_DEPTH, DIRECTORY_WIDTH, pidHexDigest + ); + Path expectedPidMetadataDirectory = METADATA_STORE_DIRECTORY.resolve( + pidRelativePath + ); + + // Check that directory exists and is not empty before attempting to delete metadata docs + if (Files.isDirectory(expectedPidMetadataDirectory) && FileHashStoreUtility + .dirContainsFiles(expectedPidMetadataDirectory)) { + try (Stream stream = Files.walk(expectedPidMetadataDirectory)) { + stream.map(Path::toFile).forEach(File::delete); + + } catch (IOException ioe) { + logFileHashStore.warn( + "FileHashStore.deleteObject - Unexpected IOException: " + ioe + .getMessage() + ); + } + } + // Get permanent address of the pid by calculating its sha-256 hex digest Path objRealPath = getExpectedPath(pid, "object", null); // Get the path to the cid refs file to work with @@ -1185,8 +1210,7 @@ public void deleteObject(String idType, String id) throws IllegalArgumentExcepti Files.delete(objRealPath); } else { String warnMsg = "FileHashStore.deleteObject - cid referenced by pid: " - + pid - + " is not empty (references exist for the cid). Skipping object deletion."; + + pid + " is not empty (refs exist for cid). Skipping object deletion."; logFileHashStore.warn(warnMsg); } logFileHashStore.info( @@ -1213,29 +1237,7 @@ public void deleteObject(String pid) throws IllegalArgumentException, IOExceptio logFileHashStore.debug( "FileHashStore.deleteObject - Called to delete all associated docs for pid: " + pid ); - // First, delete object as expected normally - // This is synchronized based on the 'cid' retrieved from the pid refs file deleteObject(HashStoreIdTypes.pid.getName("pid"), pid); - - // Second, delete all metadata documents in the associated pid metadata directory - String pidHexDigest = FileHashStoreUtility.getPidHexDigest(pid, OBJECT_STORE_ALGORITHM); - String pidRelativePath = FileHashStoreUtility.getHierarchicalPathString( - DIRECTORY_DEPTH, DIRECTORY_WIDTH, pidHexDigest - ); - Path expectedPidMetadataDirectory = METADATA_STORE_DIRECTORY.resolve(pidRelativePath); - - // Check that directory exists and is not empty before attempting to delete metadata docs - if (Files.isDirectory(expectedPidMetadataDirectory) && FileHashStoreUtility - .dirContainsFiles(expectedPidMetadataDirectory)) { - try (Stream stream = Files.walk(expectedPidMetadataDirectory)) { - stream.map(Path::toFile).forEach(File::delete); - - } catch (IOException ioe) { - logFileHashStore.warn( - "FileHashStore.deleteObject - Unexpected IOException: " + ioe.getMessage() - ); - } - } logFileHashStore.info( "FileHashStore.deleteObject - Object, references and metadata deleted for: " + pid );