CacheSpan.isEOS (is end of stream) field shows whether the {@link CacheSpan} contains the end of the original stream.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=130084365
This commit is contained in:
eguven 2016-08-12 03:15:41 -07:00 committed by Oliver Woodman
parent cc28aeadd8
commit c380ba2dc1
3 changed files with 33 additions and 24 deletions

View File

@ -26,10 +26,10 @@ import junit.framework.TestCase;
public class CacheSpanTest extends TestCase { public class CacheSpanTest extends TestCase {
public void testCacheFile() throws Exception { public void testCacheFile() throws Exception {
assertCacheSpan(new File("parent"), "key", 0, 0); assertCacheSpan(new File("parent"), "key", 0, 0, true);
assertCacheSpan(new File("parent/"), "key", 1, 2); assertCacheSpan(new File("parent/"), "key", 1, 2, false);
assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2); assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2, true);
assertCacheSpan(new File("/"), "key", 1, 2); assertCacheSpan(new File("/"), "key", 1, 2, false);
assertNullCacheSpan(new File("parent"), "", 1, 2); assertNullCacheSpan(new File("parent"), "", 1, 2);
assertNullCacheSpan(new File("parent"), "key", -1, 2); assertNullCacheSpan(new File("parent"), "key", -1, 2);
@ -44,7 +44,7 @@ public class CacheSpanTest extends TestCase {
+ "A standalone carriage-return character \r" + "A standalone carriage-return character \r"
+ "A next-line character \u0085" + "A next-line character \u0085"
+ "A line-separator character \u2028" + "A line-separator character \u2028"
+ "A paragraph-separator character \u2029", 1, 2); + "A paragraph-separator character \u2029", 1, 2, true);
} }
public void testCacheFileNameRandomData() throws Exception { public void testCacheFileNameRandomData() throws Exception {
@ -54,12 +54,14 @@ public class CacheSpanTest extends TestCase {
String key = TestUtil.buildTestString(1000, random); String key = TestUtil.buildTestString(1000, random);
long offset = Math.abs(random.nextLong()); long offset = Math.abs(random.nextLong());
long lastAccessTimestamp = Math.abs(random.nextLong()); long lastAccessTimestamp = Math.abs(random.nextLong());
assertCacheSpan(parent, key, offset, lastAccessTimestamp); boolean isEOS = random.nextBoolean();
assertCacheSpan(parent, key, offset, lastAccessTimestamp, isEOS);
} }
} }
private void assertCacheSpan(File parent, String key, long offset, long lastAccessTimestamp) { private void assertCacheSpan(File parent, String key, long offset, long lastAccessTimestamp,
File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp); boolean isEOS) {
File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp, isEOS);
CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile); CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile);
String message = cacheFile.toString(); String message = cacheFile.toString();
assertNotNull(message, cacheSpan); assertNotNull(message, cacheSpan);
@ -67,11 +69,12 @@ public class CacheSpanTest extends TestCase {
assertEquals(message, key, cacheSpan.key); assertEquals(message, key, cacheSpan.key);
assertEquals(message, offset, cacheSpan.position); assertEquals(message, offset, cacheSpan.position);
assertEquals(message, lastAccessTimestamp, cacheSpan.lastAccessTimestamp); assertEquals(message, lastAccessTimestamp, cacheSpan.lastAccessTimestamp);
assertEquals(message, isEOS, cacheSpan.isEOS);
} }
private void assertNullCacheSpan(File parent, String key, long offset, private void assertNullCacheSpan(File parent, String key, long offset,
long lastAccessTimestamp) { long lastAccessTimestamp) {
File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp); File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp, false);
CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile); CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile);
assertNull(cacheFile.toString(), cacheSpan); assertNull(cacheFile.toString(), cacheSpan);
} }

View File

@ -29,7 +29,7 @@ public final class CacheSpan implements Comparable<CacheSpan> {
private static final Pattern CACHE_FILE_PATTERN_V1 = private static final Pattern CACHE_FILE_PATTERN_V1 =
Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$", Pattern.DOTALL); Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$", Pattern.DOTALL);
private static final Pattern CACHE_FILE_PATTERN_V2 = private static final Pattern CACHE_FILE_PATTERN_V2 =
Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL); Pattern.compile("^(.+)\\.(\\d+)(E?)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL);
/** /**
* The cache key that uniquely identifies the original stream. * The cache key that uniquely identifies the original stream.
@ -55,23 +55,27 @@ public final class CacheSpan implements Comparable<CacheSpan> {
* The last access timestamp, or -1 if {@link #isCached} is false. * The last access timestamp, or -1 if {@link #isCached} is false.
*/ */
public final long lastAccessTimestamp; public final long lastAccessTimestamp;
/**
* Whether the {@link CacheSpan} is know to contain the end of the original stream.
*/
public final boolean isEOS;
public static File getCacheFileName(File cacheDir, String key, long offset, public static File getCacheFileName(File cacheDir, String key, long offset,
long lastAccessTimestamp) { long lastAccessTimestamp, boolean isEOS) {
return new File(cacheDir, return new File(cacheDir, Util.escapeFileName(key) + "." + offset + (isEOS ? "E" : "") + "."
Util.escapeFileName(key) + "." + offset + "." + lastAccessTimestamp + SUFFIX); + lastAccessTimestamp + SUFFIX);
} }
public static CacheSpan createLookup(String key, long position) { public static CacheSpan createLookup(String key, long position) {
return new CacheSpan(key, position, -1, false, -1, null); return new CacheSpan(key, position, -1, false, -1, null, false);
} }
public static CacheSpan createOpenHole(String key, long position) { public static CacheSpan createOpenHole(String key, long position) {
return new CacheSpan(key, position, -1, false, -1, null); return new CacheSpan(key, position, -1, false, -1, null, false);
} }
public static CacheSpan createClosedHole(String key, long position, long length) { public static CacheSpan createClosedHole(String key, long position, long length) {
return new CacheSpan(key, position, length, false, -1, null); return new CacheSpan(key, position, length, false, -1, null, false);
} }
/** /**
@ -87,7 +91,8 @@ public final class CacheSpan implements Comparable<CacheSpan> {
} }
String key = Util.unescapeFileName(matcher.group(1)); String key = Util.unescapeFileName(matcher.group(1));
return key == null ? null : createCacheEntry( return key == null ? null : createCacheEntry(
key, Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)), file); key, Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(4)), file,
"E".equals(matcher.group(3)));
} }
static File upgradeIfNeeded(File file) { static File upgradeIfNeeded(File file) {
@ -97,25 +102,26 @@ public final class CacheSpan implements Comparable<CacheSpan> {
} }
String key = matcher.group(1); // Keys were not escaped in version 1. String key = matcher.group(1); // Keys were not escaped in version 1.
File newCacheFile = getCacheFileName(file.getParentFile(), key, File newCacheFile = getCacheFileName(file.getParentFile(), key,
Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3))); Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)), false);
file.renameTo(newCacheFile); file.renameTo(newCacheFile);
return newCacheFile; return newCacheFile;
} }
private static CacheSpan createCacheEntry(String key, long position, long lastAccessTimestamp, private static CacheSpan createCacheEntry(String key, long position, long lastAccessTimestamp,
File file) { File file, boolean isEOS) {
return new CacheSpan(key, position, file.length(), true, lastAccessTimestamp, file); return new CacheSpan(key, position, file.length(), true, lastAccessTimestamp, file, isEOS);
} }
// Visible for testing. // Visible for testing.
CacheSpan(String key, long position, long length, boolean isCached, CacheSpan(String key, long position, long length, boolean isCached,
long lastAccessTimestamp, File file) { long lastAccessTimestamp, File file, boolean isEOS) {
this.key = key; this.key = key;
this.position = position; this.position = position;
this.length = length; this.length = length;
this.isCached = isCached; this.isCached = isCached;
this.file = file; this.file = file;
this.lastAccessTimestamp = lastAccessTimestamp; this.lastAccessTimestamp = lastAccessTimestamp;
this.isEOS = isEOS;
} }
/** /**
@ -132,9 +138,9 @@ public final class CacheSpan implements Comparable<CacheSpan> {
*/ */
public CacheSpan touch() { public CacheSpan touch() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
File newCacheFile = getCacheFileName(file.getParentFile(), key, position, now); File newCacheFile = getCacheFileName(file.getParentFile(), key, position, now, isEOS);
file.renameTo(newCacheFile); file.renameTo(newCacheFile);
return CacheSpan.createCacheEntry(key, position, now, newCacheFile); return createCacheEntry(key, position, now, newCacheFile, isEOS);
} }
@Override @Override

View File

@ -162,7 +162,7 @@ public final class SimpleCache implements Cache {
cacheDir.mkdirs(); cacheDir.mkdirs();
} }
evictor.onStartFile(this, key, position, length); evictor.onStartFile(this, key, position, length);
return CacheSpan.getCacheFileName(cacheDir, key, position, System.currentTimeMillis()); return CacheSpan.getCacheFileName(cacheDir, key, position, System.currentTimeMillis(), false);
} }
@Override @Override