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 | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 23 | #include "logging.h" |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 24 | |
| 25 | using namespace boost; |
| 26 | using namespace std; |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 27 | using namespace Ccnx; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 29 | INIT_LOGGER ("ActionLog"); |
| 30 | |
| 31 | const std::string INIT_DATABASE = "\ |
| 32 | CREATE TABLE ActionLog ( \n\ |
| 33 | device_id INTEGER NOT NULL, \n\ |
| 34 | seq_no INTEGER NOT NULL, \n\ |
| 35 | \n\ |
| 36 | action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\ |
| 37 | filename TEXT NOT NULL, \n\ |
| 38 | \n\ |
| 39 | version INTEGER NOT NULL, \n\ |
| 40 | action_timestamp TIMESTAMP NOT NULL, \n\ |
| 41 | \n\ |
| 42 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 43 | file_atime TIMESTAMP, \n\ |
| 44 | file_mtime TIMESTAMP, \n\ |
| 45 | file_ctime TIMESTAMP, \n\ |
| 46 | file_chmod INTEGER, \n\ |
| 47 | file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\ |
| 48 | \n\ |
| 49 | parent_device_id INTEGER, \n\ |
| 50 | parent_seq_no INTEGER, \n\ |
| 51 | \n\ |
| 52 | action_name TEXT, \n\ |
| 53 | action_content_object BLOB, \n\ |
| 54 | \n\ |
| 55 | PRIMARY KEY (device_id, seq_no), \n\ |
| 56 | \n\ |
| 57 | FOREIGN KEY (parent_device_id, parent_seq_no) \n\ |
| 58 | REFERENCES ActionLog (device_id, seq_no) \n\ |
| 59 | ON UPDATE RESTRICT \n\ |
| 60 | ON DELETE SET NULL \n\ |
| 61 | ); \n\ |
| 62 | \n\ |
| 63 | CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\ |
| 64 | CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\ |
| 65 | CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\ |
| 66 | \n\ |
| 67 | CREATE TRIGGER ActionLogInsert_trigger \n\ |
| 68 | AFTER INSERT ON ActionLog \n\ |
| 69 | FOR EACH ROW \n\ |
| 70 | WHEN (SELECT device_id \n\ |
| 71 | FROM ActionLog \n\ |
| 72 | WHERE filename=NEW.filename AND \n\ |
| 73 | version > NEW.version) IS NULL AND \n\ |
| 74 | (SELECT a.device_id \n\ |
| 75 | FROM ActionLog a \n\ |
| 76 | LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\ |
| 77 | WHERE filename=NEW.filename AND \n\ |
| 78 | version = NEW.version AND \n\ |
| 79 | a.device_id != NEW.device_id AND \n\ |
| 80 | s.device_name > (SELECT device_name \n\ |
| 81 | FROM SyncNodes \n\ |
| 82 | WHERE device_id=NEW.device_id)) IS NULL \n\ |
| 83 | BEGIN \n\ |
| 84 | SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \ |
| 85 | NEW.device_id, NEW.seq_no, \ |
| 86 | NEW.action,NEW.filename,NEW.file_hash, \ |
| 87 | strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \ |
| 88 | NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \ |
| 89 | END; \n\ |
| 90 | \n\ |
| 91 | CREATE TABLE FileState ( \n\ |
| 92 | type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\ |
| 93 | filename TEXT NOT NULL, \n\ |
| 94 | device_id INTEGER NOT NULL, \n\ |
| 95 | seq_no INTEGER NOT NULL, \n\ |
| 96 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 97 | file_atime TIMESTAMP, \n\ |
| 98 | file_mtime TIMESTAMP, \n\ |
| 99 | file_ctime TIMESTAMP, \n\ |
| 100 | file_chmod INTEGER, \n\ |
| 101 | file_seg_num INTEGER, \n\ |
| 102 | \n\ |
| 103 | PRIMARY KEY (type, filename) \n\ |
| 104 | ); \n\ |
| 105 | \n\ |
| 106 | CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\ |
| 107 | "; |
| 108 | |
| 109 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 110 | ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path, |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 111 | SyncLogPtr syncLog, |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 112 | const std::string &localName, const std::string &sharedFolder) |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 113 | : DbHelper (path) |
| 114 | , m_syncLog (syncLog) |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 115 | , m_ccnx (ccnx) |
| 116 | , m_sharedFolderName (sharedFolder) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 118 | sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL); |
| 119 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 120 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 121 | int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this), |
| 122 | ActionLog::apply_action_xFun, |
| 123 | 0, 0); |
| 124 | if (res != SQLITE_OK) |
| 125 | { |
| 126 | BOOST_THROW_EXCEPTION (Error::Db () |
| 127 | << errmsg_info_str ("Cannot create function ``apply_action''")); |
| 128 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 131 | tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string> |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 132 | ActionLog::GetExistingRecord (const std::string &filename) |
| 133 | { |
| 134 | // check if something already exists |
| 135 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 136 | int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name " |
| 137 | "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] | 138 | "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 139 | |
| 140 | if (res != SQLITE_OK) |
| 141 | { |
| 142 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 143 | << errmsg_info_str ("Some error with GetExistingRecord")); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 146 | // with parent with version number + 1 |
| 147 | sqlite3_int64 version = 0; |
| 148 | sqlite3_int64 parent_device_id = -1; |
| 149 | sqlite3_int64 parent_seq_no = -1; |
| 150 | string parent_device_name; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 152 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 153 | if (sqlite3_step (stmt) == SQLITE_ROW) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 154 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 155 | version = sqlite3_column_int64 (stmt, 0) + 1; |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 157 | if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted |
| 158 | { |
| 159 | parent_device_id = sqlite3_column_int64 (stmt, 1); |
| 160 | parent_seq_no = sqlite3_column_int64 (stmt, 2); |
Alexander Afanasyev | 707f6df | 2013-01-04 22:46:15 -0800 | [diff] [blame] | 161 | parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 4)), sqlite3_column_bytes (stmt, 4)); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 162 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 163 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 165 | sqlite3_finalize (stmt); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 166 | 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] | 167 | } |
| 168 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 169 | // local add action. remote action is extracted from content object |
| 170 | void |
| 171 | ActionLog::AddActionUpdate (const std::string &filename, |
| 172 | const Hash &hash, |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 173 | time_t wtime, |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 174 | int mode, |
| 175 | int seg_num) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 177 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 179 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo (); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 180 | sqlite3_int64 version = 0; |
| 181 | sqlite3_int64 parent_device_id = -1; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 182 | string parent_device_name; |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 183 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 185 | sqlite3_int64 action_time = time (0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 186 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 187 | tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 189 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 190 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 191 | "(device_id, seq_no, action, filename, version, action_timestamp, " |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 192 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 193 | "parent_device_id, parent_seq_no, " |
| 194 | "action_name, action_content_object) " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 195 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 196 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?," |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 197 | " ?, ?, " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 198 | " ?, ?);", -1, &stmt, 0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 199 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 200 | // cout << "INSERT INTO ActionLog " |
| 201 | // "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 202 | // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, " |
| 203 | // "parent_device_id, parent_seq_no) " |
| 204 | // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 205 | // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?," |
| 206 | // " ?, ?)" << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 208 | if (res != SQLITE_OK) |
| 209 | { |
| 210 | BOOST_THROW_EXCEPTION (Error::Db () |
| 211 | << errmsg_info_str (sqlite3_errmsg (m_db)) |
| 212 | ); |
| 213 | // << errmsg_info_str ("Some error with prepare AddActionUpdate")); |
| 214 | } |
| 215 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 217 | sqlite3_bind_int64 (stmt, 1, m_syncLog->GetLocalSyncNodeId ()); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 218 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 219 | sqlite3_bind_int (stmt, 3, 0); |
| 220 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 221 | sqlite3_bind_int64 (stmt, 5, version); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 222 | sqlite3_bind_int64 (stmt, 6, action_time); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 223 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 224 | sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 225 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 226 | // sqlite3_bind_int64 (stmt, 8, atime); // NULL |
| 227 | sqlite3_bind_int64 (stmt, 9, wtime); |
| 228 | // sqlite3_bind_int64 (stmt, 10, ctime); // NULL |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 229 | sqlite3_bind_int (stmt, 11, mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 230 | sqlite3_bind_int (stmt, 12, seg_num); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 231 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 232 | if (parent_device_id > 0 && parent_seq_no > 0) |
| 233 | { |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 234 | sqlite3_bind_int64 (stmt, 13, parent_device_id); |
| 235 | sqlite3_bind_int64 (stmt, 14, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 239 | sqlite3_bind_null (stmt, 13); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 240 | sqlite3_bind_null (stmt, 14); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 241 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 242 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 243 | // missing part: creating ContentObject for the action !!! |
| 244 | |
| 245 | ActionItem item; |
| 246 | item.set_action (ActionItem::UPDATE); |
| 247 | item.set_filename (filename); |
| 248 | item.set_version (version); |
| 249 | item.set_timestamp (action_time); |
| 250 | item.set_file_hash (hash.GetHash (), hash.GetHashBytes ()); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 251 | // item.set_atime (atime); |
| 252 | item.set_mtime (wtime); |
| 253 | // item.set_ctime (ctime); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 254 | item.set_mode (mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 255 | item.set_seg_num (seg_num); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 256 | |
| 257 | if (parent_device_id > 0 && parent_seq_no > 0) |
| 258 | { |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 259 | cout << Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()), |
| 260 | parent_device_name.size ()) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 261 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 262 | item.set_parent_device_name (parent_device_name); |
| 263 | item.set_parent_seq_no (parent_seq_no); |
| 264 | } |
| 265 | |
| 266 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 267 | |
| 268 | string item_msg; |
| 269 | item.SerializeToString (&item_msg); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 270 | Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 271 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 272 | Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ()); |
| 273 | CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 274 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 275 | sqlite3_bind_blob (stmt, 14, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT); |
| 276 | sqlite3_bind_blob (stmt, 15, &actionData[0], actionData.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 277 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 278 | sqlite3_step (stmt); |
| 279 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 280 | sqlite3_finalize (stmt); |
| 281 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 282 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void |
| 286 | ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile) |
| 287 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 288 | // not supported yet |
| 289 | BOOST_THROW_EXCEPTION (Error::Db () |
| 290 | << errmsg_info_str ("Move operation is not yet supported")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void |
| 294 | ActionLog::AddActionDelete (const std::string &filename) |
| 295 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 296 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 297 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 298 | sqlite3_int64 version = 0; |
| 299 | sqlite3_int64 parent_device_id = -1; |
| 300 | string parent_device_name; |
| 301 | sqlite3_int64 parent_seq_no = -1; |
| 302 | |
| 303 | sqlite3_int64 action_time = time (0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 304 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 305 | tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename); |
| 306 | if (parent_device_id < 0) // no records exist or file was already deleted |
| 307 | { |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 308 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 312 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo (); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 313 | |
| 314 | sqlite3_stmt *stmt; |
| 315 | sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 316 | "(device_id, seq_no, action, filename, version, action_timestamp, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 317 | "parent_device_id, parent_seq_no, " |
| 318 | "action_name, action_content_object) " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 319 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 320 | " ?, ?," |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 321 | " ?, ?)", -1, &stmt, 0); |
| 322 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 323 | sqlite3_bind_int64 (stmt, 1, m_syncLog->GetLocalSyncNodeId ()); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 324 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 325 | sqlite3_bind_int (stmt, 3, 1); |
| 326 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
| 327 | sqlite3_bind_int64 (stmt, 5, version); |
| 328 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 329 | |
| 330 | sqlite3_bind_int64 (stmt, 7, parent_device_id); |
| 331 | sqlite3_bind_int64 (stmt, 8, parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 332 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 333 | |
| 334 | ActionItem item; |
| 335 | item.set_action (ActionItem::UPDATE); |
| 336 | item.set_filename (filename); |
| 337 | item.set_version (version); |
| 338 | item.set_timestamp (action_time); |
| 339 | item.set_parent_device_name (parent_device_name); |
| 340 | item.set_parent_seq_no (parent_seq_no); |
| 341 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 342 | string item_msg; |
| 343 | item.SerializeToString (&item_msg); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame^] | 344 | Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 345 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 346 | Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ()); |
| 347 | CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 348 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 349 | sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT); |
| 350 | sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 351 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 352 | sqlite3_step (stmt); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 353 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 354 | // cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()), |
| 355 | // parent_device_name.size ()) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 356 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 357 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 358 | |
| 359 | sqlite3_finalize (stmt); |
| 360 | |
| 361 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 362 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 363 | |
| 364 | |
| 365 | void |
| 366 | ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 367 | { |
| 368 | ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context)); |
| 369 | |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 370 | if (argc != 11) |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 371 | { |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 372 | sqlite3_result_error (context, "``apply_action'' expects 11 arguments", -1); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 373 | return; |
| 374 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 375 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 376 | // cout << "apply_function called with " << argc << endl; |
| 377 | |
| 378 | // cout << "device_name: " << sqlite3_value_text (argv[0]) << endl; |
| 379 | // cout << "action: " << sqlite3_value_int (argv[1]) << endl; |
| 380 | // cout << "filename: " << sqlite3_value_text (argv[2]) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 381 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 382 | string device_name (reinterpret_cast<const char*> (sqlite3_value_blob (argv[0])), sqlite3_value_bytes (argv[0])); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 383 | sqlite3_int64 device_id = sqlite3_value_int64 (argv[1]); |
| 384 | sqlite3_int64 seq_no = sqlite3_value_int64 (argv[2]); |
| 385 | int action = sqlite3_value_int (argv[3]); |
| 386 | string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[4])); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 387 | |
| 388 | if (action == 0) // update |
| 389 | { |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 390 | Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5])); |
| 391 | time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6])); |
| 392 | time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7])); |
| 393 | time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8])); |
| 394 | int mode = sqlite3_value_int (argv[9]); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 395 | int seg_num = sqlite3_value_int (argv[10]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 396 | |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 397 | cout << "Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash << endl; |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 398 | |
| 399 | sqlite3_stmt *stmt; |
| 400 | sqlite3_prepare_v2 (the->m_db, "UPDATE FileState " |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 401 | "SET " |
| 402 | "device_id=?, seq_no=?, " |
| 403 | "file_hash=?," |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 404 | "file_atime=datetime(?, 'unixepoch')," |
| 405 | "file_mtime=datetime(?, 'unixepoch')," |
| 406 | "file_ctime=datetime(?, 'unixepoch')," |
| 407 | "file_chmod=? " |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 408 | "file_seg_num=? " |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 409 | "WHERE type=0 AND filename=?", -1, &stmt, 0); |
| 410 | |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 411 | sqlite3_bind_int64 (stmt, 1, device_id); |
| 412 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 413 | sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 414 | sqlite3_bind_int64 (stmt, 4, atime); |
| 415 | sqlite3_bind_int64 (stmt, 5, mtime); |
| 416 | sqlite3_bind_int64 (stmt, 6, ctime); |
| 417 | sqlite3_bind_int (stmt, 7, mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 418 | sqlite3_bind_int (stmt, 8, seg_num); |
| 419 | sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 420 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 421 | sqlite3_step (stmt); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 422 | |
| 423 | // cout << sqlite3_errmsg (the->m_db) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 424 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 425 | sqlite3_finalize (stmt); |
| 426 | |
| 427 | int affected_rows = sqlite3_changes (the->m_db); |
| 428 | if (affected_rows == 0) // file didn't exist |
| 429 | { |
| 430 | sqlite3_stmt *stmt; |
| 431 | sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState " |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 432 | "(type,filename,device_id,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod) " |
| 433 | "VALUES (0, ?, ?, ?, ?, " |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 434 | "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", -1, &stmt, 0); |
| 435 | |
| 436 | sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 437 | sqlite3_bind_int64 (stmt, 2, device_id); |
| 438 | sqlite3_bind_int64 (stmt, 3, seq_no); |
| 439 | sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 440 | sqlite3_bind_int64 (stmt, 5, atime); |
| 441 | sqlite3_bind_int64 (stmt, 6, mtime); |
| 442 | sqlite3_bind_int64 (stmt, 7, ctime); |
| 443 | sqlite3_bind_int (stmt, 8, mode); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 444 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 445 | sqlite3_step (stmt); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 446 | // cout << sqlite3_errmsg (the->m_db) << endl; |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 447 | sqlite3_finalize (stmt); |
| 448 | } |
| 449 | } |
| 450 | else if (action == 1) // delete |
| 451 | { |
| 452 | sqlite3_stmt *stmt; |
| 453 | sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0); |
| 454 | sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC); |
| 455 | |
| 456 | cout << "Delete " << filename << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 457 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 458 | sqlite3_step (stmt); |
| 459 | sqlite3_finalize (stmt); |
| 460 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 461 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 462 | sqlite3_result_null (context); |
| 463 | } |