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 | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 29 | SyncLog::SyncLog (const std::string &path, const std::string &localName) |
| 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); |
| 39 | sqlite3_bind_text (stmt, 1, m_localName.c_str (), m_localName.size (), SQLITE_STATIC); |
| 40 | |
| 41 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 42 | { |
| 43 | m_localDeviceId = sqlite3_column_int64 (stmt, 0); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | BOOST_THROW_EXCEPTION (Error::Db () |
| 48 | << errmsg_info_str ("Impossible thing in SyncLog::SyncLog")); |
| 49 | } |
| 50 | sqlite3_finalize (stmt); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | sqlite3_int64 |
| 55 | SyncLog::GetNextLocalSeqNo () |
| 56 | { |
| 57 | sqlite3_stmt *stmt_seq; |
| 58 | sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0); |
| 59 | sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId); |
| 60 | |
| 61 | if (sqlite3_step (stmt_seq) != SQLITE_ROW) |
| 62 | { |
| 63 | BOOST_THROW_EXCEPTION (Error::Db () |
| 64 | << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate")); |
| 65 | } |
| 66 | |
| 67 | sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1; |
| 68 | sqlite3_finalize (stmt_seq); |
| 69 | |
| 70 | UpdateDeviceSeqNo (m_localDeviceId, seq_no); |
| 71 | |
| 72 | return seq_no; |
| 73 | } |
| 74 | |
| 75 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 76 | HashPtr |
| 77 | SyncLog::RememberStateInStateLog () |
| 78 | { |
| 79 | int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
| 80 | |
| 81 | res += sqlite3_exec (m_db, "\ |
| 82 | INSERT INTO SyncLog \ |
| 83 | (state_hash, last_update) \ |
| 84 | SELECT \ |
| 85 | hash(device_name, seq_no), datetime('now') \ |
| 86 | FROM SyncNodes \ |
| 87 | ORDER BY device_name; \ |
| 88 | ", 0,0,0); |
| 89 | |
| 90 | if (res != SQLITE_OK) |
| 91 | { |
| 92 | BOOST_THROW_EXCEPTION (Error::Db () |
| 93 | << errmsg_info_str ("1")); |
| 94 | } |
| 95 | |
| 96 | sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db); |
| 97 | |
| 98 | sqlite3_stmt *insertStmt; |
| 99 | res += sqlite3_prepare (m_db, "\ |
| 100 | INSERT INTO SyncStateNodes \ |
| 101 | (state_id, device_id, seq_no) \ |
| 102 | SELECT ?, device_id, seq_no \ |
| 103 | FROM SyncNodes; \ |
| 104 | ", -1, &insertStmt, 0); |
| 105 | |
| 106 | res += sqlite3_bind_int64 (insertStmt, 1, rowId); |
| 107 | sqlite3_step (insertStmt); |
| 108 | |
| 109 | if (res != SQLITE_OK) |
| 110 | { |
| 111 | BOOST_THROW_EXCEPTION (Error::Db () |
| 112 | << errmsg_info_str ("4")); |
| 113 | } |
| 114 | sqlite3_finalize (insertStmt); |
| 115 | |
| 116 | sqlite3_stmt *getHashStmt; |
| 117 | res += sqlite3_prepare (m_db, "\ |
| 118 | SELECT state_hash FROM SyncLog WHERE state_id = ?\ |
| 119 | ", -1, &getHashStmt, 0); |
| 120 | res += sqlite3_bind_int64 (getHashStmt, 1, rowId); |
| 121 | |
| 122 | HashPtr retval; |
| 123 | int stepRes = sqlite3_step (getHashStmt); |
| 124 | if (stepRes == SQLITE_ROW) |
| 125 | { |
| 126 | retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0), |
| 127 | sqlite3_column_bytes (getHashStmt, 0)); |
| 128 | } |
| 129 | sqlite3_finalize (getHashStmt); |
| 130 | res += sqlite3_exec (m_db, "COMMIT;", 0,0,0); |
| 131 | |
| 132 | if (res != SQLITE_OK) |
| 133 | { |
| 134 | BOOST_THROW_EXCEPTION (Error::Db () |
| 135 | << errmsg_info_str ("Some error with rememberStateInStateLog")); |
| 136 | } |
| 137 | |
| 138 | return retval; |
| 139 | } |
| 140 | |
| 141 | sqlite3_int64 |
| 142 | SyncLog::LookupSyncLog (const std::string &stateHash) |
| 143 | { |
| 144 | return LookupSyncLog (*Hash::FromString (stateHash)); |
| 145 | } |
| 146 | |
| 147 | sqlite3_int64 |
| 148 | SyncLog::LookupSyncLog (const Hash &stateHash) |
| 149 | { |
| 150 | sqlite3_stmt *stmt; |
| 151 | int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?", |
| 152 | -1, &stmt, 0); |
| 153 | |
| 154 | if (res != SQLITE_OK) |
| 155 | { |
| 156 | BOOST_THROW_EXCEPTION (Error::Db () |
| 157 | << errmsg_info_str ("Cannot prepare statement")); |
| 158 | } |
| 159 | |
| 160 | res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC); |
| 161 | if (res != SQLITE_OK) |
| 162 | { |
| 163 | BOOST_THROW_EXCEPTION (Error::Db () |
| 164 | << errmsg_info_str ("Cannot bind")); |
| 165 | } |
| 166 | |
| 167 | sqlite3_int64 row = 0; // something bad |
| 168 | |
| 169 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 170 | { |
| 171 | row = sqlite3_column_int64 (stmt, 0); |
| 172 | } |
| 173 | |
| 174 | sqlite3_finalize (stmt); |
| 175 | |
| 176 | return row; |
| 177 | } |
| 178 | |
| 179 | void |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 180 | SyncLog::UpdateDeviceSeqno (const std::string &name, sqlite3_int64 seqNo) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 181 | { |
| 182 | sqlite3_stmt *stmt; |
| 183 | // update is performed using trigger |
| 184 | int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);", |
| 185 | -1, &stmt, 0); |
| 186 | |
| 187 | res += sqlite3_bind_text (stmt, 1, name.c_str (), name.size (), SQLITE_STATIC); |
| 188 | res += sqlite3_bind_int64 (stmt, 2, seqNo); |
| 189 | sqlite3_step (stmt); |
| 190 | |
| 191 | if (res != SQLITE_OK) |
| 192 | { |
| 193 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 194 | << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 195 | } |
| 196 | sqlite3_finalize (stmt); |
| 197 | } |
| 198 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 199 | void |
| 200 | SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo) |
| 201 | { |
| 202 | sqlite3_stmt *stmt; |
| 203 | // update is performed using trigger |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame^] | 204 | 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] | 205 | -1, &stmt, 0); |
| 206 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame^] | 207 | res += sqlite3_bind_int64 (stmt, 1, seqNo); |
| 208 | res += sqlite3_bind_int64 (stmt, 2, deviceId); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 209 | sqlite3_step (stmt); |
| 210 | |
| 211 | if (res != SQLITE_OK) |
| 212 | { |
| 213 | BOOST_THROW_EXCEPTION (Error::Db () |
| 214 | << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)")); |
| 215 | } |
| 216 | sqlite3_finalize (stmt); |
| 217 | } |
| 218 | |
| 219 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 220 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 221 | SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash) |
| 222 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 223 | return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash)); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 226 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 227 | SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash) |
| 228 | { |
| 229 | sqlite3_stmt *stmt; |
| 230 | |
| 231 | int res = sqlite3_prepare_v2 (m_db, "\ |
| 232 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 233 | FROM (SELECT * \ |
| 234 | FROM SyncStateNodes \ |
| 235 | WHERE state_id=(SELECT state_id \ |
| 236 | FROM SyncLog \ |
| 237 | WHERE state_hash=:old_hash)) s_old \ |
| 238 | LEFT JOIN (SELECT * \ |
| 239 | FROM SyncStateNodes \ |
| 240 | WHERE state_id=(SELECT state_id \ |
| 241 | FROM SyncLog \ |
| 242 | WHERE state_hash=:new_hash)) s_new \ |
| 243 | \ |
| 244 | ON s_old.device_id = s_new.device_id \ |
| 245 | JOIN SyncNodes sn ON sn.device_id = s_old.device_id \ |
| 246 | \ |
| 247 | WHERE s_new.seq_no IS NULL OR \ |
| 248 | s_old.seq_no != s_new.seq_no \ |
| 249 | \ |
| 250 | UNION ALL \ |
| 251 | \ |
| 252 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 253 | FROM (SELECT * \ |
| 254 | FROM SyncStateNodes \ |
| 255 | WHERE state_id=(SELECT state_id \ |
| 256 | FROM SyncLog \ |
| 257 | WHERE state_hash=:new_hash )) s_new \ |
| 258 | LEFT JOIN (SELECT * \ |
| 259 | FROM SyncStateNodes \ |
| 260 | WHERE state_id=(SELECT state_id \ |
| 261 | FROM SyncLog \ |
| 262 | WHERE state_hash=:old_hash)) s_old \ |
| 263 | \ |
| 264 | ON s_old.device_id = s_new.device_id \ |
| 265 | JOIN SyncNodes sn ON sn.device_id = s_new.device_id \ |
| 266 | \ |
| 267 | WHERE s_old.seq_no IS NULL \ |
| 268 | ", -1, &stmt, 0); |
| 269 | |
| 270 | if (res != SQLITE_OK) |
| 271 | { |
| 272 | BOOST_THROW_EXCEPTION (Error::Db () |
| 273 | << errmsg_info_str ("Some error with FindStateDifferences")); |
| 274 | } |
| 275 | |
| 276 | res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC); |
| 277 | res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC); |
| 278 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 279 | SyncStateMsgPtr msg = make_shared<SyncStateMsg> (); |
| 280 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 281 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 282 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 283 | SyncState *state = msg->add_state (); |
| 284 | |
| 285 | state->set_name (reinterpret_cast<const char*> (sqlite3_column_text (stmt, 0))); |
| 286 | |
| 287 | sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2); |
| 288 | if (newSeqNo > 0) |
| 289 | { |
| 290 | state->set_type (SyncState::UPDATE); |
| 291 | state->set_seq (newSeqNo); |
| 292 | } |
| 293 | else |
| 294 | state->set_type (SyncState::DELETE); |
| 295 | |
| 296 | // std::cout << sqlite3_column_text (stmt, 0) << |
| 297 | // ": from " << sqlite3_column_int64 (stmt, 1) << |
| 298 | // " to " << sqlite3_column_int64 (stmt, 2) << |
| 299 | // std::endl; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 300 | } |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 301 | sqlite3_finalize (stmt); |
| 302 | |
| 303 | return msg; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 304 | } |