blob: 19c5c9d69bb323feaa379b9269d16edc41dc2d1c [file] [log] [blame]
Alexander Afanasyeva199f972013-01-02 19:37:26 -08001/* -*- 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 Afanasyev8e2104a2013-01-22 10:56:18 -080023#include "logging.h"
Alexander Afanasyeva199f972013-01-02 19:37:26 -080024
Alexander Afanasyev0995f322013-01-22 13:16:46 -080025#include <boost/make_shared.hpp>
26
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027using namespace boost;
28using namespace std;
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080029using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080031INIT_LOGGER ("ActionLog");
32
33const std::string INIT_DATABASE = "\
34CREATE TABLE ActionLog ( \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080035 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080036 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\
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 Afanasyev0995f322013-01-22 13:16:46 -080051 parent_device_name BLOB, \n\
52 parent_seq_no INTEGER, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080053 \n\
54 action_name TEXT, \n\
55 action_content_object BLOB, \n\
56 \n\
Alexander Afanasyev785b0a02013-01-30 10:17:47 -080057 PRIMARY KEY (device_name, seq_no) \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080058); \n\
59 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080060CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
61CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080062CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
63 \n\
64CREATE TRIGGER ActionLogInsert_trigger \n\
65 AFTER INSERT ON ActionLog \n\
66 FOR EACH ROW \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080067 WHEN (SELECT device_name \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080068 FROM ActionLog \n\
69 WHERE filename=NEW.filename AND \n\
70 version > NEW.version) IS NULL AND \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080071 (SELECT device_name \n\
72 FROM ActionLog \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080073 WHERE filename=NEW.filename AND \n\
74 version = NEW.version AND \n\
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080075 device_name > NEW.device_name) IS NULL \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080076 BEGIN \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080077 SELECT apply_action (NEW.device_name, NEW.seq_no, \
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080078 NEW.action,NEW.filename,NEW.file_hash, \
79 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
80 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
81 END; \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080082";
83
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080084// static void xTrace (void*, const char* q)
85// {
86// _LOG_TRACE ("SQLITE: " << q);
87// }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080088
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080089ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080090 SyncLogPtr syncLog,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080091 const std::string &sharedFolder, const std::string &appName,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080092 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080093 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080094 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080095 , m_ccnx (ccnx)
96 , m_sharedFolderName (sharedFolder)
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080097 , m_appName (appName)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080098 , m_onFileAddedOrChanged (onFileAddedOrChanged)
99 , m_onFileRemoved (onFileRemoved)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800100{
Alexander Afanasyev785b0a02013-01-30 10:17:47 -0800101 sqlite3_exec (m_db, "PRAGMA foreign_keys = OFF", NULL, NULL, NULL);
102 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
103
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800104 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
105 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
106
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800107 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
108 ActionLog::apply_action_xFun,
109 0, 0);
110 if (res != SQLITE_OK)
111 {
112 BOOST_THROW_EXCEPTION (Error::Db ()
113 << errmsg_info_str ("Cannot create function ``apply_action''"));
114 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800115
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800116 m_fileState = make_shared<FileState> (path);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800117}
118
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800119tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
120ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800121{
122 // check if something already exists
123 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800124 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", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800127
128 if (res != SQLITE_OK)
129 {
130 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800131 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800132 }
133
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800134 sqlite3_int64 version = -1;
135 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800136 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800137
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800138 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800139 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800140 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800141 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800142
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800143 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
144 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800145 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800146 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800147 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800148 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800149
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800150 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800151 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800152}
153
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800154// local add action. remote action is extracted from content object
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800155ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800156ActionLog::AddLocalActionUpdate (const std::string &filename,
157 const Hash &hash,
158 time_t wtime,
159 int mode,
160 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800161{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800162 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800163
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800164 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
165 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
166 sqlite3_int64 version;
167 CcnxCharbufPtr parent_device_name;
168 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800169
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800170 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800171
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800172 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
173 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800174
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800175 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800176 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800177 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800178 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800179 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800180 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800181 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800182 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800183 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800184 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800185
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800186 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
187
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800188 if (res != SQLITE_OK)
189 {
190 BOOST_THROW_EXCEPTION (Error::Db ()
191 << errmsg_info_str (sqlite3_errmsg (m_db))
192 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800193 }
194
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800195 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800196 sqlite3_bind_int64 (stmt, 2, seq_no);
197 sqlite3_bind_int (stmt, 3, 0);
198 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800199 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800200 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800201
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800202 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800203
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800204 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
205 sqlite3_bind_int64 (stmt, 9, wtime);
206 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800207 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800208 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800209
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800210 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800211 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800212 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800213 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800214 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800215
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800216 ActionItemPtr item = make_shared<ActionItem> ();
217 item->set_action (ActionItem::UPDATE);
218 item->set_filename (filename);
219 item->set_version (version);
220 item->set_timestamp (action_time);
221 item->set_file_hash (hash.GetHash (), hash.GetHashBytes ());
222 // item->set_atime (atime);
223 item->set_mtime (wtime);
224 // item->set_ctime (ctime);
225 item->set_mode (mode);
226 item->set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800227
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800228 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800229 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800230 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800231
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800232 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
233 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800234 }
235
236 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800237
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800238
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800239 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800240 item->SerializeToString (&item_msg);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800241
242 // action name: /<appname>/<shared-folder>/action/<device_name>/<action-seq>
243 Name actionName = Name ("/")(m_appName)(m_sharedFolderName)("action")(m_syncLog->GetLocalName ())(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800244 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800245
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800246 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
247 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800248
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800249 // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800250
251 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
252 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800253
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800254 sqlite3_step (stmt);
255
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800256 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800257
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800258 sqlite3_finalize (stmt);
259
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800260 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800261
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800262 // set complete for local file
263 m_fileState->SetFileComplete(filename);
264
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800265 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800266}
267
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800268// void
269// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
270// {
271// // not supported yet
272// BOOST_THROW_EXCEPTION (Error::Db ()
273// << errmsg_info_str ("Move operation is not yet supported"));
274// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800275
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800276ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800277ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800278{
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800279 _LOG_DEBUG ("Adding local action DELETE");
280
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800281 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800282
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800283 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
284 sqlite3_int64 version;
285 CcnxCharbufPtr parent_device_name;
286 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800287
288 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800289
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800290 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800291 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800292 {
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800293 _LOG_DEBUG ("Nothing to delete... [" << filename << "]");
294
295 // just in case, remove data from FileState
296 sqlite3_stmt *stmt;
297 sqlite3_prepare_v2 (m_db, "DELETE FROM FileState WHERE filename = ? ", -1, &stmt, 0);
298 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
299
300 sqlite3_step (stmt);
301
302 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
303
304 sqlite3_finalize (stmt);
305
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800306 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800307 return ActionItemPtr ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800308 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800309 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800310
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800311 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800312
313 sqlite3_stmt *stmt;
314 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800315 "(device_name, seq_no, action, filename, version, action_timestamp, "
316 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800317 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800318 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800319 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800320 " ?, ?)", -1, &stmt, 0);
321
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800322 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800323 sqlite3_bind_int64 (stmt, 2, seq_no);
324 sqlite3_bind_int (stmt, 3, 1);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800325 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
326
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800327 sqlite3_bind_int64 (stmt, 5, version);
328 sqlite3_bind_int64 (stmt, 6, action_time);
329
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800330 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800331 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800332
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800333 ActionItemPtr item = make_shared<ActionItem> ();
Alexander Afanasyev6d4befc2013-01-26 00:01:28 -0800334 item->set_action (ActionItem::DELETE);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800335 item->set_filename (filename);
336 item->set_version (version);
337 item->set_timestamp (action_time);
338 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
339 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800340
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800341 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800342 item->SerializeToString (&item_msg);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800343
344 // action name: /<appname>/<shared-folder>/action/<device_name>/<action-seq>
345 Name actionName = Name ("/")(m_appName)(m_sharedFolderName)("action")(m_syncLog->GetLocalName ())(seq_no);
346 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800347
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800348 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
349 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800350
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800351 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
352 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800353
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800354 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800355
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800356 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800357
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800358 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800359
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800360 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800361
362 sqlite3_finalize (stmt);
363
364 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800365
366 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800367}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800368
369
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800370PcoPtr
371ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
372{
373 sqlite3_stmt *stmt;
374 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
375
376 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
377
378 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
379 sqlite3_bind_int64 (stmt, 2, seqno);
380
381 PcoPtr retval;
382 if (sqlite3_step (stmt) == SQLITE_ROW)
383 {
384 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
385 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
386 }
387 else
388 {
389 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
390 }
391 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
392 sqlite3_finalize (stmt);
393
394 return retval;
395}
396
397ActionItemPtr
398ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
399{
400 PcoPtr pco = LookupActionPco (deviceName, seqno);
401 if (!pco) return ActionItemPtr ();
402
403 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
404
405 return action;
406}
407
408Ccnx::PcoPtr
409ActionLog::LookupActionPco (const Ccnx::Name &actionName)
410{
411 sqlite3_stmt *stmt;
412 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
413
414 _LOG_DEBUG (actionName);
415 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
416
417 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
418
419 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
420
421 PcoPtr retval;
422 if (sqlite3_step (stmt) == SQLITE_ROW)
423 {
424 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
425 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
426 }
427 else
428 {
429 _LOG_TRACE ("No action found for name: " << actionName);
430 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800431 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800432 sqlite3_finalize (stmt);
433
434 return retval;
435}
436
437ActionItemPtr
438ActionLog::LookupAction (const Ccnx::Name &actionName)
439{
440 PcoPtr pco = LookupActionPco (actionName);
441 if (!pco) return ActionItemPtr ();
442
443 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
444
445 return action;
446}
447
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800448ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800449ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
450{
451 if (!actionPco)
452 {
453 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid"));
454 }
455 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
456
457 if (!action)
458 {
459 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded"));
460 }
461
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800462 _LOG_DEBUG ("AddRemoteAction: [" << deviceName << "] seqno: " << seqno);
463
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800464 sqlite3_stmt *stmt;
465 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
466 "(device_name, seq_no, action, filename, version, action_timestamp, "
467 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
468 "parent_device_name, parent_seq_no, "
469 "action_name, action_content_object) "
470 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
471 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
472 " ?, ?, "
473 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800474 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800475
476 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
477 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
478 sqlite3_bind_int64 (stmt, 2, seqno);
479
480 sqlite3_bind_int (stmt, 3, action->action ());
481 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
482 sqlite3_bind_int64 (stmt, 5, action->version ());
483 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
484
485 if (action->action () == ActionItem::UPDATE)
486 {
487 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
488
489 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
490 sqlite3_bind_int64 (stmt, 9, action->mtime ());
491 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
492
493 sqlite3_bind_int (stmt, 11, action->mode ());
494 sqlite3_bind_int (stmt, 12, action->seg_num ());
495 }
496
497 if (action->has_parent_device_name ())
498 {
499 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT);
500 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
501 }
502
503 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
504 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
505
506 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
507 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
508 sqlite3_step (stmt);
509
510 // if action needs to be applied to file state, the trigger will take care of it
511
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800512 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800513
514 sqlite3_finalize (stmt);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800515
516 return action;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800517}
518
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800519ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800520ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
521{
522 Name name = actionPco->name ();
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800523 // action name: /<appname>/<shared-folder>/action/<device_name>/<action-seq>
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800524
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800525 uint64_t seqno = name.getCompFromBackAsInt (0);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800526 string sharedFolder = name.getCompAsString (1);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800527
528 if (sharedFolder != m_sharedFolderName)
529 {
530 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("Action doesn't belong to this shared folder"));
531 }
532
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800533 string action = name.getCompAsString (2);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800534
535 if (action != "action")
536 {
537 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("not an action"));
538 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800539 Name deviceName = name.getPartialName (3, name.size ()-4);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800540
541 _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno);
542
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800543 return AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800544}
545
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800546sqlite3_int64
547ActionLog::LogSize ()
548{
549 sqlite3_stmt *stmt;
550 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
551
552 sqlite3_int64 retval = -1;
553 if (sqlite3_step (stmt) == SQLITE_ROW)
554 {
555 retval = sqlite3_column_int64 (stmt, 0);
556 }
557
558 return retval;
559}
560
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800561///////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800562///////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800563///////////////////////////////////////////////////////////////////////////////////
564
565void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800566ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
567{
568 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
569
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800570 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800571 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800572 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800573 return;
574 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800575
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800576 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
577 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
578 int action = sqlite3_value_int (argv[2]);
579 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800580
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800581 _LOG_TRACE ("apply_function called with " << argc);
582 _LOG_TRACE ("device_name: " << Name (device_name)
583 << ", action: " << action
584 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800585
586 if (action == 0) // update
587 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800588 Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4]));
589 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5]));
590 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
591 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
592 int mode = sqlite3_value_int (argv[8]);
593 int seg_num = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800594
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800595 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800596
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800597 the->m_fileState->UpdateFile (filename, hash, device_name, seq_no, atime, mtime, ctime, mode, seg_num);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800598
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800599 // no callback here
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800600 }
601 else if (action == 1) // delete
602 {
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800603 the->m_fileState->DeleteFile (filename);
Alexander Afanasyev0c545ed2013-01-26 00:12:32 -0800604
605 the->m_onFileRemoved (filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800606 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800607
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800608 sqlite3_result_null (context);
609}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800610
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800611