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