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