mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
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.
This commit is contained in:
parent
1b25c415b0
commit
1e70f00c29
2 changed files with 69 additions and 103 deletions
45
src/TDB2.cpp
45
src/TDB2.cpp
|
@ -65,8 +65,17 @@ const std::vector <Task>& TF2::get_tasks ()
|
||||||
// std::cout << "# TF2::get_tasks " << _file._data << "\n";
|
// std::cout << "# TF2::get_tasks " << _file._data << "\n";
|
||||||
|
|
||||||
if (! _loaded_tasks)
|
if (! _loaded_tasks)
|
||||||
|
{
|
||||||
load_tasks ();
|
load_tasks ();
|
||||||
|
|
||||||
|
// Apply previously added tasks.
|
||||||
|
std::vector <Task>::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;
|
return _tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,8 +85,17 @@ const std::vector <std::string>& TF2::get_lines ()
|
||||||
// std::cout << "# TF2::get_lines " << _file._data << "\n";
|
// std::cout << "# TF2::get_lines " << _file._data << "\n";
|
||||||
|
|
||||||
if (! _loaded_lines)
|
if (! _loaded_lines)
|
||||||
|
{
|
||||||
load_lines ();
|
load_lines ();
|
||||||
|
|
||||||
|
// Apply previously added lines.
|
||||||
|
std::vector <std::string>::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;
|
return _lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,8 +250,17 @@ void TF2::load_tasks ()
|
||||||
context.timer_load.start ();
|
context.timer_load.start ();
|
||||||
|
|
||||||
if (! _loaded_lines)
|
if (! _loaded_lines)
|
||||||
|
{
|
||||||
load_lines ();
|
load_lines ();
|
||||||
|
|
||||||
|
// Apply previously added lines.
|
||||||
|
std::vector <std::string>::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;
|
int line_number = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -308,8 +335,17 @@ void TF2::load_contents ()
|
||||||
std::string TF2::uuid (int id)
|
std::string TF2::uuid (int id)
|
||||||
{
|
{
|
||||||
if (! _loaded_tasks)
|
if (! _loaded_tasks)
|
||||||
|
{
|
||||||
load_tasks ();
|
load_tasks ();
|
||||||
|
|
||||||
|
// Apply previously added tasks.
|
||||||
|
std::vector <Task>::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 <int, std::string>::const_iterator i;
|
std::map <int, std::string>::const_iterator i;
|
||||||
if ((i = _I2U.find (id)) != _I2U.end ())
|
if ((i = _I2U.find (id)) != _I2U.end ())
|
||||||
return i->second;
|
return i->second;
|
||||||
|
@ -321,8 +357,17 @@ std::string TF2::uuid (int id)
|
||||||
int TF2::id (const std::string& uuid)
|
int TF2::id (const std::string& uuid)
|
||||||
{
|
{
|
||||||
if (! _loaded_tasks)
|
if (! _loaded_tasks)
|
||||||
|
{
|
||||||
load_tasks ();
|
load_tasks ();
|
||||||
|
|
||||||
|
// Apply previously added tasks.
|
||||||
|
std::vector <Task>::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 <std::string, int>::const_iterator i;
|
std::map <std::string, int>::const_iterator i;
|
||||||
if ((i = _U2I.find (uuid)) != _U2I.end ())
|
if ((i = _U2I.find (uuid)) != _U2I.end ())
|
||||||
return i->second;
|
return i->second;
|
||||||
|
|
127
test/tdb2.t.cpp
127
test/tdb2.t.cpp
|
@ -49,16 +49,16 @@ int main (int argc, char** argv)
|
||||||
|
|
||||||
// Set the context to allow GC.
|
// Set the context to allow GC.
|
||||||
context.config.set ("gc", "on");
|
context.config.set ("gc", "on");
|
||||||
|
context.config.set ("debug", "on");
|
||||||
|
|
||||||
TDB2 tdb2;
|
context.tdb2.set_location (".");
|
||||||
tdb2.set_location (".");
|
|
||||||
|
|
||||||
// Try reading an empty database.
|
// Try reading an empty database.
|
||||||
std::vector <Task> pending = tdb2.pending.get_tasks ();
|
std::vector <Task> pending = context.tdb2.pending.get_tasks ();
|
||||||
std::vector <Task> completed = tdb2.completed.get_tasks ();
|
std::vector <Task> completed = context.tdb2.completed.get_tasks ();
|
||||||
std::vector <std::string> undo = tdb2.undo.get_lines ();
|
std::vector <std::string> undo = context.tdb2.undo.get_lines ();
|
||||||
std::vector <std::string> backlog = tdb2.backlog.get_lines ();
|
std::vector <Task> backlog = context.tdb2.backlog.get_tasks ();
|
||||||
std::vector <std::string> synch_key = tdb2.synch_key.get_lines ();
|
std::vector <std::string> synch_key = context.tdb2.synch_key.get_lines ();
|
||||||
|
|
||||||
t.is ((int) pending.size (), 0, "TDB2 Read empty pending");
|
t.is ((int) pending.size (), 0, "TDB2 Read empty pending");
|
||||||
t.is ((int) completed.size (), 0, "TDB2 Read empty completed");
|
t.is ((int) completed.size (), 0, "TDB2 Read empty completed");
|
||||||
|
@ -68,117 +68,38 @@ int main (int argc, char** argv)
|
||||||
|
|
||||||
// Add a task.
|
// Add a task.
|
||||||
Task task ("[name:\"value\"]");
|
Task task ("[name:\"value\"]");
|
||||||
tdb2.add (task);
|
context.tdb2.add (task);
|
||||||
|
|
||||||
pending = tdb2.pending.get_tasks ();
|
pending = context.tdb2.pending.get_tasks ();
|
||||||
completed = tdb2.completed.get_tasks ();
|
completed = context.tdb2.completed.get_tasks ();
|
||||||
undo = tdb2.undo.get_lines ();
|
undo = context.tdb2.undo.get_lines ();
|
||||||
backlog = tdb2.backlog.get_lines ();
|
backlog = context.tdb2.backlog.get_tasks ();
|
||||||
synch_key = tdb2.synch_key.get_lines ();
|
synch_key = context.tdb2.synch_key.get_lines ();
|
||||||
|
|
||||||
t.is ((int) pending.size (), 1, "TDB2 after add, 1 pending 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) completed.size (), 0, "TDB2 after add, 0 completed tasks");
|
||||||
t.is ((int) undo.size (), 3, "TDB2 after add, 3 undo lines");
|
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");
|
t.is ((int) synch_key.size (), 0, "TDB2 after add, 0 synch_key");
|
||||||
|
|
||||||
|
|
||||||
task.set ("description", "This is a test");
|
task.set ("description", "This is a test");
|
||||||
tdb2.modify (task);
|
context.tdb2.modify (task);
|
||||||
|
|
||||||
pending = tdb2.pending.get_tasks ();
|
pending = context.tdb2.pending.get_tasks ();
|
||||||
completed = tdb2.completed.get_tasks ();
|
completed = context.tdb2.completed.get_tasks ();
|
||||||
undo = tdb2.undo.get_lines ();
|
undo = context.tdb2.undo.get_lines ();
|
||||||
backlog = tdb2.backlog.get_lines ();
|
backlog = context.tdb2.backlog.get_tasks ();
|
||||||
synch_key = tdb2.synch_key.get_lines ();
|
synch_key = context.tdb2.synch_key.get_lines ();
|
||||||
|
|
||||||
t.is ((int) pending.size (), 1, "TDB2 after add, 1 pending 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) completed.size (), 0, "TDB2 after add, 0 completed tasks");
|
||||||
t.is ((int) undo.size (), 7, "TDB2 after add, 7 undo lines");
|
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");
|
t.is ((int) synch_key.size (), 0, "TDB2 after add, 0 synch_key");
|
||||||
|
|
||||||
/*
|
// TODO commit
|
||||||
TDB tdb;
|
// TODO complete a task
|
||||||
tdb.location (".");
|
// TODO gc
|
||||||
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");
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (std::string& error)
|
catch (std::string& error)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue