db.close();
assertEquals(0, ((Repository) db).useCnt.get());
}
+
+ public void testRepositoryUnregisteringWhenClosing() throws Exception {
+ FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
+ Repository d2 = RepositoryCache.open(loc);
+ assertEquals(1, d2.useCnt.get());
+ assertThat(RepositoryCache.getRegisteredKeys(),
+ hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
+ assertEquals(1, RepositoryCache.getRegisteredKeys().size());
+
+ d2.close();
+
+ assertEquals(0, d2.useCnt.get());
+ assertEquals(0, RepositoryCache.getRegisteredKeys().size());
+ }
}
}
/**
- * 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.
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.
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() {