From b050d67ba962e677ab8ca32d0b2adb1592317444 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 4 Aug 2010 00:43:38 -0400 Subject: [PATCH] Dependencies - Added TDB::gc code to remove dangling dependencies. --- src/TDB.cpp | 22 ++++++++++++++++++++++ src/Task.cpp | 24 +++++++++++++++--------- src/Task.h | 1 + 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/TDB.cpp b/src/TDB.cpp index a85dd062f..316c1c803 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -569,6 +569,7 @@ int TDB::commit () // Scans the pending tasks for any that are completed or deleted, and if so, // moves them to the completed.data file. Returns a count of tasks moved. // Now reverts expired waiting tasks to pending. +// Now cleans up dangling dependencies. int TDB::gc () { Timer t ("TDB::gc"); @@ -589,6 +590,27 @@ int TDB::gc () std::vector ignore; loadPending (ignore, filter); + // Search for dangling dependencies. These are dependencies whose uuid cannot + // be converted to an id by TDB. + std::vector deps; + foreach (task, mPending) + { + if (task->has ("depends")) + { + deps.clear (); + task->getDependencies (deps); + foreach (dep, deps) + if (id (*dep) == 0) + { + task->removeDependency (*dep); + context.debug ("GC: Removed dangling dependency " + + *dep + + " from " + + task->get ("uuid")); + } + } + } + // Now move completed and deleted tasks from the pending list to the // completed list. Isn't garbage collection easy? std::vector still_pending; diff --git a/src/Task.cpp b/src/Task.cpp index a4126551c..ccbbcd6c4 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -495,16 +495,8 @@ void Task::addDependency (int id) } //////////////////////////////////////////////////////////////////////////////// -void Task::removeDependency (int id) +void Task::removeDependency (const std::string& uuid) { - std::string uuid = context.tdb.uuid (id); - if (uuid == "") - { - std::stringstream s; - s << "Could not find a UUID for id " << id << "."; - throw s.str (); - } - std::vector deps; split (deps, get ("depends"), ','); @@ -519,6 +511,20 @@ void Task::removeDependency (int id) } } +//////////////////////////////////////////////////////////////////////////////// +void Task::removeDependency (int id) +{ + std::string uuid = context.tdb.uuid (id); + if (uuid != "") + removeDependency (uuid); + else + { + std::stringstream s; + s << "Could not find a UUID for id " << id << "."; + throw s.str (); + } +} + //////////////////////////////////////////////////////////////////////////////// void Task::getDependencies (std::vector & all) const { diff --git a/src/Task.h b/src/Task.h index 11a7b95ac..4ff53405e 100644 --- a/src/Task.h +++ b/src/Task.h @@ -75,6 +75,7 @@ public: void addDependency (int); void removeDependency (int); + void removeDependency (const std::string&); void getDependencies (std::vector &) const; void getDependencies (std::vector &) const;