From c127a577d7af8e419f0d9308d66951717bbaedeb Mon Sep 17 00:00:00 2001 From: eguven Date: Tue, 19 Sep 2017 09:10:35 -0700 Subject: [PATCH] Notify span listeners even if index store fails in SimpleCache.removeSpan This fixes infinite loop in LeastRecentlyUsedCacheEvictor.evictCache when index store fails. Also made CachedContentIndex not final so it can be mocked and added a package protected SimpleCache constructor so mock index can be injected. Issue: #3260 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=169249517 --- .../upstream/cache/CachedContentIndex.java | 2 +- .../upstream/cache/SimpleCache.java | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java index d0a9bc13c1..758da3548a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java @@ -49,7 +49,7 @@ import javax.crypto.spec.SecretKeySpec; /** * This class maintains the index of cached content. */ -/*package*/ final class CachedContentIndex { +/*package*/ class CachedContentIndex { public static final String FILE_NAME = "cached_content_index.exi"; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index 15a5673a4e..10df7c802c 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -48,7 +48,7 @@ public final class SimpleCache implements Cache { * @param evictor The evictor to be used. */ public SimpleCache(File cacheDir, CacheEvictor evictor) { - this(cacheDir, evictor, null); + this(cacheDir, evictor, null, false); } /** @@ -75,10 +75,22 @@ public final class SimpleCache implements Cache { * @param encrypt When false, a plaintext index will be written. */ public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey, boolean encrypt) { + this(cacheDir, evictor, new CachedContentIndex(cacheDir, secretKey, encrypt)); + } + + /** + * Constructs the cache. The cache will delete any unrecognized files from the directory. Hence + * the directory cannot be used to store other files. + * + * @param cacheDir A dedicated cache directory. + * @param evictor The evictor to be used. + * @param index The CachedContentIndex to be used. + */ + /*package*/ SimpleCache(File cacheDir, CacheEvictor evictor, CachedContentIndex index) { this.cacheDir = cacheDir; this.evictor = evictor; this.lockedSpans = new HashMap<>(); - this.index = new CachedContentIndex(cacheDir, secretKey, encrypt); + this.index = index; this.listeners = new HashMap<>(); // Start cache initialization. final ConditionVariable conditionVariable = new ConditionVariable(); @@ -305,11 +317,14 @@ public final class SimpleCache implements Cache { return; } totalSpace -= span.length; - if (removeEmptyCachedContent && cachedContent.isEmpty()) { - index.removeEmpty(cachedContent.key); - index.store(); + try { + if (removeEmptyCachedContent && cachedContent.isEmpty()) { + index.removeEmpty(cachedContent.key); + index.store(); + } + } finally { + notifySpanRemoved(span); } - notifySpanRemoved(span); } @Override