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