@@ -22,9 +22,15 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
2222 sqlite::Statement (*db_, " CREATE TABLE IF NOT EXISTS Transitions ("
2323 " timestamp INTEGER PRIMARY KEY NOT NULL, "
2424 " session_id INTEGER NOT NULL, "
25- " uid INTEGER NOT NULL, "
25+ " node_uid INTEGER NOT NULL, "
2626 " duration INTEGER, "
27- " state INTEGER NOT NULL);" );
27+ " state INTEGER NOT NULL,"
28+ " extra_data VARCHAR );" );
29+
30+ sqlite::Statement (*db_, " CREATE TABLE IF NOT EXISTS Nodes ("
31+ " session_id INTEGER NOT NULL, "
32+ " fullpath VARCHAR, "
33+ " node_uid INTEGER NOT NULL );" );
2834
2935 sqlite::Statement (*db_, " CREATE TABLE IF NOT EXISTS Definitions ("
3036 " session_id INTEGER PRIMARY KEY AUTOINCREMENT, "
@@ -35,6 +41,7 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
3541 {
3642 sqlite::Statement (*db_, " DELETE from Transitions;" );
3743 sqlite::Statement (*db_, " DELETE from Definitions;" );
44+ sqlite::Statement (*db_, " DELETE from Nodes;" );
3845 }
3946
4047 auto tree_xml = WriteTreeToXML (tree, true , true );
@@ -43,12 +50,23 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
4350 " VALUES (datetime('now','localtime'),?);" ,
4451 tree_xml);
4552
46- auto res = sqlite::Query (*db_, " SELECT MAX(session_id) FROM Definitions LIMIT 1;" );
53+ auto res = sqlite::Query (*db_, " SELECT MAX(session_id) "
54+ " FROM Definitions LIMIT 1;" );
4755
4856 while (res.Next ())
4957 {
5058 session_id_ = res.Get (0 );
5159 }
60+
61+ for (const auto & subtree : tree.subtrees )
62+ {
63+ for (const auto & node : subtree->nodes )
64+ {
65+ sqlite::Statement (*db_, " INSERT INTO Nodes VALUES (?, ?, ?)" , session_id_,
66+ node->fullPath (), node->UID ());
67+ }
68+ }
69+
5270 writer_thread_ = std::thread (&SqliteLogger::writerLoop, this );
5371}
5472
@@ -61,6 +79,11 @@ SqliteLogger::~SqliteLogger()
6179 sqlite::Statement (*db_, " PRAGMA optimize;" );
6280}
6381
82+ void SqliteLogger::setAdditionalCallback (ExtraCallback func)
83+ {
84+ extra_func_ = func;
85+ }
86+
6487void SqliteLogger::callback (Duration timestamp, const TreeNode& node,
6588 NodeStatus prev_status, NodeStatus status)
6689{
@@ -91,11 +114,26 @@ void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
91114 trans.node_uid = node.UID ();
92115 trans.status = status;
93116
117+ if (extra_func_)
118+ {
119+ trans.extra_data = extra_func_ (timestamp, node, prev_status, status);
120+ }
121+
94122 {
95123 std::scoped_lock lk (queue_mutex_);
96124 transitions_queue_.push_back (trans);
97125 }
98126 queue_cv_.notify_one ();
127+
128+ if (extra_func_)
129+ {
130+ extra_func_ (timestamp, node, prev_status, status);
131+ }
132+ }
133+
134+ void SqliteLogger::execSqlStatement (std::string statement)
135+ {
136+ sqlite::Statement (*db_, statement);
99137}
100138
101139void SqliteLogger::writerLoop ()
@@ -116,9 +154,9 @@ void SqliteLogger::writerLoop()
116154 auto const trans = transitions.front ();
117155 transitions.pop_front ();
118156
119- sqlite::Statement (*db_, " INSERT INTO Transitions VALUES (?, ?, ?, ?, ?)" ,
157+ sqlite::Statement (*db_, " INSERT INTO Transitions VALUES (?, ?, ?, ?, ?, ? )" ,
120158 trans.timestamp , session_id_, trans.node_uid , trans.duration ,
121- static_cast <int >(trans.status ));
159+ static_cast <int >(trans.status ), trans. extra_data );
122160 }
123161 }
124162}
0 commit comments