if frame is corrupt, delete and try caching again

This commit is contained in:
itsmattkc
2021-09-30 14:14:10 -07:00
parent 5485ea5750
commit 7e884b55e7
3 changed files with 62 additions and 24 deletions
+46 -17
View File
@@ -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<QByteArray> 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<QByteArray, HashTime>::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()
+13 -7
View File
@@ -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<QByteArray, HashTime>::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<QByteArray, HashTime> 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);
+3
View File
@@ -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));
}
}