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) 2012-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 "action-log.h" |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 23 | #include <action-item.pb.h> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 24 | |
| 25 | using namespace boost; |
| 26 | using namespace std; |
| 27 | |
| 28 | ActionLog::ActionLog (const std::string &path, const std::string &localName) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 29 | : SyncLog (path, localName) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 30 | { |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame^] | 31 | int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this), |
| 32 | ActionLog::apply_action_xFun, |
| 33 | 0, 0); |
| 34 | if (res != SQLITE_OK) |
| 35 | { |
| 36 | BOOST_THROW_EXCEPTION (Error::Db () |
| 37 | << errmsg_info_str ("Cannot create function ``apply_action''")); |
| 38 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 41 | tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string> |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 42 | ActionLog::GetExistingRecord (const std::string &filename) |
| 43 | { |
| 44 | // check if something already exists |
| 45 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 46 | int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name " |
| 47 | "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id " |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame^] | 48 | "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 49 | |
| 50 | if (res != SQLITE_OK) |
| 51 | { |
| 52 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 53 | << errmsg_info_str ("Some error with GetExistingRecord")); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 56 | // with parent with version number + 1 |
| 57 | sqlite3_int64 version = 0; |
| 58 | sqlite3_int64 parent_device_id = -1; |
| 59 | sqlite3_int64 parent_seq_no = -1; |
| 60 | string parent_device_name; |
| 61 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 62 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 63 | if (sqlite3_step (stmt) == SQLITE_ROW) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 64 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 65 | version = sqlite3_column_int64 (stmt, 0) + 1; |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 67 | if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted |
| 68 | { |
| 69 | parent_device_id = sqlite3_column_int64 (stmt, 1); |
| 70 | parent_seq_no = sqlite3_column_int64 (stmt, 2); |
| 71 | parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_text (stmt, 4))); |
| 72 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 73 | } |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 75 | sqlite3_finalize (stmt); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 76 | return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 79 | // local add action. remote action is extracted from content object |
| 80 | void |
| 81 | ActionLog::AddActionUpdate (const std::string &filename, |
| 82 | const Hash &hash, |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 83 | time_t atime, time_t mtime, time_t ctime, |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 84 | int mode) |
| 85 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 86 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 87 | |
| 88 | sqlite3_int64 seq_no = GetNextLocalSeqNo (); |
| 89 | sqlite3_int64 version = 0; |
| 90 | sqlite3_int64 parent_device_id = -1; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 91 | string parent_device_name; |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 92 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 94 | sqlite3_int64 action_time = time (0); |
| 95 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 96 | tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 97 | |
| 98 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 99 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 100 | "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 101 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, " |
| 102 | "parent_device_id, parent_seq_no) " |
| 103 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 104 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?," |
| 105 | " ?, ?);", -1, &stmt, 0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 107 | // cout << "INSERT INTO ActionLog " |
| 108 | // "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 109 | // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, " |
| 110 | // "parent_device_id, parent_seq_no) " |
| 111 | // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 112 | // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?," |
| 113 | // " ?, ?)" << endl; |
| 114 | |
| 115 | if (res != SQLITE_OK) |
| 116 | { |
| 117 | BOOST_THROW_EXCEPTION (Error::Db () |
| 118 | << errmsg_info_str (sqlite3_errmsg (m_db)) |
| 119 | ); |
| 120 | // << errmsg_info_str ("Some error with prepare AddActionUpdate")); |
| 121 | } |
| 122 | |
| 123 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 124 | sqlite3_bind_int64 (stmt, 1, m_localDeviceId); |
| 125 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 126 | sqlite3_bind_int (stmt, 3, 0); |
| 127 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 128 | sqlite3_bind_int64 (stmt, 5, version); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 129 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 130 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 131 | sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 132 | |
| 133 | sqlite3_bind_int64 (stmt, 8, atime); |
| 134 | sqlite3_bind_int64 (stmt, 9, mtime); |
| 135 | sqlite3_bind_int64 (stmt, 10, ctime); |
| 136 | sqlite3_bind_int (stmt, 11, mode); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 138 | if (parent_device_id > 0 && parent_seq_no > 0) |
| 139 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 140 | sqlite3_bind_int64 (stmt, 12, parent_device_id); |
| 141 | sqlite3_bind_int64 (stmt, 13, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 142 | } |
| 143 | else |
| 144 | { |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 145 | sqlite3_bind_null (stmt, 12); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 146 | sqlite3_bind_null (stmt, 13); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | sqlite3_step (stmt); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 151 | // missing part: creating ContentObject for the action !!! |
| 152 | |
| 153 | ActionItem item; |
| 154 | item.set_action (ActionItem::UPDATE); |
| 155 | item.set_filename (filename); |
| 156 | item.set_version (version); |
| 157 | item.set_timestamp (action_time); |
| 158 | item.set_file_hash (hash.GetHash (), hash.GetHashBytes ()); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 159 | item.set_atime (atime); |
| 160 | item.set_mtime (mtime); |
| 161 | item.set_ctime (ctime); |
| 162 | item.set_mode (mode); |
| 163 | |
| 164 | if (parent_device_id > 0 && parent_seq_no > 0) |
| 165 | { |
| 166 | item.set_parent_device_name (parent_device_name); |
| 167 | item.set_parent_seq_no (parent_seq_no); |
| 168 | } |
| 169 | |
| 170 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 171 | |
| 172 | sqlite3_finalize (stmt); |
| 173 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 174 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void |
| 178 | ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile) |
| 179 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 180 | // not supported yet |
| 181 | BOOST_THROW_EXCEPTION (Error::Db () |
| 182 | << errmsg_info_str ("Move operation is not yet supported")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
| 186 | ActionLog::AddActionDelete (const std::string &filename) |
| 187 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 188 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
| 189 | |
| 190 | sqlite3_int64 version = 0; |
| 191 | sqlite3_int64 parent_device_id = -1; |
| 192 | string parent_device_name; |
| 193 | sqlite3_int64 parent_seq_no = -1; |
| 194 | |
| 195 | sqlite3_int64 action_time = time (0); |
| 196 | |
| 197 | tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename); |
| 198 | if (parent_device_id < 0) // no records exist or file was already deleted |
| 199 | { |
| 200 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | sqlite3_int64 seq_no = GetNextLocalSeqNo (); |
| 205 | |
| 206 | sqlite3_stmt *stmt; |
| 207 | sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 208 | "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 209 | "parent_device_id, parent_seq_no) " |
| 210 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 211 | " ?, ?)", -1, &stmt, 0); |
| 212 | |
| 213 | sqlite3_bind_int64 (stmt, 1, m_localDeviceId); |
| 214 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 215 | sqlite3_bind_int (stmt, 3, 1); |
| 216 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
| 217 | sqlite3_bind_int64 (stmt, 5, version); |
| 218 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 219 | |
| 220 | sqlite3_bind_int64 (stmt, 7, parent_device_id); |
| 221 | sqlite3_bind_int64 (stmt, 8, parent_seq_no); |
| 222 | |
| 223 | sqlite3_step (stmt); |
| 224 | |
| 225 | // missing part: creating ContentObject for the action !!! |
| 226 | |
| 227 | ActionItem item; |
| 228 | item.set_action (ActionItem::UPDATE); |
| 229 | item.set_filename (filename); |
| 230 | item.set_version (version); |
| 231 | item.set_timestamp (action_time); |
| 232 | item.set_parent_device_name (parent_device_name); |
| 233 | item.set_parent_seq_no (parent_seq_no); |
| 234 | |
| 235 | // assign name to the action, serialize action, and create content object |
| 236 | |
| 237 | sqlite3_finalize (stmt); |
| 238 | |
| 239 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 240 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame^] | 241 | |
| 242 | |
| 243 | void |
| 244 | ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 245 | { |
| 246 | ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context)); |
| 247 | |
| 248 | cout << "apply_function called with " << argc << endl; |
| 249 | |
| 250 | cout << "device_name: " << sqlite3_value_text (argv[0]) << endl; |
| 251 | cout << "action: " << sqlite3_value_int (argv[1]) << endl; |
| 252 | cout << "filename: " << sqlite3_value_text (argv[2]) << endl; |
| 253 | |
| 254 | sqlite3_result_null (context); |
| 255 | } |