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