|
|
|
@ -45,8 +45,6 @@ package org.eclipse.jgit.lib;
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.lang.ref.Reference; |
|
|
|
|
import java.lang.ref.SoftReference; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.Iterator; |
|
|
|
@ -202,8 +200,7 @@ public class RepositoryCache {
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
FileKey key = new FileKey(gitDir, repo.getFS()); |
|
|
|
|
Reference<Repository> repoRef = cache.cacheMap.get(key); |
|
|
|
|
return repoRef != null && repoRef.get() == repo; |
|
|
|
|
return cache.cacheMap.get(key) == repo; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Unregister all repositories from the cache. */ |
|
|
|
@ -219,7 +216,7 @@ public class RepositoryCache {
|
|
|
|
|
cache.configureEviction(repositoryCacheConfig); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private final ConcurrentHashMap<Key, Reference<Repository>> cacheMap; |
|
|
|
|
private final ConcurrentHashMap<Key, Repository> cacheMap; |
|
|
|
|
|
|
|
|
|
private final Lock[] openLocks; |
|
|
|
|
|
|
|
|
@ -228,7 +225,7 @@ public class RepositoryCache {
|
|
|
|
|
private volatile long expireAfter; |
|
|
|
|
|
|
|
|
|
private RepositoryCache() { |
|
|
|
|
cacheMap = new ConcurrentHashMap<Key, Reference<Repository>>(); |
|
|
|
|
cacheMap = new ConcurrentHashMap<Key, Repository>(); |
|
|
|
|
openLocks = new Lock[4]; |
|
|
|
|
for (int i = 0; i < openLocks.length; i++) { |
|
|
|
|
openLocks[i] = new Lock(); |
|
|
|
@ -261,19 +258,15 @@ public class RepositoryCache {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("resource") |
|
|
|
|
private Repository openRepository(final Key location, |
|
|
|
|
final boolean mustExist) throws IOException { |
|
|
|
|
Reference<Repository> ref = cacheMap.get(location); |
|
|
|
|
Repository db = ref != null ? ref.get() : null; |
|
|
|
|
Repository db = cacheMap.get(location); |
|
|
|
|
if (db == null) { |
|
|
|
|
synchronized (lockFor(location)) { |
|
|
|
|
ref = cacheMap.get(location); |
|
|
|
|
db = ref != null ? ref.get() : null; |
|
|
|
|
db = cacheMap.get(location); |
|
|
|
|
if (db == null) { |
|
|
|
|
db = location.open(mustExist); |
|
|
|
|
ref = new SoftReference<Repository>(db); |
|
|
|
|
cacheMap.put(location, ref); |
|
|
|
|
cacheMap.put(location, db); |
|
|
|
|
} else { |
|
|
|
|
db.incrementOpen(); |
|
|
|
|
} |
|
|
|
@ -285,16 +278,13 @@ public class RepositoryCache {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void registerRepository(final Key location, final Repository db) { |
|
|
|
|
SoftReference<Repository> newRef = new SoftReference<Repository>(db); |
|
|
|
|
Reference<Repository> oldRef = cacheMap.put(location, newRef); |
|
|
|
|
Repository oldDb = oldRef != null ? oldRef.get() : null; |
|
|
|
|
Repository oldDb = cacheMap.put(location, db); |
|
|
|
|
if (oldDb != null) |
|
|
|
|
oldDb.close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Repository unregisterRepository(final Key location) { |
|
|
|
|
Reference<Repository> oldRef = cacheMap.remove(location); |
|
|
|
|
return oldRef != null ? oldRef.get() : null; |
|
|
|
|
return cacheMap.remove(location); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isExpired(Repository db) { |
|
|
|
@ -316,8 +306,7 @@ public class RepositoryCache {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void clearAllExpired() { |
|
|
|
|
for (Reference<Repository> ref : cacheMap.values()) { |
|
|
|
|
Repository db = ref.get(); |
|
|
|
|
for (Repository db : cacheMap.values()) { |
|
|
|
|
if (isExpired(db)) { |
|
|
|
|
RepositoryCache.close(db); |
|
|
|
|
} |
|
|
|
@ -325,7 +314,7 @@ public class RepositoryCache {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void clearAll() { |
|
|
|
|
for (Iterator<Map.Entry<Key, Reference<Repository>>> i = cacheMap |
|
|
|
|
for (Iterator<Map.Entry<Key, Repository>> i = cacheMap |
|
|
|
|
.entrySet().iterator(); i.hasNext();) { |
|
|
|
|
unregisterAndCloseRepository(i.next().getKey()); |
|
|
|
|
} |
|
|
|
|