replaced Algorythm and made it way faster

This commit is contained in:
BlyDoesCoding 2023-03-24 21:04:32 +01:00
parent a0d2e5b90f
commit c721ba3486
3 changed files with 54 additions and 43 deletions

15
.idea/workspace.xml generated
View file

@ -10,7 +10,9 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9b57ac51-c870-474b-9dfd-64a5fc490635" name="Changes" comment="Fix MD"> <list default="true" id="9b57ac51-c870-474b-9dfd-64a5fc490635" name="Changes" comment="Fix MD">
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/ThreadedCompare.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/ThreadedCompare.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -72,7 +74,7 @@
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" /> <option name="MAIN_CLASS_NAME" value="Main" />
<module name="Sherly" /> <module name="Sherly" />
<option name="PROGRAM_PARAMETERS" value="-f $USER_HOME$/Pictures/ -p -h" /> <option name="PROGRAM_PARAMETERS" value="-f $USER_HOME$/.local/share/Steam -p" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
@ -260,7 +262,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1679508433400</updated> <updated>1679508433400</updated>
</task> </task>
<option name="localTasksCounter" value="25" /> <task id="LOCAL-00025" summary="Fix MD">
<created>1679508468115</created>
<option name="number" value="00025" />
<option name="presentableId" value="LOCAL-00025" />
<option name="project" value="LOCAL" />
<updated>1679508468115</updated>
</task>
<option name="localTasksCounter" value="26" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">

View file

@ -121,7 +121,8 @@ public class Main {
//sectionedList gives the thread their Assigned Part of Files and allFiles are all the Files //sectionedList gives the thread their Assigned Part of Files and allFiles are all the Files
ThreadedCompare threadedCompare = new ThreadedCompare(sectionedList, allFiles);
ThreadedCompare threadedCompare = new ThreadedCompare(sectionedList);
threadedCompare.start(); threadedCompare.start();
} }
@ -136,6 +137,13 @@ public class Main {
} }
} }
ArrayList toRemove = new ArrayList<String>();
for (String md5: fileMap.keySet()) {
if (Main.fileMap.get(md5).size() == 1) {
toRemove.add(md5);
}
}
fileMap.keySet().removeAll(toRemove);
//now everything is finished and the Filemap (hashmap with all Dups) can be printed out in a nice view //now everything is finished and the Filemap (hashmap with all Dups) can be printed out in a nice view
//System.out.println(fileMap); //System.out.println(fileMap);

View file

@ -4,55 +4,49 @@ import java.nio.file.Path;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class ThreadedCompare extends Thread { public class ThreadedCompare extends Thread {
private final List<Path> pathsToCompareTo; private final List<Path> pathsToCompareTo;
private final List<Path> pathsToBeCompared; //private HashMap<String, List<Path>> threadFileMap = new HashMap<>();
public ThreadedCompare (List<Path> pathsToBeCompared, List<Path> pathsToCompareTo) {
this.pathsToBeCompared = pathsToBeCompared; public ThreadedCompare (List<Path> pathsToCompareTo) {
this.pathsToCompareTo = pathsToCompareTo; this.pathsToCompareTo = pathsToCompareTo;
} }
@Override @Override
public void run() { public void run() {
//Compare every File for (Path file : pathsToCompareTo) {
for (Path file1 : pathsToBeCompared) { List<Path> fileArray = new ArrayList<>();
for (Path file2 : pathsToCompareTo) { assert fileArray != null;
try { fileArray.add(file);
if (sameContent(file1, file2)) { String MD5;
List<Path> bothList = new ArrayList<>(); try {
String md5 = getMD5Sum(file1.toFile()); MD5 = getMD5Sum(file.toFile());
} catch (IOException e) {
bothList.add(file1); throw new RuntimeException(e);
bothList.add(file2);
//here it is trying to add the values in the HashMap so everything is nice and clear
Main.fileMap.putIfAbsent(md5, bothList);
if (!bothList.isEmpty()) {
Main.fileMap.get(md5).remove(file1);
Main.fileMap.get(md5).remove(file2);
Main.fileMap.get(md5).addAll(bothList);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
//Update the Progress that can be found in The Main Class
Main.progress += 1;
if (Main.fileMap.containsKey(MD5)) {
fileArray.addAll(Main.fileMap.get(MD5));
Main.fileMap.put(MD5, fileArray);
} else {
Main.fileMap.put(MD5, fileArray);
}
Main.progress++;
} }
//Update the thread Completion Counter that can be found in the Main Class
Main.completedThreads += 1;
} Main.completedThreads++;
private boolean sameContent(Path file1, Path file2) throws IOException {
if (file1 != file2) {
return Files.mismatch(file1, file2) == -1;
}
return false;
} }
//this is used to get the MD5 String of one of the files (one of them is just fine since they both have the same value) //this is used to get the MD5 String of one of the files (one of them is just fine since they both have the same value)