Skip to content

Commit

Permalink
optimized saving progress update to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Apr 25, 2018
1 parent f64236f commit 535a5b5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tonyodev.fetch2.database

import android.arch.persistence.db.SupportSQLiteDatabase
import android.arch.persistence.room.Room
import android.content.Context
import android.database.sqlite.SQLiteException
Expand Down Expand Up @@ -38,6 +39,8 @@ class DatabaseManagerImpl constructor(context: Context,
builder.build()
}()

val database: SupportSQLiteDatabase = requestDatabase.openHelper.writableDatabase

override fun insert(downloadInfo: DownloadInfo): Pair<DownloadInfo, Boolean> {
synchronized(lock) {
throwExceptionIfClosed()
Expand Down Expand Up @@ -101,7 +104,6 @@ class DatabaseManagerImpl constructor(context: Context,
override fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo) {
synchronized(lock) {
throwExceptionIfClosed()
val database = requestDatabase.openHelper.writableDatabase
try {
database.beginTransaction()
database.execSQL("UPDATE ${DownloadDatabase.TABLE_NAME} SET "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ interface FileDownloader : Runnable {
fun onError(download: Download)

fun onComplete(download: Download)

fun saveDownloadProgress(download: Download)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class FileDownloaderImpl(private val initialDownload: Download,
downloaderOutputStream?.write(buffer, 0, read)
if (!terminated) {
downloaded += read
downloadInfo.downloaded = downloaded
downloadInfo.total = total
downloadSpeedStopTime = System.nanoTime()
val downloadSpeedCheckTimeElapsed = hasIntervalTimeElapsed(downloadSpeedStartTime,
downloadSpeedStopTime, DEFAULT_DOWNLOAD_SPEED_REPORTING_INTERVAL_IN_MILLISECONDS)
Expand All @@ -187,15 +189,19 @@ class FileDownloaderImpl(private val initialDownload: Download,
totalBytes = total,
downloadedBytesPerSecond = getAverageDownloadedBytesPerSecond())
downloadedBytesPerSecond = downloaded
if (progressReportingIntervalMillis > DEFAULT_DOWNLOAD_SPEED_REPORTING_INTERVAL_IN_MILLISECONDS) {
delegate?.saveDownloadProgress(downloadInfo)
}
}

reportingStopTime = System.nanoTime()
val hasReportingTimeElapsed = hasIntervalTimeElapsed(reportingStartTime,
reportingStopTime, progressReportingIntervalMillis)

if (hasReportingTimeElapsed) {
downloadInfo.downloaded = downloaded
downloadInfo.total = total
if (progressReportingIntervalMillis <= DEFAULT_DOWNLOAD_SPEED_REPORTING_INTERVAL_IN_MILLISECONDS) {
delegate?.saveDownloadProgress(downloadInfo)
}
if (!terminated) {
delegate?.onProgress(
download = downloadInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ class FileDownloaderDelegate(private val downloadInfoUpdater: DownloadInfoUpdate
}
}

private val progressRunnable = object: DownloadReportingRunnable() {
private val progressRunnable = object : DownloadReportingRunnable() {
override fun run() {
fetchListener.onProgress(download, etaInMilliSeconds, downloadedBytesPerSecond)
}
}

override fun onProgress(download: Download, etaInMilliSeconds: Long, downloadedBytesPerSecond: Long) {
try {
val downloadInfo = download as DownloadInfo
downloadInfo.status = Status.DOWNLOADING
downloadInfoUpdater.updateFileBytesInfoAndStatusOnly(downloadInfo)
progressRunnable.download = download
progressRunnable.etaInMilliSeconds = etaInMilliSeconds
progressRunnable.downloadedBytesPerSecond = downloadedBytesPerSecond
Expand Down Expand Up @@ -85,4 +82,13 @@ class FileDownloaderDelegate(private val downloadInfoUpdater: DownloadInfoUpdate
}
}

override fun saveDownloadProgress(download: Download) {
try {
val downloadInfo = download as DownloadInfo
downloadInfo.status = Status.DOWNLOADING
downloadInfoUpdater.updateFileBytesInfoAndStatusOnly(downloadInfo)
} catch (e: Exception) {
logger.e("DownloadManagerDelegate", e)
}
}
}

0 comments on commit 535a5b5

Please sign in to comment.