Revert "Merge branch 'MD5-rework' into 'master'"
This commit is contained in:
@@ -275,7 +275,8 @@ import lombok.Data;
|
|||||||
if (md5_local.equals(name) == false) {
|
if (md5_local.equals(name) == false) {
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
Md5.remove(file.getAbsolutePath());
|
|
||||||
|
// TODO: remove old one
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
file.delete();
|
file.delete();
|
||||||
|
|||||||
@@ -28,73 +28,23 @@ import java.security.DigestInputStream;
|
|||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides facilities to get an MD5 checksum and cache the results thereof
|
|
||||||
*/
|
|
||||||
public class Md5 {
|
public class Md5 {
|
||||||
/**
|
private static Map<String, String> cache = new HashMap<>();
|
||||||
* Hashmap of all files uniqueMD5keys (last modification time + file)
|
|
||||||
* and their checksums that have been calculated so far
|
|
||||||
*/
|
|
||||||
private static final Map<UniqueMD5Key, String> cache = new HashMap<>();
|
|
||||||
private static final int cacheCleanTrigger = 64; //After how many cache hits to check and clean the cache
|
|
||||||
private static int cacheHits = 0;
|
|
||||||
|
|
||||||
|
// TODO: to avoid memory increase, check if files are deleted
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes a file from cache
|
|
||||||
* @param path Path to the file
|
|
||||||
*/
|
|
||||||
public static void remove(String path) {
|
|
||||||
UniqueMD5Key key = new UniqueMD5Key(path);
|
|
||||||
cache.remove(key); //Doesn't handle entries that the files moved or had their lastModified changed
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a MD5 checksum either from the cache or computes it and puts it into the cache
|
|
||||||
*
|
|
||||||
* @param path Path of the file to get the MD5 checksum for
|
|
||||||
* @return the string The MD5 checksum
|
|
||||||
*/
|
|
||||||
public String get(String path) {
|
public String get(String path) {
|
||||||
UniqueMD5Key key = new UniqueMD5Key(path);
|
String key = getUniqueKey(path);
|
||||||
if (cache.containsKey(key) == false) {
|
if (cache.containsKey(key) == false) {
|
||||||
generate(path);
|
generate(path);
|
||||||
}
|
}
|
||||||
if (cacheHits >= cacheCleanTrigger){
|
|
||||||
cacheCheck();
|
|
||||||
cacheHits = 0;
|
|
||||||
} else {
|
|
||||||
cacheHits++;
|
|
||||||
}
|
|
||||||
return cache.get(key);
|
return cache.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks cache for files that don't exist anymore according to cached values and removes them
|
|
||||||
* Catches entries tha fall through the cracks
|
|
||||||
*/
|
|
||||||
private void cacheCheck(){
|
|
||||||
Iterator<Map.Entry<UniqueMD5Key, String>> it = cache.entrySet().iterator();
|
|
||||||
while (it.hasNext()) {
|
|
||||||
UniqueMD5Key itKey = (UniqueMD5Key)it.next();
|
|
||||||
if (itKey.exists() == false) {
|
|
||||||
it.remove(); //Iterator instead of foreach because we remove items
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the md5 checksum for a given file
|
|
||||||
* @param path Path to the file to calculate the checksum for
|
|
||||||
*/
|
|
||||||
private void generate(String path) {
|
private void generate(String path) {
|
||||||
UniqueMD5Key key = new UniqueMD5Key(path);
|
String key = getUniqueKey(path);
|
||||||
try {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
InputStream is = Files.newInputStream(Paths.get(path));
|
InputStream is = Files.newInputStream(Paths.get(path));
|
||||||
@@ -102,65 +52,18 @@ public class Md5 {
|
|||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
while (dis.read(buffer) > 0)
|
while (dis.read(buffer) > 0)
|
||||||
; // process the entire file
|
; // process the entire file
|
||||||
String checksum = Utils.convertBinaryToHex(md.digest());
|
String data = Utils.convertBinaryToHex(md.digest());
|
||||||
dis.close();
|
dis.close();
|
||||||
is.close();
|
is.close();
|
||||||
cache.put(key, checksum);
|
cache.put(key, data);
|
||||||
}
|
}
|
||||||
catch (NoSuchAlgorithmException | IOException e) {
|
catch (NoSuchAlgorithmException | IOException e) {
|
||||||
cache.put(key, "");
|
cache.put(key, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private String getUniqueKey(String path) {
|
||||||
* Used as key for the hashmap that is the cache
|
File file = new File(path);
|
||||||
* while also making it accessible to check if key is still valid
|
return Long.toString(file.lastModified()) + '_' + path;
|
||||||
*/
|
|
||||||
class UniqueMD5Key {
|
|
||||||
private final long lastModified;
|
|
||||||
private final File file;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a UniqueMD5Key for a given filepath
|
|
||||||
* Automatically fills in lastModified time
|
|
||||||
* @param filepath The filepath to the file
|
|
||||||
*/
|
|
||||||
public UniqueMD5Key(String filepath) {
|
|
||||||
this.file = new File(filepath);
|
|
||||||
this.lastModified = this.file.lastModified();
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @see File#exists()
|
|
||||||
* also takes lastModified into account.
|
|
||||||
*/
|
|
||||||
public boolean exists(){
|
|
||||||
return file.exists() && lastModified == this.file.lastModified();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @see Object#toString()
|
|
||||||
*/
|
|
||||||
@Override public String toString() {
|
|
||||||
return "UniqueMD5Key{" + "lastModified=" + lastModified + ", file=" + file + '}';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @see Object#equals(Object)
|
|
||||||
*/
|
|
||||||
@Override public boolean equals(Object o) {
|
|
||||||
if (this == o)
|
|
||||||
return true;
|
|
||||||
if (o == null || getClass() != o.getClass())
|
|
||||||
return false;
|
|
||||||
UniqueMD5Key that = (UniqueMD5Key) o;
|
|
||||||
return lastModified == that.lastModified && Objects.equals(file, that.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @see Object#hashCode()
|
|
||||||
*/
|
|
||||||
@Override public int hashCode() {
|
|
||||||
return Objects.hash(lastModified, file);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,7 +227,6 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
file.delete();
|
file.delete();
|
||||||
Md5.remove(file.getAbsolutePath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user