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 | { |
| 33 | UpdateDeviceSeqno (localName, 0); |
| 34 | |
| 35 | sqlite3_stmt *stmt; |
| 36 | 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] | 37 | |
| 38 | Ccnx::CcnxCharbufPtr name = m_localName; |
| 39 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 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 () |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 93 | << errmsg_info_str (sqlite3_errmsg(m_db))); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 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 () |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 112 | << errmsg_info_str (sqlite3_errmsg(m_db))); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 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 | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 180 | SyncLog::UpdateDeviceSeqno (const Ccnx::Name &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 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 187 | Ccnx::CcnxCharbufPtr nameBuf = name; |
| 188 | res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 189 | res += sqlite3_bind_int64 (stmt, 2, seqNo); |
| 190 | sqlite3_step (stmt); |
| 191 | |
| 192 | if (res != SQLITE_OK) |
| 193 | { |
| 194 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 195 | << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 196 | } |
| 197 | sqlite3_finalize (stmt); |
| 198 | } |
| 199 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 200 | void |
| 201 | SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo) |
| 202 | { |
| 203 | sqlite3_stmt *stmt; |
| 204 | // update is performed using trigger |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 205 | 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] | 206 | -1, &stmt, 0); |
| 207 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 208 | res += sqlite3_bind_int64 (stmt, 1, seqNo); |
| 209 | res += sqlite3_bind_int64 (stmt, 2, deviceId); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 210 | sqlite3_step (stmt); |
| 211 | |
| 212 | if (res != SQLITE_OK) |
| 213 | { |
| 214 | BOOST_THROW_EXCEPTION (Error::Db () |
| 215 | << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)")); |
| 216 | } |
| 217 | sqlite3_finalize (stmt); |
| 218 | } |
| 219 | |
| 220 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 221 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 222 | SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash) |
| 223 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 224 | return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash)); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 227 | SyncStateMsgPtr |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 228 | SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash) |
| 229 | { |
| 230 | sqlite3_stmt *stmt; |
| 231 | |
| 232 | int res = sqlite3_prepare_v2 (m_db, "\ |
| 233 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 234 | FROM (SELECT * \ |
| 235 | FROM SyncStateNodes \ |
| 236 | WHERE state_id=(SELECT state_id \ |
| 237 | FROM SyncLog \ |
| 238 | WHERE state_hash=:old_hash)) s_old \ |
| 239 | LEFT JOIN (SELECT * \ |
| 240 | FROM SyncStateNodes \ |
| 241 | WHERE state_id=(SELECT state_id \ |
| 242 | FROM SyncLog \ |
| 243 | WHERE state_hash=:new_hash)) s_new \ |
| 244 | \ |
| 245 | ON s_old.device_id = s_new.device_id \ |
| 246 | JOIN SyncNodes sn ON sn.device_id = s_old.device_id \ |
| 247 | \ |
| 248 | WHERE s_new.seq_no IS NULL OR \ |
| 249 | s_old.seq_no != s_new.seq_no \ |
| 250 | \ |
| 251 | UNION ALL \ |
| 252 | \ |
| 253 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 254 | FROM (SELECT * \ |
| 255 | FROM SyncStateNodes \ |
| 256 | WHERE state_id=(SELECT state_id \ |
| 257 | FROM SyncLog \ |
| 258 | WHERE state_hash=:new_hash )) s_new \ |
| 259 | LEFT JOIN (SELECT * \ |
| 260 | FROM SyncStateNodes \ |
| 261 | WHERE state_id=(SELECT state_id \ |
| 262 | FROM SyncLog \ |
| 263 | WHERE state_hash=:old_hash)) s_old \ |
| 264 | \ |
| 265 | ON s_old.device_id = s_new.device_id \ |
| 266 | JOIN SyncNodes sn ON sn.device_id = s_new.device_id \ |
| 267 | \ |
| 268 | WHERE s_old.seq_no IS NULL \ |
| 269 | ", -1, &stmt, 0); |
| 270 | |
| 271 | if (res != SQLITE_OK) |
| 272 | { |
| 273 | BOOST_THROW_EXCEPTION (Error::Db () |
| 274 | << errmsg_info_str ("Some error with FindStateDifferences")); |
| 275 | } |
| 276 | |
| 277 | res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC); |
| 278 | res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC); |
| 279 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 280 | SyncStateMsgPtr msg = make_shared<SyncStateMsg> (); |
| 281 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 282 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 283 | { |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 284 | SyncState *state = msg->add_state (); |
| 285 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 286 | 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] | 287 | |
| 288 | sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2); |
| 289 | if (newSeqNo > 0) |
| 290 | { |
| 291 | state->set_type (SyncState::UPDATE); |
| 292 | state->set_seq (newSeqNo); |
| 293 | } |
| 294 | else |
| 295 | state->set_type (SyncState::DELETE); |
| 296 | |
| 297 | // std::cout << sqlite3_column_text (stmt, 0) << |
| 298 | // ": from " << sqlite3_column_int64 (stmt, 1) << |
| 299 | // " to " << sqlite3_column_int64 (stmt, 2) << |
| 300 | // std::endl; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 301 | } |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 302 | sqlite3_finalize (stmt); |
| 303 | |
| 304 | return msg; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 305 | } |