mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Revert CacheSpan EOS changes.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=133123235
This commit is contained in:
parent
80d08a6e25
commit
5512aae451
3 changed files with 24 additions and 33 deletions
|
|
@ -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, true);
|
assertCacheSpan(new File("parent"), "key", 0, 0);
|
||||||
assertCacheSpan(new File("parent/"), "key", 1, 2, false);
|
assertCacheSpan(new File("parent/"), "key", 1, 2);
|
||||||
assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2, true);
|
assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2);
|
||||||
assertCacheSpan(new File("/"), "key", 1, 2, false);
|
assertCacheSpan(new File("/"), "key", 1, 2);
|
||||||
|
|
||||||
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, true);
|
+ "A paragraph-separator character \u2029", 1, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCacheFileNameRandomData() throws Exception {
|
public void testCacheFileNameRandomData() throws Exception {
|
||||||
|
|
@ -54,14 +54,12 @@ 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());
|
||||||
boolean isEOS = random.nextBoolean();
|
assertCacheSpan(parent, key, offset, lastAccessTimestamp);
|
||||||
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) {
|
||||||
boolean isEOS) {
|
File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp);
|
||||||
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);
|
||||||
|
|
@ -69,12 +67,11 @@ 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, false);
|
File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp);
|
||||||
CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile);
|
CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile);
|
||||||
assertNull(cacheFile.toString(), cacheSpan);
|
assertNull(cacheFile.toString(), cacheSpan);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,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+)(E?)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL);
|
Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The cache key that uniquely identifies the original stream.
|
* The cache key that uniquely identifies the original stream.
|
||||||
|
|
@ -56,27 +56,23 @@ public final class CacheSpan implements Comparable<CacheSpan> {
|
||||||
* The last access timestamp, or {@link C#TIME_UNSET} if {@link #isCached} is false.
|
* The last access timestamp, or {@link C#TIME_UNSET} 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, boolean isEOS) {
|
long lastAccessTimestamp) {
|
||||||
return new File(cacheDir, Util.escapeFileName(key) + "." + offset + (isEOS ? "E" : "") + "."
|
return new File(cacheDir,
|
||||||
+ lastAccessTimestamp + SUFFIX);
|
Util.escapeFileName(key) + "." + offset + "." + lastAccessTimestamp + SUFFIX);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CacheSpan createLookup(String key, long position) {
|
public static CacheSpan createLookup(String key, long position) {
|
||||||
return new CacheSpan(key, position, C.LENGTH_UNSET, false, C.TIME_UNSET, null, false);
|
return new CacheSpan(key, position, C.LENGTH_UNSET, false, C.TIME_UNSET, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CacheSpan createOpenHole(String key, long position) {
|
public static CacheSpan createOpenHole(String key, long position) {
|
||||||
return new CacheSpan(key, position, C.LENGTH_UNSET, false, C.TIME_UNSET, null, false);
|
return new CacheSpan(key, position, C.LENGTH_UNSET, false, C.TIME_UNSET, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
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, C.TIME_UNSET, null, false);
|
return new CacheSpan(key, position, length, false, C.TIME_UNSET, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -92,8 +88,7 @@ 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(4)), file,
|
key, Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)), file);
|
||||||
"E".equals(matcher.group(3)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static File upgradeIfNeeded(File file) {
|
static File upgradeIfNeeded(File file) {
|
||||||
|
|
@ -103,26 +98,25 @@ 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)), false);
|
Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)));
|
||||||
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, boolean isEOS) {
|
File file) {
|
||||||
return new CacheSpan(key, position, file.length(), true, lastAccessTimestamp, file, isEOS);
|
return new CacheSpan(key, position, file.length(), true, lastAccessTimestamp, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, boolean isEOS) {
|
long lastAccessTimestamp, File file) {
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -139,9 +133,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, isEOS);
|
File newCacheFile = getCacheFileName(file.getParentFile(), key, position, now);
|
||||||
file.renameTo(newCacheFile);
|
file.renameTo(newCacheFile);
|
||||||
return createCacheEntry(key, position, now, newCacheFile, isEOS);
|
return createCacheEntry(key, position, now, newCacheFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,7 @@ public final class SimpleCache implements Cache {
|
||||||
cacheDir.mkdirs();
|
cacheDir.mkdirs();
|
||||||
}
|
}
|
||||||
evictor.onStartFile(this, key, position, maxLength);
|
evictor.onStartFile(this, key, position, maxLength);
|
||||||
return CacheSpan.getCacheFileName(cacheDir, key, position, System.currentTimeMillis(), false);
|
return CacheSpan.getCacheFileName(cacheDir, key, position, System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue