diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 803ea0552..8351c370f 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -121,14 +121,28 @@ bool TF2::get (const std::string& uuid, Task& task) if (! _loaded_tasks) load_tasks (); - for (auto& i : _tasks) + if (_tasks_map.size () > 0 && uuid.size () == 36) { - if (closeEnough (i.get ("uuid"), uuid, uuid.length ())) + // Fast lookup, same result as below. Only used during "task import". + auto i = _tasks_map.find (uuid); + if (i != _tasks_map.end ()) { - task = i; + task = i->second; return true; } } + else + { + // Slow lookup, same result as above. + for (auto& i : _tasks) + { + if (closeEnough (i.get ("uuid"), uuid, uuid.length ())) + { + task = i; + return true; + } + } + } return false; } @@ -152,6 +166,10 @@ void TF2::add_task (Task& task) _tasks.push_back (task); // For subsequent queries _added_tasks.push_back (task); // For commit/synch + // For faster lookup + if (context.cli2.getCommand () == "import") + _tasks_map.insert (std::pair (task.get("uuid"), task)); + Task::status status = task.getStatus (); if (task.id == 0 && (status == Task::pending || @@ -170,12 +188,23 @@ void TF2::add_task (Task& task) //////////////////////////////////////////////////////////////////////////////// bool TF2::modify_task (const Task& task) { - // Modify in-place. std::string uuid = task.get ("uuid"); + + if (context.cli2.getCommand () == "import") + { + // Update map used for faster lookup + auto i = _tasks_map.find (uuid); + if (i != _tasks_map.end ()) + { + i->second = task; + } + } + for (auto& i : _tasks) { if (i.get ("uuid") == uuid) { + // Modify in-place. i = task; _modified_tasks.push_back (task); _dirty = true; @@ -302,6 +331,8 @@ void TF2::load_tasks () } _tasks.push_back (task); + if (context.cli2.getCommand () == "import") // For faster lookup only + _tasks_map.insert (std::pair (task.get("uuid"), task)); // Maintain mapping for ease of link/dependency resolution. // Note that this mapping is not restricted by the filter, and is diff --git a/src/TDB2.h b/src/TDB2.h index f41d7d357..829a02a37 100644 --- a/src/TDB2.h +++ b/src/TDB2.h @@ -28,6 +28,7 @@ #define INCLUDED_TDB2 #include +#include #include #include #include @@ -81,6 +82,12 @@ public: bool _has_ids; bool _auto_dep_scan; std::vector _tasks; + + // _tasks_map was introduced mainly for speeding up "task import". + // Iterating over all _tasks for each imported task is slow, making use of + // appropriate data structures is fast. + std::unordered_map _tasks_map; + std::vector _added_tasks; std::vector _modified_tasks; std::vector _lines;