Add CacheDataSourceFactory getters

A previous change added these getters to CacheDataSource, but it can
also be useful to retrieve these components directly from the factory.
This is useful for tasks where we're going to need to build multiple
CacheDataSource instances (e.g., to make requests in parallel), and
also need to operate directly on the same components. It's a bit more
natural to retrieve them from the factory than from an arbitrary
CacheDataSource in this case, since it can avoid unnatural code where
you create a CacheDataSource instance earlier than you would otherwise
just to use its getters, and/or create one just to use its getters and
then throw it away.

PiperOrigin-RevId: 308606020
This commit is contained in:
olly 2020-04-27 14:06:54 +01:00 committed by Oliver Woodman
parent 86fb3dfede
commit 2e1024f0d1
2 changed files with 35 additions and 9 deletions

View File

@ -105,12 +105,17 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
@Override
public final void download(@Nullable ProgressListener progressListener)
throws IOException, InterruptedException {
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForDownloading();
@Nullable PriorityTaskManager priorityTaskManager = dataSource.getUpstreamPriorityTaskManager();
@Nullable
PriorityTaskManager priorityTaskManager =
cacheDataSourceFactory.getUpstreamPriorityTaskManager();
if (priorityTaskManager != null) {
priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
}
try {
Cache cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache());
CacheKeyFactory cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForDownloading();
// Get the manifest and all of the segments.
M manifest = getManifest(dataSource, manifestDataSpec);
if (!streamKeys.isEmpty()) {
@ -118,7 +123,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
}
List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);
Collections.sort(segments);
mergeSegments(segments, dataSource.getCacheKeyFactory());
mergeSegments(segments, cacheKeyFactory);
// Scan the segments, removing any that are fully downloaded.
int totalSegments = segments.size();
@ -128,8 +133,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
for (int i = segments.size() - 1; i >= 0; i--) {
Segment segment = segments.get(i);
Pair<Long, Long> segmentLengthAndBytesDownloaded =
CacheUtil.getCached(
segment.dataSpec, dataSource.getCache(), dataSource.getCacheKeyFactory());
CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory);
long segmentLength = segmentLengthAndBytesDownloaded.first;
long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second;
bytesDownloaded += segmentBytesDownloaded;
@ -185,9 +189,9 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
@Override
public final void remove() throws InterruptedException {
Cache cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache());
CacheKeyFactory cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload();
Cache cache = dataSource.getCache();
CacheKeyFactory cacheKeyFactory = dataSource.getCacheKeyFactory();
try {
M manifest = getManifest(dataSource, manifestDataSpec);
List<Segment> segments = getSegments(dataSource, manifest, true);

View File

@ -80,6 +80,15 @@ public final class CacheDataSource implements DataSource {
return this;
}
/**
* Returns the cache that will be used, or {@code null} if {@link #setCache} has yet to be
* called.
*/
@Nullable
public Cache getCache() {
return cache;
}
/**
* Sets the {@link DataSource.Factory} for {@link DataSource DataSources} for reading from the
* cache.
@ -124,6 +133,11 @@ public final class CacheDataSource implements DataSource {
return this;
}
/** Returns the {@link CacheKeyFactory} that will be used. */
public CacheKeyFactory getCacheKeyFactory() {
return cacheKeyFactory;
}
/**
* Sets the {@link DataSource.Factory} for upstream {@link DataSource DataSources}, which are
* used to read data in the case of a cache miss.
@ -165,6 +179,15 @@ public final class CacheDataSource implements DataSource {
return this;
}
/**
* Returns the {@link PriorityTaskManager} that will bs used when requesting data from upstream,
* or {@code null} if there is none.
*/
@Nullable
public PriorityTaskManager getUpstreamPriorityTaskManager() {
return upstreamPriorityTaskManager;
}
/**
* Sets the priority to use when requesting data from upstream. The priority is only used if a
* {@link PriorityTaskManager} is set by calling {@link #setUpstreamPriorityTaskManager}.
@ -492,6 +515,7 @@ public final class CacheDataSource implements DataSource {
this.ignoreCacheForUnsetLengthRequests =
(flags & FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS) != 0;
this.upstreamPriority = upstreamPriority;
this.upstreamPriorityTaskManager = upstreamPriorityTaskManager;
if (upstreamDataSource != null) {
if (upstreamPriorityTaskManager != null) {
upstreamDataSource =
@ -499,14 +523,12 @@ public final class CacheDataSource implements DataSource {
upstreamDataSource, upstreamPriorityTaskManager, upstreamPriority);
}
this.upstreamDataSource = upstreamDataSource;
this.upstreamPriorityTaskManager = upstreamPriorityTaskManager;
this.cacheWriteDataSource =
cacheWriteDataSink != null
? new TeeDataSource(upstreamDataSource, cacheWriteDataSink)
: null;
} else {
this.upstreamDataSource = DummyDataSource.INSTANCE;
this.upstreamPriorityTaskManager = null;
this.cacheWriteDataSource = null;
}
this.eventListener = eventListener;