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