SimpleCache: Scan sub-directories during initialization

This is the initialization part of mitigating issue #4253. The
remaining work is on the writing side, and is simply a case of
having startFile return File instances that are sharded into
sub-directories. We still need to decide what scheme we want
to use for doing that.

Issue: #4253
PiperOrigin-RevId: 228306327
This commit is contained in:
olly 2019-01-08 11:51:15 +00:00 committed by Oliver Woodman
parent 194caf23e5
commit 6e127d0152

View file

@ -382,25 +382,9 @@ public final class SimpleCache implements Cache {
}
index.load();
File[] files = cacheDir.listFiles();
if (files == null) {
return;
}
for (File file : files) {
if (file.getName().equals(CachedContentIndex.FILE_NAME)) {
continue;
}
SimpleCacheSpan span =
file.length() > 0 ? SimpleCacheSpan.createCacheEntry(file, file.length(), index) : null;
if (span != null) {
addSpan(span);
} else {
file.delete();
}
}
loadDirectory(cacheDir, /* isRootDirectory= */ true);
index.removeEmpty();
try {
index.store();
} catch (CacheException e) {
@ -408,6 +392,38 @@ public final class SimpleCache implements Cache {
}
}
private void loadDirectory(File directory, boolean isRootDirectory) {
File[] files = directory.listFiles();
if (files == null) {
// Not a directory.
return;
}
if (!isRootDirectory && files.length == 0) {
// Empty non-root directory.
directory.delete();
return;
}
for (File file : files) {
String fileName = file.getName();
if (fileName.indexOf('.') == -1) {
loadDirectory(file, /* isRootDirectory= */ false);
} else {
if (isRootDirectory && CachedContentIndex.FILE_NAME.equals(fileName)) {
// Skip the (expected) index file in the root directory.
continue;
}
long fileLength = file.length();
SimpleCacheSpan span =
fileLength > 0 ? SimpleCacheSpan.createCacheEntry(file, fileLength, index) : null;
if (span != null) {
addSpan(span);
} else {
file.delete();
}
}
}
}
/**
* Adds a cached span to the in-memory representation.
*