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