Skip to content

Commit

Permalink
Cache download type refactoring (for listing auto-refreshes)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Sep 14, 2016
1 parent 3be3cf6 commit 2802cb3
Show file tree
Hide file tree
Showing 28 changed files with 301 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyAlways;
import org.quantumbadger.redreader.common.AndroidApi;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand Down Expand Up @@ -72,7 +73,7 @@ protected void onSuccess(final String captchaId) {
final URI captchaUrl = General.uriFromString("https://reddit.com/captcha/" + captchaId);

cm.makeRequest(new CacheRequest(captchaUrl, RedditAccountManager.getAnon(), null, Constants.Priority.CAPTCHA,
0, CacheRequest.DOWNLOAD_FORCE, Constants.FileType.CAPTCHA,
0, DownloadStrategyAlways.INSTANCE, Constants.FileType.CAPTCHA,
CacheRequest.DOWNLOAD_QUEUE_IMMEDIATE, false, true, CaptchaActivity.this) {
@Override
protected void onCallbackException(Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfNotCached;
import org.quantumbadger.redreader.common.AndroidApi;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand Down Expand Up @@ -217,7 +218,7 @@ public void onSuccess(final ImageInfo info) {
null,
Constants.Priority.IMAGE_VIEW,
0,
CacheRequest.DOWNLOAD_IF_NECESSARY,
DownloadStrategyIfNotCached.INSTANCE,
Constants.FileType.IMAGE,
CacheRequest.DOWNLOAD_QUEUE_IMMEDIATE,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyAlways;
import org.quantumbadger.redreader.common.AndroidApi;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand Down Expand Up @@ -280,7 +281,7 @@ private void uploadImage() {
null,
Constants.Priority.API_ACTION,
0,
CacheRequest.DOWNLOAD_FORCE,
DownloadStrategyAlways.INSTANCE,
Constants.FileType.NOCACHE,
CacheRequest.DOWNLOAD_QUEUE_IMGUR_API,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.quantumbadger.redreader.adapters.GroupedRecyclerViewAdapter;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyAlways;
import org.quantumbadger.redreader.common.AndroidApi;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand Down Expand Up @@ -208,7 +209,7 @@ private void makeFirstRequest(final Context context) {

// TODO parameterise limit
request = new CacheRequest(url, user, null, Constants.Priority.API_INBOX_LIST, 0,
CacheRequest.DOWNLOAD_FORCE, Constants.FileType.INBOX_LIST,
DownloadStrategyAlways.INSTANCE, Constants.FileType.INBOX_LIST,
CacheRequest.DOWNLOAD_QUEUE_REDDIT_API, true, true, context) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccountChangeListener;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.common.DialogUtils;
import org.quantumbadger.redreader.common.General;
import org.quantumbadger.redreader.common.LinkHandler;
Expand Down Expand Up @@ -125,7 +124,7 @@ protected void doRefresh(final RefreshableFragment which, final boolean force, f
mUrls,
null,
mSearchString,
force ? CacheRequest.DOWNLOAD_FORCE : CacheRequest.DOWNLOAD_IF_NECESSARY);
force);

mPane.removeAllViews();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfNotCached;
import org.quantumbadger.redreader.common.AndroidApi;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand Down Expand Up @@ -114,17 +115,17 @@ public void onBindViewHolder(final VH2TextIcon vh, final int position) {
vh.text2.setVisibility(View.VISIBLE);

CacheManager.getInstance(activity).makeRequest(new CacheRequest(
General.uriFromString(imageInfo.urlBigSquare),
RedditAccountManager.getAnon(),
null,
Constants.Priority.THUMBNAIL,
position,
CacheRequest.DOWNLOAD_IF_NECESSARY,
Constants.FileType.THUMBNAIL,
CacheRequest.DOWNLOAD_QUEUE_IMMEDIATE,
false,
false,
activity
General.uriFromString(imageInfo.urlBigSquare),
RedditAccountManager.getAnon(),
null,
Constants.Priority.THUMBNAIL,
position,
DownloadStrategyIfNotCached.INSTANCE,
Constants.FileType.THUMBNAIL,
CacheRequest.DOWNLOAD_QUEUE_IMMEDIATE,
false,
false,
activity
) {
@Override
protected void onCallbackException(final Throwable t) {
Expand Down
61 changes: 29 additions & 32 deletions src/main/java/org/quantumbadger/redreader/cache/CacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,45 +353,44 @@ public void run() {
private void handleRequest(final CacheRequest request) {

if(request.url == null) {
request.notifyFailure(CacheRequest.REQUEST_FAILURE_MALFORMED_URL, new NullPointerException("URL was null"), null, "URL was null");
request.notifyFailure(
CacheRequest.REQUEST_FAILURE_MALFORMED_URL,
new NullPointerException("URL was null"),
null,
"URL was null");
return;
}

switch(request.downloadType) {
if(request.downloadStrategy.shouldDownloadWithoutCheckingCache()) {
queueDownload(request);

case CacheRequest.DOWNLOAD_NEVER: {
} else {

final LinkedList<CacheEntry> result = dbManager.select(request.url, request.user.username, request.requestSession);
final LinkedList<CacheEntry> result = dbManager.select(request.url, request.user.username, request.requestSession);

if(result.size() == 0) {
request.notifyFailure(CacheRequest.REQUEST_FAILURE_CACHE_MISS, null, null, "Could not find this data in the cache");
if(result.isEmpty()) {

if(request.downloadStrategy.shouldDownloadIfNotCached()) {
queueDownload(request);

} else {
final CacheEntry entry = mostRecentFromList(result);
handleCacheEntryFound(entry, request);
request.notifyFailure(
CacheRequest.REQUEST_FAILURE_CACHE_MISS,
null,
null,
"Could not find this data in the cache");
}

break;
}

case CacheRequest.DOWNLOAD_IF_NECESSARY: {
} else {

final LinkedList<CacheEntry> result = dbManager.select(request.url, request.user.username, request.requestSession);
final CacheEntry entry = mostRecentFromList(result);

if(result.size() == 0) {
if(request.downloadStrategy.shouldDownloadIfCacheEntryFound(entry)) {
queueDownload(request);

} else {
final CacheEntry entry = mostRecentFromList(result);
handleCacheEntryFound(entry, request);
}

break;
}

case CacheRequest.DOWNLOAD_FORCE:
queueDownload(request);
break;
}
}

Expand Down Expand Up @@ -424,11 +423,13 @@ private void handleCacheEntryFound(final CacheEntry entry, final CacheRequest re

if(cacheFile == null) {

if(request.downloadType == CacheRequest.DOWNLOAD_IF_NECESSARY) {
queueDownload(request);
} else {
request.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE, null, null, "A cache entry was found in the database, but the actual data couldn't be found. Press refresh to download the content again.");
}
request.notifyFailure(
CacheRequest.REQUEST_FAILURE_STORAGE,
null,
null,
"A cache entry was found in the database, but the actual data couldn't be found. Press refresh to download the content again.");

dbManager.delete(entry.id);

return;
}
Expand Down Expand Up @@ -481,11 +482,7 @@ public void run() {
existingCacheFile.delete();
}

if(request.downloadType == CacheRequest.DOWNLOAD_IF_NECESSARY) {
queueDownload(request);
} else {
request.notifyFailure(CacheRequest.REQUEST_FAILURE_PARSE, t, null, "Error parsing the JSON stream");
}
request.notifyFailure(CacheRequest.REQUEST_FAILURE_PARSE, t, null, "Error parsing the JSON stream");

return;
}
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/org/quantumbadger/redreader/cache/CacheRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.util.Log;
import org.quantumbadger.redreader.account.RedditAccount;
import org.quantumbadger.redreader.activities.BugReportActivity;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategy;
import org.quantumbadger.redreader.common.PrioritisedCachedThreadPool;
import org.quantumbadger.redreader.common.RRError;
import org.quantumbadger.redreader.http.HTTPBackend;
Expand Down Expand Up @@ -57,10 +59,6 @@ public abstract class CacheRequest implements Comparable<CacheRequest> {
public static final int REQUEST_FAILURE_UPLOAD_FAIL_IMGUR = 10;
public static final int REQUEST_FAILURE_CACHE_DIR_DOES_NOT_EXIST = 11;

@IntDef({DOWNLOAD_NEVER, DOWNLOAD_IF_NECESSARY, DOWNLOAD_FORCE})
@Retention(RetentionPolicy.SOURCE)
public @interface DownloadType {}

@IntDef({DOWNLOAD_QUEUE_REDDIT_API, DOWNLOAD_QUEUE_IMGUR_API, DOWNLOAD_QUEUE_IMMEDIATE,
DOWNLOAD_QUEUE_IMAGE_PRECACHE})
@Retention(RetentionPolicy.SOURCE)
Expand All @@ -82,7 +80,7 @@ public abstract class CacheRequest implements Comparable<CacheRequest> {
public final int priority;
public final int listId;

public final @DownloadType int downloadType;
@NonNull public final DownloadStrategy downloadStrategy;

public final int fileType;

Expand Down Expand Up @@ -116,17 +114,17 @@ public synchronized void cancel() {
}

protected CacheRequest(final URI url, final RedditAccount user, final UUID requestSession, final int priority,
final int listId, final @DownloadType int downloadType, final int fileType,
final int listId, @NonNull final DownloadStrategy downloadStrategy, final int fileType,
final @DownloadQueueType int queueType, final boolean isJson, final boolean cancelExisting,
final Context context) {

this(url, user, requestSession, priority, listId, downloadType, fileType, queueType, isJson, null,
this(url, user, requestSession, priority, listId, downloadStrategy, fileType, queueType, isJson, null,
true, cancelExisting, context);
}

// TODO remove this huge constructor, make mutable
protected CacheRequest(final URI url, final RedditAccount user, final UUID requestSession, final int priority,
final int listId, final @DownloadType int downloadType, final int fileType,
final int listId, @NonNull final DownloadStrategy downloadStrategy, final int fileType,
final @DownloadQueueType int queueType, final boolean isJson, final List<HTTPBackend.PostField> postFields,
final boolean cache, final boolean cancelExisting, final Context context) {

Expand All @@ -135,8 +133,8 @@ protected CacheRequest(final URI url, final RedditAccount user, final UUID reque
if (user == null)
throw new NullPointerException("User was null - set to empty string for anonymous");

if (downloadType != DOWNLOAD_FORCE && postFields != null)
throw new IllegalArgumentException("Download type must be forced for POST requests");
if (!downloadStrategy.shouldDownloadWithoutCheckingCache() && postFields != null)
throw new IllegalArgumentException("Should not perform cache lookup for POST requests");

if (!isJson && postFields != null)
throw new IllegalArgumentException("POST requests must be for JSON values");
Expand All @@ -152,7 +150,7 @@ protected CacheRequest(final URI url, final RedditAccount user, final UUID reque
this.requestSession = requestSession;
this.priority = priority;
this.listId = listId;
this.downloadType = downloadType;
this.downloadStrategy = downloadStrategy;
this.fileType = fileType;
this.queueType = queueType;
this.isJson = isJson;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.cache.downloadstrategy;

import org.quantumbadger.redreader.cache.CacheEntry;

public interface DownloadStrategy {

boolean shouldDownloadWithoutCheckingCache();

boolean shouldDownloadIfCacheEntryFound(final CacheEntry entry);

boolean shouldDownloadIfNotCached();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.cache.downloadstrategy;

import org.quantumbadger.redreader.cache.CacheEntry;

public class DownloadStrategyAlways implements DownloadStrategy {

public static final DownloadStrategyAlways INSTANCE = new DownloadStrategyAlways();

private DownloadStrategyAlways() {}

@Override
public boolean shouldDownloadWithoutCheckingCache() {
return true;
}

@Override
public boolean shouldDownloadIfCacheEntryFound(final CacheEntry entry) {
// Should never get here
return true;
}

@Override
public boolean shouldDownloadIfNotCached() {
// Should never get here
return true;
}
}

0 comments on commit 2802cb3

Please sign in to comment.