Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remove deleted files from cache #1304

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/util/cache/FileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private static void schedule(TimerTask task, long delay) {
protected String name;
protected final int softLimit, minElements, hardLimit;
protected final long period; // msecs
private boolean removeDeleted = false;

private final AtomicBoolean disabled = new AtomicBoolean(false); // cache is disabled
protected final AtomicBoolean hasScheduled = new AtomicBoolean(false); // a cleanup is scheduled
Expand Down Expand Up @@ -147,6 +148,23 @@ public FileCache(int minElementsInMemory, int softLimit, int hardLimit, int peri
this("", minElementsInMemory, softLimit, hardLimit, period);
}

/**
* Constructor.
*
* @param name of file cache
* @param minElementsInMemory keep this number in the cache
* @param softLimit trigger a cleanup if it goes over this number.
* @param hardLimit if > 0, never allow more than this many elements. This causes a cleanup to be done in the calling
* thread.
* @param period if > 0, do periodic cleanups every this number of seconds.
* @param removeDeleted if true, then remove deleted files from the cache when a cleanup is performed.
*/
public FileCache(String name, int minElementsInMemory, int softLimit, int hardLimit, int period,
boolean removeDeleted) {
this(name, minElementsInMemory, softLimit, hardLimit, period);
this.removeDeleted = removeDeleted;
}

/**
* Constructor.
*
Expand Down Expand Up @@ -699,9 +717,11 @@ public int compareTo(Tracker o) {
synchronized void cleanup(int maxElements) {

try {
for (CacheElement.CacheFile cacheFile : files.values()) {
if (!Files.exists(Paths.get(cacheFile.ncfile.getLocation()))) {
remove(cacheFile);
if (removeDeleted) {
for (CacheElement.CacheFile cacheFile : files.values()) {
if (!Files.exists(Paths.get(cacheFile.ncfile.getLocation()))) {
remove(cacheFile);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TestRandomAccessFileCacheCleanup {
@BeforeClass
public static void enableCache() {
RandomAccessFile.shutdown();
cache = new FileCache("RandomAccessFile", 0, 1, 1, 0);
cache = new FileCache("RandomAccessFile", 0, 1, 1, 0, true);
RandomAccessFile.setGlobalFileCache(cache);
assertThat(cache.showCache().size()).isEqualTo(0);
}
Expand Down
Loading