Commit 6e127d01 by olly Committed by Oliver Woodman

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
parent 194caf23
...@@ -382,30 +382,46 @@ public final class SimpleCache implements Cache { ...@@ -382,30 +382,46 @@ public final class SimpleCache implements Cache {
} }
index.load(); index.load();
loadDirectory(cacheDir, /* isRootDirectory= */ true);
index.removeEmpty();
File[] files = cacheDir.listFiles(); try {
index.store();
} catch (CacheException e) {
Log.e(TAG, "Storing index file failed", e);
}
}
private void loadDirectory(File directory, boolean isRootDirectory) {
File[] files = directory.listFiles();
if (files == null) { if (files == null) {
// Not a directory.
return;
}
if (!isRootDirectory && files.length == 0) {
// Empty non-root directory.
directory.delete();
return; return;
} }
for (File file : files) { for (File file : files) {
if (file.getName().equals(CachedContentIndex.FILE_NAME)) { String fileName = file.getName();
continue; if (fileName.indexOf('.') == -1) {
} loadDirectory(file, /* isRootDirectory= */ false);
SimpleCacheSpan span =
file.length() > 0 ? SimpleCacheSpan.createCacheEntry(file, file.length(), index) : null;
if (span != null) {
addSpan(span);
} else { } else {
file.delete(); 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();
}
} }
} }
index.removeEmpty();
try {
index.store();
} catch (CacheException e) {
Log.e(TAG, "Storing index file failed", e);
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment