Use ReadWriteLock instead of synchronized
This commit is contained in:
@@ -28,6 +28,9 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -36,7 +39,7 @@ public class HMCLLocalRepository extends LocalRepository {
|
|||||||
|
|
||||||
private Path librariesDir;
|
private Path librariesDir;
|
||||||
private Path indexFile;
|
private Path indexFile;
|
||||||
|
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||||
private Index index = null;
|
private Index index = null;
|
||||||
|
|
||||||
public HMCLLocalRepository() {
|
public HMCLLocalRepository() {
|
||||||
@@ -62,11 +65,14 @@ public class HMCLLocalRepository extends LocalRepository {
|
|||||||
librariesDir = commonDir.resolve("libraries");
|
librariesDir = commonDir.resolve("libraries");
|
||||||
indexFile = getCacheDirectory().resolve("index.json");
|
indexFile = getCacheDirectory().resolve("index.json");
|
||||||
|
|
||||||
|
lock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
index = Constants.GSON.fromJson(FileUtils.readText(indexFile.toFile()), Index.class);
|
index = Constants.GSON.fromJson(FileUtils.readText(indexFile.toFile()), Index.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Unable to read index file", e);
|
Logging.LOG.log(Level.WARNING, "Unable to read index file", e);
|
||||||
index = new Index();
|
index = new Index();
|
||||||
|
} finally {
|
||||||
|
lock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,8 +85,13 @@ public class HMCLLocalRepository extends LocalRepository {
|
|||||||
* @param jar the file of library
|
* @param jar the file of library
|
||||||
*/
|
*/
|
||||||
public void tryCacheLibrary(Library library, Path jar) {
|
public void tryCacheLibrary(Library library, Path jar) {
|
||||||
if (index.getLibraries().stream().anyMatch(it -> library.getName().equals(it.getName())))
|
lock.readLock().lock();
|
||||||
return;
|
try {
|
||||||
|
if (index.getLibraries().stream().anyMatch(it -> library.getName().equals(it.getName())))
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
lock.readLock().unlock();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
LibraryDownloadInfo info = library.getDownload();
|
LibraryDownloadInfo info = library.getDownload();
|
||||||
@@ -106,25 +117,32 @@ public class HMCLLocalRepository extends LocalRepository {
|
|||||||
* @param library the library we check if cached.
|
* @param library the library we check if cached.
|
||||||
* @return the cached path if exists, otherwise empty
|
* @return the cached path if exists, otherwise empty
|
||||||
*/
|
*/
|
||||||
public synchronized Optional<Path> getLibrary(Library library) {
|
public Optional<Path> getLibrary(Library library) {
|
||||||
LibraryDownloadInfo info = library.getDownload();
|
LibraryDownloadInfo info = library.getDownload();
|
||||||
String hash = info.getSha1();
|
String hash = info.getSha1();
|
||||||
|
|
||||||
if (fileExists(SHA1, hash))
|
if (fileExists(SHA1, hash))
|
||||||
return Optional.of(getFile(SHA1, hash));
|
return Optional.of(getFile(SHA1, hash));
|
||||||
|
|
||||||
// check if this library is from Forge
|
Lock readLock = lock.readLock();
|
||||||
List<LibraryIndex> libraries = index.getLibraries().stream()
|
readLock.lock();
|
||||||
.filter(it -> it.getName().equals(library.getName()))
|
|
||||||
.collect(Collectors.toList());
|
try {
|
||||||
for (LibraryIndex libIndex : libraries) {
|
// check if this library is from Forge
|
||||||
if (fileExists(SHA1, libIndex.getHash())) {
|
List<LibraryIndex> libraries = index.getLibraries().stream()
|
||||||
Path file = getFile(SHA1, libIndex.getHash());
|
.filter(it -> it.getName().equals(library.getName()))
|
||||||
if (libIndex.getType().equalsIgnoreCase(LibraryIndex.TYPE_FORGE)) {
|
.collect(Collectors.toList());
|
||||||
if (LibraryDownloadTask.checksumValid(file.toFile(), library.getChecksums()))
|
for (LibraryIndex libIndex : libraries) {
|
||||||
return Optional.of(file);
|
if (fileExists(SHA1, libIndex.getHash())) {
|
||||||
|
Path file = getFile(SHA1, libIndex.getHash());
|
||||||
|
if (libIndex.getType().equalsIgnoreCase(LibraryIndex.TYPE_FORGE)) {
|
||||||
|
if (LibraryDownloadTask.checksumValid(file.toFile(), library.getChecksums()))
|
||||||
|
return Optional.of(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
readLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
// check old common directory
|
// check old common directory
|
||||||
@@ -158,7 +176,7 @@ public class HMCLLocalRepository extends LocalRepository {
|
|||||||
* @return cached file location
|
* @return cached file location
|
||||||
* @throws IOException if failed to calculate hash code of {@code path} or copy the file to cache
|
* @throws IOException if failed to calculate hash code of {@code path} or copy the file to cache
|
||||||
*/
|
*/
|
||||||
public synchronized Path cacheLibrary(Library library, Path path, boolean forge) throws IOException {
|
public Path cacheLibrary(Library library, Path path, boolean forge) throws IOException {
|
||||||
String hash = library.getDownload().getSha1();
|
String hash = library.getDownload().getSha1();
|
||||||
if (hash == null)
|
if (hash == null)
|
||||||
hash = Hex.encodeHex(DigestUtils.digest(SHA1, path));
|
hash = Hex.encodeHex(DigestUtils.digest(SHA1, path));
|
||||||
@@ -166,9 +184,15 @@ public class HMCLLocalRepository extends LocalRepository {
|
|||||||
Path cache = getFile(SHA1, hash);
|
Path cache = getFile(SHA1, hash);
|
||||||
FileUtils.copyFile(path.toFile(), cache.toFile());
|
FileUtils.copyFile(path.toFile(), cache.toFile());
|
||||||
|
|
||||||
LibraryIndex libIndex = new LibraryIndex(library.getName(), hash, forge ? LibraryIndex.TYPE_FORGE : LibraryIndex.TYPE_JAR);
|
Lock writeLock = lock.writeLock();
|
||||||
index.getLibraries().add(libIndex);
|
writeLock.lock();
|
||||||
saveIndex();
|
try {
|
||||||
|
LibraryIndex libIndex = new LibraryIndex(library.getName(), hash, forge ? LibraryIndex.TYPE_FORGE : LibraryIndex.TYPE_JAR);
|
||||||
|
index.getLibraries().add(libIndex);
|
||||||
|
saveIndex();
|
||||||
|
} finally {
|
||||||
|
writeLock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user