From 1dc225715678c0f8f81db05bab0517391ee35102 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 23 Aug 2011 00:47:46 -0400 Subject: [PATCH] TDB2 Conversion - Migrated several commands over to use TDB2. Have to start somewhere. --- src/TDB2.cpp | 35 ++++++++++++++++++++++++++++++---- src/TDB2.h | 1 + src/commands/CmdCount.cpp | 10 +--------- src/commands/CmdCustom.cpp | 3 ++- src/commands/CmdIDs.cpp | 32 +++++++++---------------------- src/commands/CmdStatistics.cpp | 11 ++--------- src/commands/Command.cpp | 4 ++-- src/commands/Command.h | 2 +- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 024de921a..29404b5c3 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -25,7 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// -//#include // TODO Remove. +#include // TODO Remove. #include #include #include @@ -98,6 +98,13 @@ void TF2::add_task (const Task& task) _tasks.push_back (task); // For subsequent queries _added_tasks.push_back (task); // For commit/synch + +/* + int id = next_id (); + _I2U[id] = task.get ("uuid"); + _U2I[task.get ("uuid")] = id; +*/ + _dirty = true; } @@ -237,6 +244,25 @@ void TF2::commit () */ } +//////////////////////////////////////////////////////////////////////////////// +void TF2::commitUndo () +{ +/* + for each _added_task + fprintf (file, + "time %u\nnew %s---\n", + (unsigned int) time (NULL), + after.composeF4 ().c_str ()); + + for each _modified_task + fprintf (file, + "time %u\nold %snew %s---\n", + (unsigned int) time (NULL), + before.composeF4 ().c_str (), + after.composeF4 ().c_str ()); +*/ +} + //////////////////////////////////////////////////////////////////////////////// void TF2::load_tasks () { @@ -451,6 +477,7 @@ void TDB2::add (const Task& task) else pending.add_task (task); + undo.add_task (task); backlog.add_task (task); } @@ -477,7 +504,7 @@ void TDB2::commit () pending.commit (); completed.commit (); - undo.commit (); + undo.commitUndo (); backlog.commit (); synch_key.commit (); @@ -535,7 +562,7 @@ int TDB2::gc () int TDB2::next_id () { if (! pending._loaded_tasks) - pending.load_tasks (); + pending.load_tasks (); _id = pending._tasks.back ().id + 1; return _id++; @@ -544,7 +571,7 @@ int TDB2::next_id () //////////////////////////////////////////////////////////////////////////////// bool TDB2::verifyUniqueUUID (const std::string& uuid) { - if (pending.id (uuid) != 0 || + if (pending.id (uuid) != 0 || completed.id (uuid) != 0) return false; diff --git a/src/TDB2.h b/src/TDB2.h index 86dee9431..8b6c9cb37 100644 --- a/src/TDB2.h +++ b/src/TDB2.h @@ -54,6 +54,7 @@ public: void add_line (const std::string&); void clear_lines (); void commit (); + void commitUndo (); void load_tasks (); void load_lines (); diff --git a/src/commands/CmdCount.cpp b/src/commands/CmdCount.cpp index c84fc02df..fff39651c 100644 --- a/src/commands/CmdCount.cpp +++ b/src/commands/CmdCount.cpp @@ -48,17 +48,9 @@ CmdCount::CmdCount () //////////////////////////////////////////////////////////////////////////////// int CmdCount::execute (std::string& output) { - // Get all the tasks. - std::vector tasks; - context.tdb.lock (context.config.getBoolean ("locking")); - handleRecurrence (); - context.tdb.load (tasks); - context.tdb.commit (); - context.tdb.unlock (); - // Apply filter. std::vector filtered; - filter (tasks, filtered); + filter (filtered); // Find number of matching tasks. Skip recurring parent tasks. int count = 0; diff --git a/src/commands/CmdCustom.cpp b/src/commands/CmdCustom.cpp index 134baae74..1218ae374 100644 --- a/src/commands/CmdCustom.cpp +++ b/src/commands/CmdCustom.cpp @@ -93,7 +93,6 @@ int CmdCustom::execute (std::string& output) // Load the data. handleRecurrence (); - context.tdb2.commit (); std::vector filtered; filter (filtered); @@ -177,6 +176,8 @@ int CmdCustom::execute (std::string& output) rc = 1; } + context.tdb2.commit (); + output = out.str (); return rc; } diff --git a/src/commands/CmdIDs.cpp b/src/commands/CmdIDs.cpp index 6d93fdf85..02d1edba5 100644 --- a/src/commands/CmdIDs.cpp +++ b/src/commands/CmdIDs.cpp @@ -50,17 +50,11 @@ CmdIDs::CmdIDs () //////////////////////////////////////////////////////////////////////////////// int CmdIDs::execute (std::string& output) { - // Scan the pending tasks, applying any filter. - std::vector tasks; - context.tdb.lock (context.config.getBoolean ("locking")); - handleRecurrence (); - context.tdb.load (tasks); - context.tdb.commit (); - context.tdb.unlock (); - // Apply filter. + handleRecurrence (); std::vector filtered; - filter (tasks, filtered); + filter (filtered); + context.tdb.commit (); // Find number of matching tasks. std::vector ids; @@ -87,15 +81,11 @@ CmdCompletionIds::CmdCompletionIds () //////////////////////////////////////////////////////////////////////////////// int CmdCompletionIds::execute (std::string& output) { - std::vector tasks; - context.tdb.lock (context.config.getBoolean ("locking")); - context.tdb.loadPending (tasks); - context.tdb.commit (); - context.tdb.unlock (); - // Apply filter. + handleRecurrence (); std::vector filtered; - filter (tasks, filtered); + filter (filtered); + context.tdb.commit (); std::vector ids; std::vector ::iterator task; @@ -128,15 +118,11 @@ CmdZshCompletionIds::CmdZshCompletionIds () //////////////////////////////////////////////////////////////////////////////// int CmdZshCompletionIds::execute (std::string& output) { - std::vector tasks; - context.tdb.lock (context.config.getBoolean ("locking")); - context.tdb.loadPending (tasks); - context.tdb.commit (); - context.tdb.unlock (); - // Apply filter. + handleRecurrence (); std::vector filtered; - filter (tasks, filtered); + filter (filtered); + context.tdb.commit (); std::stringstream out; std::vector ::iterator task; diff --git a/src/commands/CmdStatistics.cpp b/src/commands/CmdStatistics.cpp index b994455f3..04d96d45e 100644 --- a/src/commands/CmdStatistics.cpp +++ b/src/commands/CmdStatistics.cpp @@ -81,16 +81,9 @@ int CmdStatistics::execute (std::string& output) ++undoCount; // Get all the tasks. - std::vector tasks; - context.tdb.lock (context.config.getBoolean ("locking")); - handleRecurrence (); - context.tdb.load (tasks); - context.tdb.commit (); - context.tdb.unlock (); - - // Apply filter. std::vector filtered; - filter (tasks, filtered); + filter (context.tdb2.pending.get_tasks (), filtered); + filter (context.tdb2.completed.get_tasks (), filtered); Date now; time_t earliest = time (NULL); diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index db4d4e19a..e0704f4d5 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -267,7 +267,7 @@ bool Command::displays_id () const //////////////////////////////////////////////////////////////////////////////// // Filter a specific list of tasks. -void Command::filter (std::vector & input, std::vector & output) +void Command::filter (const std::vector & input, std::vector & output) { context.timer_filter.start (); @@ -278,7 +278,7 @@ void Command::filter (std::vector & input, std::vector & output) { E9 e (filt); - std::vector ::iterator task; + std::vector ::const_iterator task; for (task = input.begin (); task != input.end (); ++task) if (e.evalFilter (*task)) output.push_back (*task); diff --git a/src/commands/Command.h b/src/commands/Command.h index 309c0ea4e..ec8836f69 100644 --- a/src/commands/Command.h +++ b/src/commands/Command.h @@ -53,7 +53,7 @@ public: virtual int execute (std::string&) = 0; protected: - void filter (std::vector &, std::vector &); + void filter (const std::vector &, std::vector &); void filter (std::vector &); bool filter_shortcut (const A3&);