Commit a6e27701 by eguven Committed by Oliver Woodman

Upgrade SimpleCacheSpan files during createCacheEntry call.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=138379386
parent ff77d1e7
...@@ -54,7 +54,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { ...@@ -54,7 +54,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
assertReadDataContentLength(cacheDataSource, false, false); assertReadDataContentLength(cacheDataSource, false, false);
File[] files = cacheDir.listFiles(); File[] files = cacheDir.listFiles();
for (File file : files) { for (File file : files) {
if (file.getName().endsWith(SimpleCacheSpan.SUFFIX)) { if (!file.getName().equals(CachedContentIndex.FILE_NAME)) {
assertTrue(file.length() <= MAX_CACHE_FILE_SIZE); assertTrue(file.length() <= MAX_CACHE_FILE_SIZE);
} }
} }
......
...@@ -83,7 +83,9 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase { ...@@ -83,7 +83,9 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase {
File wrongEscapedV2file = createTestFile("asd%za.3.4.v2.exo"); File wrongEscapedV2file = createTestFile("asd%za.3.4.v2.exo");
File v1File = createTestFile("asd\u00aa.5.6.v1.exo"); File v1File = createTestFile("asd\u00aa.5.6.v1.exo");
SimpleCacheSpan.upgradeOldFiles(cacheDir, index); for (File file : cacheDir.listFiles()) {
SimpleCacheSpan.createCacheEntry(file, index);
}
assertTrue(v3file.exists()); assertTrue(v3file.exists());
assertFalse(v2file.exists()); assertFalse(v2file.exists());
......
...@@ -227,21 +227,14 @@ public final class SimpleCache implements Cache { ...@@ -227,21 +227,14 @@ public final class SimpleCache implements Cache {
index.load(); index.load();
SimpleCacheSpan.upgradeOldFiles(cacheDir, index);
File[] files = cacheDir.listFiles(); File[] files = cacheDir.listFiles();
if (files == null) { if (files == null) {
return; return;
} }
for (File file : files) { for (File file : files) {
String name = file.getName(); if (file.getName().equals(CachedContentIndex.FILE_NAME)) {
if (!name.endsWith(SimpleCacheSpan.SUFFIX)) {
if (!name.equals(CachedContentIndex.FILE_NAME)) {
file.delete(); // Delete unknown files
}
continue; continue;
} }
SimpleCacheSpan span = file.length() > 0 SimpleCacheSpan span = file.length() > 0
? SimpleCacheSpan.createCacheEntry(file, index) : null; ? SimpleCacheSpan.createCacheEntry(file, index) : null;
if (span != null) { if (span != null) {
......
...@@ -27,14 +27,13 @@ import java.util.regex.Pattern; ...@@ -27,14 +27,13 @@ import java.util.regex.Pattern;
*/ */
/*package*/ final class SimpleCacheSpan extends CacheSpan { /*package*/ final class SimpleCacheSpan extends CacheSpan {
private static final String FILE_EXTENSION = "exo"; private static final String SUFFIX = ".v3.exo";
public static final String SUFFIX = ".v3." + FILE_EXTENSION;
private static final Pattern CACHE_FILE_PATTERN_V1 = Pattern.compile( private static final Pattern CACHE_FILE_PATTERN_V1 = Pattern.compile(
"^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\." + FILE_EXTENSION + "$", Pattern.DOTALL); "^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$", Pattern.DOTALL);
private static final Pattern CACHE_FILE_PATTERN_V2 = Pattern.compile( private static final Pattern CACHE_FILE_PATTERN_V2 = Pattern.compile(
"^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\." + FILE_EXTENSION + "$", Pattern.DOTALL); "^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL);
private static final Pattern CACHE_FILE_PATTERN_V3 = Pattern.compile( private static final Pattern CACHE_FILE_PATTERN_V3 = Pattern.compile(
"^(\\d+)\\.(\\d+)\\.(\\d+)\\.v3\\." + FILE_EXTENSION + "$", Pattern.DOTALL); "^(\\d+)\\.(\\d+)\\.(\\d+)\\.v3\\.exo$", Pattern.DOTALL);
public static File getCacheFile(File cacheDir, int id, long position, public static File getCacheFile(File cacheDir, int id, long position,
long lastAccessTimestamp) { long lastAccessTimestamp) {
...@@ -54,7 +53,7 @@ import java.util.regex.Pattern; ...@@ -54,7 +53,7 @@ import java.util.regex.Pattern;
} }
/** /**
* Creates a cache span from an underlying cache file. * Creates a cache span from an underlying cache file. Upgrades the file if necessary.
* *
* @param file The cache file. * @param file The cache file.
* @param index Cached content index. * @param index Cached content index.
...@@ -62,7 +61,15 @@ import java.util.regex.Pattern; ...@@ -62,7 +61,15 @@ import java.util.regex.Pattern;
* present in the content index. * present in the content index.
*/ */
public static SimpleCacheSpan createCacheEntry(File file, CachedContentIndex index) { public static SimpleCacheSpan createCacheEntry(File file, CachedContentIndex index) {
Matcher matcher = CACHE_FILE_PATTERN_V3.matcher(file.getName()); String name = file.getName();
if (!name.endsWith(SUFFIX)) {
file = upgradeFile(file, index);
if (file == null) {
return null;
}
}
Matcher matcher = CACHE_FILE_PATTERN_V3.matcher(name);
if (!matcher.matches()) { if (!matcher.matches()) {
return null; return null;
} }
...@@ -73,36 +80,29 @@ import java.util.regex.Pattern; ...@@ -73,36 +80,29 @@ import java.util.regex.Pattern;
Long.parseLong(matcher.group(3)), file); Long.parseLong(matcher.group(3)), file);
} }
/** Upgrades span files with old versions. */ private static File upgradeFile(File file, CachedContentIndex index) {
public static void upgradeOldFiles(File cacheDir, CachedContentIndex index) {
for (File file : cacheDir.listFiles()) {
String name = file.getName();
if (!name.endsWith(SUFFIX) && name.endsWith(FILE_EXTENSION)) {
upgradeFile(file, index);
}
}
}
private static void upgradeFile(File file, CachedContentIndex index) {
String key; String key;
String filename = file.getName(); String filename = file.getName();
Matcher matcher = CACHE_FILE_PATTERN_V2.matcher(filename); Matcher matcher = CACHE_FILE_PATTERN_V2.matcher(filename);
if (matcher.matches()) { if (matcher.matches()) {
key = Util.unescapeFileName(matcher.group(1)); key = Util.unescapeFileName(matcher.group(1));
if (key == null) { if (key == null) {
return; return null;
} }
} else { } else {
matcher = CACHE_FILE_PATTERN_V1.matcher(filename); matcher = CACHE_FILE_PATTERN_V1.matcher(filename);
if (!matcher.matches()) { if (!matcher.matches()) {
return; return null;
} }
key = matcher.group(1); // Keys were not escaped in version 1. key = matcher.group(1); // Keys were not escaped in version 1.
} }
File newCacheFile = getCacheFile(file.getParentFile(), index.assignIdForKey(key), File newCacheFile = getCacheFile(file.getParentFile(), index.assignIdForKey(key),
Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3))); Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)));
file.renameTo(newCacheFile); if (!file.renameTo(newCacheFile)) {
return null;
}
return newCacheFile;
} }
private SimpleCacheSpan(String key, long position, long length, long lastAccessTimestamp, private SimpleCacheSpan(String key, long position, long length, long lastAccessTimestamp,
......
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