CmdMove: Do not load entire database

Related to issue #245
This commit is contained in:
Shaun Ruffell 2020-01-05 21:06:08 -06:00 committed by lauft
parent 18559df275
commit 1b8c674771

View file

@ -40,6 +40,7 @@ int CmdMove (
Journal& journal) Journal& journal)
{ {
// Gather ID and TAGs. // Gather ID and TAGs.
const bool verbose = rules.getBoolean ("verbose");
std::set <int> ids = cli.getIds (); std::set <int> ids = cli.getIds ();
if (ids.size() > 1) if (ids.size() > 1)
@ -63,57 +64,47 @@ int CmdMove (
new_start = arg.attribute ("raw"); new_start = arg.attribute ("raw");
} }
// Load the data. std::vector <Interval> intervals = getIntervalsByIds (database, rules, ids);
// Note: There is no filter. Interval interval = intervals.at (0);
Interval filter;
auto tracked = getTracked (database, rules, filter);
if (id > static_cast <int> (tracked.size ())) if (interval.synthetic)
throw format ("ID '@{1}' does not correspond to any tracking.", id);
if (tracked[tracked.size() - id].synthetic)
{ {
auto latest = getLatestInterval(database); flattenDatabase (database, rules);
auto exclusions = getAllExclusions (rules, filter); intervals = getIntervalsByIds (database, rules, ids);
interval = intervals.at (0);
Interval modified {latest};
// Update database.
database.deleteInterval (latest);
for (auto& interval : flatten (modified, exclusions))
database.addInterval (interval, rules.getBoolean ("verbose"));
} }
// Move start time. // Move start time.
Interval i = tracked[tracked.size () - id];
Datetime start (new_start); Datetime start (new_start);
// Changing the start date should also change the end date by the same // Changing the start date should also change the end date by the same
// amount. // amount.
if (i.start < start) if (interval.start < start)
{ {
auto delta = start - i.start; auto delta = start - interval.start;
i.start = start; interval.start = start;
if (! i.is_open ()) if (! interval.is_open ())
i.end += delta; interval.end += delta;
} }
else else
{ {
auto delta = i.start - start; auto delta = interval.start - start;
i.start = start; interval.start = start;
if (! i.is_open ()) if (! interval.is_open ())
i.end -= delta; interval.end -= delta;
} }
database.deleteInterval (tracked[tracked.size () - id]); database.deleteInterval (intervals.at (0));
validate (cli, rules, database, i); validate (cli, rules, database, interval);
database.addInterval (i, rules.getBoolean ("verbose")); database.addInterval (interval, verbose);
journal.endTransaction (); journal.endTransaction ();
if (rules.getBoolean ("verbose")) if (verbose)
std::cout << "Moved @" << id << " to " << i.start.toISOLocalExtended () << '\n'; {
std::cout << "Moved @" << id << " to " << interval.start.toISOLocalExtended () << '\n';
}
return 0; return 0;
} }