diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2016-04-07 13:06:14 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2016-04-07 13:06:23 +0200 |
commit | 27d06cb8d5763ea28008d90c3189e7ffb875a75d (patch) | |
tree | 4fb2536bb4463015bc7109fc9d0c4d008e6e71a1 /org.eclipse.jgit | |
parent | f56b9001fc61e50a94a4ae2f7dbb8d4bc9158e82 (diff) | |
parent | 7d2b3b9e25a430241a2d277e0cb222ad40cd0c2c (diff) | |
download | jgit-27d06cb8d5763ea28008d90c3189e7ffb875a75d.tar.gz jgit-27d06cb8d5763ea28008d90c3189e7ffb875a75d.zip |
Merge branch 'master' into stable-4.3
* master:
Remove repository from cache when it's closed
Fix RefDirectory not closing resources
Fix repository cache never closing repository
Change-Id: I9dc9d017806cba25125f69b53812cc3e062ef975
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
3 files changed, 44 insertions, 15 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java index b8c2fb4a51..6f3166a680 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java @@ -203,10 +203,10 @@ public class RefDirectory extends RefDatabase { @Override public void close() { - // We have no resources to close. + clearReferences(); } - void rescan() { + private void clearReferences() { looseRefs.set(RefList.<LooseRef> emptyList()); packedRefs.set(PackedRefList.NO_PACKED_REFS); } @@ -214,7 +214,7 @@ public class RefDirectory extends RefDatabase { @Override public void refresh() { super.refresh(); - rescan(); + clearReferences(); } @Override diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index f8266133a6..ba0dea39f5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -110,7 +110,8 @@ public abstract class Repository implements AutoCloseable { return globalListeners; } - private final AtomicInteger useCnt = new AtomicInteger(1); + /** Use counter */ + final AtomicInteger useCnt = new AtomicInteger(1); /** Metadata directory holding the repository's critical files. */ private final File gitDir; @@ -864,6 +865,7 @@ public abstract class Repository implements AutoCloseable { public void close() { if (useCnt.decrementAndGet() == 0) { doClose(); + RepositoryCache.unregister(this); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java index 23cc264c1c..22b5fcd112 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java @@ -130,10 +130,10 @@ public class RepositoryCache { } /** - * Remove a repository from the cache. + * Close and remove a repository from the cache. * <p> - * Removes a repository from the cache, if it is still registered here, - * permitting it to close. + * Removes a repository from the cache, if it is still registered here, and + * close it. * * @param db * repository to unregister. @@ -141,15 +141,35 @@ public class RepositoryCache { public static void close(final Repository db) { if (db.getDirectory() != null) { FileKey key = FileKey.exact(db.getDirectory(), db.getFS()); - cache.unregisterRepository(key); + cache.unregisterAndCloseRepository(key); } } /** * Remove a repository from the cache. * <p> - * Removes a repository from the cache, if it is still registered here, - * permitting it to close. + * Removes a repository from the cache, if it is still registered here. This + * method will not close the repository, only remove it from the cache. See + * {@link RepositoryCache#close(Repository)} to remove and close the + * repository. + * + * @param db + * repository to unregister. + * @since 4.3 + */ + public static void unregister(final Repository db) { + if (db.getDirectory() != null) { + unregister(FileKey.exact(db.getDirectory(), db.getFS())); + } + } + + /** + * Remove a repository from the cache. + * <p> + * Removes a repository from the cache, if it is still registered here. This + * method will not close the repository, only remove it from the cache. See + * {@link RepositoryCache#close(Repository)} to remove and close the + * repository. * * @param location * location of the repository to remove. @@ -196,15 +216,17 @@ public class RepositoryCache { db = location.open(mustExist); ref = new SoftReference<Repository>(db); cacheMap.put(location, ref); + } else { + db.incrementOpen(); } } + } else { + db.incrementOpen(); } - db.incrementOpen(); return db; } private void registerRepository(final Key location, final Repository db) { - db.incrementOpen(); SoftReference<Repository> newRef = new SoftReference<Repository>(db); Reference<Repository> oldRef = cacheMap.put(location, newRef); Repository oldDb = oldRef != null ? oldRef.get() : null; @@ -212,11 +234,16 @@ public class RepositoryCache { oldDb.close(); } - private void unregisterRepository(final Key location) { + private Repository unregisterRepository(final Key location) { Reference<Repository> oldRef = cacheMap.remove(location); - Repository oldDb = oldRef != null ? oldRef.get() : null; - if (oldDb != null) + return oldRef != null ? oldRef.get() : null; + } + + private void unregisterAndCloseRepository(final Key location) { + Repository oldDb = unregisterRepository(location); + if (oldDb != null) { oldDb.close(); + } } private Collection<Key> getKeys() { |