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 | |
| 29 | HashPtr |
| 30 | SyncLog::RememberStateInStateLog () |
| 31 | { |
| 32 | int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
| 33 | |
| 34 | res += sqlite3_exec (m_db, "\ |
| 35 | INSERT INTO SyncLog \ |
| 36 | (state_hash, last_update) \ |
| 37 | SELECT \ |
| 38 | hash(device_name, seq_no), datetime('now') \ |
| 39 | FROM SyncNodes \ |
| 40 | ORDER BY device_name; \ |
| 41 | ", 0,0,0); |
| 42 | |
| 43 | if (res != SQLITE_OK) |
| 44 | { |
| 45 | BOOST_THROW_EXCEPTION (Error::Db () |
| 46 | << errmsg_info_str ("1")); |
| 47 | } |
| 48 | |
| 49 | sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db); |
| 50 | |
| 51 | sqlite3_stmt *insertStmt; |
| 52 | res += sqlite3_prepare (m_db, "\ |
| 53 | INSERT INTO SyncStateNodes \ |
| 54 | (state_id, device_id, seq_no) \ |
| 55 | SELECT ?, device_id, seq_no \ |
| 56 | FROM SyncNodes; \ |
| 57 | ", -1, &insertStmt, 0); |
| 58 | |
| 59 | res += sqlite3_bind_int64 (insertStmt, 1, rowId); |
| 60 | sqlite3_step (insertStmt); |
| 61 | |
| 62 | if (res != SQLITE_OK) |
| 63 | { |
| 64 | BOOST_THROW_EXCEPTION (Error::Db () |
| 65 | << errmsg_info_str ("4")); |
| 66 | } |
| 67 | sqlite3_finalize (insertStmt); |
| 68 | |
| 69 | sqlite3_stmt *getHashStmt; |
| 70 | res += sqlite3_prepare (m_db, "\ |
| 71 | SELECT state_hash FROM SyncLog WHERE state_id = ?\ |
| 72 | ", -1, &getHashStmt, 0); |
| 73 | res += sqlite3_bind_int64 (getHashStmt, 1, rowId); |
| 74 | |
| 75 | HashPtr retval; |
| 76 | int stepRes = sqlite3_step (getHashStmt); |
| 77 | if (stepRes == SQLITE_ROW) |
| 78 | { |
| 79 | retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0), |
| 80 | sqlite3_column_bytes (getHashStmt, 0)); |
| 81 | } |
| 82 | sqlite3_finalize (getHashStmt); |
| 83 | res += sqlite3_exec (m_db, "COMMIT;", 0,0,0); |
| 84 | |
| 85 | if (res != SQLITE_OK) |
| 86 | { |
| 87 | BOOST_THROW_EXCEPTION (Error::Db () |
| 88 | << errmsg_info_str ("Some error with rememberStateInStateLog")); |
| 89 | } |
| 90 | |
| 91 | return retval; |
| 92 | } |
| 93 | |
| 94 | sqlite3_int64 |
| 95 | SyncLog::LookupSyncLog (const std::string &stateHash) |
| 96 | { |
| 97 | return LookupSyncLog (*Hash::FromString (stateHash)); |
| 98 | } |
| 99 | |
| 100 | sqlite3_int64 |
| 101 | SyncLog::LookupSyncLog (const Hash &stateHash) |
| 102 | { |
| 103 | sqlite3_stmt *stmt; |
| 104 | int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?", |
| 105 | -1, &stmt, 0); |
| 106 | |
| 107 | if (res != SQLITE_OK) |
| 108 | { |
| 109 | BOOST_THROW_EXCEPTION (Error::Db () |
| 110 | << errmsg_info_str ("Cannot prepare statement")); |
| 111 | } |
| 112 | |
| 113 | res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC); |
| 114 | if (res != SQLITE_OK) |
| 115 | { |
| 116 | BOOST_THROW_EXCEPTION (Error::Db () |
| 117 | << errmsg_info_str ("Cannot bind")); |
| 118 | } |
| 119 | |
| 120 | sqlite3_int64 row = 0; // something bad |
| 121 | |
| 122 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 123 | { |
| 124 | row = sqlite3_column_int64 (stmt, 0); |
| 125 | } |
| 126 | |
| 127 | sqlite3_finalize (stmt); |
| 128 | |
| 129 | return row; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | SyncLog::UpdateDeviceSeqno (const std::string &name, uint64_t seqNo) |
| 134 | { |
| 135 | sqlite3_stmt *stmt; |
| 136 | // update is performed using trigger |
| 137 | int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);", |
| 138 | -1, &stmt, 0); |
| 139 | |
| 140 | res += sqlite3_bind_text (stmt, 1, name.c_str (), name.size (), SQLITE_STATIC); |
| 141 | res += sqlite3_bind_int64 (stmt, 2, seqNo); |
| 142 | sqlite3_step (stmt); |
| 143 | |
| 144 | if (res != SQLITE_OK) |
| 145 | { |
| 146 | BOOST_THROW_EXCEPTION (Error::Db () |
| 147 | << errmsg_info_str ("Some error with UpdateDeviceSeqno")); |
| 148 | } |
| 149 | sqlite3_finalize (stmt); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash) |
| 154 | { |
| 155 | FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash)); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash) |
| 160 | { |
| 161 | sqlite3_stmt *stmt; |
| 162 | |
| 163 | int res = sqlite3_prepare_v2 (m_db, "\ |
| 164 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 165 | FROM (SELECT * \ |
| 166 | FROM SyncStateNodes \ |
| 167 | WHERE state_id=(SELECT state_id \ |
| 168 | FROM SyncLog \ |
| 169 | WHERE state_hash=:old_hash)) s_old \ |
| 170 | LEFT JOIN (SELECT * \ |
| 171 | FROM SyncStateNodes \ |
| 172 | WHERE state_id=(SELECT state_id \ |
| 173 | FROM SyncLog \ |
| 174 | WHERE state_hash=:new_hash)) s_new \ |
| 175 | \ |
| 176 | ON s_old.device_id = s_new.device_id \ |
| 177 | JOIN SyncNodes sn ON sn.device_id = s_old.device_id \ |
| 178 | \ |
| 179 | WHERE s_new.seq_no IS NULL OR \ |
| 180 | s_old.seq_no != s_new.seq_no \ |
| 181 | \ |
| 182 | UNION ALL \ |
| 183 | \ |
| 184 | SELECT sn.device_name, s_old.seq_no, s_new.seq_no \ |
| 185 | FROM (SELECT * \ |
| 186 | FROM SyncStateNodes \ |
| 187 | WHERE state_id=(SELECT state_id \ |
| 188 | FROM SyncLog \ |
| 189 | WHERE state_hash=:new_hash )) s_new \ |
| 190 | LEFT JOIN (SELECT * \ |
| 191 | FROM SyncStateNodes \ |
| 192 | WHERE state_id=(SELECT state_id \ |
| 193 | FROM SyncLog \ |
| 194 | WHERE state_hash=:old_hash)) s_old \ |
| 195 | \ |
| 196 | ON s_old.device_id = s_new.device_id \ |
| 197 | JOIN SyncNodes sn ON sn.device_id = s_new.device_id \ |
| 198 | \ |
| 199 | WHERE s_old.seq_no IS NULL \ |
| 200 | ", -1, &stmt, 0); |
| 201 | |
| 202 | if (res != SQLITE_OK) |
| 203 | { |
| 204 | BOOST_THROW_EXCEPTION (Error::Db () |
| 205 | << errmsg_info_str ("Some error with FindStateDifferences")); |
| 206 | } |
| 207 | |
| 208 | res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC); |
| 209 | res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC); |
| 210 | |
| 211 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 212 | { |
| 213 | std::cout << sqlite3_column_text (stmt, 0) << |
| 214 | ": from " << sqlite3_column_int64 (stmt, 1) << |
| 215 | " to " << sqlite3_column_int64 (stmt, 2) << |
| 216 | std::endl; |
| 217 | } |
| 218 | sqlite3_finalize (stmt); |
| 219 | } |