清理 CacheRepository (#2672)
This commit is contained in:
@@ -63,7 +63,7 @@ public class DefaultCacheRepository extends CacheRepository {
|
|||||||
lock.writeLock().lock();
|
lock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
if (Files.isRegularFile(indexFile))
|
if (Files.isRegularFile(indexFile))
|
||||||
index = JsonUtils.fromNonNullJson(FileUtils.readText(indexFile.toFile()), Index.class);
|
index = JsonUtils.fromNonNullJson(FileUtils.readText(indexFile), Index.class);
|
||||||
else
|
else
|
||||||
index = new Index();
|
index = new Index();
|
||||||
} catch (IOException | JsonParseException e) {
|
} catch (IOException | JsonParseException e) {
|
||||||
@@ -180,7 +180,7 @@ public class DefaultCacheRepository extends CacheRepository {
|
|||||||
hash = DigestUtils.digestToString(SHA1, path);
|
hash = DigestUtils.digestToString(SHA1, path);
|
||||||
|
|
||||||
Path cache = getFile(SHA1, hash);
|
Path cache = getFile(SHA1, hash);
|
||||||
FileUtils.copyFile(path.toFile(), cache.toFile());
|
FileUtils.copyFile(path, cache);
|
||||||
|
|
||||||
Lock writeLock = lock.writeLock();
|
Lock writeLock = lock.writeLock();
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
@@ -198,7 +198,7 @@ public class DefaultCacheRepository extends CacheRepository {
|
|||||||
private void saveIndex() {
|
private void saveIndex() {
|
||||||
if (indexFile == null || index == null) return;
|
if (indexFile == null || index == null) return;
|
||||||
try {
|
try {
|
||||||
FileUtils.writeText(indexFile.toFile(), JsonUtils.GSON.toJson(index));
|
FileUtils.writeText(indexFile, JsonUtils.GSON.toJson(index));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logging.LOG.log(Level.SEVERE, "Unable to save index.json", e);
|
Logging.LOG.log(Level.SEVERE, "Unable to save index.json", e);
|
||||||
}
|
}
|
||||||
@@ -217,7 +217,7 @@ public class DefaultCacheRepository extends CacheRepository {
|
|||||||
* // assets and versions will not be included in index.
|
* // assets and versions will not be included in index.
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
private class Index implements Validation {
|
private static final class Index implements Validation {
|
||||||
private final Set<LibraryIndex> libraries;
|
private final Set<LibraryIndex> libraries;
|
||||||
|
|
||||||
public Index() {
|
public Index() {
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import org.jackhuang.hmcl.util.io.IOUtils;
|
|||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.RandomAccessFile;
|
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
@@ -35,6 +34,7 @@ import java.nio.channels.FileChannel;
|
|||||||
import java.nio.channels.FileLock;
|
import java.nio.channels.FileLock;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -69,7 +69,7 @@ public class CacheRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Files.isRegularFile(indexFile)) {
|
if (Files.isRegularFile(indexFile)) {
|
||||||
ETagIndex raw = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile.toFile()), ETagIndex.class);
|
ETagIndex raw = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile), ETagIndex.class);
|
||||||
if (raw == null)
|
if (raw == null)
|
||||||
index = new HashMap<>();
|
index = new HashMap<>();
|
||||||
else
|
else
|
||||||
@@ -123,12 +123,12 @@ public class CacheRepository {
|
|||||||
public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
|
public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
|
||||||
Path cache = getFile(algorithm, hash);
|
Path cache = getFile(algorithm, hash);
|
||||||
if (Files.isRegularFile(cache)) return;
|
if (Files.isRegularFile(cache)) return;
|
||||||
FileUtils.copyFile(path.toFile(), cache.toFile());
|
FileUtils.copyFile(path, cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Path cacheFile(Path path, String algorithm, String hash) throws IOException {
|
public Path cacheFile(Path path, String algorithm, String hash) throws IOException {
|
||||||
Path cache = getFile(algorithm, hash);
|
Path cache = getFile(algorithm, hash);
|
||||||
FileUtils.copyFile(path.toFile(), cache.toFile());
|
FileUtils.copyFile(path, cache);
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +222,7 @@ public class CacheRepository {
|
|||||||
cacheData(() -> {
|
cacheData(() -> {
|
||||||
String hash = DigestUtils.digestToString(SHA1, bytes);
|
String hash = DigestUtils.digestToString(SHA1, bytes);
|
||||||
Path cached = getFile(SHA1, hash);
|
Path cached = getFile(SHA1, hash);
|
||||||
FileUtils.writeBytes(cached.toFile(), bytes);
|
FileUtils.writeBytes(cached, bytes);
|
||||||
return new CacheResult(hash, cached);
|
return new CacheResult(hash, cached);
|
||||||
}, conn);
|
}, conn);
|
||||||
}
|
}
|
||||||
@@ -287,7 +287,7 @@ public class CacheRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveETagIndex() throws IOException {
|
public void saveETagIndex() throws IOException {
|
||||||
try (RandomAccessFile file = new RandomAccessFile(indexFile.toFile(), "rw"); FileChannel channel = file.getChannel()) {
|
try (FileChannel channel = FileChannel.open(indexFile, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
|
||||||
FileLock lock = channel.lock();
|
FileLock lock = channel.lock();
|
||||||
try {
|
try {
|
||||||
ETagIndex indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), ETagIndex.class);
|
ETagIndex indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), ETagIndex.class);
|
||||||
@@ -371,7 +371,7 @@ public class CacheRepository {
|
|||||||
/**
|
/**
|
||||||
* Universal cache
|
* Universal cache
|
||||||
*/
|
*/
|
||||||
public static class Storage {
|
public static final class Storage {
|
||||||
private final String name;
|
private final String name;
|
||||||
private Map<String, Object> storage;
|
private Map<String, Object> storage;
|
||||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||||
@@ -409,7 +409,7 @@ public class CacheRepository {
|
|||||||
try {
|
try {
|
||||||
indexFile = cacheDirectory.resolve(name + ".json");
|
indexFile = cacheDirectory.resolve(name + ".json");
|
||||||
if (Files.isRegularFile(indexFile)) {
|
if (Files.isRegularFile(indexFile)) {
|
||||||
joinEntries(JsonUtils.fromNonNullJson(FileUtils.readText(indexFile.toFile()), new TypeToken<Map<String, Object>>() {
|
joinEntries(JsonUtils.fromNonNullJson(FileUtils.readText(indexFile), new TypeToken<Map<String, Object>>() {
|
||||||
}.getType()));
|
}.getType()));
|
||||||
}
|
}
|
||||||
} catch (IOException | JsonParseException e) {
|
} catch (IOException | JsonParseException e) {
|
||||||
@@ -420,7 +420,7 @@ public class CacheRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveToFile() {
|
public void saveToFile() {
|
||||||
try (RandomAccessFile file = new RandomAccessFile(indexFile.toFile(), "rw"); FileChannel channel = file.getChannel()) {
|
try (FileChannel channel = FileChannel.open(indexFile, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
|
||||||
FileLock lock = channel.lock();
|
FileLock lock = channel.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), new TypeToken<Map<String, Object>>() {
|
Map<String, Object> indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), new TypeToken<Map<String, Object>>() {
|
||||||
|
|||||||
Reference in New Issue
Block a user