Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "sync-log.h" |
| 23 | |
| 24 | #include <boost/make_shared.hpp> |
| 25 | |
| 26 | using namespace boost; |
| 27 | using namespace std; |
| 28 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 29 | SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 30 | : DbHelper (path) |
| 31 | , m_localName (localName) |
| 32 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 33 | SyncLog::RememberStateInStateLog (); |
| 34 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 35 | UpdateDeviceSeqno (localName, 0); |
| 36 | |
| 37 | sqlite3_stmt *stmt; |
| 38 | 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] | 39 | |
| 40 | Ccnx::CcnxCharbufPtr name = m_localName; |
| 41 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 42 | |
| 43 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 44 | { |
| 45 | m_localDeviceId = sqlite3_column_int64 (stmt, 0); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | BOOST_THROW_EXCEPTION (Error::Db () |
| 50 | << errmsg_info_str ("Impossible thing in SyncLog::SyncLog")); |
| 51 | } |
| 52 | sqlite3_finalize (stmt); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | sqlite3_int64 |
| 57 | SyncLog::GetNextLocalSeqNo () |
| 58 | { |
| 59 | sqlite3_stmt *stmt_seq; |
| 60 | sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0); |
| 61 | sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId); |
| 62 | |
| 63 | if (sqlite3_step (stmt_seq) != SQLITE_ROW) |
| 64 | { |
| 65 | BOOST_THROW_EXCEPTION (Error::Db () |
| 66 | << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate")); |
| 67 | } |
| 68 | |
| 69 | sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1; |
| 70 | sqlite3_finalize (stmt_seq); |
| 71 | |
| 72 | UpdateDeviceSeqNo (m_localDeviceId, seq_no); |
| 73 | |
| 74 | return seq_no; |
| 75 | } |
| 76 | |
| 77 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 78 | HashPtr |
| 79 | SyncLog::RememberStateInStateLog () |
| 80 | { |
| 81 | int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
| 82 | |
| 83 | res += sqlite3_exec (m_db, "\ |
| 84 | INSERT INTO SyncLog \ |
| 85 | (state_hash, last_update) \ |
| 86 | SELECT \ |
| 87 | hash(device_name, seq_no), datetime('now') \ |
| 88 | FROM SyncNodes \ |
| 89 | ORDER BY device_name; \ |
| 90 | ", 0,0,0); |
| 91 | |
| 92 | if (res != SQLITE_OK) |
| 93 | { |
| 94 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 95 | << errmsg_info_str (sqlite3_errmsg(m_db))); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db); |
| 99 | |
| 100 | sqlite3_stmt *insertStmt; |
| 101 | res += sqlite3_prepare (m_db, "\ |
| 102 | INSERT INTO SyncStateNodes \ |
| 103 | (state_id, device_id, seq_no) \ |
| 104 | SELECT ?, device_id, seq_no \ |
| 105 | FROM SyncNodes; \ |
| 106 | ", -1, &insertStmt, 0); |
| 107 | |
| 108 | res += sqlite3_bind_int64 (insertStmt, 1, rowId); |
| 109 | sqlite3_step (insertStmt); |
| 110 | |
| 111 | if (res != SQLITE_OK) |
| 112 | { |
| 113 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 114 | << errmsg_info_str (sqlite3_errmsg(m_db))); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 115 | } |
| 116 | sqlite3_finalize (insertStmt); |
| 117 | |
| 118 | sqlite3_stmt *getHashStmt; |
| 119 | res += sqlite3_prepare (m_db, "\ |
| 120 | SELECT state_hash FROM SyncLog WHERE state_id = ?\ |
| 121 | ", -1, &getHashStmt, 0); |
| 122 | res += sqlite3_bind_int64 (getHashStmt, 1, rowId); |
| 123 | |
| 124 | HashPtr retval; |
| 125 | int stepRes = sqlite3_step (getHashStmt); |
| 126 | if (stepRes == SQLITE_ROW) |
| 127 | { |
| 128 | retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0), |
| 129 | sqlite3_column_bytes (getHashStmt, 0)); |
| 130 | } |
| 131 | sqlite3_finalize (getHashStmt); |
| 132 | res += sqlite3_exec (m_db, "COMMIT;", 0,0,0); |
| 133 | |
| 134 | if (res != SQLITE_OK) |
| 135 | { |
| 136 | BOOST_THROW_EXCEPTION (Error::Db () |
| 137 | << errmsg_info_str ("Some error with rememberStateInStateLog")); |
| 138 | } |
| 139 | |
| 140 | return retval; |
| 141 | } |
| 142 | |
| 143 | sqlite3_int64 |
| 144 | SyncLog::LookupSyncLog (const std::string &stateHash) |
| 145 | { |
| 146 | return LookupSyncLog (*Hash::FromString (stateHash)); |
| 147 | } |
| 148 | |
| 149 | sqlite3_int64 |
| 150 | SyncLog::LookupSyncLog (const Hash &stateHash) |
| 151 | { |
| 152 | sqlite3_stmt *stmt; |
| 153 | int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?", |
| 154 | -1, &stmt, 0); |
| 155 | |
| 156 | if (res != SQLITE_OK) |
| 157 | { |
| 158 | BOOST_THROW_EXCEPTION (Error::Db () |
| 159 | << errmsg_info_str ("Cannot prepare statement")); |
| 160 | } |
| 161 | |
| 162 | res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC); |
| 163 | if (res != SQLITE_OK) |
| 164 | { |
| 165 | BOOST_THROW_EXCEPTION (Error::Db () |
| 166 | << errmsg_info_str ("Cannot bind")); |
| 167 | } |
| 168 | |
| 169 | sqlite3_int64 row = 0; // something bad |
| 170 | |
| 171 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 172 | { |
| 173 | row = sqlite3_column_int64 (stmt, 0); |
| 174 | } |
| 175 | |
| 176 | sqlite3_finalize (stmt); |
| 177 | |
| 178 | return row; |
| 179 | } |
| 180 | |
| 181 | void |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 182 | SyncLog::UpdateDeviceSeqno (const Ccnx::Name &name, sqlite3_int64 seqNo) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 183 | { |
| 184 | sqlite3_stmt *stmt; |
| 185 | // update is performed using trigger |
| 186 | int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);", |
| 187 | -1, &stmt, 0); |
| 188 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 189 | Ccnx::CcnxCharbufPtr nameBuf = name; |
| 190 | res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 191 | res += sqlite3_bind_int64 (stmt, 2, seqNo); |
| 192 | sqlite3_step (stmt); |
| 193 | |
| 194 | if (res != SQLITE_OK) |
| 195 | { |
| 196 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 197 | << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 198 | } |
| 199 | sqlite3_finalize (stmt); |
| 200 | } |
| 201 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 202 | void |
| 203 | SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo) |
| 204 | { |
| 205 | sqlite3_stmt *stmt; |
| 206 | // update is performed using trigger |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 207 | 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] | 208 | -1, &stmt, 0); |
| 209 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 210 | res += sqlite3_bind_int64 (stmt, 1, seqNo); |
| 211 | res += sqlite3_bind_int64 (stmt, 2, deviceId); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 212 | sqlite3_step (stmt); |
| 213 | |
| 214 | if (res != SQLITE_OK) |
| 215 | { |
| 216 | BOOST_THROW_EXCEPTION (Error::Db () |
| 217 | << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)")); |
| 218 | } |
| 219 | sqlite3_finalize (stmt); |
| 220 | } |
| 221 | |
| 222 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 223 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 224 | SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash) |
| 225 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 226 | return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash)); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 229 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 230 | SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash) |
| 231 | { |
| 232 | sqlite3_stmt *stmt; |
| 233 | |
| 234 | int res = sqlite3_prepare_v2 (m_db, "\ |
| 235 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 236 | FROM (SELECT * \ |
| 237 | FROM SyncStateNodes \ |
| 238 | WHERE state_id=(SELECT state_id \ |
| 239 | FROM SyncLog \ |
| 240 | WHERE state_hash=:old_hash)) s_old \ |
| 241 | LEFT JOIN (SELECT * \ |
| 242 | FROM SyncStateNodes \ |
| 243 | WHERE state_id=(SELECT state_id \ |
| 244 | FROM SyncLog \ |
| 245 | WHERE state_hash=:new_hash)) s_new \ |
| 246 | \ |
| 247 | ON s_old.device_id = s_new.device_id \ |
| 248 | JOIN SyncNodes sn ON sn.device_id = s_old.device_id \ |
| 249 | \ |
| 250 | WHERE s_new.seq_no IS NULL OR \ |
| 251 | s_old.seq_no != s_new.seq_no \ |
| 252 | \ |
| 253 | UNION ALL \ |
| 254 | \ |
| 255 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 256 | FROM (SELECT * \ |
| 257 | FROM SyncStateNodes \ |
| 258 | WHERE state_id=(SELECT state_id \ |
| 259 | FROM SyncLog \ |
| 260 | WHERE state_hash=:new_hash )) s_new \ |
| 261 | LEFT JOIN (SELECT * \ |
| 262 | FROM SyncStateNodes \ |
| 263 | WHERE state_id=(SELECT state_id \ |
| 264 | FROM SyncLog \ |
| 265 | WHERE state_hash=:old_hash)) s_old \ |
| 266 | \ |
| 267 | ON s_old.device_id = s_new.device_id \ |
| 268 | JOIN SyncNodes sn ON sn.device_id = s_new.device_id \ |
| 269 | \ |
| 270 | WHERE s_old.seq_no IS NULL \ |
| 271 | ", -1, &stmt, 0); |
| 272 | |
| 273 | if (res != SQLITE_OK) |
| 274 | { |
| 275 | BOOST_THROW_EXCEPTION (Error::Db () |
| 276 | << errmsg_info_str ("Some error with FindStateDifferences")); |
| 277 | } |
| 278 | |
| 279 | res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC); |
| 280 | res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC); |
| 281 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 282 | SyncStateMsgPtr msg = make_shared<SyncStateMsg> (); |
| 283 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 284 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 285 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 286 | SyncState *state = msg->add_state (); |
| 287 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 288 | 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] | 289 | |
| 290 | sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2); |
| 291 | if (newSeqNo > 0) |
| 292 | { |
| 293 | state->set_type (SyncState::UPDATE); |
| 294 | state->set_seq (newSeqNo); |
| 295 | } |
| 296 | else |
| 297 | state->set_type (SyncState::DELETE); |
| 298 | |
| 299 | // std::cout << sqlite3_column_text (stmt, 0) << |
| 300 | // ": from " << sqlite3_column_int64 (stmt, 1) << |
| 301 | // " to " << sqlite3_column_int64 (stmt, 2) << |
| 302 | // std::endl; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 303 | } |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 304 | sqlite3_finalize (stmt); |
| 305 | |
| 306 | return msg; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 307 | } |