diff --git a/src/Context.cpp b/src/Context.cpp index 99b21751c..aa548f558 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -143,6 +143,7 @@ int Context::initialize (int argc, const char** argv) tdb.clear (); // TODO Obsolete tdb.location (data_dir); // TODO Obsolete tdb2.set_location (data_dir); + tdb2.dump (); // Hook system init, plus post-start event occurring at the first possible // moment after hook initialization. diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 1824b8e66..0d42e1a0f 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -25,13 +25,16 @@ // //////////////////////////////////////////////////////////////////////////////// +#include #include #include +extern Context context; //////////////////////////////////////////////////////////////////////////////// TF2::TF2 () -: _dirty (false) +: _read_only (false) +, _dirty (false) , _loaded_tasks (false) , _loaded_lines (false) , _loaded_contents (false) @@ -48,10 +51,11 @@ TF2::~TF2 () void TF2::target (const std::string& f) { _file = File (f); + _read_only = ! _file.writable (); } //////////////////////////////////////////////////////////////////////////////// -std::vector & TF2::get_tasks () +const std::vector & TF2::get_tasks () { if (! _loaded_tasks) load_tasks (); @@ -60,7 +64,7 @@ std::vector & TF2::get_tasks () } //////////////////////////////////////////////////////////////////////////////// -std::vector & TF2::get_lines () +const std::vector & TF2::get_lines () { if (! _loaded_lines) load_lines (); @@ -69,7 +73,7 @@ std::vector & TF2::get_lines () } //////////////////////////////////////////////////////////////////////////////// -std::string& TF2::get_contents () +const std::string& TF2::get_contents () { if (! _loaded_contents) load_contents (); @@ -155,54 +159,44 @@ void TF2::commit () //////////////////////////////////////////////////////////////////////////////// void TF2::load_tasks () { - if (! _loaded_tasks) - { - if (! _loaded_lines) - load_lines (); + if (! _loaded_lines) + load_lines (); - std::vector ::iterator i; - for (i = _lines.begin (); i != _lines.end (); ++i) - _tasks.push_back (Task (*i)); + std::vector ::iterator i; + for (i = _lines.begin (); i != _lines.end (); ++i) + _tasks.push_back (Task (*i)); - _loaded_tasks = true; - } + _loaded_tasks = true; } //////////////////////////////////////////////////////////////////////////////// void TF2::load_lines () { - if (! _loaded_lines) - { - if (! _loaded_contents) - load_contents (); + if (! _loaded_contents) + load_contents (); - split (_lines, _contents, '\n'); - _loaded_lines = true; - } + split (_lines, _contents, '\n'); + _loaded_lines = true; } //////////////////////////////////////////////////////////////////////////////// void TF2::load_contents () { - if (! _loaded_contents) - { - _contents = ""; + _contents = ""; - if (_file.open ()) + if (_file.open ()) + { + if (_file.lock ()) { - if (_file.lock ()) - { - _file.read (_contents); - _loaded_contents = true; - } - // TODO Error handling? + _file.read (_contents); + _loaded_contents = true; } // TODO Error handling? } + // TODO Error handling? } - - +//////////////////////////////////////////////////////////////////////////////// @@ -269,10 +263,79 @@ void TDB2::commit () //////////////////////////////////////////////////////////////////////////////// int TDB2::gc () { +/* + pending.load_tasks + completed.load_tasks + for each pending + if status == completed || status == deleted + pending.remove + completed.add + if status == waiting && wait < now + status = pending + wait.clear + + for each completed + if status == pending || status == waiting + completed.remove + pending.add +*/ return 0; } +//////////////////////////////////////////////////////////////////////////////// +// +// File RW State Tasks + - ~ lines + - Bytes +// -------------- -- ----- ----- - - - ----- - - ----- +// pending.data rw clean 123t +2t -1t ~1t +// completed.data rw clean 123t +2t ~1t +// undo.data rw clean 123t +2t ~1t +// backlog.data rw clean 123t +2t ~1t +// synch-key.data rw clean 123b +// +void TDB2::dump () +{ + if (context.config.getBoolean ("debug")) + { + ViewText view; + view.width (context.getWidth ()); + view.add (Column::factory ("string", "File")); + view.add (Column::factory ("string.right", "RW")); + view.add (Column::factory ("string.right", "State")); + view.add (Column::factory ("string.right", "Tasks")); + view.add (Column::factory ("string.right", "+")); + view.add (Column::factory ("string.right", "-")); + view.add (Column::factory ("string.right", "~")); + view.add (Column::factory ("string.right", "Lines")); + view.add (Column::factory ("string.right", "+")); + view.add (Column::factory ("string.right", "Bytes")); + + dump_file (view, "pending.data", pending); + dump_file (view, "completed.data", completed); + dump_file (view, "undo.data", undo); + dump_file (view, "backlog.data", backlog); + dump_file (view, "synch_key.data", synch_key); + context.debug (view.render ()); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void TDB2::dump_file (ViewText& view, const std::string& label, TF2& tf) +{ + int row = view.addRow (); + view.set (row, 0, label); + view.set (row, 1, std::string (tf._file.readable () ? "r" : "-") + + std::string (tf._file.writable () ? "w" : "-")); + view.set (row, 2, tf._dirty ? "dirty" : "clean"); + view.set (row, 3, tf._loaded_tasks ? (format ((int)tf._tasks.size ())) : "-"); + view.set (row, 4, (int)tf._added_tasks.size ()); + view.set (row, 5, (int)tf._removed_tasks.size ()); + view.set (row, 6, (int)tf._modified_tasks.size ()); + view.set (row, 7, tf._loaded_lines ? (format ((int)tf._lines.size ())) : "-"); + view.set (row, 8, (int)tf._added_lines.size ()); + view.set (row, 9, tf._loaded_contents ? (format ((int)tf._contents.size ())) : "-"); +} + //////////////////////////////////////////////////////////////////////////////// #if 0 diff --git a/src/TDB2.h b/src/TDB2.h index 1e0126b7e..edb5c89bb 100644 --- a/src/TDB2.h +++ b/src/TDB2.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -44,9 +45,9 @@ public: void target (const std::string&); - std::vector & get_tasks (); - std::vector & get_lines (); - std::string& get_contents (); + const std::vector & get_tasks (); + const std::vector & get_lines (); + const std::string& get_contents (); void add_task (const Task&); void modify_task (const Task&); @@ -54,18 +55,20 @@ public: void clear_lines (); void commit (); -private: +public: void load_tasks (); void load_lines (); void load_contents (); -private: +public: + bool _read_only; bool _dirty; bool _loaded_tasks; bool _loaded_lines; bool _loaded_contents; std::vector _tasks; std::vector _added_tasks; + std::vector _removed_tasks; std::vector _modified_tasks; std::vector _lines; std::vector _added_lines; @@ -73,7 +76,6 @@ private: File _file; }; - // TDB2 Class represents all the files in the task database. class TDB2 { @@ -87,6 +89,9 @@ public: void commit (); int gc (); + void dump (); + void dump_file (ViewText&, const std::string&, TF2&); + public: TF2 pending; TF2 completed;