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