From bb71b050c9d42dfb6184cafd426204e02466b5c3 Mon Sep 17 00:00:00 2001 From: eguven Date: Thu, 15 Mar 2018 05:00:30 -0700 Subject: [PATCH] Add getContentMetadata and applyContentMetadataMutations to SimpleCache ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=189169899 --- .../cache/CachedContentIndexTest.java | 8 ++--- .../upstream/cache/CachedContent.java | 25 +++++++++------- .../upstream/cache/CachedContentIndex.java | 15 ++++++++++ .../cache/DefaultContentMetadata.java | 29 +++++++++++-------- .../upstream/cache/SimpleCache.java | 23 +++++++++++++++ .../cache/DefaultContentMetadataTest.java | 4 +-- 6 files changed, 75 insertions(+), 29 deletions(-) diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java index f2bcdf3f78..4b52a15144 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java @@ -144,10 +144,10 @@ public class CachedContentIndexTest extends InstrumentationTestCase { } public void testStore() throws Exception { - CachedContent cachedContent1 = index.getOrAdd("KLMNO"); - cachedContent1.setLength(2560); - CachedContent cachedContent2 = index.getOrAdd("ABCDE"); - cachedContent2.setLength(10); + index.getOrAdd("KLMNO"); + index.setContentLength("KLMNO", 2560); + index.getOrAdd("ABCDE"); + index.setContentLength("ABCDE", 10); index.store(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContent.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContent.java index 68eb17c63e..1a9a84c2af 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContent.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContent.java @@ -54,13 +54,12 @@ import java.util.TreeSet; throws IOException { int id = input.readInt(); String key = input.readUTF(); - CachedContent cachedContent; + CachedContent cachedContent = new CachedContent(id, key); if (version < VERSION_METADATA_INTRODUCED) { - cachedContent = new CachedContent(id, key); long length = input.readLong(); cachedContent.setLength(length); } else { - cachedContent = new CachedContent(id, key, DefaultContentMetadata.readFromStream(input)); + cachedContent.metadata = DefaultContentMetadata.readFromStream(input); } return cachedContent; } @@ -72,13 +71,9 @@ import java.util.TreeSet; * @param key The cache stream key. */ public CachedContent(int id, String key) { - this(id, key, new DefaultContentMetadata()); - } - - private CachedContent(int id, String key, DefaultContentMetadata metadata) { this.id = id; this.key = key; - this.metadata = metadata; + this.metadata = DefaultContentMetadata.EMPTY; this.cachedSpans = new TreeSet<>(); } @@ -94,6 +89,16 @@ import java.util.TreeSet; metadata.writeToStream(output); } + /** Returns the metadata. */ + public ContentMetadata getMetadata() { + return metadata; + } + + /** Applies {@code mutations} to the metadata. */ + public void applyMetadataMutations(ContentMetadataMutations mutations) { + this.metadata = new DefaultContentMetadata(metadata, mutations); + } + /** * Returns the length of the original stream, or {@link C#LENGTH_UNSET} if the length is unknown. */ @@ -103,9 +108,7 @@ import java.util.TreeSet; /** Sets the length of the content. */ public void setLength(long length) { - ContentMetadataMutations mutations = - new ContentMetadataMutations().set(METADATA_NAME_LENGTH, length); - metadata = new DefaultContentMetadata(metadata, mutations); + applyMetadataMutations(new ContentMetadataMutations().set(METADATA_NAME_LENGTH, length)); } /** Returns whether the content is locked. */ 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 9ad0a25b87..d59af5b69f 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 @@ -218,6 +218,21 @@ import javax.crypto.spec.SecretKeySpec; return cachedContent == null ? C.LENGTH_UNSET : cachedContent.getLength(); } + /** + * Applies {@code mutations} to the {@link ContentMetadata} for the given key. A new {@link + * CachedContent} is added if there isn't one already with the given key. + */ + public void applyContentMetadataMutations(String key, ContentMetadataMutations mutations) { + CachedContent cachedContent = getOrAdd(key); + cachedContent.applyMetadataMutations(mutations); + } + + /** Returns a snapshot of the {@link ContentMetadata} for the given key. */ + public ContentMetadata getContentMetadataSnapshot(String key) { + CachedContent cachedContent = get(key); + return cachedContent != null ? cachedContent.getMetadata() : DefaultContentMetadata.EMPTY; + } + private boolean readFile() { DataInputStream input = null; try { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadata.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadata.java index b70fff9605..caab925769 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadata.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadata.java @@ -31,6 +31,10 @@ import java.util.Map.Entry; /** Default implementation of {@link ContentMetadata}. Values are stored as byte arrays. */ public final class DefaultContentMetadata implements ContentMetadata { + /** An empty DefaultContentMetadata. */ + public static final DefaultContentMetadata EMPTY = + new DefaultContentMetadata(Collections.emptyMap()); + private static final int MAX_VALUE_LENGTH = 10 * 1024 * 1024; private int hashCode; @@ -59,11 +63,6 @@ public final class DefaultContentMetadata implements ContentMetadata { private final Map metadata; - /** Constructs an empty {@link DefaultContentMetadata}. */ - public DefaultContentMetadata() { - this(Collections.emptyMap()); - } - /** * Constructs a {@link DefaultContentMetadata} by copying metadata values from {@code other} and * applying {@code mutations}. @@ -165,13 +164,20 @@ public final class DefaultContentMetadata implements ContentMetadata { private static Map applyMutations( Map otherMetadata, ContentMetadataMutations mutations) { HashMap metadata = new HashMap<>(otherMetadata); - List removedValues = mutations.getRemovedValues(); - for (int i = 0; i < removedValues.size(); i++) { - metadata.remove(removedValues.get(i)); + removeValues(metadata, mutations.getRemovedValues()); + addValues(metadata, mutations.getEditedValues()); + return metadata; + } + + private static void removeValues(HashMap metadata, List names) { + for (int i = 0; i < names.size(); i++) { + metadata.remove(names.get(i)); } - Map editedValues = mutations.getEditedValues(); - for (String name : editedValues.keySet()) { - Object value = editedValues.get(name); + } + + private static void addValues(HashMap metadata, Map values) { + for (String name : values.keySet()) { + Object value = values.get(name); byte[] bytes = getBytes(value); if (bytes.length > MAX_VALUE_LENGTH) { throw new IllegalArgumentException( @@ -181,7 +187,6 @@ public final class DefaultContentMetadata implements ContentMetadata { } metadata.put(name, bytes); } - return metadata; } private static byte[] getBytes(Object value) { 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 f2679fee0c..4a1d92ad76 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 @@ -412,4 +412,27 @@ public final class SimpleCache implements Cache { return index.getContentLength(key); } + /** + * Applies {@code mutations} to the {@link ContentMetadata} for the given key. A new {@link + * CachedContent} is added if there isn't one already with the given key. + * + * @param key The cache key for the data. + * @param mutations Contains mutations to be applied to the metadata. + * @throws CacheException If an error is encountered. + */ + public void applyContentMetadataMutations(String key, ContentMetadataMutations mutations) + throws CacheException { + index.applyContentMetadataMutations(key, mutations); + index.store(); + } + + /** + * Returns a snapshot of the {@link ContentMetadata} for the given key. + * + * @param key The cache key for the data. + * @return A snapshot of the {@link ContentMetadata} for the given key. + */ + public ContentMetadata getContentMetadataSnapshot(String key) { + return index.getContentMetadataSnapshot(key); + } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadataTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadataTest.java index 01af4f4d1c..02826e8908 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadataTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/DefaultContentMetadataTest.java @@ -62,7 +62,7 @@ public class DefaultContentMetadataTest { @Test public void testEmptyMutationDoesNotFail() throws Exception { ContentMetadataMutations mutations = new ContentMetadataMutations(); - new DefaultContentMetadata(new DefaultContentMetadata(), mutations); + new DefaultContentMetadata(DefaultContentMetadata.EMPTY, mutations); } @Test @@ -194,6 +194,6 @@ public class DefaultContentMetadataTest { throw new IllegalArgumentException(); } } - return new DefaultContentMetadata(new DefaultContentMetadata(), mutations); + return new DefaultContentMetadata(DefaultContentMetadata.EMPTY, mutations); } }