From 0dbcf8de4512d8a66c200791bb76307f6ded78d3 Mon Sep 17 00:00:00 2001 From: io42630 Date: Mon, 4 Apr 2022 21:05:28 +0200 Subject: [PATCH] _ nest map of RecordFile inside Record _ call readFromRecord() only once _ use FileUtils for copy --- src/main/java/com/olexyn/ensync/Flow.java | 9 +++--- .../com/olexyn/ensync/artifacts/Record.kt | 8 +++++ .../olexyn/ensync/artifacts/RecordFile.java | 5 +++ .../ensync/artifacts/SyncDirectory.java | 31 ++++++++++--------- .../com/olexyn/ensync/artifacts/SyncFile.java | 21 ++----------- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/olexyn/ensync/Flow.java b/src/main/java/com/olexyn/ensync/Flow.java index 83a22b3..c3b7fcd 100644 --- a/src/main/java/com/olexyn/ensync/Flow.java +++ b/src/main/java/com/olexyn/ensync/Flow.java @@ -54,20 +54,21 @@ public class Flow implements Runnable { LOGGER.info("DO SYNC " + sDir.directoryPath); var listFileSystem = sDir.readFileSystem(); LOGGER.info("# of files on FS: " + listFileSystem.size()); - var record = sDir.readRecord(); - LOGGER.info("# of files on Record: " + record.size()); + var record = new Record(sDir.directoryPath); + record.getFiles().putAll(sDir.readRecord()); + LOGGER.info("# of files on Record: " + record.getFiles().size()); var listCreated = sDir.fillListOfLocallyCreatedFiles(listFileSystem, record); LOGGER.info("# of files in Created: " + listCreated.size()); var listDeleted = sDir.makeListOfLocallyDeletedFiles(listFileSystem, record); LOGGER.info("# of files in Deleted: " + listDeleted.size()); - var listModified = sDir.makeListOfLocallyModifiedFiles(listFileSystem); + var listModified = sDir.makeListOfLocallyModifiedFiles(listFileSystem, record); LOGGER.info("# of files in Modified: " + listModified.size()); sDir.doCreateOpsOnOtherSDs(listCreated); sDir.doDeleteOpsOnOtherSDs(listDeleted); sDir.doModifyOpsOnOtherSDs(listModified); - sDir.writeRecord(new Record(sDir.directoryPath)); + sDir.writeRecord(record); } /** diff --git a/src/main/java/com/olexyn/ensync/artifacts/Record.kt b/src/main/java/com/olexyn/ensync/artifacts/Record.kt index 6938d17..9b9553e 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/Record.kt +++ b/src/main/java/com/olexyn/ensync/artifacts/Record.kt @@ -5,6 +5,8 @@ import java.nio.file.Path class Record(val targetPath: Path) { + var files: Map = HashMap() + fun getPath(): Path { return targetPath.resolve(Constants.STATE_FILE_NAME) } @@ -17,4 +19,10 @@ class Record(val targetPath: Path) { return getFile().exists() } + fun lastModified(key: String): Long { + val record: RecordFile? = files.get(key); + if (record == null) { return -1 } + return record.timeModifiedFromRecord + } + } \ No newline at end of file diff --git a/src/main/java/com/olexyn/ensync/artifacts/RecordFile.java b/src/main/java/com/olexyn/ensync/artifacts/RecordFile.java index 337b367..35b9c26 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/RecordFile.java +++ b/src/main/java/com/olexyn/ensync/artifacts/RecordFile.java @@ -9,6 +9,11 @@ public class RecordFile extends SyncFile { super(sDir, absolutePath); } + @Override + public long lastModified() { + return timeModifiedFromRecord; + } + public void setTimeModifiedFromRecord(long value) { timeModifiedFromRecord = value; } diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java index 09b858d..b2f6843 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -3,6 +3,7 @@ package com.olexyn.ensync.artifacts; import com.olexyn.ensync.LogUtil; import com.olexyn.ensync.MainApp; import com.olexyn.ensync.Tools; +import org.apache.commons.io.FileUtils; import java.io.File; import java.io.FileInputStream; @@ -19,7 +20,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -92,8 +92,8 @@ public class SyncDirectory { * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public Map fillListOfLocallyCreatedFiles(Map listFileSystem, Map record) { - var listCreated = tools.setMinus(listFileSystem.keySet(), record.keySet()); + public Map fillListOfLocallyCreatedFiles(Map listFileSystem, Record record) { + var listCreated = tools.setMinus(listFileSystem.keySet(), record.getFiles().keySet()); return listCreated.stream().collect(Collectors.toMap(key -> key, listFileSystem::get)); } @@ -101,24 +101,24 @@ public class SyncDirectory { * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public Map makeListOfLocallyDeletedFiles(Map listFileSystem, Map record) { - var listDeleted = tools.setMinus(record.keySet(), listFileSystem.keySet()); - return listDeleted.stream().collect(Collectors.toMap(key -> key, record::get)); + public Map makeListOfLocallyDeletedFiles(Map listFileSystem, Record record) { + var listDeleted = tools.setMinus(record.getFiles().keySet(), listFileSystem.keySet()); + return listDeleted.stream().collect(Collectors.toMap(key -> key, key -> record.getFiles().get(key))); } /** * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public Map makeListOfLocallyModifiedFiles(Map listFileSystem) { + public Map makeListOfLocallyModifiedFiles(Map listFileSystem, Record record) { return listFileSystem.entrySet().stream().filter( fileEntry -> { String fileKey = fileEntry.getKey(); SyncFile file = fileEntry.getValue(); if (file.isDirectory()) { return false; } // no need to modify Directories, the Filesystem will do that, if a File changed. - boolean isKnown = readRecord().containsKey(fileKey); // If KEY exists in OLD , thus FILE was NOT created. - boolean isModified = file.lastModified() > file.lastModifiedFromRecord(); + boolean isKnown = record.getFiles().containsKey(fileKey); // If KEY exists in OLD , thus FILE was NOT created. + boolean isModified = file.lastModified() > record.lastModified(fileKey); return isKnown && isModified; } ).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); @@ -189,7 +189,7 @@ public class SyncDirectory { } } - public void doDeleteOpsOnOtherSDs(Map listDeleted) { + public void doDeleteOpsOnOtherSDs(Map listDeleted) { for (var deletedFile : listDeleted.values()) { for (var otherFile : otherFiles(deletedFile)) { deleteFileIfNewer(deletedFile, otherFile); @@ -214,10 +214,11 @@ public class SyncDirectory { /** * Delete other file if this file is newer. + * Here the >= is crucial, since otherFile might have == modified, + * but in that case we still want to delete both files. */ - private void deleteFileIfNewer(SyncFile thisFile, SyncFile otherFile) { + private void deleteFileIfNewer(RecordFile thisFile, SyncFile otherFile) { if (!otherFile.exists()) { return; } - // if the otherFile was created with ensync it will have the == TimeModified. if (thisFile.lastModified() >= otherFile.lastModified()) { try { Files.delete(otherFile.toPath()); @@ -262,9 +263,9 @@ public class SyncDirectory { private void copyFile(SyncFile thisFile, SyncFile otherFile) { try { - Files.copy( - Path.of(thisFile.getPath()), - Path.of(otherFile.getPath()), + FileUtils.copyFile( + thisFile, + otherFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES ); diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java index c503955..d66708e 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java @@ -11,8 +11,6 @@ public class SyncFile extends File { private static final Logger LOGGER = LogUtil.get(SyncFile.class); - - private final String relativePath; private final SyncDirectory sDir; @@ -27,21 +25,6 @@ public class SyncFile extends File { } - - public long lastModifiedFromRecord() { - RecordFile record = getFromRecord(); - if (record == null) { - LOGGER.severe("Did not find record for " + this.toPath() + " in Record."); - LOGGER.severe("Returning -1 (never existed)."); - return -1; - } - return record.timeModifiedFromRecord; - } - - public RecordFile getFromRecord() { - return sDir.readRecord().get(getRelativePath()); - } - /** * If File exists on Disk get the TimeModified from there. * Else try to read it from Record. @@ -53,7 +36,9 @@ public class SyncFile extends File { @Override public long lastModified(){ if (exists()) { return super.lastModified(); } - return lastModifiedFromRecord(); + LOGGER.info("Did not find File for " + this); + LOGGER.info("Returning -1 (never existed)."); + return -1; } public boolean isNewer(SyncFile otherFile) {