From 09dde2b82fbf0a3045613f175f92d9699e8b3349 Mon Sep 17 00:00:00 2001 From: Andrey Prygunkov Date: Thu, 7 Jan 2016 00:07:06 +0100 Subject: [PATCH] #143: store Scanner objects directly in container --- daemon/queue/Scanner.cpp | 71 ++++++++++------------------------------ daemon/queue/Scanner.h | 9 +++-- 2 files changed, 21 insertions(+), 59 deletions(-) diff --git a/daemon/queue/Scanner.cpp b/daemon/queue/Scanner.cpp index 95bf8587..311ed85b 100644 --- a/daemon/queue/Scanner.cpp +++ b/daemon/queue/Scanner.cpp @@ -33,14 +33,6 @@ #include "Util.h" #include "FileSystem.h" -Scanner::FileData::FileData(const char* filename) -{ - m_filename = filename; - m_size = 0; - m_lastChange = 0; -} - - Scanner::QueueData::QueueData(const char* filename, const char* nzbName, const char* category, int priority, const char* dupeKey, int dupeScore, EDupeMode dupeMode, NzbParameterList* parameters, bool addTop, bool addPaused, NzbInfo* urlInfo, @@ -93,19 +85,6 @@ Scanner::Scanner() m_scanScript = false; } -Scanner::~Scanner() -{ - debug("Destroying Scanner"); - - for (FileList::iterator it = m_fileList.begin(); it != m_fileList.end(); it++) - { - delete *it; - } - m_fileList.clear(); - - ClearQueueList(); -} - void Scanner::InitOptions() { m_nzbDirInterval = g_Options->GetNzbDirInterval() * 1000; @@ -113,15 +92,6 @@ void Scanner::InitOptions() m_scanScript = scanScript && strlen(scanScript) > 0; } -void Scanner::ClearQueueList() -{ - for (QueueList::iterator it = m_queueList.begin(); it != m_queueList.end(); it++) - { - delete *it; - } - m_queueList.clear(); -} - void Scanner::ServiceWork() { if (!DownloadQueue::IsLoaded()) @@ -169,7 +139,7 @@ void Scanner::ServiceWork() } DropOldFiles(); - ClearQueueList(); + m_queueList.clear(); } m_nzbDirInterval += 200; @@ -236,23 +206,22 @@ bool Scanner::CanProcessFile(const char* fullFilename, bool checkStat) for (FileList::iterator it = m_fileList.begin(); it != m_fileList.end(); it++) { - FileData* fileData = *it; - if (!strcmp(fileData->GetFilename(), fullFilename)) + FileData& fileData = *it; + if (!strcmp(fileData.GetFilename(), fullFilename)) { inList = true; - if (fileData->GetSize() == size && - current - fileData->GetLastChange() >= g_Options->GetNzbDirFileAge()) + if (fileData.GetSize() == size && + current - fileData.GetLastChange() >= g_Options->GetNzbDirFileAge()) { canProcess = true; - delete fileData; m_fileList.erase(it); } else { - fileData->SetSize(size); - if (fileData->GetSize() != size) + fileData.SetSize(size); + if (fileData.GetSize() != size) { - fileData->SetLastChange(current); + fileData.SetLastChange(current); } } break; @@ -261,10 +230,7 @@ bool Scanner::CanProcessFile(const char* fullFilename, bool checkStat) if (!inList) { - FileData* fileData = new FileData(fullFilename); - fileData->SetSize(size); - fileData->SetLastChange(current); - m_fileList.push_back(fileData); + m_fileList.emplace_back(fullFilename, size, current); } return canProcess; @@ -284,15 +250,13 @@ void Scanner::DropOldFiles() int i = 0; for (FileList::iterator it = m_fileList.begin(); it != m_fileList.end(); ) { - FileData* fileData = *it; - if ((current - fileData->GetLastChange() >= + FileData& fileData = *it; + if ((current - fileData.GetLastChange() >= (g_Options->GetNzbDirInterval() + g_Options->GetNzbDirFileAge()) * 2) || // can occur if the system clock was adjusted - current < fileData->GetLastChange()) + current < fileData.GetLastChange()) { - debug("Removing file %s from scan file list", fileData->GetFilename()); - - delete fileData; + debug("Removing file %s from scan file list", fileData.GetFilename()); m_fileList.erase(it); it = m_fileList.begin() + i; } @@ -329,10 +293,10 @@ void Scanner::ProcessIncomingFile(const char* directory, const char* baseFilenam for (QueueList::iterator it = m_queueList.begin(); it != m_queueList.end(); it++) { - QueueData* queueData1 = *it; - if (FileSystem::SameFilename(queueData1->GetFilename(), fullFilename)) + QueueData& queueData1 = *it; + if (FileSystem::SameFilename(queueData1.GetFilename(), fullFilename)) { - queueData = queueData1; + queueData = &queueData1; free(nzbName); nzbName = strdup(queueData->GetNzbName()); free(nzbCategory); @@ -625,10 +589,9 @@ Scanner::EAddStatus Scanner::AddExternalFile(const char* nzbName, const char* ca } EAddStatus addStatus = asSkipped; - QueueData* queueData = new QueueData(scanFileName, nzbName, useCategory, priority, + m_queueList.emplace_back(scanFileName, nzbName, useCategory, priority, dupeKey, dupeScore, dupeMode, parameters, addTop, addPaused, urlInfo, &addStatus, nzbId); - m_queueList.push_back(queueData); m_scanMutex.Unlock(); diff --git a/daemon/queue/Scanner.h b/daemon/queue/Scanner.h index e33c4f4a..e4ac24c3 100644 --- a/daemon/queue/Scanner.h +++ b/daemon/queue/Scanner.h @@ -50,7 +50,8 @@ private: time_t m_lastChange; public: - FileData(const char* filename); + FileData(const char* filename, int64 size, time_t lastChange) : + m_filename(filename), m_size(size), m_lastChange(lastChange) {} const char* GetFilename() { return m_filename; } int64 GetSize() { return m_size; } void SetSize(int64 size) { m_size = size; } @@ -58,7 +59,7 @@ private: void SetLastChange(time_t lastChange) { m_lastChange = lastChange; } }; - typedef std::deque FileList; + typedef std::deque FileList; class QueueData { @@ -97,7 +98,7 @@ private: void SetNzbId(int nzbId); }; - typedef std::deque QueueList; + typedef std::deque QueueList; bool m_requestedNzbDirScan; int m_nzbDirInterval; @@ -116,7 +117,6 @@ private: const char* fullFilename, const char* category); bool CanProcessFile(const char* fullFilename, bool checkStat); void DropOldFiles(); - void ClearQueueList(); protected: virtual int ServiceInterval() { return 200; } @@ -124,7 +124,6 @@ protected: public: Scanner(); - ~Scanner(); void InitOptions(); void ScanNzbDir(bool syncMode); EAddStatus AddExternalFile(const char* nzbName, const char* category, int priority,