Просмотр исходного кода

Remove repository from cache when it's closed

RepositoryCache has 2 methods to remove a repository from the cache but
they are never called when a repository is closed. Users of the cache
were expected to call one of those 2 methods but how could they have
called them at proper time without having visibility of the repository
usage count.

Ideally, I would have reworked the RepositoryCache to wrap any
repository it opens in a class that would be responsible to unregister
them from the cache when it's really closed, i.e. when usage counter
reaches 0. The problem preventing the wrapping solution is the
RepositoryCache.register method that allows to register an already
opened repository in the cache. Such repositories cannot be wrapped
because callers are still holding a reference on the unwrapped
repository.

Document that RepositoryCache.close method is removing the repository
from the cache as well as closing it and rework
RepositoryCache.unregister method to only remove the repository from the
cache. Use the latter to unregister repository when Repository.doClose
is getting executed.

Change-Id: Ia364816e4da8d7b6cfa72f10758ca31aa8a1f9db
Signed-off-by: Hugo Arès <hugo.ares@ericsson.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v4.3.0.201604071810-r
Hugo Arès 8 лет назад
Родитель
Сommit
7d2b3b9e25

+ 14
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java Просмотреть файл

db.close(); db.close();
assertEquals(0, ((Repository) db).useCnt.get()); 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());
}
} }

+ 1
- 0
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java Просмотреть файл

public void close() { public void close() {
if (useCnt.decrementAndGet() == 0) { if (useCnt.decrementAndGet() == 0) {
doClose(); doClose();
RepositoryCache.unregister(this);
} }
} }



+ 34
- 9
org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java Просмотреть файл

} }


/** /**
* Remove a repository from the cache.
* Close and remove a repository from the cache.
* <p> * <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 * @param db
* repository to unregister. * repository to unregister.
public static void close(final Repository db) { public static void close(final Repository db) {
if (db.getDirectory() != null) { if (db.getDirectory() != null) {
FileKey key = FileKey.exact(db.getDirectory(), db.getFS()); FileKey key = FileKey.exact(db.getDirectory(), db.getFS());
cache.unregisterRepository(key);
cache.unregisterAndCloseRepository(key);
} }
} }


/** /**
* Remove a repository from the cache. * Remove a repository from the cache.
* <p> * <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 * @param location
* location of the repository to remove. * location of the repository to remove.
oldDb.close(); oldDb.close();
} }


private void unregisterRepository(final Key location) {
private Repository unregisterRepository(final Key location) {
Reference<Repository> oldRef = cacheMap.remove(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(); oldDb.close();
}
} }


private Collection<Key> getKeys() { private Collection<Key> getKeys() {

Загрузка…
Отмена
Сохранить