Skip to content

Commit

Permalink
Refactor 'deleteMetadata(String pid)' to remove all metadata related …
Browse files Browse the repository at this point in the history
…for the given pid and update junit test
  • Loading branch information
doulikecookiedough committed Feb 5, 2024
1 parent c66b5a1 commit 97cc255
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/dataone/hashstore/HashStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,21 @@ public void deleteObject(String pid) throws IllegalArgumentException, IOExceptio
* @param pid Authority-based identifier
* @param formatId Metadata namespace/format
* @throws IllegalArgumentException When pid or formatId is null or empty
* @throws IOException I/O error when deleting empty directories
* @throws IOException I/O error when deleting metadata or empty directories
* @throws NoSuchAlgorithmException When algorithm used to calculate object address is not
* supported
*/
public void deleteMetadata(String pid, String formatId) throws IllegalArgumentException,
IOException, NoSuchAlgorithmException;

/**
* @see #deleteMetadata(String, String)
* Deletes all metadata related for the given 'pid' from HashStore
*
* If `deleteMetadata` is called with signature (String pid), the metadata
* document deleted will be the given pid's 'sysmeta'
* @param pid Authority-based identifier
* @throws IllegalArgumentException If pid is invalid
* @throws IOException I/O error when deleting metadata or empty directories
* @throws NoSuchAlgorithmException When algorithm used to calculate object address is not
* supported
*/
public void deleteMetadata(String pid) throws IllegalArgumentException, IOException,
NoSuchAlgorithmException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,31 @@ public void deleteMetadata(String pid, String formatId) throws IllegalArgumentEx
@Override
public void deleteMetadata(String pid) throws IllegalArgumentException, IOException,
NoSuchAlgorithmException {
deleteMetadata(pid, DEFAULT_METADATA_NAMESPACE);
logFileHashStore.debug(
"FileHashStore.deleteMetadata - Called to delete all metadata for pid: " + pid
);
FileHashStoreUtility.ensureNotNull(pid, "pid", "deleteMetadata");
FileHashStoreUtility.checkForEmptyString(pid, "pid", "deleteMetadata");

List<Path> deleteList = new ArrayList<>();
// 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);
// Add all metadata doc paths to a List to iterate over below
List<Path> metadataDocPaths = FileHashStoreUtility.getFilesFromDir(
expectedPidMetadataDirectory
);
for (Path metadataDoc : metadataDocPaths) {
deleteList.add(FileHashStoreUtility.renamePathForDeletion(metadataDoc));
}
// Delete all items in the list
FileHashStoreUtility.deleteListItems(deleteList);
logFileHashStore.info(
"FileHashStore.deleteMetadata - All related metadata deleted for: " + pid
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ public void deleteMetadata() throws Exception {
}

/**
* Confirm that deleteMetadata deletes object and empty subdirectories with overload method
* Confirm that deleteMetadata deletes all metadata stored for a given pid.
*/
@Test
public void deleteMetadata_overload() throws Exception {
Expand All @@ -1662,16 +1662,27 @@ public void deleteMetadata_overload() throws Exception {

// Get test metadata file
Path testMetaDataFile = testData.getTestFile(pidFormatted + ".xml");
String formatIdTwo = "ns.type.2";
String formatIdThree = "ns.type.3";

InputStream metadataStream = Files.newInputStream(testMetaDataFile);
fileHashStore.storeMetadata(metadataStream, pid, null);
fileHashStore.storeMetadata(metadataStream, pid, formatIdTwo);
fileHashStore.storeMetadata(metadataStream, pid, formatIdThree);

fileHashStore.deleteMetadata(pid);

// Check that file doesn't exist
String storeFormatId = (String) fhsProperties.get("storeMetadataNamespace");
Path metadataCidPath = fileHashStore.getExpectedPath(pid, "metadata", storeFormatId);
Path metadataCidPathTwo = fileHashStore.getExpectedPath(pid, "metadata", formatIdTwo);
Path metadataCidPathThree = fileHashStore.getExpectedPath(
pid, "metadata", formatIdThree
);

assertFalse(Files.exists(metadataCidPath));
assertFalse(Files.exists(metadataCidPathTwo));
assertFalse(Files.exists(metadataCidPathThree));

// Check that parent directories are not deleted
assertTrue(Files.exists(metadataCidPath.getParent()));
Expand Down

0 comments on commit 97cc255

Please sign in to comment.