@@ -22,9 +22,15 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
22
22
sqlite::Statement (*db_, " CREATE TABLE IF NOT EXISTS Transitions ("
23
23
" timestamp INTEGER PRIMARY KEY NOT NULL, "
24
24
" session_id INTEGER NOT NULL, "
25
- " uid INTEGER NOT NULL, "
25
+ " node_uid INTEGER NOT NULL, "
26
26
" 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 );" );
28
34
29
35
sqlite::Statement (*db_, " CREATE TABLE IF NOT EXISTS Definitions ("
30
36
" session_id INTEGER PRIMARY KEY AUTOINCREMENT, "
@@ -35,6 +41,7 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
35
41
{
36
42
sqlite::Statement (*db_, " DELETE from Transitions;" );
37
43
sqlite::Statement (*db_, " DELETE from Definitions;" );
44
+ sqlite::Statement (*db_, " DELETE from Nodes;" );
38
45
}
39
46
40
47
auto tree_xml = WriteTreeToXML (tree, true , true );
@@ -43,12 +50,23 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
43
50
" VALUES (datetime('now','localtime'),?);" ,
44
51
tree_xml);
45
52
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;" );
47
55
48
56
while (res.Next ())
49
57
{
50
58
session_id_ = res.Get (0 );
51
59
}
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
+
52
70
writer_thread_ = std::thread (&SqliteLogger::writerLoop, this );
53
71
}
54
72
@@ -61,6 +79,11 @@ SqliteLogger::~SqliteLogger()
61
79
sqlite::Statement (*db_, " PRAGMA optimize;" );
62
80
}
63
81
82
+ void SqliteLogger::setAdditionalCallback (ExtraCallback func)
83
+ {
84
+ extra_func_ = func;
85
+ }
86
+
64
87
void SqliteLogger::callback (Duration timestamp, const TreeNode& node,
65
88
NodeStatus prev_status, NodeStatus status)
66
89
{
@@ -91,11 +114,26 @@ void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
91
114
trans.node_uid = node.UID ();
92
115
trans.status = status;
93
116
117
+ if (extra_func_)
118
+ {
119
+ trans.extra_data = extra_func_ (timestamp, node, prev_status, status);
120
+ }
121
+
94
122
{
95
123
std::scoped_lock lk (queue_mutex_);
96
124
transitions_queue_.push_back (trans);
97
125
}
98
126
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);
99
137
}
100
138
101
139
void SqliteLogger::writerLoop ()
@@ -116,9 +154,9 @@ void SqliteLogger::writerLoop()
116
154
auto const trans = transitions.front ();
117
155
transitions.pop_front ();
118
156
119
- sqlite::Statement (*db_, " INSERT INTO Transitions VALUES (?, ?, ?, ?, ?)" ,
157
+ sqlite::Statement (*db_, " INSERT INTO Transitions VALUES (?, ?, ?, ?, ?, ? )" ,
120
158
trans.timestamp , session_id_, trans.node_uid , trans.duration ,
121
- static_cast <int >(trans.status ));
159
+ static_cast <int >(trans.status ), trans. extra_data );
122
160
}
123
161
}
124
162
}
0 commit comments