From 4ebd0ffb39972a81037e0bbe365d887e3c911303 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Fri, 1 Apr 2022 22:15:55 +0000 Subject: [PATCH] make TDB2.undo non-public The stats command calls an API to provide this information in a way that will still be relevant for TaskChampion, while CmdInfo's access to the data remains. The TaskChampion interface for per-task hitsory is still not ready. --- src/TDB2.cpp | 7 +++++++ src/TDB2.h | 4 +++- src/commands/CmdStats.cpp | 5 ++--- test/tdb2.t.cpp | 42 +++++++++++++++++++-------------------- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 026b41441..5f7378d57 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -1266,6 +1266,13 @@ int TDB2::num_local_changes () return std::count_if(lines.begin(), lines.end(), [](const auto& line){ return line.front() == '{'; }); } +//////////////////////////////////////////////////////////////////////////////// +int TDB2::num_reverts_possible () +{ + std::vector lines = undo.get_lines (); + return std::count(lines.begin(), lines.end(), "---"); +} + //////////////////////////////////////////////////////////////////////////////// size_t TDB2::data_size () { diff --git a/src/TDB2.h b/src/TDB2.h index f8fd94d99..58ba6866a 100644 --- a/src/TDB2.h +++ b/src/TDB2.h @@ -136,6 +136,7 @@ public: bool read_only (); int num_local_changes (); + int num_reverts_possible (); size_t data_size (); void clear (); @@ -154,11 +155,12 @@ private: public: TF2 pending; TF2 completed; - TF2 undo; protected: friend class CmdSync; // CmdSync accesses the backlog directly TF2 backlog; + friend class CmdInfo; // CmdInfo uses undo data to give history + TF2 undo; private: std::string _location; diff --git a/src/commands/CmdStats.cpp b/src/commands/CmdStats.cpp index f8d890fb5..393c7d02a 100644 --- a/src/commands/CmdStats.cpp +++ b/src/commands/CmdStats.cpp @@ -65,9 +65,8 @@ int CmdStats::execute (std::string& output) // Go get the file sizes. size_t dataSize = Context::getContext ().tdb2.data_size (); - // Count the undo transactions. - std::vector undoTxns = Context::getContext ().tdb2.undo.get_lines (); - int undoCount = std::count(undoTxns.begin(), undoTxns.end(), "---"); + // Count the possible reverts. + int undoCount = Context::getContext ().tdb2.num_reverts_possible (); // Count the backlog transactions. int numLocalChanges = Context::getContext ().tdb2.num_local_changes (); diff --git a/test/tdb2.t.cpp b/test/tdb2.t.cpp index b20319a17..4e0b83e7f 100644 --- a/test/tdb2.t.cpp +++ b/test/tdb2.t.cpp @@ -60,40 +60,40 @@ int main (int, char**) // Try reading an empty database. std::vector pending = context.tdb2.pending.get_tasks (); std::vector completed = context.tdb2.completed.get_tasks (); - std::vector undo = context.tdb2.undo.get_lines (); + int num_reverts_possible = context.tdb2.num_reverts_possible (); int num_local_changes = context.tdb2.num_local_changes (); - t.is ((int) pending.size (), 0, "TDB2 Read empty pending"); - t.is ((int) completed.size (), 0, "TDB2 Read empty completed"); - t.is ((int) undo.size (), 0, "TDB2 Read empty undo"); - t.is ((int) num_local_changes, 0, "TDB2 Read empty backlog"); + t.is ((int) pending.size (), 0, "TDB2 Read empty pending"); + t.is ((int) completed.size (), 0, "TDB2 Read empty completed"); + t.is ((int) num_reverts_possible, 0, "TDB2 Read empty undo"); + t.is ((int) num_local_changes, 0, "TDB2 Read empty backlog"); // Add a task. Task task (R"([description:"description" name:"value"])"); context.tdb2.add (task); - pending = context.tdb2.pending.get_tasks (); - completed = context.tdb2.completed.get_tasks (); - undo = context.tdb2.undo.get_lines (); - num_local_changes = context.tdb2.num_local_changes (); + pending = context.tdb2.pending.get_tasks (); + completed = context.tdb2.completed.get_tasks (); + num_reverts_possible = context.tdb2.num_reverts_possible (); + num_local_changes = context.tdb2.num_local_changes (); - t.is ((int) pending.size (), 1, "TDB2 after add, 1 pending task"); - t.is ((int) completed.size (), 0, "TDB2 after add, 0 completed tasks"); - t.is ((int) undo.size (), 3, "TDB2 after add, 3 undo lines"); - t.is ((int) num_local_changes, 1, "TDB2 after add, 1 backlog task"); + t.is ((int) pending.size (), 1, "TDB2 after add, 1 pending task"); + t.is ((int) completed.size (), 0, "TDB2 after add, 0 completed tasks"); + t.is ((int) num_reverts_possible, 3, "TDB2 after add, 3 undo lines"); + t.is ((int) num_local_changes, 1, "TDB2 after add, 1 backlog task"); task.set ("description", "This is a test"); context.tdb2.modify (task); - pending = context.tdb2.pending.get_tasks (); - completed = context.tdb2.completed.get_tasks (); - undo = context.tdb2.undo.get_lines (); - num_local_changes = context.tdb2.num_local_changes (); + pending = context.tdb2.pending.get_tasks (); + completed = context.tdb2.completed.get_tasks (); + num_reverts_possible = context.tdb2.num_reverts_possible (); + num_local_changes = context.tdb2.num_local_changes (); - t.is ((int) pending.size (), 1, "TDB2 after add, 1 pending task"); - t.is ((int) completed.size (), 0, "TDB2 after add, 0 completed tasks"); - t.is ((int) undo.size (), 7, "TDB2 after add, 7 undo lines"); - t.is ((int) num_local_changes, 2, "TDB2 after add, 2 backlog task"); + t.is ((int) pending.size (), 1, "TDB2 after add, 1 pending task"); + t.is ((int) completed.size (), 0, "TDB2 after add, 0 completed tasks"); + t.is ((int) num_reverts_possible, 7, "TDB2 after add, 7 undo lines"); + t.is ((int) num_local_changes, 2, "TDB2 after add, 2 backlog task"); context.tdb2.commit ();