Index: src/libchcore/TObsoleteFiles.cpp =================================================================== diff -u --- src/libchcore/TObsoleteFiles.cpp (revision 0) +++ src/libchcore/TObsoleteFiles.cpp (revision a99c8baeb8f6c237603df46c0f5c4cf943152c09) @@ -0,0 +1,115 @@ +// ============================================================================ +// Copyright (C) 2001-2014 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#include "stdafx.h" +#include "TObsoleteFiles.h" +#include "ISerializerContainer.h" + +BEGIN_CHCORE_NAMESPACE + +ObsoleteFileInfo::ObsoleteFileInfo(TSmartPath path, bool bAdded) : + m_path(path), + m_bAdded(bAdded) +{ +} + +ObsoleteFileInfo::ObsoleteFileInfo() : + m_bAdded(false) +{ +} + +TObsoleteFiles::TObsoleteFiles() : + m_oidLast(0) +{ +} + +TObsoleteFiles::~TObsoleteFiles() +{ +} + +void TObsoleteFiles::DeleteObsoleteFile(const TSmartPath& pathToDelete) +{ + if(!DeleteFile(pathToDelete.ToString()) && GetLastError() != ERROR_FILE_NOT_FOUND) + m_mapPaths.insert(std::make_pair(++m_oidLast, ObsoleteFileInfo(pathToDelete, true))); +} + +void TObsoleteFiles::Store(const ISerializerContainerPtr& spContainer) const +{ + InitColumns(spContainer); + + spContainer->DeleteRows(m_setRemovedObjects); + m_setRemovedObjects.Clear(); + + for(MapPaths::const_iterator iter = m_mapPaths.begin(); iter != m_mapPaths.end(); ++iter) + { + if(iter->second.m_bAdded) + { + ISerializerRowData& rRow = spContainer->GetRow(iter->first, true); + rRow.SetValue(_T("path"), iter->second.m_path); + } + + iter->second.m_bAdded = false; + } +} + +void TObsoleteFiles::Load(const ISerializerContainerPtr& spContainer) +{ + InitColumns(spContainer); + + ISerializerRowReaderPtr spRowReader = spContainer->GetRowReader(); + + ObsoleteFileInfo tEntry; + object_id_t oid = 0; + while(spRowReader->Next()) + { + spRowReader->GetValue(_T("id"), oid); + spRowReader->GetValue(_T("path"), tEntry.m_path); + tEntry.m_bAdded = false; + + m_mapPaths.insert(std::make_pair(oid, tEntry)); + m_oidLast = std::max(m_oidLast, oid); + } + + m_setRemovedObjects.Clear(); + + // try to delete files + MapPaths::iterator iter = m_mapPaths.begin(); + while(iter != m_mapPaths.end()) + { + BOOL bDeleted = DeleteFile(iter->second.m_path.ToString()); + if(bDeleted || GetLastError() == ERROR_FILE_NOT_FOUND) + { + m_setRemovedObjects.Add(iter->first); + iter = m_mapPaths.erase(iter); + } + else + ++iter; + } +} + +void TObsoleteFiles::InitColumns(const ISerializerContainerPtr& spContainer) const +{ + IColumnsDefinition& rColumns = spContainer->GetColumnsDefinition(); + if(rColumns.IsEmpty()) + { + rColumns.AddColumn(_T("id"), ColumnType::value); + rColumns.AddColumn(_T("path"), IColumnsDefinition::eType_path); + } +} + +END_CHCORE_NAMESPACE Index: src/libchcore/TObsoleteFiles.h =================================================================== diff -u --- src/libchcore/TObsoleteFiles.h (revision 0) +++ src/libchcore/TObsoleteFiles.h (revision a99c8baeb8f6c237603df46c0f5c4cf943152c09) @@ -0,0 +1,66 @@ +// ============================================================================ +// Copyright (C) 2001-2014 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#ifndef __TOBSOLETEFILES_H__ +#define __TOBSOLETEFILES_H__ + +#include "libchcore.h" +#include "TPath.h" +#include +#include "ISerializerRowData.h" +#include "SerializerDataTypes.h" +#include "TRemovedObjects.h" + +BEGIN_CHCORE_NAMESPACE + +struct ObsoleteFileInfo +{ + ObsoleteFileInfo(); + ObsoleteFileInfo(TSmartPath path, bool bAdded); + + TSmartPath m_path; + mutable bool m_bAdded; +}; + +class LIBCHCORE_API TObsoleteFiles +{ +public: + TObsoleteFiles(); + virtual ~TObsoleteFiles(); + + void DeleteObsoleteFile(const TSmartPath& pathToDelete); + + void Store(const ISerializerContainerPtr& spContainer) const; + void Load(const ISerializerContainerPtr& spContainer); + + void InitColumns(const ISerializerContainerPtr& spContainer) const; + +private: +#pragma warning(push) +#pragma warning(disable: 4251) + typedef std::map MapPaths; + MapPaths m_mapPaths; +#pragma warning(pop) + + mutable TRemovedObjects m_setRemovedObjects; + object_id_t m_oidLast; +}; + +END_CHCORE_NAMESPACE + +#endif Index: src/libchcore/TSQLiteTaskManagerSchema.cpp =================================================================== diff -u -r0c5027d5173ab0daeba6aa6b735a2b11d4cd2164 -ra99c8baeb8f6c237603df46c0f5c4cf943152c09 --- src/libchcore/TSQLiteTaskManagerSchema.cpp (.../TSQLiteTaskManagerSchema.cpp) (revision 0c5027d5173ab0daeba6aa6b735a2b11d4cd2164) +++ src/libchcore/TSQLiteTaskManagerSchema.cpp (.../TSQLiteTaskManagerSchema.cpp) (revision a99c8baeb8f6c237603df46c0f5c4cf943152c09) @@ -45,13 +45,24 @@ if(tVersion.GetVersion() == 0) { TSQLiteStatement tStatement(spDatabase); + tStatement.Prepare(_T("CREATE TABLE tasks(id BIGINT UNIQUE PRIMARY KEY, task_order INT NOT NULL, path VARCHAR(32768) NOT NULL)")); tStatement.Step(); // and finally set the database version to current one tVersion.SetVersion(1); } + if(tVersion.GetVersion() == 1) + { + TSQLiteStatement tStatement(spDatabase); + tStatement.Prepare(_T("CREATE TABLE obsolete_tasks(id BIGINT UNIQUE PRIMARY KEY, path VARCHAR(32768) NOT NULL)")); + tStatement.Step(); + + // and finally set the database version to current one + tVersion.SetVersion(2); + } + tTransaction.Commit(); } Index: src/libchcore/TTaskManager.cpp =================================================================== diff -u -ra44714d5c7ec0f50a376f4d0ea919ee5a224f834 -ra99c8baeb8f6c237603df46c0f5c4cf943152c09 --- src/libchcore/TTaskManager.cpp (.../TTaskManager.cpp) (revision a44714d5c7ec0f50a376f4d0ea919ee5a224f834) +++ src/libchcore/TTaskManager.cpp (.../TTaskManager.cpp) (revision a99c8baeb8f6c237603df46c0f5c4cf943152c09) @@ -140,8 +140,6 @@ void TTaskManager::RemoveAllFinished() { - std::vector vTasksSerializersToRemove; - // separate scope for locking { boost::unique_lock lock(m_lock); @@ -163,23 +161,17 @@ spTask->OnUnregisterTask(); - vTasksSerializersToRemove.push_back(spTask->GetSerializer()->GetLocation().ToWString()); + m_tObsoleteFiles.DeleteObsoleteFile(spTask->GetSerializer()->GetLocation()); + m_tObsoleteFiles.DeleteObsoleteFile(spTask->GetLogPath()); + m_tTasks.RemoveAt(stIndex); } } } - - BOOST_FOREACH(const TString& strSerializerPath, vTasksSerializersToRemove) - { - // delete associated files - DeleteFile(strSerializerPath.c_str()); - } } void TTaskManager::RemoveFinished(const TTaskPtr& spSelTask) { - std::vector vTasksToRemove; - // separate scope for locking { boost::unique_lock lock(m_lock); @@ -203,19 +195,15 @@ spTask->OnUnregisterTask(); - vTasksToRemove.push_back(rEntry.GetTaskSerializeLocation()); + m_tObsoleteFiles.DeleteObsoleteFile(spTask->GetSerializer()->GetLocation()); + m_tObsoleteFiles.DeleteObsoleteFile(spTask->GetLogPath()); + m_tTasks.RemoveAt(stIndex); } break; } } } - - BOOST_FOREACH(const TSmartPath& spTaskPath, vTasksToRemove) - { - // delete associated files - DeleteFile(spTaskPath.ToString()); - } } void TTaskManager::StopAllTasks() @@ -442,10 +430,10 @@ { TSimpleTimer timer(true); - ISerializerContainerPtr spContainer = m_spSerializer->GetContainer(_T("tasks")); - // store this container information { + ISerializerContainerPtr spContainer = m_spSerializer->GetContainer(_T("tasks")); + boost::shared_lock lock(m_lock); m_tTasks.Store(spContainer); @@ -456,6 +444,14 @@ } } + // store obsolete info + { + ISerializerContainerPtr spContainer = m_spSerializer->GetContainer(_T("obsolete_tasks")); + + boost::shared_lock lock(m_lock); + m_tObsoleteFiles.Store(spContainer); + } + unsigned long long ullGatherTime = timer.Checkpoint(); ullGatherTime; m_spSerializer->Flush(); @@ -477,6 +473,14 @@ m_tTasks.Load(spContainer); } + // load list of task files to delete + { + boost::unique_lock lock(m_lock); + + ISerializerContainerPtr spContainer = m_spSerializer->GetContainer(_T("obsolete_tasks")); + m_tObsoleteFiles.Load(spContainer); // loader also tries to delete files + } + // retrieve information about tasks to load std::vector > vObjects; { Index: src/libchcore/TTaskManager.h =================================================================== diff -u -rd0bc3c187684f54894c7280a936d5507a5e49f35 -ra99c8baeb8f6c237603df46c0f5c4cf943152c09 --- src/libchcore/TTaskManager.h (.../TTaskManager.h) (revision d0bc3c187684f54894c7280a936d5507a5e49f35) +++ src/libchcore/TTaskManager.h (.../TTaskManager.h) (revision a99c8baeb8f6c237603df46c0f5c4cf943152c09) @@ -26,6 +26,7 @@ #include "TTaskInfo.h" #include "ISerializer.h" #include "ISerializerFactory.h" +#include "TObsoleteFiles.h" BEGIN_CHCORE_NAMESPACE @@ -93,6 +94,8 @@ TSmartPath m_pathLogDir; // config-based, not serializable + TObsoleteFiles m_tObsoleteFiles; + #pragma warning(push) #pragma warning(disable: 4251) IFeedbackHandlerFactoryPtr m_spFeedbackFactory; Index: src/libchcore/libchcore.vc90.vcproj =================================================================== diff -u -ra44714d5c7ec0f50a376f4d0ea919ee5a224f834 -ra99c8baeb8f6c237603df46c0f5c4cf943152c09 --- src/libchcore/libchcore.vc90.vcproj (.../libchcore.vc90.vcproj) (revision a44714d5c7ec0f50a376f4d0ea919ee5a224f834) +++ src/libchcore/libchcore.vc90.vcproj (.../libchcore.vc90.vcproj) (revision a99c8baeb8f6c237603df46c0f5c4cf943152c09) @@ -1116,6 +1116,14 @@ > + + + +