Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -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 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "sync-log.h" |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 23 | #include <utility> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 24 | |
| 25 | #include <boost/make_shared.hpp> |
| 26 | |
| 27 | using namespace boost; |
| 28 | using namespace std; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 29 | using namespace Ccnx; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 31 | SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 32 | : DbHelper (path) |
| 33 | , m_localName (localName) |
| 34 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 35 | UpdateDeviceSeqNo (localName, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 36 | |
| 37 | sqlite3_stmt *stmt; |
| 38 | int res = sqlite3_prepare_v2 (m_db, "SELECT device_id, seq_no FROM SyncNodes WHERE device_name=?", -1, &stmt, 0); |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 39 | |
| 40 | Ccnx::CcnxCharbufPtr name = m_localName; |
| 41 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 42 | |
| 43 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 44 | { |
| 45 | m_localDeviceId = sqlite3_column_int64 (stmt, 0); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | BOOST_THROW_EXCEPTION (Error::Db () |
| 50 | << errmsg_info_str ("Impossible thing in SyncLog::SyncLog")); |
| 51 | } |
| 52 | sqlite3_finalize (stmt); |
| 53 | } |
| 54 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 55 | void |
| 56 | SyncLog::initYP(map<Name, Name> &yp) |
| 57 | { |
| 58 | sqlite3_stmt *stmt; |
| 59 | sqlite3_prepare_v2(m_db, "SELECT device_name, last_known_locator FROM SyncNodes;", -1, &stmt, 0); |
| 60 | |
| 61 | while (sqlite3_step(stmt) == SQLITE_ROW) |
| 62 | { |
| 63 | Name deviceName((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); |
| 64 | Name locator; |
| 65 | if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB) |
| 66 | { |
| 67 | locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1)); |
| 68 | } |
| 69 | yp.insert(make_pair(deviceName, locator)); |
| 70 | } |
| 71 | |
| 72 | sqlite3_finalize(stmt); |
| 73 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 74 | |
| 75 | sqlite3_int64 |
| 76 | SyncLog::GetNextLocalSeqNo () |
| 77 | { |
| 78 | sqlite3_stmt *stmt_seq; |
| 79 | sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0); |
| 80 | sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId); |
| 81 | |
| 82 | if (sqlite3_step (stmt_seq) != SQLITE_ROW) |
| 83 | { |
| 84 | BOOST_THROW_EXCEPTION (Error::Db () |
| 85 | << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate")); |
| 86 | } |
| 87 | |
| 88 | sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1; |
| 89 | sqlite3_finalize (stmt_seq); |
| 90 | |
| 91 | UpdateDeviceSeqNo (m_localDeviceId, seq_no); |
| 92 | |
| 93 | return seq_no; |
| 94 | } |
| 95 | |
| 96 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 97 | HashPtr |
| 98 | SyncLog::RememberStateInStateLog () |
| 99 | { |
| 100 | int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
| 101 | |
| 102 | res += sqlite3_exec (m_db, "\ |
| 103 | INSERT INTO SyncLog \ |
| 104 | (state_hash, last_update) \ |
| 105 | SELECT \ |
| 106 | hash(device_name, seq_no), datetime('now') \ |
| 107 | FROM SyncNodes \ |
| 108 | ORDER BY device_name; \ |
| 109 | ", 0,0,0); |
| 110 | |
| 111 | if (res != SQLITE_OK) |
| 112 | { |
| 113 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 114 | << errmsg_info_str (sqlite3_errmsg(m_db))); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db); |
| 118 | |
| 119 | sqlite3_stmt *insertStmt; |
| 120 | res += sqlite3_prepare (m_db, "\ |
| 121 | INSERT INTO SyncStateNodes \ |
| 122 | (state_id, device_id, seq_no) \ |
| 123 | SELECT ?, device_id, seq_no \ |
| 124 | FROM SyncNodes; \ |
| 125 | ", -1, &insertStmt, 0); |
| 126 | |
| 127 | res += sqlite3_bind_int64 (insertStmt, 1, rowId); |
| 128 | sqlite3_step (insertStmt); |
| 129 | |
| 130 | if (res != SQLITE_OK) |
| 131 | { |
| 132 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 133 | << errmsg_info_str (sqlite3_errmsg(m_db))); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 134 | } |
| 135 | sqlite3_finalize (insertStmt); |
| 136 | |
| 137 | sqlite3_stmt *getHashStmt; |
| 138 | res += sqlite3_prepare (m_db, "\ |
| 139 | SELECT state_hash FROM SyncLog WHERE state_id = ?\ |
| 140 | ", -1, &getHashStmt, 0); |
| 141 | res += sqlite3_bind_int64 (getHashStmt, 1, rowId); |
| 142 | |
| 143 | HashPtr retval; |
| 144 | int stepRes = sqlite3_step (getHashStmt); |
| 145 | if (stepRes == SQLITE_ROW) |
| 146 | { |
| 147 | retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0), |
| 148 | sqlite3_column_bytes (getHashStmt, 0)); |
| 149 | } |
| 150 | sqlite3_finalize (getHashStmt); |
| 151 | res += sqlite3_exec (m_db, "COMMIT;", 0,0,0); |
| 152 | |
| 153 | if (res != SQLITE_OK) |
| 154 | { |
| 155 | BOOST_THROW_EXCEPTION (Error::Db () |
| 156 | << errmsg_info_str ("Some error with rememberStateInStateLog")); |
| 157 | } |
| 158 | |
| 159 | return retval; |
| 160 | } |
| 161 | |
| 162 | sqlite3_int64 |
| 163 | SyncLog::LookupSyncLog (const std::string &stateHash) |
| 164 | { |
| 165 | return LookupSyncLog (*Hash::FromString (stateHash)); |
| 166 | } |
| 167 | |
| 168 | sqlite3_int64 |
| 169 | SyncLog::LookupSyncLog (const Hash &stateHash) |
| 170 | { |
| 171 | sqlite3_stmt *stmt; |
| 172 | int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?", |
| 173 | -1, &stmt, 0); |
| 174 | |
| 175 | if (res != SQLITE_OK) |
| 176 | { |
| 177 | BOOST_THROW_EXCEPTION (Error::Db () |
| 178 | << errmsg_info_str ("Cannot prepare statement")); |
| 179 | } |
| 180 | |
| 181 | res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC); |
| 182 | if (res != SQLITE_OK) |
| 183 | { |
| 184 | BOOST_THROW_EXCEPTION (Error::Db () |
| 185 | << errmsg_info_str ("Cannot bind")); |
| 186 | } |
| 187 | |
| 188 | sqlite3_int64 row = 0; // something bad |
| 189 | |
| 190 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 191 | { |
| 192 | row = sqlite3_column_int64 (stmt, 0); |
| 193 | } |
| 194 | |
| 195 | sqlite3_finalize (stmt); |
| 196 | |
| 197 | return row; |
| 198 | } |
| 199 | |
| 200 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 201 | SyncLog::UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 202 | { |
| 203 | sqlite3_stmt *stmt; |
| 204 | // update is performed using trigger |
| 205 | int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);", |
| 206 | -1, &stmt, 0); |
| 207 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 208 | Ccnx::CcnxCharbufPtr nameBuf = name; |
| 209 | res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 210 | res += sqlite3_bind_int64 (stmt, 2, seqNo); |
| 211 | sqlite3_step (stmt); |
| 212 | |
| 213 | if (res != SQLITE_OK) |
| 214 | { |
| 215 | BOOST_THROW_EXCEPTION (Error::Db () |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 216 | << errmsg_info_str ("Some error with UpdateDeviceSeqNo (name)")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 217 | } |
| 218 | sqlite3_finalize (stmt); |
| 219 | } |
| 220 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 221 | void |
| 222 | SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo) |
| 223 | { |
| 224 | sqlite3_stmt *stmt; |
| 225 | // update is performed using trigger |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 226 | int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;", |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 227 | -1, &stmt, 0); |
| 228 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 229 | res += sqlite3_bind_int64 (stmt, 1, seqNo); |
| 230 | res += sqlite3_bind_int64 (stmt, 2, deviceId); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 231 | sqlite3_step (stmt); |
| 232 | |
| 233 | if (res != SQLITE_OK) |
| 234 | { |
| 235 | BOOST_THROW_EXCEPTION (Error::Db () |
| 236 | << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)")); |
| 237 | } |
| 238 | sqlite3_finalize (stmt); |
| 239 | } |
| 240 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 241 | Name |
| 242 | SyncLog::LookupLocator(const Name &deviceName) |
| 243 | { |
| 244 | sqlite3_stmt *stmt; |
| 245 | sqlite3_prepare_v2 (m_db, "SELECT last_known_locator FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0); |
| 246 | Ccnx::CcnxCharbufPtr nameBuf = deviceName; |
| 247 | sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC); |
| 248 | int res = sqlite3_step (stmt); |
| 249 | Name locator; |
| 250 | switch (res) |
| 251 | { |
| 252 | case SQLITE_ROW: |
| 253 | { |
| 254 | locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); |
| 255 | } |
| 256 | case SQLITE_DONE: break; |
| 257 | default: |
| 258 | BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in LookupLocator()")); |
| 259 | } |
| 260 | |
| 261 | sqlite3_finalize(stmt); |
| 262 | |
| 263 | return locator; |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | SyncLog::UpdateLocator(const Name &deviceName, const Name &locator) |
| 268 | { |
| 269 | sqlite3_stmt *stmt; |
| 270 | sqlite3_prepare_v2 (m_db, "UPDATE SyncNodes SET last_known_locator=? WHERE device_name=?;", -1, &stmt, 0); |
| 271 | Ccnx::CcnxCharbufPtr nameBuf = deviceName; |
| 272 | Ccnx::CcnxCharbufPtr locatorBuf = locator; |
| 273 | sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC); |
| 274 | sqlite3_bind_blob (stmt, 2, locatorBuf->buf(), locatorBuf->length(), SQLITE_STATIC); |
| 275 | int res = sqlite3_step (stmt); |
| 276 | |
| 277 | if (res != SQLITE_OK && res != SQLITE_DONE) |
| 278 | { |
| 279 | BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in UpdateLoactor()")); |
| 280 | } |
| 281 | |
| 282 | sqlite3_finalize(stmt); |
| 283 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 284 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 285 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 286 | SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash) |
| 287 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 288 | return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash)); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 291 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 292 | SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash) |
| 293 | { |
| 294 | sqlite3_stmt *stmt; |
| 295 | |
| 296 | int res = sqlite3_prepare_v2 (m_db, "\ |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 297 | SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\ |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 298 | FROM (SELECT * \ |
| 299 | FROM SyncStateNodes \ |
| 300 | WHERE state_id=(SELECT state_id \ |
| 301 | FROM SyncLog \ |
| 302 | WHERE state_hash=:old_hash)) s_old \ |
| 303 | LEFT JOIN (SELECT * \ |
| 304 | FROM SyncStateNodes \ |
| 305 | WHERE state_id=(SELECT state_id \ |
| 306 | FROM SyncLog \ |
| 307 | WHERE state_hash=:new_hash)) s_new \ |
| 308 | \ |
| 309 | ON s_old.device_id = s_new.device_id \ |
| 310 | JOIN SyncNodes sn ON sn.device_id = s_old.device_id \ |
| 311 | \ |
| 312 | WHERE s_new.seq_no IS NULL OR \ |
| 313 | s_old.seq_no != s_new.seq_no \ |
| 314 | \ |
| 315 | UNION ALL \ |
| 316 | \ |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 317 | SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\ |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 318 | FROM (SELECT * \ |
| 319 | FROM SyncStateNodes \ |
| 320 | WHERE state_id=(SELECT state_id \ |
| 321 | FROM SyncLog \ |
| 322 | WHERE state_hash=:new_hash )) s_new \ |
| 323 | LEFT JOIN (SELECT * \ |
| 324 | FROM SyncStateNodes \ |
| 325 | WHERE state_id=(SELECT state_id \ |
| 326 | FROM SyncLog \ |
| 327 | WHERE state_hash=:old_hash)) s_old \ |
| 328 | \ |
| 329 | ON s_old.device_id = s_new.device_id \ |
| 330 | JOIN SyncNodes sn ON sn.device_id = s_new.device_id \ |
| 331 | \ |
| 332 | WHERE s_old.seq_no IS NULL \ |
| 333 | ", -1, &stmt, 0); |
| 334 | |
| 335 | if (res != SQLITE_OK) |
| 336 | { |
| 337 | BOOST_THROW_EXCEPTION (Error::Db () |
| 338 | << errmsg_info_str ("Some error with FindStateDifferences")); |
| 339 | } |
| 340 | |
| 341 | res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC); |
| 342 | res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC); |
| 343 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 344 | SyncStateMsgPtr msg = make_shared<SyncStateMsg> (); |
| 345 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 346 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 347 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 348 | SyncState *state = msg->add_state (); |
| 349 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 350 | state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0))); |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 351 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame^] | 352 | // locator is optional, so must check if it is null |
| 353 | if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB) |
| 354 | { |
| 355 | state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1))); |
| 356 | } |
| 357 | |
| 358 | sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3); |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 359 | if (newSeqNo > 0) |
| 360 | { |
| 361 | state->set_type (SyncState::UPDATE); |
| 362 | state->set_seq (newSeqNo); |
| 363 | } |
| 364 | else |
| 365 | state->set_type (SyncState::DELETE); |
| 366 | |
| 367 | // std::cout << sqlite3_column_text (stmt, 0) << |
| 368 | // ": from " << sqlite3_column_int64 (stmt, 1) << |
| 369 | // " to " << sqlite3_column_int64 (stmt, 2) << |
| 370 | // std::endl; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 371 | } |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 372 | sqlite3_finalize (stmt); |
| 373 | |
| 374 | return msg; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 375 | } |