blob: a160ae87a6efd88777146d59bccb0af0616493eb [file] [log] [blame]
Alexander Afanasyeveb575e02013-01-26 17:14:51 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "dispatcher.h"
22#include "fs-watcher.h"
23#include "logging.h"
24#include "ccnx-wrapper.h"
25
26#include <boost/make_shared.hpp>
27#include <boost/lexical_cast.hpp>
28
29using namespace boost;
30using namespace std;
31using namespace Ccnx;
32
Alexander Afanasyev24b449f2013-02-06 13:27:59 -080033INIT_LOGGER ("DumpDb");
34
35class StateLogDumper : public DbHelper
36{
37public:
38 StateLogDumper (const filesystem::path &path)
39 : DbHelper (path / ".chronoshare", "sync-log.db")
40 {
41 }
42
43 void
44 DumpState ()
45 {
46 sqlite3_stmt *stmt;
47 sqlite3_prepare_v2 (m_db, "SELECT hash(device_name, seq_no) FROM (SELECT * FROM SyncNodes ORDER BY device_name)", -1, &stmt, 0);
48 sqlite3_step (stmt);
49 Hash hash (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0));
50 sqlite3_finalize (stmt);
51
52 sqlite3_prepare_v2 (m_db,
53 "SELECT device_name, seq_no, last_known_locator, last_update "
54 " FROM SyncNodes "
55 " ORDER BY device_name", -1, &stmt, 0);
56
57 cout.setf(std::ios::left, std::ios::adjustfield);
58 cout << ">> SYNC NODES (" << hash.shortHash () << ") <<" << endl;
59 cout << "====================================================================================" << endl;
60 cout << setw(30) << "device_name" << " | seq_no | " << setw(20) << "locator" << " | last_update " << endl;
61 cout << "====================================================================================" << endl;
62
63 while (sqlite3_step (stmt) == SQLITE_ROW)
64 {
65 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0))) << " | "; // device_name
66 cout << setw (6) << sqlite3_column_int64 (stmt, 1) << " | "; // seq_no
67 cout << setw (20) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 2), sqlite3_column_bytes (stmt, 2))) << " | "; // locator
68 if (sqlite3_column_bytes (stmt, 3) > 0)
69 {
70 cout << setw (10) << sqlite3_column_text (stmt, 3) << endl;
71 }
72 else
73 {
74 cout << "unknown" << endl;
75 }
76 }
77 sqlite3_finalize (stmt);
78 }
79
80 void
81 DumpLog ()
82 {
83 sqlite3_stmt *stmt;
84 sqlite3_prepare_v2 (m_db,
85 "SELECT state_hash, last_update, state_id "
86 " FROM SyncLog "
87 " ORDER BY last_update", -1, &stmt, 0);
88
89 cout.setf(std::ios::left, std::ios::adjustfield);
90 cout << ">> SYNC LOG <<" << endl;
91 cout << "====================================================================================" << endl;
92 cout << setw(10) << "state_hash" << " | state details " << endl;
93 cout << "====================================================================================" << endl;
94
95 while (sqlite3_step (stmt) == SQLITE_ROW)
96 {
97 cout << setw (10) << Hash (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)).shortHash () << " | "; // state hash
98
99 sqlite3_stmt *stmt2;
100 sqlite3_prepare_v2 (m_db,
101 "SELECT device_name, ss.seq_no "
102 " FROM SyncStateNodes ss JOIN SyncNodes sn ON ss.device_id = sn.device_id "
103 " WHERE state_id=? "
104 " ORDER BY device_name", -1, &stmt2, 0);
105 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
106 sqlite3_bind_int64 (stmt2, 1, sqlite3_column_int64 (stmt, 2));
107
108 while (sqlite3_step (stmt2) == SQLITE_ROW)
109 {
110 cout << lexical_cast<string> (Name (sqlite3_column_blob (stmt2, 0), sqlite3_column_bytes (stmt2, 0)))
111 << "("
112 << sqlite3_column_int64 (stmt2, 1)
113 << "); ";
114 }
115
116 sqlite3_finalize (stmt2);
117
118 cout << endl;
119 }
120 sqlite3_finalize (stmt);
121 }
122};
123
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800124class ActionLogDumper : public DbHelper
125{
126public:
127 ActionLogDumper (const filesystem::path &path)
128 : DbHelper (path / ".chronoshare", "action-log.db")
129 {
130 }
131
132 void
Alexander Afanasyev24b449f2013-02-06 13:27:59 -0800133 Dump ()
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800134 {
135 sqlite3_stmt *stmt;
136 sqlite3_prepare_v2 (m_db,
137 "SELECT device_name, seq_no, action, filename, version, file_hash, file_seg_num, parent_device_name, parent_seq_no "
138 " FROM ActionLog "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800139 " ORDER BY action_timestamp", -1, &stmt, 0);
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800140
141 cout.setf(std::ios::left, std::ios::adjustfield);
142 cout << ">> ACTION LOG <<" << endl;
143 cout << "=============================================================================================================================================================================" << endl;
144 cout << setw(30) << "device_name" << " | seq_no | action |" << setw(40) << " filename " << " | version | file_hash | seg_num | parent_device_name | parent_seq_no" << endl;
145 cout << "=============================================================================================================================================================================" << endl;
146
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800147 while (sqlite3_step (stmt) == SQLITE_ROW)
148 {
149 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0))) << " | "; // device_name
150 cout << setw (6) << sqlite3_column_int64 (stmt, 1) << " | "; // seq_no
151 cout << setw (6) << (sqlite3_column_int (stmt, 2)==0?"UPDATE":"DELETE") << " | "; // action
152 cout << setw (40) << sqlite3_column_text (stmt, 3) << " | "; // filename
153 cout << setw (7) << sqlite3_column_int64 (stmt, 4) << " | "; // version
154
155 if (sqlite3_column_int (stmt, 2) == 0)
156 {
157 cout << setw (10) << Hash (sqlite3_column_blob (stmt, 5), sqlite3_column_bytes (stmt, 5)).shortHash () << " | ";
158 cout << setw (7) << sqlite3_column_int64 (stmt, 6) << " | "; // seg_num
159 }
160 else
161 cout << " | | ";
162
163 if (sqlite3_column_bytes (stmt, 7) > 0)
164 {
165 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 7), sqlite3_column_bytes (stmt, 7))) << " | "; // parent_device_name
166 cout << setw (5) << sqlite3_column_int64 (stmt, 8); // seq_no
167 }
168 else
169 cout << setw (30) << " " << " | ";
170 cout << endl;
171 }
172
173 sqlite3_finalize (stmt);
174 }
Zhenkai Zhued457c32013-02-13 15:51:48 -0800175
176 void
177 DumpActionData(const Ccnx::Name &deviceName, int64_t seqno)
178 {
179 sqlite3_stmt *stmt;
180 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name = ? and seq_no = ?", -1, &stmt, 0);
181 Ccnx::CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf();
182 sqlite3_bind_blob (stmt, 1, device_name->buf(), device_name->length(), SQLITE_STATIC);
183 sqlite3_bind_int64 (stmt, 2, seqno);
184 cout << "Dumping action data for: [" << deviceName << ", " << seqno << "]" <<endl;
185 if (sqlite3_step(stmt) == SQLITE_ROW)
186 {
187 PcoPtr pco = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
188 if (pco)
189 {
190 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content());
191 if (action)
192 {
193 string type = action->action() == ActionItem::UPDATE ? "UPDATE" : "DELETE";
194 cout << "Action Type = " << type << endl;
195 cout << "Timestamp = " << action->timestamp() << endl;
196 string filename = action->filename();
197 cout << "Filename = " << filename << endl;
198 if (action->has_seg_num())
199 {
200 cout << "Segment number = " << action->seg_num() << endl;
201 }
202 if (action->has_file_hash())
203 {
204 cout << "File hash = " << Hash(action->file_hash().c_str(), action->file_hash().size()) << endl;
205 }
206 }
207 else
208 {
209 cerr << "Error! Failed to parse action from pco! " << endl;
210 }
211 }
212 else
213 {
214 cerr << "Error! Invalid pco! " << endl;
215 }
216 }
217 else
218 {
219 cerr << "Error! Can not find the requested action" << endl;
220 }
221 sqlite3_finalize(stmt);
222 }
Alexander Afanasyev24b449f2013-02-06 13:27:59 -0800223};
224
225class FileStateDumper : public DbHelper
226{
227public:
228 FileStateDumper (const filesystem::path &path)
229 : DbHelper (path / ".chronoshare", "file-state.db")
230 {
231 }
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800232
233 void
Alexander Afanasyev24b449f2013-02-06 13:27:59 -0800234 Dump ()
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800235 {
236 sqlite3_stmt *stmt;
237 sqlite3_prepare_v2 (m_db,
Alexander Afanasyev38826ce2013-01-31 16:31:42 -0800238 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,directory,is_complete "
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800239 " FROM FileState "
240 " WHERE type = 0 ORDER BY filename", -1, &stmt, 0);
241
242 cout.setf(std::ios::left, std::ios::adjustfield);
243 cout << ">> FILE STATE <<" << endl;
Alexander Afanasyev38826ce2013-01-31 16:31:42 -0800244 cout << "===================================================================================================================================" << endl;
245 cout << "filename | device_name | seq_no | file_hash | seg_num | directory | is_complete" << endl;
246 cout << "===================================================================================================================================" << endl;
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800247
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800248 while (sqlite3_step (stmt) == SQLITE_ROW)
249 {
250 cout << setw (40) << sqlite3_column_text (stmt, 0) << " | ";
251 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1))) << " | ";
252 cout << setw (6) << sqlite3_column_int64 (stmt, 2) << " | ";
253 cout << setw (10) << Hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3)).shortHash () << " | ";
Alexander Afanasyev38826ce2013-01-31 16:31:42 -0800254 cout << setw (6) << sqlite3_column_int64 (stmt, 6) << " | ";
255 if (sqlite3_column_bytes (stmt, 7) == 0)
256 cout << setw (20) << "<NULL>" << " | ";
257 else
258 cout << setw (20) << sqlite3_column_text (stmt, 7) << " | ";
259
260 if (sqlite3_column_int (stmt, 8) == 0)
261 cout << setw (20) << "no" << endl;
262 else
263 cout << setw (20) << "yes" << endl;
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800264 }
265
266 sqlite3_finalize (stmt);
267 }
268};
269
270int main(int argc, char *argv[])
271{
272 INIT_LOGGERS ();
273
Zhenkai Zhued457c32013-02-13 15:51:48 -0800274 if (argc != 3 && !(argc == 5 && string(argv[1]) == "action"))
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800275 {
Alexander Afanasyev24b449f2013-02-06 13:27:59 -0800276 cerr << "Usage: ./dump-db state|action|file|all <path-to-shared-folder>" << endl;
Zhenkai Zhued457c32013-02-13 15:51:48 -0800277 cerr << " or: ./dump-db action <path-to-shared-folder> <device-name> <seq-no>" << endl;
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800278 return 1;
279 }
280
Alexander Afanasyev24b449f2013-02-06 13:27:59 -0800281 string type = argv[1];
282 if (type == "state")
283 {
284 StateLogDumper dumper (argv[2]);
285 dumper.DumpState ();
286 dumper.DumpLog ();
287 }
288 else if (type == "action")
289 {
290 ActionLogDumper dumper (argv[2]);
Zhenkai Zhued457c32013-02-13 15:51:48 -0800291 if (argc == 5)
292 {
293 dumper.DumpActionData(string(argv[3]), boost::lexical_cast<int64_t>(argv[4]));
294 }
295 else
296 {
297 dumper.Dump ();
298 }
Alexander Afanasyev24b449f2013-02-06 13:27:59 -0800299 }
300 else if (type == "file")
301 {
302 FileStateDumper dumper (argv[2]);
303 dumper.Dump ();
304 }
305 else if (type == "all")
306 {
307 {
308 StateLogDumper dumper (argv[2]);
309 dumper.DumpState ();
310 dumper.DumpLog ();
311 }
312
313 {
314 ActionLogDumper dumper (argv[2]);
315 dumper.Dump ();
316 }
317
318 {
319 FileStateDumper dumper (argv[2]);
320 dumper.Dump ();
321 }
322 }
323 else
324 {
325 cerr << "ERROR: Wrong database type" << endl;
326 cerr << "\nUsage: ./dump-db state|action|file|all <path-to-shared-folder>" << endl;
327 return 1;
328 }
329
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800330
331 return 0;
332}