Skip to content

Commit

Permalink
Compress cached post/comment listings (makes cache use around 10x les…
Browse files Browse the repository at this point in the history
…s space)
  • Loading branch information
QuantumBadger committed Dec 29, 2020
1 parent d7f1d75 commit 42f2051
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 43 deletions.
4 changes: 4 additions & 0 deletions assets/changelog-alpha.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/Alpha 263 (2020-12-29)
Compress cached post/comment listings (makes cache use around 10x less space)
Minimum supported Android version is now 4.1

/Alpha 262 (2020-12-28)
Fixing alpha changelog numbering

Expand Down
2 changes: 2 additions & 0 deletions assets/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
96/1.16
Fixed error message when marking all inbox messages as read
Always set navigation bar to black in image viewer
Compress cached post/comment listings (makes cache use around 10x less space)
Performance improvement when loading fonts
Set notification accent color to red
No longer auto-collapses own comments, or user profile comments (thanks to Cameron Merkel)
Show announcements from developer in main menu (with preference to disable)
Inbox requests are no longer cached
Fix for cache errors caused by too many files in one directory
Fix for using internal browser as image viewer
Minimum supported Android version is now 4.1

95/1.15
Inline image previews (and preference to disable)
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation 'info.guardianproject.netcipher:netcipher:1.2.1'
implementation 'com.google.android.exoplayer:exoplayer-core:2.9.6'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.6'
implementation "com.github.luben:zstd-jni:1.4.8-1@aar"

testImplementation 'junit:junit:4.13'

Expand All @@ -47,7 +48,7 @@ android {

defaultConfig {
applicationId "org.quantumbadger.redreader"
minSdkVersion 14
minSdkVersion 16
targetSdkVersion 29
versionCode 95
versionName "1.15"
Expand Down Expand Up @@ -86,6 +87,7 @@ android {
// Temporarily disabled
disable 'ImpliedQuantity'
disable 'InflateParams'
disable 'ObsoleteSdkInt'

// Too many false positives
disable 'UnusedResources'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ private Optional<CacheManager.ReadableCacheFile> getReadableCacheFile(@NonNull f
return Optional.empty();
}

return Optional.of(mCacheManager.getExistingCacheFileById(cacheId.get()));
return Optional.of(mCacheManager.getExistingCacheFileById(
cacheId.get(),
CacheCompressionType.NONE)); // No compression is used for images
}

@NonNull
Expand Down
47 changes: 36 additions & 11 deletions src/main/java/org/quantumbadger/redreader/cache/CacheDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import android.util.Log;
import androidx.annotation.NonNull;
import org.quantumbadger.redreader.activities.BugReportActivity;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
import org.quantumbadger.redreader.common.PrioritisedCachedThreadPool;
import org.quantumbadger.redreader.common.Priority;
import org.quantumbadger.redreader.common.RRTime;
import org.quantumbadger.redreader.common.TorCommon;
import org.quantumbadger.redreader.common.datastream.MemoryDataStream;
import org.quantumbadger.redreader.common.datastream.MemoryDataStreamInputStream;
import org.quantumbadger.redreader.http.HTTPBackend;
import org.quantumbadger.redreader.reddit.api.RedditOAuth;

Expand Down Expand Up @@ -253,9 +253,40 @@ public void onSuccess(

@NonNull final CacheManager.WritableCacheFile writableCacheFile;

final CacheCompressionType cacheCompressionType;

switch(mInitiator.fileType) {
case Constants.FileType.CAPTCHA:
case Constants.FileType.IMAGE:
case Constants.FileType.INLINE_IMAGE_PREVIEW:
case Constants.FileType.NOCACHE:
case Constants.FileType.THUMBNAIL:
// Image saving/sharing relies the file on disk being "raw"
cacheCompressionType = CacheCompressionType.NONE;
break;

case Constants.FileType.COMMENT_LIST:
case Constants.FileType.IMAGE_INFO:
case Constants.FileType.INBOX_LIST:
case Constants.FileType.MULTIREDDIT_LIST:
case Constants.FileType.POST_LIST:
case Constants.FileType.SUBREDDIT_ABOUT:
case Constants.FileType.SUBREDDIT_LIST:
case Constants.FileType.USER_ABOUT:
cacheCompressionType = CacheCompressionType.ZSTD;
break;

default:
Log.e(TAG, "Unhandled filetype: " + mInitiator.fileType);
cacheCompressionType = CacheCompressionType.NONE;
}

try {
writableCacheFile
= manager.openNewCacheFile(mInitiator, session, mimetype);
writableCacheFile = manager.openNewCacheFile(
mInitiator,
session,
mimetype,
cacheCompressionType);

} catch(final IOException e) {

Expand All @@ -279,15 +310,9 @@ public void onSuccess(
return;
}

final MemoryDataStreamInputStream inputStream = stream.getInputStream();

final byte[] buf = new byte[64 * 1024];
int bytesRead;

try {
while((bytesRead = inputStream.read(buf)) > 0) {
writableCacheFile.writeChunk(buf, 0, bytesRead);
}
stream.getUnderlyingByteArrayWhenComplete(
writableCacheFile::writeWholeFile);

writableCacheFile.onWriteFinished();

Expand Down

0 comments on commit 42f2051

Please sign in to comment.