_ nest map of RecordFile inside Record

_ call readFromRecord() only once
_ use FileUtils for copy
pull/1/head
io42630 3 years ago
parent 414b9e3727
commit 0dbcf8de45

@ -54,20 +54,21 @@ public class Flow implements Runnable {
LOGGER.info("DO SYNC " + sDir.directoryPath); LOGGER.info("DO SYNC " + sDir.directoryPath);
var listFileSystem = sDir.readFileSystem(); var listFileSystem = sDir.readFileSystem();
LOGGER.info("# of files on FS: " + listFileSystem.size()); LOGGER.info("# of files on FS: " + listFileSystem.size());
var record = sDir.readRecord(); var record = new Record(sDir.directoryPath);
LOGGER.info("# of files on Record: " + record.size()); record.getFiles().putAll(sDir.readRecord());
LOGGER.info("# of files on Record: " + record.getFiles().size());
var listCreated = sDir.fillListOfLocallyCreatedFiles(listFileSystem, record); var listCreated = sDir.fillListOfLocallyCreatedFiles(listFileSystem, record);
LOGGER.info("# of files in Created: " + listCreated.size()); LOGGER.info("# of files in Created: " + listCreated.size());
var listDeleted = sDir.makeListOfLocallyDeletedFiles(listFileSystem, record); var listDeleted = sDir.makeListOfLocallyDeletedFiles(listFileSystem, record);
LOGGER.info("# of files in Deleted: " + listDeleted.size()); 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()); LOGGER.info("# of files in Modified: " + listModified.size());
sDir.doCreateOpsOnOtherSDs(listCreated); sDir.doCreateOpsOnOtherSDs(listCreated);
sDir.doDeleteOpsOnOtherSDs(listDeleted); sDir.doDeleteOpsOnOtherSDs(listDeleted);
sDir.doModifyOpsOnOtherSDs(listModified); sDir.doModifyOpsOnOtherSDs(listModified);
sDir.writeRecord(new Record(sDir.directoryPath)); sDir.writeRecord(record);
} }
/** /**

@ -5,6 +5,8 @@ import java.nio.file.Path
class Record(val targetPath: Path) { class Record(val targetPath: Path) {
var files: Map<String, RecordFile> = HashMap()
fun getPath(): Path { fun getPath(): Path {
return targetPath.resolve(Constants.STATE_FILE_NAME) return targetPath.resolve(Constants.STATE_FILE_NAME)
} }
@ -17,4 +19,10 @@ class Record(val targetPath: Path) {
return getFile().exists() return getFile().exists()
} }
fun lastModified(key: String): Long {
val record: RecordFile? = files.get(key);
if (record == null) { return -1 }
return record.timeModifiedFromRecord
}
} }

@ -9,6 +9,11 @@ public class RecordFile extends SyncFile {
super(sDir, absolutePath); super(sDir, absolutePath);
} }
@Override
public long lastModified() {
return timeModifiedFromRecord;
}
public void setTimeModifiedFromRecord(long value) { public void setTimeModifiedFromRecord(long value) {
timeModifiedFromRecord = value; timeModifiedFromRecord = value;
} }

@ -3,6 +3,7 @@ package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.LogUtil; import com.olexyn.ensync.LogUtil;
import com.olexyn.ensync.MainApp; import com.olexyn.ensync.MainApp;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import org.apache.commons.io.FileUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -19,7 +20,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -92,8 +92,8 @@ public class SyncDirectory {
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time. * List is cleared and created each time.
*/ */
public Map<String, SyncFile> fillListOfLocallyCreatedFiles(Map<String, SyncFile> listFileSystem, Map<String, RecordFile> record) { public Map<String, SyncFile> fillListOfLocallyCreatedFiles(Map<String, SyncFile> listFileSystem, Record record) {
var listCreated = tools.setMinus(listFileSystem.keySet(), record.keySet()); var listCreated = tools.setMinus(listFileSystem.keySet(), record.getFiles().keySet());
return listCreated.stream().collect(Collectors.toMap(key -> key, listFileSystem::get)); return listCreated.stream().collect(Collectors.toMap(key -> key, listFileSystem::get));
} }
@ -101,24 +101,24 @@ public class SyncDirectory {
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time. * List is cleared and created each time.
*/ */
public Map<String, SyncFile> makeListOfLocallyDeletedFiles(Map<String, SyncFile> listFileSystem, Map<String, RecordFile> record) { public Map<String, RecordFile> makeListOfLocallyDeletedFiles(Map<String, SyncFile> listFileSystem, Record record) {
var listDeleted = tools.setMinus(record.keySet(), listFileSystem.keySet()); var listDeleted = tools.setMinus(record.getFiles().keySet(), listFileSystem.keySet());
return listDeleted.stream().collect(Collectors.toMap(key -> key, record::get)); return listDeleted.stream().collect(Collectors.toMap(key -> key, key -> record.getFiles().get(key)));
} }
/** /**
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time. * List is cleared and created each time.
*/ */
public Map<String, SyncFile> makeListOfLocallyModifiedFiles(Map<String, SyncFile> listFileSystem) { public Map<String, SyncFile> makeListOfLocallyModifiedFiles(Map<String, SyncFile> listFileSystem, Record record) {
return listFileSystem.entrySet().stream().filter( return listFileSystem.entrySet().stream().filter(
fileEntry -> { fileEntry -> {
String fileKey = fileEntry.getKey(); String fileKey = fileEntry.getKey();
SyncFile file = fileEntry.getValue(); SyncFile file = fileEntry.getValue();
if (file.isDirectory()) { return false; } // no need to modify Directories, the Filesystem will do that, if a File changed. 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 isKnown = record.getFiles().containsKey(fileKey); // If KEY exists in OLD , thus FILE was NOT created.
boolean isModified = file.lastModified() > file.lastModifiedFromRecord(); boolean isModified = file.lastModified() > record.lastModified(fileKey);
return isKnown && isModified; return isKnown && isModified;
} }
).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); ).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
@ -189,7 +189,7 @@ public class SyncDirectory {
} }
} }
public void doDeleteOpsOnOtherSDs(Map<String, SyncFile> listDeleted) { public void doDeleteOpsOnOtherSDs(Map<String, RecordFile> listDeleted) {
for (var deletedFile : listDeleted.values()) { for (var deletedFile : listDeleted.values()) {
for (var otherFile : otherFiles(deletedFile)) { for (var otherFile : otherFiles(deletedFile)) {
deleteFileIfNewer(deletedFile, otherFile); deleteFileIfNewer(deletedFile, otherFile);
@ -214,10 +214,11 @@ public class SyncDirectory {
/** /**
* Delete other file if this file is newer. * 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 (!otherFile.exists()) { return; }
// if the otherFile was created with ensync it will have the == TimeModified.
if (thisFile.lastModified() >= otherFile.lastModified()) { if (thisFile.lastModified() >= otherFile.lastModified()) {
try { try {
Files.delete(otherFile.toPath()); Files.delete(otherFile.toPath());
@ -262,9 +263,9 @@ public class SyncDirectory {
private void copyFile(SyncFile thisFile, SyncFile otherFile) { private void copyFile(SyncFile thisFile, SyncFile otherFile) {
try { try {
Files.copy( FileUtils.copyFile(
Path.of(thisFile.getPath()), thisFile,
Path.of(otherFile.getPath()), otherFile,
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES StandardCopyOption.COPY_ATTRIBUTES
); );

@ -11,8 +11,6 @@ public class SyncFile extends File {
private static final Logger LOGGER = LogUtil.get(SyncFile.class); private static final Logger LOGGER = LogUtil.get(SyncFile.class);
private final String relativePath; private final String relativePath;
private final SyncDirectory sDir; 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. * If File exists on Disk get the TimeModified from there.
* Else try to read it from Record. * Else try to read it from Record.
@ -53,7 +36,9 @@ public class SyncFile extends File {
@Override @Override
public long lastModified(){ public long lastModified(){
if (exists()) { return super.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) { public boolean isNewer(SyncFile otherFile) {

Loading…
Cancel
Save