Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 29 | using namespace boost; |
| 30 | using namespace std; |
| 31 | using namespace Ccnx; |
| 32 | |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 33 | INIT_LOGGER ("DumpDb"); |
| 34 | |
| 35 | class StateLogDumper : public DbHelper |
| 36 | { |
| 37 | public: |
| 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 Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 124 | class ActionLogDumper : public DbHelper |
| 125 | { |
| 126 | public: |
| 127 | ActionLogDumper (const filesystem::path &path) |
| 128 | : DbHelper (path / ".chronoshare", "action-log.db") |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | void |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 133 | Dump () |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 134 | { |
| 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 Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 139 | " ORDER BY action_timestamp", -1, &stmt, 0); |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 140 | |
| 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 Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 147 | 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 | } |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | class FileStateDumper : public DbHelper |
| 178 | { |
| 179 | public: |
| 180 | FileStateDumper (const filesystem::path &path) |
| 181 | : DbHelper (path / ".chronoshare", "file-state.db") |
| 182 | { |
| 183 | } |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 184 | |
| 185 | void |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 186 | Dump () |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 187 | { |
| 188 | sqlite3_stmt *stmt; |
| 189 | sqlite3_prepare_v2 (m_db, |
Alexander Afanasyev | 38826ce | 2013-01-31 16:31:42 -0800 | [diff] [blame] | 190 | "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,directory,is_complete " |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 191 | " FROM FileState " |
| 192 | " WHERE type = 0 ORDER BY filename", -1, &stmt, 0); |
| 193 | |
| 194 | cout.setf(std::ios::left, std::ios::adjustfield); |
| 195 | cout << ">> FILE STATE <<" << endl; |
Alexander Afanasyev | 38826ce | 2013-01-31 16:31:42 -0800 | [diff] [blame] | 196 | cout << "===================================================================================================================================" << endl; |
| 197 | cout << "filename | device_name | seq_no | file_hash | seg_num | directory | is_complete" << endl; |
| 198 | cout << "===================================================================================================================================" << endl; |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 199 | |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 200 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 201 | { |
| 202 | cout << setw (40) << sqlite3_column_text (stmt, 0) << " | "; |
| 203 | cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1))) << " | "; |
| 204 | cout << setw (6) << sqlite3_column_int64 (stmt, 2) << " | "; |
| 205 | cout << setw (10) << Hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3)).shortHash () << " | "; |
Alexander Afanasyev | 38826ce | 2013-01-31 16:31:42 -0800 | [diff] [blame] | 206 | cout << setw (6) << sqlite3_column_int64 (stmt, 6) << " | "; |
| 207 | if (sqlite3_column_bytes (stmt, 7) == 0) |
| 208 | cout << setw (20) << "<NULL>" << " | "; |
| 209 | else |
| 210 | cout << setw (20) << sqlite3_column_text (stmt, 7) << " | "; |
| 211 | |
| 212 | if (sqlite3_column_int (stmt, 8) == 0) |
| 213 | cout << setw (20) << "no" << endl; |
| 214 | else |
| 215 | cout << setw (20) << "yes" << endl; |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | sqlite3_finalize (stmt); |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | int main(int argc, char *argv[]) |
| 223 | { |
| 224 | INIT_LOGGERS (); |
| 225 | |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 226 | if (argc != 3) |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 227 | { |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 228 | cerr << "Usage: ./dump-db state|action|file|all <path-to-shared-folder>" << endl; |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 232 | string type = argv[1]; |
| 233 | if (type == "state") |
| 234 | { |
| 235 | StateLogDumper dumper (argv[2]); |
| 236 | dumper.DumpState (); |
| 237 | dumper.DumpLog (); |
| 238 | } |
| 239 | else if (type == "action") |
| 240 | { |
| 241 | ActionLogDumper dumper (argv[2]); |
| 242 | dumper.Dump (); |
| 243 | } |
| 244 | else if (type == "file") |
| 245 | { |
| 246 | FileStateDumper dumper (argv[2]); |
| 247 | dumper.Dump (); |
| 248 | } |
| 249 | else if (type == "all") |
| 250 | { |
| 251 | { |
| 252 | StateLogDumper dumper (argv[2]); |
| 253 | dumper.DumpState (); |
| 254 | dumper.DumpLog (); |
| 255 | } |
| 256 | |
| 257 | { |
| 258 | ActionLogDumper dumper (argv[2]); |
| 259 | dumper.Dump (); |
| 260 | } |
| 261 | |
| 262 | { |
| 263 | FileStateDumper dumper (argv[2]); |
| 264 | dumper.Dump (); |
| 265 | } |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | cerr << "ERROR: Wrong database type" << endl; |
| 270 | cerr << "\nUsage: ./dump-db state|action|file|all <path-to-shared-folder>" << endl; |
| 271 | return 1; |
| 272 | } |
| 273 | |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |