From 349cade946ad6a981465f8810c59fe3f61841779 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 15 Jun 2019 21:48:37 +0100 Subject: [PATCH] All: Optimised resource download queue by exiting early if resources are already downloaded --- ReactNativeClient/lib/models/Resource.js | 5 +++++ ReactNativeClient/lib/services/ResourceFetcher.js | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ReactNativeClient/lib/models/Resource.js b/ReactNativeClient/lib/models/Resource.js index fbd75b332..e696cd34e 100644 --- a/ReactNativeClient/lib/models/Resource.js +++ b/ReactNativeClient/lib/models/Resource.js @@ -32,6 +32,11 @@ class Resource extends BaseItem { return imageMimeTypes.indexOf(type.toLowerCase()) >= 0; } + static fetchStatuses(resourceIds) { + if (!resourceIds.length) return []; + return this.db().selectAll('SELECT resource_id, fetch_status FROM resource_local_states WHERE resource_id IN ("' + resourceIds.join('","') + '")'); + } + static needToBeFetched(resourceDownloadMode = null, limit = null) { let sql = ['SELECT * FROM resources WHERE encryption_applied = 0 AND id IN (SELECT resource_id FROM resource_local_states WHERE fetch_status = ?)']; if (resourceDownloadMode !== 'always') { diff --git a/ReactNativeClient/lib/services/ResourceFetcher.js b/ReactNativeClient/lib/services/ResourceFetcher.js index 040108b5e..91bfa542c 100644 --- a/ReactNativeClient/lib/services/ResourceFetcher.js +++ b/ReactNativeClient/lib/services/ResourceFetcher.js @@ -75,11 +75,19 @@ class ResourceFetcher extends BaseService { async markForDownload(resourceIds) { if (!Array.isArray(resourceIds)) resourceIds = [resourceIds]; - for (const id of resourceIds) { + const fetchStatuses = await Resource.fetchStatuses(resourceIds); + + const idsToKeep = []; + for (const status of fetchStatuses) { + if (status.fetch_status !== Resource.FETCH_STATUS_IDLE) continue; + idsToKeep.push(status.resource_id); + } + + for (const id of idsToKeep) { await Resource.markForDownload(id); } - for (const id of resourceIds) { + for (const id of idsToKeep) { this.queueDownload_(id, 'high'); } }