/** * Releases the repository acquired via {@link #add} or {@link #get} method. Decrements an * openCounter for the repository and if it reaches 0 repository is closed and removed from the * cache. Does nothing if repository is not present found in the cache. * * @param db repository to release */ synchronized void release(@NotNull Repository db) { RepositoryCache.FileKey key = RepositoryCache.FileKey.exact(db.getDirectory(), FS.DETECTED); CachedRepository cachedRepository = myRepositories.get(key); if (cachedRepository != null && cachedRepository.getRepository() == db && cachedRepository.dec() == 0) { myRepositories.remove(key); db.close(); } }
/** * Returns a repository for the given key or null if repository is not found in cache. If * repository is found its openCounter is incremented. When the caller is done with repository it * must call {@link #release} method. * * @param key repository key * @return see above */ @Nullable synchronized Repository get(@NotNull RepositoryCache.FileKey key) { CachedRepository cachedRepository = myRepositories.get(key); if (cachedRepository != null) { Repository result = cachedRepository.getRepository(); cachedRepository.inc(); return result; } return null; }
/** * Adds a new repository with the specified key in cache. Returns the added repository if there * was no repository in cache associated with the given key, otherwise existing repository * associated with the key is returned and its openCounter is incremented. When the caller is done * with repository it must call the {@link #release} method. * * @param key repository key * @param db repository * @return see above */ @NotNull synchronized Repository add(@NotNull RepositoryCache.FileKey key, @NotNull Repository db) { CachedRepository existing = myRepositories.get(key); if (existing == null) { myRepositories.put(key, new CachedRepository(db)); return db; } else { Repository result = existing.getRepository(); existing.inc(); return result; } }