]> source.dussan.org Git - jgit.git/commitdiff
API to remove repositories from RepositoryCache 38/50238/3
authorTobias Oberlies <tobias.oberlies@sap.com>
Tue, 16 Jun 2015 08:14:44 +0000 (10:14 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 23 Jun 2015 12:30:18 +0000 (14:30 +0200)
Add methods that allow to unregister repositories from the
RepositoryCache individually.

Bug: 470234
Change-Id: Ib918a634d829c9898072ae7bdeb22b099a32b1c9
Signed-off-by: Tobias Oberlies <tobias.oberlies@sap.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java

index 0cab987e6ae611a1455345563eb675291c299ab1..df9e0294e8d0638438ef412000c4a6aacdf6700d 100644 (file)
 
 package org.eclipse.jgit.lib;
 
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -59,6 +62,7 @@ import org.eclipse.jgit.junit.RepositoryTestCase;
 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
 import org.junit.Test;
 
+@SuppressWarnings("boxing")
 public class RepositoryCacheTest extends RepositoryTestCase {
        @Test
        public void testNonBareFileKey() throws IOException {
@@ -147,4 +151,28 @@ public class RepositoryCacheTest extends RepositoryTestCase {
                d2.close();
                d2.close();
        }
+
+       @Test
+       public void testGetRegisteredWhenEmpty() {
+               assertThat(RepositoryCache.getRegisteredKeys().size(), is(0));
+       }
+
+       @Test
+       public void testGetRegistered() {
+               RepositoryCache.register(db);
+
+               assertThat(RepositoryCache.getRegisteredKeys(),
+                               hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
+               assertThat(RepositoryCache.getRegisteredKeys().size(), is(1));
+       }
+
+       @Test
+       public void testUnregister() {
+               RepositoryCache.register(db);
+               RepositoryCache
+                               .unregister(FileKey.exact(db.getDirectory(), db.getFS()));
+
+               assertThat(RepositoryCache.getRegisteredKeys().size(), is(0));
+       }
+
 }
index 0c58a0bea43c5208961dd676bace7a233084fbe2..23cc264c1cf9786d74f9e1f35049144ccb464a59 100644 (file)
@@ -47,6 +47,8 @@ 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;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -143,6 +145,28 @@ public class RepositoryCache {
                }
        }
 
+       /**
+        * Remove a repository from the cache.
+        * <p>
+        * Removes a repository from the cache, if it is still registered here,
+        * permitting it to close.
+        *
+        * @param location
+        *            location of the repository to remove.
+        * @since 4.1
+        */
+       public static void unregister(Key location) {
+               cache.unregisterRepository(location);
+       }
+
+       /**
+        * @return the locations of all repositories registered in the cache.
+        * @since 4.1
+        */
+       public static Collection<Key> getRegisteredKeys() {
+               return cache.getKeys();
+       }
+
        /** Unregister all repositories from the cache. */
        public static void clear() {
                cache.clearAll();
@@ -195,6 +219,10 @@ public class RepositoryCache {
                        oldDb.close();
        }
 
+       private Collection<Key> getKeys() {
+               return new ArrayList<Key>(cacheMap.keySet());
+       }
+
        private void clearAll() {
                for (int stage = 0; stage < 2; stage++) {
                        for (Iterator<Map.Entry<Key, Reference<Repository>>> i = cacheMap