Add actual byte size to Resource

This commit is contained in:
Zach Pomerantz 2016-03-31 14:32:08 -07:00
parent 9d3abe5513
commit b80f0fc8a6
2 changed files with 15 additions and 6 deletions

View file

@ -117,22 +117,22 @@ void ResourceCache::setUnusedResourceCacheSize(qint64 unusedResourcesMaxSize) {
} }
void ResourceCache::addUnusedResource(const QSharedPointer<Resource>& resource) { void ResourceCache::addUnusedResource(const QSharedPointer<Resource>& resource) {
if (resource->getBytesTotal() > _unusedResourcesMaxSize) { if (resource->getBytes() > _unusedResourcesMaxSize) {
// If it doesn't fit anyway, let's leave whatever is already in the cache. // If it doesn't fit anyway, let's leave whatever is already in the cache.
resource->setCache(nullptr); resource->setCache(nullptr);
return; return;
} }
reserveUnusedResource(resource->getBytesTotal()); reserveUnusedResource(resource->getBytes());
resource->setLRUKey(++_lastLRUKey); resource->setLRUKey(++_lastLRUKey);
_unusedResources.insert(resource->getLRUKey(), resource); _unusedResources.insert(resource->getLRUKey(), resource);
_unusedResourcesSize += resource->getBytesTotal(); _unusedResourcesSize += resource->getBytes();
} }
void ResourceCache::removeUnusedResource(const QSharedPointer<Resource>& resource) { void ResourceCache::removeUnusedResource(const QSharedPointer<Resource>& resource) {
if (_unusedResources.contains(resource->getLRUKey())) { if (_unusedResources.contains(resource->getLRUKey())) {
_unusedResources.remove(resource->getLRUKey()); _unusedResources.remove(resource->getLRUKey());
_unusedResourcesSize -= resource->getBytesTotal(); _unusedResourcesSize -= resource->getBytes();
} }
} }
@ -142,7 +142,7 @@ void ResourceCache::reserveUnusedResource(qint64 resourceSize) {
// unload the oldest resource // unload the oldest resource
QMap<int, QSharedPointer<Resource> >::iterator it = _unusedResources.begin(); QMap<int, QSharedPointer<Resource> >::iterator it = _unusedResources.begin();
_unusedResourcesSize -= it.value()->getBytesTotal(); _unusedResourcesSize -= it.value()->getBytes();
it.value()->setCache(nullptr); it.value()->setCache(nullptr);
_unusedResources.erase(it); _unusedResources.erase(it);
} }
@ -399,7 +399,7 @@ void Resource::makeRequest() {
connect(_request, &ResourceRequest::progress, this, &Resource::handleDownloadProgress); connect(_request, &ResourceRequest::progress, this, &Resource::handleDownloadProgress);
connect(_request, &ResourceRequest::finished, this, &Resource::handleReplyFinished); connect(_request, &ResourceRequest::finished, this, &Resource::handleReplyFinished);
_bytesReceived = _bytesTotal = 0; _bytesReceived = _bytesTotal = _bytes = 0;
_request->send(); _request->send();
} }
@ -412,6 +412,8 @@ void Resource::handleDownloadProgress(uint64_t bytesReceived, uint64_t bytesTota
void Resource::handleReplyFinished() { void Resource::handleReplyFinished() {
Q_ASSERT_X(_request, "Resource::handleReplyFinished", "Request should not be null while in handleReplyFinished"); Q_ASSERT_X(_request, "Resource::handleReplyFinished", "Request should not be null while in handleReplyFinished");
_bytes = _bytesTotal;
if (!_request || _request != sender()) { if (!_request || _request != sender()) {
// This can happen in the edge case that a request is timed out, but a `finished` signal is emitted before it is deleted. // This can happen in the edge case that a request is timed out, but a `finished` signal is emitted before it is deleted.
qWarning(networking) << "Received signal Resource::handleReplyFinished from ResourceRequest that is not the current" qWarning(networking) << "Received signal Resource::handleReplyFinished from ResourceRequest that is not the current"

View file

@ -181,6 +181,9 @@ public:
/// For loading resources, returns the number of total bytes (<= zero if unknown). /// For loading resources, returns the number of total bytes (<= zero if unknown).
qint64 getBytesTotal() const { return _bytesTotal; } qint64 getBytesTotal() const { return _bytesTotal; }
/// For loaded resources, returns the number of actual bytes (defaults to total bytes if not explicitly set).
qint64 getBytes() const { return _bytes; }
/// For loading resources, returns the load progress. /// For loading resources, returns the load progress.
float getProgress() const { return (_bytesTotal <= 0) ? 0.0f : (float)_bytesReceived / _bytesTotal; } float getProgress() const { return (_bytesTotal <= 0) ? 0.0f : (float)_bytesReceived / _bytesTotal; }
@ -222,6 +225,9 @@ protected:
/// This should be overridden by subclasses that need to process the data once it is downloaded. /// This should be overridden by subclasses that need to process the data once it is downloaded.
virtual void downloadFinished(const QByteArray& data) { finishedLoading(true); } virtual void downloadFinished(const QByteArray& data) { finishedLoading(true); }
/// Called when the download is finished and processed, sets the number of actual bytes.
void setBytes(qint64 bytes) { _bytes = bytes; }
/// Called when the download is finished and processed. /// Called when the download is finished and processed.
/// This should be called by subclasses that override downloadFinished to mark the end of processing. /// This should be called by subclasses that override downloadFinished to mark the end of processing.
Q_INVOKABLE void finishedLoading(bool success); Q_INVOKABLE void finishedLoading(bool success);
@ -255,6 +261,7 @@ private:
QTimer* _replyTimer = nullptr; QTimer* _replyTimer = nullptr;
qint64 _bytesReceived = 0; qint64 _bytesReceived = 0;
qint64 _bytesTotal = 0; qint64 _bytesTotal = 0;
qint64 _bytes = 0;
int _attempts = 0; int _attempts = 0;
}; };