From 9feb6a8865f5a6ac65ba1b2f2a0cf979fbc6ab64 Mon Sep 17 00:00:00 2001 From: Andreas Romeyke <andreas.romeyke@slub-dresden.de> Date: Mon, 3 Apr 2023 11:35:54 +0200 Subject: [PATCH] - refactoring, more precise variable namings - refactoring, more precise function namings - added contract to saveAbsoluteDestPathsTmpFile() - extracted loadAbsoluteDestPathsTmpFile() - added contract to loadAbsoluteDestPathsTmpFile() --- .../plugin/storage/nfs/SLUBStoragePlugin.java | 81 +++++++++++-------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java index a0b4cb8..889244c 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java @@ -54,7 +54,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.function.Function; /** * SLUBStoragePlugin @@ -402,6 +401,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.info("SLUBStoragePlugin.retrieveEntitybyRange() with storedEntityIdentifier='" + storedEntityIdentifier + "' start=" + start + " end=" + end); /* try-with-ressource block, no close if failed needed */ var absolute_filename = getFullFilePath(storedEntityIdentifier); + log.info("SLUBStoragePlugin.retrieveEntitybyRange() with absolute filename=" + absolute_filename); try (RandomAccessFile file = new RandomAccessFile(absolute_filename, "r")) { long starttime = System.currentTimeMillis(); file.seek(start); @@ -414,7 +414,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.error("SLUBStoragePlugin.retrieveEntitybyRange(), file not found, " + e.getMessage()); throw e; } catch (IOException e) { - log.error("SLUBStoragePlugin.retrieveEntityByRange(), I/O error, " + e.getMessage()); + log.error("SLUBStoragePlugin.retrieveEntityByRange(), I/O error, file=" + absolute_filename + " " + e.getMessage()); throw e; } catch (Exception e) { log.error("SLUBStoragePlugin.retrieveEntityByRange(), unknown problem, " + e.getMessage()); @@ -450,8 +450,8 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { String storedEntityIdentifier = paths.get("relativeDirectoryPath"); contractAssertIsRelativePath(storedEntityIdentifier); //log.debug("SLUBStoragePlugin.storeEntity() storedEntityIdentifier='" + storedEntityIdentifier + "'"); - String absolute_destFilePath = paths.get("destFilePath"); - contractAssertIsAbsolutePath(absolute_destFilePath); + String absoluteDestFilePath = paths.get("destFilePath"); + contractAssertIsAbsolutePath(absoluteDestFilePath); //log.debug("SLUBStoragePlugin.storeEntity() destFilePath (2)='" + absolute_destFilePath + "'"); //log.debug("SLUBStoragePlugin.storeEntity() isCopyFileNeeded='" + isCopyFileNeeded + "'"); if (isCopyFileNeeded) @@ -462,7 +462,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { if (is != null) { is.close(); } - copyStream(storedEntityMetadata, absolute_destFilePath); + copyStream(storedEntityMetadata, absoluteDestFilePath); //log.debug("SLUBStoragePlugin.storeEntity() try copy (copyStream(storedEntityMetadata, '" + absolute_destFilePath + "')) was successfull"); } else @@ -473,13 +473,13 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.warn("SLUBStoragePlugin.storeEntity() InputStream is null"); return null; } - try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( absolute_destFilePath))) + try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( absoluteDestFilePath))) { var blocksize = getBlockSize(); long starttime = System.currentTimeMillis(); IOUtil.copy(is, output, blocksize); long endtime = System.currentTimeMillis(); - log.info("SLUBStoragePlugin.storeEntity() try copy (IOUtil.copy(is, '"+absolute_destFilePath+"', "+blocksize+") was successfull (" + throughput(starttime, endtime, absolute_destFilePath)+ ")" ); + log.info("SLUBStoragePlugin.storeEntity() try copy (IOUtil.copy(is, '"+absoluteDestFilePath+"', "+blocksize+") was successfull (" + throughput(starttime, endtime, absoluteDestFilePath)+ ")" ); } } if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) { @@ -516,7 +516,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { long endtime = System.currentTimeMillis(); log.info("SLUBStoragePlugin.copyFile (" + source + ", " + destination + ") was successfull (" + throughput(starttime, endtime, destination) + ")"); } - protected void copyStream(StoredEntityMetaData storedEntityMetadata, String destPath) + protected void copyStream(StoredEntityMetaData storedEntityMetadata, String absoluteDestPath) throws IOException { log.info("SLUBStoragePlugin.copyStream()"); @@ -529,25 +529,25 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { //log.debug("SLUBStoragePlugin.copyStream() pid='" + pid + "'"); String iePid = storedEntityMetadata.getIePid(); //log.debug("SLUBStoragePlugin.copyStream() iePid='" + iePid + "'"); - String attr = "('"+srcPath+"','" + destPath + "')"; + String attr = "('"+srcPath+"','" + absoluteDestPath + "')"; if ("copy".equalsIgnoreCase(filesHandlingMethod)) { // FileUtil.copyFile(srcPath, destPath); - copyFile(srcPath, destPath); - saveDestPathsTmpFile(iePid, pid, destPath); + copyFile(srcPath, absoluteDestPath); + saveAbsoluteDestPathsTmpFile(storedEntityMetadata, absoluteDestPath); log.info("SLUBStoragePlugin.copyStream(), copy"+attr + " was successful"); } else if ("move".equalsIgnoreCase(filesHandlingMethod)) { File canonicalSrcFile = getCanonicalFile(srcPath); assert canonicalSrcFile != null; - FileUtil.moveFile(canonicalSrcFile, new File(destPath)); - saveDestPathsTmpFile(iePid, pid, destPath); + FileUtil.moveFile(canonicalSrcFile, new File(absoluteDestPath)); + saveAbsoluteDestPathsTmpFile(storedEntityMetadata, absoluteDestPath); log.info("SLUBStoragePlugin.copyStream(), move"+attr+" was successful"); } else if ("soft_link".equalsIgnoreCase(filesHandlingMethod)) { - softLink(srcPath, destPath); + softLink(srcPath, absoluteDestPath); log.info("SLUBStoragePlugin.copyStream(), softlink"+attr+" was successful"); } else if ("hard_link".equalsIgnoreCase(filesHandlingMethod)) { - hardLink(srcPath, destPath); + hardLink(srcPath, absoluteDestPath); log.info("SLUBStoragePlugin.copyStream(), hardlink"+attr+" was successful"); } else { @@ -556,18 +556,18 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { throw new IOException(msg); } } - protected String getFilePathInDescIfExists(StoredEntityMetaData storedEntityMetadata) - { + protected String getFilePathInDescIfExists(StoredEntityMetaData storedEntityMetadata) throws IOException { log.info("SLUBStoragePlugin.getFilePathInDescIfExists()"); if (storedEntityMetadata.getIePid() == null) { return null; } - String tmpFilePath = getTempStorageDirectory(false) + "destPath"; - String existsDescPath = StorageUtil.readDestPathFromTmpFile(storedEntityMetadata.getIePid(), tmpFilePath, storedEntityMetadata.getEntityPid()); + String existsDescPath = loadAbsoluteDestPathsTmpFile(storedEntityMetadata); //log.debug("SLUBStoragePlugin.getFilePathInDescIfExists() existsDescPath='" + existsDescPath + "'"); return existsDescPath; } + + /** copied from NFS Storage Plugin, enhanced with debugging info, {@inheritDoc} */ private boolean canHandleSourcePath(String srcPath) { //log.debug("SLUBStoragePlugin.canHandleSourcePath path='" + srcPath + "'"); @@ -617,9 +617,9 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } /** copied from NFS Storage Plugin, enhanced with debugging info, {@inheritDoc} */ - private Map<String, String> getStoreEntityIdentifier(StoredEntityMetaData storedEntityMetadata, String destFilePath) throws Exception + private Map<String, String> getStoreEntityIdentifier(StoredEntityMetaData storedEntityMetadata, String absoluteDestFilePath) throws Exception { - log.info("SLUBStoragePlugin.getStoreEntityIdentifier() destFilePath='" + destFilePath +"'"); + log.info("SLUBStoragePlugin.getStoreEntityIdentifier() destFilePath='" + absoluteDestFilePath +"'"); Map<String, String> paths = new HashMap<>(); //log.debug("(1) storedEntityMetadata is null?" + (null == storedEntityMetadata)); String fileName = createFileName(storedEntityMetadata); @@ -627,21 +627,21 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { //log.debug("(2) storedEntityMetadata is null?" + (null == storedEntityMetadata)); String relativeDirectoryPath = getStreamRelativePath(storedEntityMetadata); //log.debug("relativeDirectoryPath='" + relativeDirectoryPath + "'"); - if (destFilePath == null) + if (absoluteDestFilePath == null) { File destFile = getStreamDirectory(relativeDirectoryPath, fileName); - destFilePath = destFile.getAbsolutePath(); + absoluteDestFilePath = destFile.getAbsolutePath(); } - contractAssertIsAbsolutePath(destFilePath); + contractAssertIsAbsolutePath(absoluteDestFilePath); //log.debug("destFilePath (2)='" + destFilePath + "'"); - paths.put("destFilePath", destFilePath); + paths.put("destFilePath", absoluteDestFilePath); String relativePath = relativeDirectoryPath + File.separator + fileName; contractAssertIsRelativePath(relativePath); paths.put("relativeDirectoryPath", relativePath); log.info( "SLUBStoragePlugin.getStoreEntityIdentifier() stored:" - + " destFilePath='" + destFilePath + "'" + + " destFilePath='" + absoluteDestFilePath + "'" + " relativeDirectoryPath='" + relativePath + "'" ); return paths; @@ -739,15 +739,14 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { Files.createLink(Path.of(srcPath), Path.of(destPath)); } - private void saveDestPathsTmpFile(String folder, String key, String path) - { + /** + * + * @param storedEntityMetadata + * @param absolutePath + */ + private void saveAbsoluteDestPathsTmpFile(StoredEntityMetaData storedEntityMetadata, String absolutePath) throws IOException { log.info("SLUBStoragePlugin.saveDestPathsTmpFile()"); - //log.debug("SLUBStoragePlugin.saveDestPathsTmpFile folder='" + folder + "'"); - //log.debug("SLUBStoragePlugin.saveDestPathsTmpFile key='" + key + "'"); - //log.debug("SLUBStoragePlugin.saveDestPathsTmpFile path='" + path + "'"); - if (folder == null) { - return; - } + contractAssertIsAbsolutePath(absolutePath); String tmpFilePath = getTempStorageDirectory(false) + "destPath"; //log.debug("SLUBStoragePlugin.saveDestPathsTmpFile tmpFilePath='" + tmpFilePath + "'"); File destPathDir = new File(getTempStorageDirectory(false) + "destPath" + File.separator); @@ -758,6 +757,18 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.error("SLUBStoragePlugin.saveDestPathsTmpFile() destPathdir='" + destPathDir + "' could not be created"); } } - StorageUtil.saveDestPathToTmpFile(folder, tmpFilePath, key, path); + StorageUtil.saveDestPathToTmpFile(storedEntityMetadata.getIePid(), tmpFilePath, storedEntityMetadata.getEntityPid(), absolutePath); + } + + /** + * + * @param storedEntityMetadata + * @return absolute path + */ + private String loadAbsoluteDestPathsTmpFile(StoredEntityMetaData storedEntityMetadata) throws IOException { + String tmpFilePath = getTempStorageDirectory(false) + "destPath"; + String absolutePath = StorageUtil.readDestPathFromTmpFile(storedEntityMetadata.getIePid(), tmpFilePath, storedEntityMetadata.getEntityPid()); + contractAssertIsAbsolutePath(absolutePath); + return absolutePath; } } -- GitLab