CmdUntag: Do not load entire database when untagging intervals

Related to issue #245
This commit is contained in:
Shaun Ruffell 2020-01-05 20:25:56 -06:00 committed by lauft
parent aa44df24c0
commit a5785dd41a

View file

@ -39,76 +39,52 @@ int CmdUntag (
Journal& journal) Journal& journal)
{ {
// Gather IDs and TAGs. // Gather IDs and TAGs.
const bool verbose = rules.getBoolean ("verbose");
std::set <int> ids = cli.getIds (); std::set <int> ids = cli.getIds ();
std::vector<std::string> tags = cli.getTags (); std::vector<std::string> tags = cli.getTags ();
if (tags.empty ()) 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 (); journal.startTransaction ();
// Load the data. flattenDatabase (database, rules);
// Note: There is no filter. auto intervals = getIntervalsByIds (database, rules, ids);
Interval filter;
auto tracked = getTracked (database, rules, filter);
bool dirty = true; if (intervals.empty ())
for (auto& id : ids)
{ {
if (id > static_cast <int> (tracked.size ())) if (database.empty ())
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 ())
{ {
throw std::string ("There is no active time tracking."); 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'."); 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. // Apply tags to ids.
for (auto& id : ids) for (const auto& interval : intervals)
{ {
if (id > static_cast <int> (tracked.size ())) Interval modified {interval};
throw format ("ID '@{1}' does not correspond to any tracking.", id);
Interval i = tracked[tracked.size () - id];
for (auto& tag : tags) for (auto& tag : tags)
i.untag (tag); modified.untag (tag);
// TODO validate (cli, rules, database, i); //TODO validate (cli, rules, database, i);
database.modifyInterval (tracked[tracked.size () - id], i, rules.getBoolean ("verbose")); 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';
} }
} }