From a5785dd41ac56c11c2d46b6346bd52814fb0ba05 Mon Sep 17 00:00:00 2001 From: Shaun Ruffell Date: Sun, 5 Jan 2020 20:25:56 -0600 Subject: [PATCH] CmdUntag: Do not load entire database when untagging intervals Related to issue #245 --- src/commands/CmdUntag.cpp | 62 ++++++++++++--------------------------- 1 file changed, 19 insertions(+), 43 deletions(-) diff --git a/src/commands/CmdUntag.cpp b/src/commands/CmdUntag.cpp index 55e87f8f..294edaf3 100644 --- a/src/commands/CmdUntag.cpp +++ b/src/commands/CmdUntag.cpp @@ -39,76 +39,52 @@ int CmdUntag ( Journal& journal) { // Gather IDs and TAGs. + const bool verbose = rules.getBoolean ("verbose"); std::set ids = cli.getIds (); std::vector tags = cli.getTags (); if (tags.empty ()) { - throw std::string ("At least one tag must be specified. See 'timew help untag'."); + throw std::string ("At least one tag must be specified. See 'timew help tag'."); } journal.startTransaction (); - // Load the data. - // Note: There is no filter. - Interval filter; - auto tracked = getTracked (database, rules, filter); + flattenDatabase (database, rules); + auto intervals = getIntervalsByIds (database, rules, ids); - bool dirty = true; - - for (auto& id : ids) + if (intervals.empty ()) { - if (id > static_cast (tracked.size ())) - throw format ("ID '@{1}' does not correspond to any tracking.", id); - - if (tracked[tracked.size() - id].synthetic && dirty) - { - auto latest = getLatestInterval(database); - auto exclusions = getAllExclusions (rules, filter); - - Interval modified {latest}; - - // Update database. - database.deleteInterval (latest); - for (auto& interval : flatten (modified, exclusions)) - database.addInterval (interval, rules.getBoolean ("verbose")); - - dirty = false; - } - } - - if (ids.empty ()) - { - if (tracked.empty ()) + if (database.empty ()) { throw std::string ("There is no active time tracking."); } - if (!tracked.back ().is_open ()) + auto latest = getLatestInterval (database); + + if (!latest.is_open ()) { throw std::string ("At least one ID must be specified. See 'timew help tag'."); } - ids.insert (1); + latest.id = 1; + intervals.push_back (latest); } - // Remove tags from ids. - for (auto& id : ids) + // Apply tags to ids. + for (const auto& interval : intervals) { - if (id > static_cast (tracked.size ())) - throw format ("ID '@{1}' does not correspond to any tracking.", id); - - Interval i = tracked[tracked.size () - id]; + Interval modified {interval}; for (auto& tag : tags) - i.untag (tag); + modified.untag (tag); - // TODO validate (cli, rules, database, i); - database.modifyInterval (tracked[tracked.size () - id], i, rules.getBoolean ("verbose")); + //TODO validate (cli, rules, database, i); + database.modifyInterval (interval, modified, verbose); - if (rules.getBoolean ("verbose")) + if (verbose) { - std::cout << "Removed " << joinQuotedIfNeeded (" ", tags) << " from @" << id << '\n'; + std::cout << "Removed " << joinQuotedIfNeeded (" ", tags) << " from @" << interval.id << '\n'; } }