From 7e884b55e7098b13fb60e704863c08abeaa06a17 Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Thu, 30 Sep 2021 14:14:10 -0700 Subject: [PATCH] if frame is corrupt, delete and try caching again --- app/render/diskmanager.cpp | 63 +++++++++++++++++++++++++---------- app/render/diskmanager.h | 20 +++++++---- app/render/framehashcache.cpp | 3 ++ 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/app/render/diskmanager.cpp b/app/render/diskmanager.cpp index b47394c1c..29c60c925 100644 --- a/app/render/diskmanager.cpp +++ b/app/render/diskmanager.cpp @@ -117,6 +117,13 @@ void DiskManager::CreatedFile(const QString &cache_folder, const QString &file_n f->CreatedFile(file_name, hash); } +void DiskManager::DeleteSpecificFile(const QString &filename) +{ + foreach (DiskCacheFolder* f, open_folders_) { + f->DeleteSpecificFile(filename); + } +} + bool DiskManager::ClearDiskCache(const QString &cache_folder) { DiskCacheFolder* f = GetOpenFolder(cache_folder); @@ -239,14 +246,8 @@ void DiskCacheFolder::CreatedFile(const QString &file_name, const QByteArray &ha consumption_ += file_size; - QList deleted_hashes; - while (consumption_ > limit_) { - deleted_hashes.append(DeleteLeastRecent()); - } - - foreach (const QByteArray& h, deleted_hashes) { - emit DeletedFrame(path_, h); + DeleteLeastRecent(); } } @@ -305,7 +306,43 @@ void DiskCacheFolder::SetPath(const QString &path) } } -QByteArray DiskCacheFolder::DeleteLeastRecent() +bool DiskCacheFolder::DeleteFileInternal(QMap::iterator hash_to_delete) +{ + // Cache HashTime object + QByteArray hash = hash_to_delete.key(); + HashTime ht = hash_to_delete.value(); + + // Remove from disk + if (QFile::remove(ht.file_name)) { + // Remove from internal map + disk_data_.erase(hash_to_delete); + + // Reduce consumption + consumption_ -= ht.file_size; + + if (!hash.isEmpty()) { + emit DeletedFrame(path_, hash); + } + + return true; + } + + return false; +} + +bool DiskCacheFolder::DeleteSpecificFile(const QString &f) +{ + for (auto it=disk_data_.begin(); it!=disk_data_.end(); it++) { + if (it->file_name == f) { + // Break out of this loop, assuming we'll only have one instance of each filename + return DeleteFileInternal(it); + } + } + + return false; +} + +bool DiskCacheFolder::DeleteLeastRecent() { auto hash_to_delete = disk_data_.begin(); @@ -315,15 +352,7 @@ QByteArray DiskCacheFolder::DeleteLeastRecent() } } - QByteArray hash = hash_to_delete.key(); - HashTime ht = hash_to_delete.value(); - disk_data_.erase(hash_to_delete); - - QFile::remove(ht.file_name); - - consumption_ -= ht.file_size; - - return hash; + return DeleteFileInternal(hash_to_delete); } void DiskCacheFolder::CloseCacheFolder() diff --git a/app/render/diskmanager.h b/app/render/diskmanager.h index e5dc4b190..905db45b3 100644 --- a/app/render/diskmanager.h +++ b/app/render/diskmanager.h @@ -72,11 +72,21 @@ public: clear_on_close_ = e; } + bool DeleteSpecificFile(const QString &f); + signals: void DeletedFrame(const QString& path, const QByteArray& hash); private: - QByteArray DeleteLeastRecent(); + struct HashTime { + QString file_name; + qint64 file_size; + qint64 access_time; + }; + + bool DeleteFileInternal(QMap::iterator hash_to_delete); + + bool DeleteLeastRecent(); void CloseCacheFolder(); @@ -84,12 +94,6 @@ private: QString index_path_; - struct HashTime { - QString file_name; - qint64 file_size; - qint64 access_time; - }; - QMap disk_data_; qint64 consumption_; @@ -149,6 +153,8 @@ public slots: void CreatedFile(const QString& cache_folder, const QString& file_name, const QByteArray& hash); + void DeleteSpecificFile(const QString &filename); + signals: void DeletedFrame(const QString& path, const QByteArray& hash); diff --git a/app/render/framehashcache.cpp b/app/render/framehashcache.cpp index c4f2c1f9d..0945d1928 100644 --- a/app/render/framehashcache.cpp +++ b/app/render/framehashcache.cpp @@ -242,6 +242,9 @@ FramePtr FrameHashCache::LoadCacheFrame(const QString &fn) // Clear frame to signal that nothing was loaded frame = nullptr; + + // Assume this frame is corrupt in some way and delete it + QMetaObject::invokeMethod(DiskManager::instance(), "DeleteSpecificFile", Qt::QueuedConnection, Q_ARG(QString, fn)); } }