From 1e70f00c2923742b9895a49e50d18679a0bf1c43 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 27 Aug 2011 23:30:51 -0400 Subject: [PATCH] TDB2 - Properly handles cases where tasks are added, then existing data files are loaded. The 'add' list and the 'loaded' list needed to be merged. --- src/TDB2.cpp | 45 +++++++++++++++++ test/tdb2.t.cpp | 127 +++++++++--------------------------------------- 2 files changed, 69 insertions(+), 103 deletions(-) diff --git a/src/TDB2.cpp b/src/TDB2.cpp index a68bf46e9..7c2752181 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -65,8 +65,17 @@ const std::vector & TF2::get_tasks () // std::cout << "# TF2::get_tasks " << _file._data << "\n"; if (! _loaded_tasks) + { load_tasks (); + // Apply previously added tasks. + std::vector ::iterator i; + for (i = _added_tasks.begin (); i != _added_tasks.end (); ++i) + _tasks.push_back (*i); + +// std::cout << "# TF2::get_tasks added " << _added_tasks.size () << " tasks\n"; + } + return _tasks; } @@ -76,8 +85,17 @@ const std::vector & TF2::get_lines () // std::cout << "# TF2::get_lines " << _file._data << "\n"; if (! _loaded_lines) + { load_lines (); + // Apply previously added lines. + std::vector ::iterator i; + for (i = _added_lines.begin (); i != _added_lines.end (); ++i) + _lines.push_back (*i); + +// std::cout << "# TF2::get_lines added " << _added_lines.size () << " lines\n"; + } + return _lines; } @@ -232,8 +250,17 @@ void TF2::load_tasks () context.timer_load.start (); if (! _loaded_lines) + { load_lines (); + // Apply previously added lines. + std::vector ::iterator i; + for (i = _added_lines.begin (); i != _added_lines.end (); ++i) + _lines.push_back (*i); + +// std::cout << "# TF2::load_tasks added " << _added_lines.size () << " lines\n"; + } + int line_number = 0; try { @@ -308,8 +335,17 @@ void TF2::load_contents () std::string TF2::uuid (int id) { if (! _loaded_tasks) + { load_tasks (); + // Apply previously added tasks. + std::vector ::iterator i; + for (i = _added_tasks.begin (); i != _added_tasks.end (); ++i) + _tasks.push_back (*i); + +// std::cout << "# TF2::uuid added " << _added_tasks.size () << " tasks\n"; + } + std::map ::const_iterator i; if ((i = _I2U.find (id)) != _I2U.end ()) return i->second; @@ -321,8 +357,17 @@ std::string TF2::uuid (int id) int TF2::id (const std::string& uuid) { if (! _loaded_tasks) + { load_tasks (); + // Apply previously added tasks. + std::vector ::iterator i; + for (i = _added_tasks.begin (); i != _added_tasks.end (); ++i) + _tasks.push_back (*i); + +// std::cout << "# TF2::id added " << _added_tasks.size () << " tasks\n"; + } + std::map ::const_iterator i; if ((i = _U2I.find (uuid)) != _U2I.end ()) return i->second; diff --git a/test/tdb2.t.cpp b/test/tdb2.t.cpp index 2022b858c..20ec42f87 100644 --- a/test/tdb2.t.cpp +++ b/test/tdb2.t.cpp @@ -49,16 +49,16 @@ int main (int argc, char** argv) // Set the context to allow GC. context.config.set ("gc", "on"); + context.config.set ("debug", "on"); - TDB2 tdb2; - tdb2.set_location ("."); + context.tdb2.set_location ("."); // Try reading an empty database. - std::vector pending = tdb2.pending.get_tasks (); - std::vector completed = tdb2.completed.get_tasks (); - std::vector undo = tdb2.undo.get_lines (); - std::vector backlog = tdb2.backlog.get_lines (); - std::vector synch_key = tdb2.synch_key.get_lines (); + std::vector pending = context.tdb2.pending.get_tasks (); + std::vector completed = context.tdb2.completed.get_tasks (); + std::vector undo = context.tdb2.undo.get_lines (); + std::vector backlog = context.tdb2.backlog.get_tasks (); + std::vector synch_key = context.tdb2.synch_key.get_lines (); t.is ((int) pending.size (), 0, "TDB2 Read empty pending"); t.is ((int) completed.size (), 0, "TDB2 Read empty completed"); @@ -68,117 +68,38 @@ int main (int argc, char** argv) // Add a task. Task task ("[name:\"value\"]"); - tdb2.add (task); + context.tdb2.add (task); - pending = tdb2.pending.get_tasks (); - completed = tdb2.completed.get_tasks (); - undo = tdb2.undo.get_lines (); - backlog = tdb2.backlog.get_lines (); - synch_key = tdb2.synch_key.get_lines (); + pending = context.tdb2.pending.get_tasks (); + completed = context.tdb2.completed.get_tasks (); + undo = context.tdb2.undo.get_lines (); + backlog = context.tdb2.backlog.get_tasks (); + synch_key = context.tdb2.synch_key.get_lines (); 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) backlog.size (), 1, "TDB2 after add, 1 backlog line"); + t.is ((int) backlog.size (), 1, "TDB2 after add, 1 backlog task"); t.is ((int) synch_key.size (), 0, "TDB2 after add, 0 synch_key"); - task.set ("description", "This is a test"); - tdb2.modify (task); + context.tdb2.modify (task); - pending = tdb2.pending.get_tasks (); - completed = tdb2.completed.get_tasks (); - undo = tdb2.undo.get_lines (); - backlog = tdb2.backlog.get_lines (); - synch_key = tdb2.synch_key.get_lines (); + pending = context.tdb2.pending.get_tasks (); + completed = context.tdb2.completed.get_tasks (); + undo = context.tdb2.undo.get_lines (); + backlog = context.tdb2.backlog.get_tasks (); + synch_key = context.tdb2.synch_key.get_lines (); 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) backlog.size (), 2, "TDB2 after add, 2 backlog line"); + t.is ((int) backlog.size (), 2, "TDB2 after add, 2 backlog task"); t.is ((int) synch_key.size (), 0, "TDB2 after add, 0 synch_key"); -/* - TDB tdb; - tdb.location ("."); - tdb.lock (); - Task task ("[name:\"value\"]"); - tdb.add (task); // P0 C0 N1 M0 - tdb.unlock (); - - pending.clear (); - completed.clear (); - get (pending, completed); - - t.ok (pending.size () == 0, "TDB add -> no commit -> empty"); - t.ok (completed.size () == 0, "TDB add -> no commit -> empty"); - - // Add with commit. - tdb.lock (); - tdb.add (task); // P0 C0 N1 M0 - tdb.commit (); // P1 C0 N0 M0 - tdb.unlock (); - - get (pending, completed); - t.ok (pending.size () == 1, "TDB add -> commit -> saved"); - t.is (pending[0].get ("name"), "value", "TDB load name=value"); - t.is (pending[0].id, 1, "TDB load verification id=1"); - t.ok (completed.size () == 0, "TDB add -> commit -> saved"); - - // Update with commit. - pending.clear (); - completed.clear (); - - tdb.lock (); - tdb.load (all); - all[0].set ("name", "value2"); - tdb.update (all[0]); // P1 C0 N0 M1 - tdb.commit (); // P1 C0 N0 M0 - tdb.unlock (); - - pending.clear (); - completed.clear (); - get (pending, completed); - - t.ok (all.size () == 1, "TDB update -> commit -> saved"); - t.is (all[0].get ("name"), "value2", "TDB load name=value2"); - t.is (all[0].id, 1, "TDB load verification id=1"); - - // GC. - all.clear (); - - tdb.lock (); - tdb.loadPending (all); - all[0].setStatus (Task::completed); - tdb.update (all[0]); // P1 C0 N0 M1 - Task t2 ("[foo:\"bar\" status:\"pending\"]"); - tdb.add (t2); // P1 C0 N1 M1 - tdb.commit (); - tdb.unlock (); - - pending.clear (); - completed.clear (); - get (pending, completed); - - t.is (pending.size (), (size_t)2, "TDB before gc pending #2"); - t.is (pending[0].id, 1, "TDB before gc pending id 1"); - t.is (pending[0].getStatus (), Task::completed, "TDB before gc pending status completed"); - t.is (pending[1].id, 2, "TDB before gc pending id 2"); - t.is (pending[1].getStatus (), Task::pending, "TDB before gc pending status pending"); - t.is (completed.size (), (size_t)0, "TDB before gc completed 0"); - - tdb.gc (); // P1 C1 N0 M0 - - pending.clear (); - completed.clear (); - get (pending, completed); - - t.is (pending.size (), (size_t)1, "TDB after gc pending #1"); - t.is (pending[0].id, 1, "TDB after gc pending id 2"); - t.is (pending[0].getStatus (), Task::pending, "TDB after gc pending status pending"); - t.is (completed.size (), (size_t)1, "TDB after gc completed #1"); - t.is (completed[0].getStatus (), Task::completed, "TDB after gc completed status completed"); -*/ + // TODO commit + // TODO complete a task + // TODO gc } catch (std::string& error)