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 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 25 | #include <boost/make_shared.hpp> |
| 26 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 27 | using namespace boost; |
| 28 | using namespace std; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 29 | using namespace Ndnx; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 31 | INIT_LOGGER ("ActionLog"); |
| 32 | |
| 33 | const std::string INIT_DATABASE = "\ |
| 34 | CREATE TABLE ActionLog ( \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 35 | device_name BLOB NOT NULL, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 36 | seq_no INTEGER NOT NULL, \n\ |
| 37 | \n\ |
| 38 | action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\ |
| 39 | filename TEXT NOT NULL, \n\ |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 40 | directory TEXT, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 41 | \n\ |
| 42 | version INTEGER NOT NULL, \n\ |
| 43 | action_timestamp TIMESTAMP NOT NULL, \n\ |
| 44 | \n\ |
| 45 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 46 | file_atime TIMESTAMP, \n\ |
| 47 | file_mtime TIMESTAMP, \n\ |
| 48 | file_ctime TIMESTAMP, \n\ |
| 49 | file_chmod INTEGER, \n\ |
| 50 | file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\ |
| 51 | \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 52 | parent_device_name BLOB, \n\ |
| 53 | parent_seq_no INTEGER, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 54 | \n\ |
| 55 | action_name TEXT, \n\ |
| 56 | action_content_object BLOB, \n\ |
| 57 | \n\ |
Alexander Afanasyev | 785b0a0 | 2013-01-30 10:17:47 -0800 | [diff] [blame] | 58 | PRIMARY KEY (device_name, seq_no) \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 59 | ); \n\ |
| 60 | \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 61 | CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\ |
| 62 | CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 63 | CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\ |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 64 | CREATE INDEX ActionLog_filename_version_hash ON ActionLog (filename,version,file_hash); \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 65 | \n\ |
| 66 | CREATE TRIGGER ActionLogInsert_trigger \n\ |
| 67 | AFTER INSERT ON ActionLog \n\ |
| 68 | FOR EACH ROW \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 69 | WHEN (SELECT device_name \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 70 | FROM ActionLog \n\ |
| 71 | WHERE filename=NEW.filename AND \n\ |
| 72 | version > NEW.version) IS NULL AND \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 73 | (SELECT device_name \n\ |
| 74 | FROM ActionLog \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 75 | WHERE filename=NEW.filename AND \n\ |
| 76 | version = NEW.version AND \n\ |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 77 | device_name > NEW.device_name) IS NULL \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 78 | BEGIN \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 79 | SELECT apply_action (NEW.device_name, NEW.seq_no, \ |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 80 | NEW.action,NEW.filename,NEW.version,NEW.file_hash, \ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 81 | strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \ |
| 82 | NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \ |
| 83 | END; \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 84 | "; |
| 85 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 86 | // static void xTrace (void*, const char* q) |
| 87 | // { |
| 88 | // _LOG_TRACE ("SQLITE: " << q); |
| 89 | // } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 91 | ActionLog::ActionLog (Ndnx::NdnxWrapperPtr ndnx, const boost::filesystem::path &path, |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 92 | SyncLogPtr syncLog, |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 93 | const std::string &sharedFolder, const std::string &appName, |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 94 | OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 95 | : DbHelper (path / ".chronoshare", "action-log.db") |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 96 | , m_syncLog (syncLog) |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 97 | , m_ndnx (ndnx) |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 98 | , m_sharedFolderName (sharedFolder) |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 99 | , m_appName (appName) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 100 | , m_onFileAddedOrChanged (onFileAddedOrChanged) |
| 101 | , m_onFileRemoved (onFileRemoved) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 785b0a0 | 2013-01-30 10:17:47 -0800 | [diff] [blame] | 103 | sqlite3_exec (m_db, "PRAGMA foreign_keys = OFF", NULL, NULL, NULL); |
| 104 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 105 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 106 | sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL); |
| 107 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 108 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 109 | int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this), |
| 110 | ActionLog::apply_action_xFun, |
| 111 | 0, 0); |
| 112 | if (res != SQLITE_OK) |
| 113 | { |
| 114 | BOOST_THROW_EXCEPTION (Error::Db () |
| 115 | << errmsg_info_str ("Cannot create function ``apply_action''")); |
| 116 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 118 | m_fileState = make_shared<FileState> (path); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 121 | tuple<sqlite3_int64 /*version*/, Ndnx::NdnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/> |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 122 | ActionLog::GetLatestActionForFile (const std::string &filename) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 123 | { |
| 124 | // check if something already exists |
| 125 | sqlite3_stmt *stmt; |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 126 | int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action " |
| 127 | "FROM ActionLog " |
| 128 | "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 129 | |
| 130 | if (res != SQLITE_OK) |
| 131 | { |
| 132 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 133 | << errmsg_info_str ("Some error with GetExistingRecord")); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 136 | sqlite3_int64 version = -1; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 137 | NdnxCharbufPtr parent_device_name; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 138 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 140 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 141 | if (sqlite3_step (stmt) == SQLITE_ROW) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 142 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 143 | version = sqlite3_column_int64 (stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 145 | if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted |
| 146 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 147 | parent_device_name = make_shared<NdnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1)); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 148 | parent_seq_no = sqlite3_column_int64 (stmt, 2); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 149 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 150 | } |
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_finalize (stmt); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 153 | return make_tuple (version, parent_device_name, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 156 | // local add action. remote action is extracted from content object |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 157 | ActionItemPtr |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 158 | ActionLog::AddLocalActionUpdate (const std::string &filename, |
| 159 | const Hash &hash, |
| 160 | time_t wtime, |
| 161 | int mode, |
| 162 | int seg_num) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 163 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 164 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 166 | NdnxCharbufPtr device_name = m_syncLog->GetLocalName ().toNdnxCharbuf (); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 167 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo (); |
| 168 | sqlite3_int64 version; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 169 | NdnxCharbufPtr parent_device_name; |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 170 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 172 | sqlite3_int64 action_time = time (0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 174 | tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename); |
| 175 | version ++; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 177 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 178 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 179 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 180 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 181 | "parent_device_name, parent_seq_no, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 182 | "action_name, action_content_object) " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 183 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 184 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 185 | " ?, ?, " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 186 | " ?, ?);", -1, &stmt, 0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 188 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 189 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 190 | if (res != SQLITE_OK) |
| 191 | { |
| 192 | BOOST_THROW_EXCEPTION (Error::Db () |
| 193 | << errmsg_info_str (sqlite3_errmsg (m_db)) |
| 194 | ); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 197 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 198 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 199 | sqlite3_bind_int (stmt, 3, 0); |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 200 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_STATIC); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 201 | sqlite3_bind_int64 (stmt, 5, version); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 202 | sqlite3_bind_int64 (stmt, 6, action_time); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 203 | |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 204 | sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 206 | // sqlite3_bind_int64 (stmt, 8, atime); // NULL |
| 207 | sqlite3_bind_int64 (stmt, 9, wtime); |
| 208 | // sqlite3_bind_int64 (stmt, 10, ctime); // NULL |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 209 | sqlite3_bind_int (stmt, 11, mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 210 | sqlite3_bind_int (stmt, 12, seg_num); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 212 | if (parent_device_name && parent_seq_no > 0) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 213 | { |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 214 | sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_STATIC); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 215 | sqlite3_bind_int64 (stmt, 14, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 216 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 217 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 218 | ActionItemPtr item = make_shared<ActionItem> (); |
| 219 | item->set_action (ActionItem::UPDATE); |
| 220 | item->set_filename (filename); |
| 221 | item->set_version (version); |
| 222 | item->set_timestamp (action_time); |
| 223 | item->set_file_hash (hash.GetHash (), hash.GetHashBytes ()); |
| 224 | // item->set_atime (atime); |
| 225 | item->set_mtime (wtime); |
| 226 | // item->set_ctime (ctime); |
| 227 | item->set_mode (mode); |
| 228 | item->set_seg_num (seg_num); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 229 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 230 | if (parent_device_name && parent_seq_no > 0) |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 231 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 232 | // cout << Name (*parent_device_name) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 233 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 234 | item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ()); |
| 235 | item->set_parent_seq_no (parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 239 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 241 | string item_msg; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 242 | item->SerializeToString (&item_msg); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 243 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 244 | // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
| 245 | Name actionName = Name ("/")(m_syncLog->GetLocalName ())(m_appName)("action")(m_sharedFolderName)(seq_no); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 246 | _LOG_DEBUG ("ActionName: " << actionName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 247 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 248 | Bytes actionData = m_ndnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ()); |
| 249 | NdnxCharbufPtr namePtr = actionName.toNdnxCharbuf (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 250 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 251 | // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ()); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 252 | |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 253 | sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC); |
| 254 | sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 255 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 256 | sqlite3_step (stmt); |
| 257 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 258 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 259 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 260 | sqlite3_finalize (stmt); |
| 261 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 262 | // I had a problem including directory_name assignment as part of the initial insert. |
| 263 | sqlite3_prepare_v2 (m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0); |
| 264 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 265 | |
| 266 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
| 267 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 268 | sqlite3_step (stmt); |
| 269 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 270 | |
| 271 | sqlite3_finalize (stmt); |
| 272 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 273 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 274 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 275 | // set complete for local file |
| 276 | m_fileState->SetFileComplete(filename); |
| 277 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 278 | return item; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 281 | // void |
| 282 | // ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile) |
| 283 | // { |
| 284 | // // not supported yet |
| 285 | // BOOST_THROW_EXCEPTION (Error::Db () |
| 286 | // << errmsg_info_str ("Move operation is not yet supported")); |
| 287 | // } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 288 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 289 | ActionItemPtr |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 290 | ActionLog::AddLocalActionDelete (const std::string &filename) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 291 | { |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 292 | _LOG_DEBUG ("Adding local action DELETE"); |
| 293 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 294 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 295 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 296 | NdnxCharbufPtr device_name = m_syncLog->GetLocalName ().toNdnxCharbuf (); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 297 | sqlite3_int64 version; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 298 | NdnxCharbufPtr parent_device_name; |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 299 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 300 | |
| 301 | sqlite3_int64 action_time = time (0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 302 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 303 | tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 304 | if (!parent_device_name) // no records exist or file was already deleted |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 305 | { |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 306 | _LOG_DEBUG ("Nothing to delete... [" << filename << "]"); |
| 307 | |
| 308 | // just in case, remove data from FileState |
| 309 | sqlite3_stmt *stmt; |
| 310 | sqlite3_prepare_v2 (m_db, "DELETE FROM FileState WHERE filename = ? ", -1, &stmt, 0); |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 311 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); // file |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 312 | |
| 313 | sqlite3_step (stmt); |
| 314 | |
| 315 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 316 | |
| 317 | sqlite3_finalize (stmt); |
| 318 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 319 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 320 | return ActionItemPtr (); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 321 | } |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 322 | version ++; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 323 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 324 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo (); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 325 | |
| 326 | sqlite3_stmt *stmt; |
| 327 | sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 328 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 329 | "parent_device_name, parent_seq_no, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 330 | "action_name, action_content_object) " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 331 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 332 | " ?, ?," |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 333 | " ?, ?)", -1, &stmt, 0); |
| 334 | |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 335 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 336 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 337 | sqlite3_bind_int (stmt, 3, 1); |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 338 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_STATIC); // file |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 339 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 340 | sqlite3_bind_int64 (stmt, 5, version); |
| 341 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 342 | |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 343 | sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_STATIC); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 344 | sqlite3_bind_int64 (stmt, 8, parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 345 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 346 | ActionItemPtr item = make_shared<ActionItem> (); |
Alexander Afanasyev | 6d4befc | 2013-01-26 00:01:28 -0800 | [diff] [blame] | 347 | item->set_action (ActionItem::DELETE); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 348 | item->set_filename (filename); |
| 349 | item->set_version (version); |
| 350 | item->set_timestamp (action_time); |
| 351 | item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ()); |
| 352 | item->set_parent_seq_no (parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 353 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 354 | string item_msg; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 355 | item->SerializeToString (&item_msg); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 356 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 357 | // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
| 358 | Name actionName = Name ("/")(m_syncLog->GetLocalName ())(m_appName)("action")(m_sharedFolderName)(seq_no); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 359 | _LOG_DEBUG ("ActionName: " << actionName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 360 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 361 | Bytes actionData = m_ndnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ()); |
| 362 | NdnxCharbufPtr namePtr = actionName.toNdnxCharbuf (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 363 | |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 364 | sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_STATIC); |
| 365 | sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 366 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 367 | sqlite3_step (stmt); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 368 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 369 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 370 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 371 | // cout << Ndnx::Name (parent_device_name) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 372 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 373 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 374 | |
| 375 | sqlite3_finalize (stmt); |
| 376 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 377 | // I had a problem including directory_name assignment as part of the initial insert. |
| 378 | sqlite3_prepare_v2 (m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0); |
| 379 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 380 | |
| 381 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
| 382 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 383 | sqlite3_step (stmt); |
| 384 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 385 | |
| 386 | sqlite3_finalize (stmt); |
| 387 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 388 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 389 | |
| 390 | return item; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 391 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 392 | |
| 393 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 394 | PcoPtr |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 395 | ActionLog::LookupActionPco (const Ndnx::Name &deviceName, sqlite3_int64 seqno) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 396 | { |
| 397 | sqlite3_stmt *stmt; |
| 398 | sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0); |
| 399 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 400 | NdnxCharbufPtr name = deviceName.toNdnxCharbuf (); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 401 | |
| 402 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
| 403 | sqlite3_bind_int64 (stmt, 2, seqno); |
| 404 | |
| 405 | PcoPtr retval; |
| 406 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 407 | { |
| 408 | // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0)); |
| 409 | retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno); |
| 414 | } |
| 415 | // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
| 416 | sqlite3_finalize (stmt); |
| 417 | |
| 418 | return retval; |
| 419 | } |
| 420 | |
| 421 | ActionItemPtr |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 422 | ActionLog::LookupAction (const Ndnx::Name &deviceName, sqlite3_int64 seqno) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 423 | { |
| 424 | PcoPtr pco = LookupActionPco (deviceName, seqno); |
| 425 | if (!pco) return ActionItemPtr (); |
| 426 | |
| 427 | ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ()); |
| 428 | |
| 429 | return action; |
| 430 | } |
| 431 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 432 | Ndnx::PcoPtr |
| 433 | ActionLog::LookupActionPco (const Ndnx::Name &actionName) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 434 | { |
| 435 | sqlite3_stmt *stmt; |
| 436 | sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0); |
| 437 | |
| 438 | _LOG_DEBUG (actionName); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 439 | NdnxCharbufPtr name = actionName.toNdnxCharbuf (); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 440 | |
| 441 | _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ()); |
| 442 | |
| 443 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
| 444 | |
| 445 | PcoPtr retval; |
| 446 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 447 | { |
| 448 | // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0)); |
| 449 | retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | _LOG_TRACE ("No action found for name: " << actionName); |
| 454 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 455 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 456 | sqlite3_finalize (stmt); |
| 457 | |
| 458 | return retval; |
| 459 | } |
| 460 | |
| 461 | ActionItemPtr |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 462 | ActionLog::LookupAction (const Ndnx::Name &actionName) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 463 | { |
| 464 | PcoPtr pco = LookupActionPco (actionName); |
| 465 | if (!pco) return ActionItemPtr (); |
| 466 | |
| 467 | ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ()); |
| 468 | |
| 469 | return action; |
| 470 | } |
| 471 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 472 | FileItemPtr |
| 473 | ActionLog::LookupAction (const std::string &filename, sqlite3_int64 version, const Hash &filehash) |
| 474 | { |
| 475 | sqlite3_stmt *stmt; |
| 476 | sqlite3_prepare_v2 (m_db, |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 477 | "SELECT device_name, seq_no, strftime('%s', file_mtime), file_chmod, file_seg_num, file_hash " |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 478 | " FROM ActionLog " |
| 479 | " WHERE action = 0 AND " |
| 480 | " filename=? AND " |
| 481 | " version=? AND " |
| 482 | " is_prefix (?, file_hash)=1", -1, &stmt, 0); |
| 483 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 484 | |
| 485 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); |
| 486 | sqlite3_bind_int64 (stmt, 2, version); |
| 487 | sqlite3_bind_blob (stmt, 3, filehash.GetHash (), filehash.GetHashBytes (), SQLITE_STATIC); |
| 488 | |
| 489 | FileItemPtr fileItem; |
| 490 | |
| 491 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 492 | { |
| 493 | fileItem = make_shared<FileItem> (); |
| 494 | fileItem->set_filename (filename); |
| 495 | fileItem->set_device_name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)); |
| 496 | fileItem->set_seq_no (sqlite3_column_int64 (stmt, 1)); |
| 497 | fileItem->set_mtime (sqlite3_column_int64 (stmt, 2)); |
| 498 | fileItem->set_mode (sqlite3_column_int64 (stmt, 3)); |
| 499 | fileItem->set_seg_num (sqlite3_column_int64 (stmt, 4)); |
| 500 | |
| 501 | fileItem->set_file_hash (sqlite3_column_blob (stmt, 5), sqlite3_column_bytes (stmt, 5)); |
| 502 | } |
| 503 | |
| 504 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE || sqlite3_errcode (m_db) != SQLITE_ROW || sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 505 | |
| 506 | return fileItem; |
| 507 | } |
| 508 | |
| 509 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 510 | ActionItemPtr |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 511 | ActionLog::AddRemoteAction (const Ndnx::Name &deviceName, sqlite3_int64 seqno, Ndnx::PcoPtr actionPco) |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 512 | { |
| 513 | if (!actionPco) |
| 514 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 515 | _LOG_ERROR ("actionPco is not valid"); |
| 516 | return ActionItemPtr (); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 517 | } |
| 518 | ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ()); |
| 519 | |
| 520 | if (!action) |
| 521 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 522 | _LOG_ERROR ("action cannot be decoded"); |
| 523 | return ActionItemPtr (); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 526 | _LOG_DEBUG ("AddRemoteAction: [" << deviceName << "] seqno: " << seqno); |
| 527 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 528 | sqlite3_stmt *stmt; |
| 529 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 530 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 531 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " |
| 532 | "parent_device_name, parent_seq_no, " |
| 533 | "action_name, action_content_object) " |
| 534 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 535 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " |
| 536 | " ?, ?, " |
| 537 | " ?, ?);", -1, &stmt, 0); |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 538 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 539 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 540 | NdnxCharbufPtr device_name = deviceName.toNdnxCharbuf (); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 541 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
| 542 | sqlite3_bind_int64 (stmt, 2, seqno); |
| 543 | |
| 544 | sqlite3_bind_int (stmt, 3, action->action ()); |
| 545 | sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC); |
| 546 | sqlite3_bind_int64 (stmt, 5, action->version ()); |
| 547 | sqlite3_bind_int64 (stmt, 6, action->timestamp ()); |
| 548 | |
| 549 | if (action->action () == ActionItem::UPDATE) |
| 550 | { |
| 551 | sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC); |
| 552 | |
| 553 | // sqlite3_bind_int64 (stmt, 8, atime); // NULL |
| 554 | sqlite3_bind_int64 (stmt, 9, action->mtime ()); |
| 555 | // sqlite3_bind_int64 (stmt, 10, ctime); // NULL |
| 556 | |
| 557 | sqlite3_bind_int (stmt, 11, action->mode ()); |
| 558 | sqlite3_bind_int (stmt, 12, action->seg_num ()); |
| 559 | } |
| 560 | |
| 561 | if (action->has_parent_device_name ()) |
| 562 | { |
Alexander Afanasyev | 2b760d0 | 2013-02-07 13:47:29 -0800 | [diff] [blame] | 563 | sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_STATIC); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 564 | sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ()); |
| 565 | } |
| 566 | |
| 567 | Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 568 | NdnxCharbufPtr namePtr = actionName.toNdnxCharbuf (); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 569 | |
| 570 | sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC); |
| 571 | sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC); |
| 572 | sqlite3_step (stmt); |
| 573 | |
| 574 | // if action needs to be applied to file state, the trigger will take care of it |
| 575 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 576 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 577 | |
| 578 | sqlite3_finalize (stmt); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 579 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 580 | // I had a problem including directory_name assignment as part of the initial insert. |
| 581 | sqlite3_prepare_v2 (m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0); |
| 582 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 583 | |
| 584 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
| 585 | sqlite3_bind_int64 (stmt, 2, seqno); |
| 586 | sqlite3_step (stmt); |
| 587 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 588 | |
| 589 | sqlite3_finalize (stmt); |
| 590 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 591 | return action; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 594 | ActionItemPtr |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 595 | ActionLog::AddRemoteAction (Ndnx::PcoPtr actionPco) |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 596 | { |
| 597 | Name name = actionPco->name (); |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 598 | // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 599 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 600 | uint64_t seqno = name.getCompFromBackAsInt (0); |
| 601 | string sharedFolder = name.getCompFromBackAsString (1); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 602 | |
| 603 | if (sharedFolder != m_sharedFolderName) |
| 604 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 605 | _LOG_ERROR ("Action doesn't belong to this shared folder"); |
| 606 | return ActionItemPtr (); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 609 | string action = name.getCompFromBackAsString (2); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 610 | |
| 611 | if (action != "action") |
| 612 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 613 | _LOG_ERROR ("not an action"); |
| 614 | return ActionItemPtr (); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 615 | } |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 616 | |
| 617 | string appName = name.getCompFromBackAsString (3); |
| 618 | if (appName != m_appName) |
| 619 | { |
| 620 | _LOG_ERROR ("Action doesn't belong to this application"); |
| 621 | return ActionItemPtr (); |
| 622 | } |
| 623 | |
| 624 | Name deviceName = name.getPartialName (0, name.size ()-4); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 625 | |
| 626 | _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno); |
| 627 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 628 | return AddRemoteAction (deviceName, seqno, actionPco); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 631 | sqlite3_int64 |
| 632 | ActionLog::LogSize () |
| 633 | { |
| 634 | sqlite3_stmt *stmt; |
| 635 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0); |
| 636 | |
| 637 | sqlite3_int64 retval = -1; |
| 638 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 639 | { |
| 640 | retval = sqlite3_column_int64 (stmt, 0); |
| 641 | } |
| 642 | |
| 643 | return retval; |
| 644 | } |
| 645 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 646 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 647 | bool |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 648 | ActionLog::LookupActionsInFolderRecursively (const boost::function<void (const Ndnx::Name &name, sqlite3_int64 seq_no, const ActionItem &)> &visitor, |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 649 | const std::string &folder, int offset/*=0*/, int limit/*=-1*/) |
| 650 | { |
| 651 | _LOG_DEBUG ("LookupActionsInFolderRecursively: [" << folder << "]"); |
| 652 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 653 | if (limit >= 0) |
| 654 | limit += 1; // to check if there is more data |
| 655 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 656 | sqlite3_stmt *stmt; |
| 657 | if (folder != "") |
| 658 | { |
| 659 | /// @todo Do something to improve efficiency of this query. Right now it is basically scanning the whole database |
| 660 | |
| 661 | sqlite3_prepare_v2 (m_db, |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 662 | "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), " |
| 663 | " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, " |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 664 | " parent_device_name,parent_seq_no " |
| 665 | " FROM ActionLog " |
Alexander Afanasyev | 20fd84a | 2013-02-27 12:17:00 -0800 | [diff] [blame] | 666 | " WHERE is_dir_prefix (?, directory)=1 " |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 667 | " ORDER BY action_timestamp DESC " |
| 668 | " LIMIT ? OFFSET ?", -1, &stmt, 0); // there is a small ambiguity with is_prefix matching, but should be ok for now |
| 669 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 670 | |
| 671 | sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC); |
| 672 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 673 | |
| 674 | sqlite3_bind_int (stmt, 2, limit); |
| 675 | sqlite3_bind_int (stmt, 3, offset); |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | sqlite3_prepare_v2 (m_db, |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 680 | "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), " |
| 681 | " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, " |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 682 | " parent_device_name,parent_seq_no " |
| 683 | " FROM ActionLog " |
| 684 | " ORDER BY action_timestamp DESC " |
| 685 | " LIMIT ? OFFSET ?", -1, &stmt, 0); |
| 686 | sqlite3_bind_int (stmt, 1, limit); |
| 687 | sqlite3_bind_int (stmt, 2, offset); |
| 688 | } |
| 689 | |
| 690 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 691 | |
| 692 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 693 | { |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 694 | if (limit == 1) |
| 695 | break; |
| 696 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 697 | ActionItem action; |
| 698 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 699 | Ndnx::Name device_name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 700 | sqlite3_int64 seq_no = sqlite3_column_int64 (stmt, 1); |
| 701 | action.set_action (static_cast<ActionItem_ActionType> (sqlite3_column_int (stmt, 2))); |
| 702 | action.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 3)), sqlite3_column_bytes (stmt, 3)); |
| 703 | std::string directory (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 4)), sqlite3_column_bytes (stmt, 4)); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 704 | action.set_version (sqlite3_column_int64 (stmt, 5)); |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 705 | action.set_timestamp (sqlite3_column_int64 (stmt, 6)); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 706 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 707 | if (action.action () == 0) |
| 708 | { |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 709 | action.set_file_hash (sqlite3_column_blob (stmt, 7), sqlite3_column_bytes (stmt, 7)); |
| 710 | action.set_mtime (sqlite3_column_int (stmt, 8)); |
| 711 | action.set_mode (sqlite3_column_int (stmt, 9)); |
| 712 | action.set_seg_num (sqlite3_column_int64 (stmt, 10)); |
| 713 | } |
| 714 | if (sqlite3_column_bytes (stmt, 11) > 0) |
| 715 | { |
| 716 | action.set_parent_device_name (sqlite3_column_blob (stmt, 11), sqlite3_column_bytes (stmt, 11)); |
| 717 | action.set_parent_seq_no (sqlite3_column_int64 (stmt, 12)); |
| 718 | } |
| 719 | |
| 720 | visitor (device_name, seq_no, action); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 721 | limit --; |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 725 | |
| 726 | sqlite3_finalize (stmt); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 727 | |
| 728 | return (limit == 1); // more data is available |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 731 | /** |
| 732 | * @todo Figure out the way to minimize code duplication |
| 733 | */ |
| 734 | bool |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 735 | ActionLog::LookupActionsForFile (const boost::function<void (const Ndnx::Name &name, sqlite3_int64 seq_no, const ActionItem &)> &visitor, |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 736 | const std::string &file, int offset/*=0*/, int limit/*=-1*/) |
| 737 | { |
| 738 | _LOG_DEBUG ("LookupActionsInFolderRecursively: [" << file << "]"); |
| 739 | if (file.empty ()) |
| 740 | return false; |
| 741 | |
| 742 | if (limit >= 0) |
| 743 | limit += 1; // to check if there is more data |
| 744 | |
| 745 | sqlite3_stmt *stmt; |
| 746 | sqlite3_prepare_v2 (m_db, |
| 747 | "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), " |
| 748 | " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, " |
| 749 | " parent_device_name,parent_seq_no " |
| 750 | " FROM ActionLog " |
| 751 | " WHERE filename=? " |
| 752 | " ORDER BY action_timestamp DESC " |
| 753 | " LIMIT ? OFFSET ?", -1, &stmt, 0); // there is a small ambiguity with is_prefix matching, but should be ok for now |
| 754 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 755 | |
| 756 | sqlite3_bind_text (stmt, 1, file.c_str (), file.size (), SQLITE_STATIC); |
| 757 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 758 | |
| 759 | sqlite3_bind_int (stmt, 2, limit); |
| 760 | sqlite3_bind_int (stmt, 3, offset); |
| 761 | |
| 762 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 763 | |
| 764 | while (sqlite3_step (stmt) == SQLITE_ROW) |
| 765 | { |
| 766 | if (limit == 1) |
| 767 | break; |
| 768 | |
| 769 | ActionItem action; |
| 770 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 771 | Ndnx::Name device_name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 772 | sqlite3_int64 seq_no = sqlite3_column_int64 (stmt, 1); |
| 773 | action.set_action (static_cast<ActionItem_ActionType> (sqlite3_column_int (stmt, 2))); |
| 774 | action.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 3)), sqlite3_column_bytes (stmt, 3)); |
| 775 | std::string directory (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 4)), sqlite3_column_bytes (stmt, 4)); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 776 | action.set_version (sqlite3_column_int64 (stmt, 5)); |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 777 | action.set_timestamp (sqlite3_column_int64 (stmt, 6)); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 778 | |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 779 | if (action.action () == 0) |
| 780 | { |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 781 | action.set_file_hash (sqlite3_column_blob (stmt, 7), sqlite3_column_bytes (stmt, 7)); |
| 782 | action.set_mtime (sqlite3_column_int (stmt, 8)); |
| 783 | action.set_mode (sqlite3_column_int (stmt, 9)); |
| 784 | action.set_seg_num (sqlite3_column_int64 (stmt, 10)); |
| 785 | } |
| 786 | if (sqlite3_column_bytes (stmt, 11) > 0) |
| 787 | { |
| 788 | action.set_parent_device_name (sqlite3_column_blob (stmt, 11), sqlite3_column_bytes (stmt, 11)); |
| 789 | action.set_parent_seq_no (sqlite3_column_int64 (stmt, 12)); |
| 790 | } |
| 791 | |
| 792 | visitor (device_name, seq_no, action); |
| 793 | limit --; |
| 794 | } |
| 795 | |
| 796 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 797 | |
| 798 | sqlite3_finalize (stmt); |
| 799 | |
| 800 | return (limit == 1); // more data is available |
| 801 | } |
| 802 | |
| 803 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 804 | void |
| 805 | ActionLog::LookupRecentFileActions(const boost::function<void (const string &, int, int)> &visitor, int limit) |
| 806 | { |
| 807 | sqlite3_stmt *stmt; |
| 808 | |
| 809 | sqlite3_prepare_v2 (m_db, |
| 810 | "SELECT AL.filename, AL.action" |
| 811 | " FROM ActionLog AL" |
| 812 | " JOIN " |
| 813 | " (SELECT filename, MAX(action_timestamp) AS action_timestamp " |
| 814 | " FROM ActionLog " |
| 815 | " GROUP BY filename ) AS GAL" |
| 816 | " ON AL.filename = GAL.filename AND AL.action_timestamp = GAL.action_timestamp " |
| 817 | " ORDER BY AL.action_timestamp DESC " |
| 818 | " LIMIT ?;", |
| 819 | -1, &stmt, 0); |
| 820 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 821 | sqlite3_bind_int(stmt, 1, limit); |
| 822 | int index = 0; |
| 823 | while (sqlite3_step(stmt) == SQLITE_ROW) |
| 824 | { |
| 825 | std::string filename(reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 826 | int action = sqlite3_column_int (stmt, 1); |
| 827 | visitor(filename, action, index); |
| 828 | index++; |
| 829 | } |
| 830 | |
| 831 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db)); |
| 832 | |
| 833 | sqlite3_finalize (stmt); |
| 834 | } |
| 835 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 836 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 837 | /////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 838 | /////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 839 | /////////////////////////////////////////////////////////////////////////////////// |
| 840 | |
| 841 | void |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 842 | ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 843 | { |
| 844 | ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context)); |
| 845 | |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 846 | if (argc != 11) |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 847 | { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 848 | sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 849 | return; |
| 850 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 851 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 852 | NdnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0])); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 853 | sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]); |
| 854 | int action = sqlite3_value_int (argv[2]); |
| 855 | string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3])); |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 856 | sqlite3_int64 version = sqlite3_value_int64 (argv[4]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 857 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 858 | _LOG_TRACE ("apply_function called with " << argc); |
| 859 | _LOG_TRACE ("device_name: " << Name (device_name) |
| 860 | << ", action: " << action |
| 861 | << ", file: " << filename); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 862 | |
| 863 | if (action == 0) // update |
| 864 | { |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 865 | Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5])); |
| 866 | time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6])); |
| 867 | time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7])); |
| 868 | time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8])); |
| 869 | int mode = sqlite3_value_int (argv[9]); |
| 870 | int seg_num = sqlite3_value_int (argv[10]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 871 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 872 | _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 873 | |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 874 | the->m_fileState->UpdateFile (filename, version, hash, device_name, seq_no, atime, mtime, ctime, mode, seg_num); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 875 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 876 | // no callback here |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 877 | } |
| 878 | else if (action == 1) // delete |
| 879 | { |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 880 | the->m_fileState->DeleteFile (filename); |
Alexander Afanasyev | 0c545ed | 2013-01-26 00:12:32 -0800 | [diff] [blame] | 881 | |
| 882 | the->m_onFileRemoved (filename); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 883 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 884 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 885 | sqlite3_result_null (context); |
| 886 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 887 | |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 888 | |