Delete old files if etag was updated
This commit is contained in:
@@ -86,7 +86,7 @@ public class LibraryDownloadTask extends Task {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preExecute() throws Exception {
|
public void preExecute() throws Exception {
|
||||||
Optional<Path> libPath = cacheRepository.getLibrary(library);
|
Optional<Path> libPath = cacheRepository.getLibrary(library.setClassifier(null));
|
||||||
if (libPath.isPresent()) {
|
if (libPath.isPresent()) {
|
||||||
try {
|
try {
|
||||||
FileUtils.copyFile(libPath.get().toFile(), jar);
|
FileUtils.copyFile(libPath.get().toFile(), jar);
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReadWriteLock;
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -45,6 +46,7 @@ import org.jackhuang.hmcl.util.io.FileUtils;
|
|||||||
import org.jackhuang.hmcl.util.io.IOUtils;
|
import org.jackhuang.hmcl.util.io.IOUtils;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
|
||||||
public class CacheRepository {
|
public class CacheRepository {
|
||||||
private Path commonDirectory;
|
private Path commonDirectory;
|
||||||
@@ -69,7 +71,7 @@ public class CacheRepository {
|
|||||||
} else
|
} else
|
||||||
index = new HashMap<>();
|
index = new HashMap<>();
|
||||||
} catch (IOException | JsonParseException e) {
|
} catch (IOException | JsonParseException e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Unable to read index file", e);
|
LOG.log(Level.WARNING, "Unable to read index file", e);
|
||||||
index = new HashMap<>();
|
index = new HashMap<>();
|
||||||
} finally {
|
} finally {
|
||||||
lock.writeLock().unlock();
|
lock.writeLock().unlock();
|
||||||
@@ -189,7 +191,7 @@ public class CacheRepository {
|
|||||||
Lock writeLock = lock.writeLock();
|
Lock writeLock = lock.writeLock();
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
index.put(url, eTagItem);
|
index.compute(eTagItem.url, updateEntity(eTagItem));
|
||||||
saveETagIndex();
|
saveETagIndex();
|
||||||
} finally {
|
} finally {
|
||||||
writeLock.unlock();
|
writeLock.unlock();
|
||||||
@@ -208,13 +210,31 @@ public class CacheRepository {
|
|||||||
Lock writeLock = lock.writeLock();
|
Lock writeLock = lock.writeLock();
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
index.put(url, eTagItem);
|
index.compute(eTagItem.url, updateEntity(eTagItem));
|
||||||
saveETagIndex();
|
saveETagIndex();
|
||||||
} finally {
|
} finally {
|
||||||
writeLock.unlock();
|
writeLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BiFunction<String, ETagItem, ETagItem> updateEntity(ETagItem newItem) {
|
||||||
|
return (key, oldItem) -> {
|
||||||
|
if (oldItem == null) {
|
||||||
|
return newItem;
|
||||||
|
} else if (oldItem.compareTo(newItem) < 0) {
|
||||||
|
Path cached = getFile(SHA1, oldItem.hash);
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(cached);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.log(Level.WARNING, "Cannot delete old file");
|
||||||
|
}
|
||||||
|
return newItem;
|
||||||
|
} else {
|
||||||
|
return oldItem;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
private final Map<String, ETagItem> joinETagIndexes(Collection<ETagItem>... indexes) {
|
private final Map<String, ETagItem> joinETagIndexes(Collection<ETagItem>... indexes) {
|
||||||
Map<String, ETagItem> eTags = new ConcurrentHashMap<>();
|
Map<String, ETagItem> eTags = new ConcurrentHashMap<>();
|
||||||
@@ -223,12 +243,7 @@ public class CacheRepository {
|
|||||||
.reduce(Stream.empty(), Stream::concat);
|
.reduce(Stream.empty(), Stream::concat);
|
||||||
|
|
||||||
stream.forEach(eTag -> {
|
stream.forEach(eTag -> {
|
||||||
eTags.compute(eTag.url, (key, oldValue) -> {
|
eTags.compute(eTag.url, updateEntity(eTag));
|
||||||
if (oldValue == null || oldValue.compareTo(eTag) < 0)
|
|
||||||
return eTag;
|
|
||||||
else
|
|
||||||
return oldValue;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return eTags;
|
return eTags;
|
||||||
|
|||||||
Reference in New Issue
Block a user