From 535a5b56b9ed2e6bcf9eece4b250b57297096bfa Mon Sep 17 00:00:00 2001 From: Tonyo Francis Date: Tue, 24 Apr 2018 21:14:05 -0400 Subject: [PATCH] optimized saving progress update to the database --- .../fetch2/database/DatabaseManagerImpl.kt | 4 +++- .../tonyodev/fetch2/downloader/FileDownloader.kt | 3 +++ .../fetch2/downloader/FileDownloaderImpl.kt | 10 ++++++++-- .../fetch2/helper/FileDownloaderDelegate.kt | 14 ++++++++++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt index 0f327c73..239e101d 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt @@ -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 @@ -38,6 +39,8 @@ class DatabaseManagerImpl constructor(context: Context, builder.build() }() + val database: SupportSQLiteDatabase = requestDatabase.openHelper.writableDatabase + override fun insert(downloadInfo: DownloadInfo): Pair { synchronized(lock) { throwExceptionIfClosed() @@ -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 " diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloader.kt b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloader.kt index 75d10b59..f87fc0c6 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloader.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloader.kt @@ -23,6 +23,9 @@ interface FileDownloader : Runnable { fun onError(download: Download) fun onComplete(download: Download) + + fun saveDownloadProgress(download: Download) + } } \ No newline at end of file diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloaderImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloaderImpl.kt index 2b270f9a..ac2a8262 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloaderImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/FileDownloaderImpl.kt @@ -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) @@ -187,6 +189,9 @@ 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() @@ -194,8 +199,9 @@ class FileDownloaderImpl(private val initialDownload: Download, 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, diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/helper/FileDownloaderDelegate.kt b/fetch2/src/main/java/com/tonyodev/fetch2/helper/FileDownloaderDelegate.kt index be66e645..923790d8 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/helper/FileDownloaderDelegate.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/helper/FileDownloaderDelegate.kt @@ -30,7 +30,7 @@ class FileDownloaderDelegate(private val downloadInfoUpdater: DownloadInfoUpdate } } - private val progressRunnable = object: DownloadReportingRunnable() { + private val progressRunnable = object : DownloadReportingRunnable() { override fun run() { fetchListener.onProgress(download, etaInMilliSeconds, downloadedBytesPerSecond) } @@ -38,9 +38,6 @@ class FileDownloaderDelegate(private val downloadInfoUpdater: DownloadInfoUpdate 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 @@ -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) + } + } } \ No newline at end of file