blob: 740430f2fcdb94f379f43f6c2aee984dee1eeca8 [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\
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080040 directory TEXT, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080041 \n\
42 version INTEGER NOT NULL, \n\
43 action_timestamp TIMESTAMP NOT NULL, \n\
44 \n\
45 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
46 file_atime TIMESTAMP, \n\
47 file_mtime TIMESTAMP, \n\
48 file_ctime TIMESTAMP, \n\
49 file_chmod INTEGER, \n\
50 file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\
51 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080052 parent_device_name BLOB, \n\
53 parent_seq_no INTEGER, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080054 \n\
55 action_name TEXT, \n\
56 action_content_object BLOB, \n\
57 \n\
Alexander Afanasyev785b0a02013-01-30 10:17:47 -080058 PRIMARY KEY (device_name, seq_no) \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080059); \n\
60 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080061CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
62CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080063CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080064CREATE INDEX ActionLog_filename_version_hash ON ActionLog (filename,version,file_hash); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080065 \n\
66CREATE TRIGGER ActionLogInsert_trigger \n\
67 AFTER INSERT ON ActionLog \n\
68 FOR EACH ROW \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080069 WHEN (SELECT device_name \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080070 FROM ActionLog \n\
71 WHERE filename=NEW.filename AND \n\
72 version > NEW.version) IS NULL AND \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080073 (SELECT device_name \n\
74 FROM ActionLog \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080075 WHERE filename=NEW.filename AND \n\
76 version = NEW.version AND \n\
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080077 device_name > NEW.device_name) IS NULL \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080078 BEGIN \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080079 SELECT apply_action (NEW.device_name, NEW.seq_no, \
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -080080 NEW.action,NEW.filename,NEW.version,NEW.file_hash, \
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080081 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
82 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
83 END; \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080084";
85
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080086// static void xTrace (void*, const char* q)
87// {
88// _LOG_TRACE ("SQLITE: " << q);
89// }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080090
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080091ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080092 SyncLogPtr syncLog,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080093 const std::string &sharedFolder, const std::string &appName,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080094 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080095 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080096 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080097 , m_ccnx (ccnx)
98 , m_sharedFolderName (sharedFolder)
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080099 , m_appName (appName)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800100 , m_onFileAddedOrChanged (onFileAddedOrChanged)
101 , m_onFileRemoved (onFileRemoved)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800102{
Alexander Afanasyev785b0a02013-01-30 10:17:47 -0800103 sqlite3_exec (m_db, "PRAGMA foreign_keys = OFF", NULL, NULL, NULL);
104 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
105
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800106 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
107 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
108
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800109 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
110 ActionLog::apply_action_xFun,
111 0, 0);
112 if (res != SQLITE_OK)
113 {
114 BOOST_THROW_EXCEPTION (Error::Db ()
115 << errmsg_info_str ("Cannot create function ``apply_action''"));
116 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800117
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800118 m_fileState = make_shared<FileState> (path);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800119}
120
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800121tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
122ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800123{
124 // check if something already exists
125 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800126 int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action "
127 "FROM ActionLog "
128 "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800129
130 if (res != SQLITE_OK)
131 {
132 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800133 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800134 }
135
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800136 sqlite3_int64 version = -1;
137 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800138 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800139
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800140 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800141 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800142 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800143 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800144
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800145 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
146 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800147 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800148 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800149 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800150 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800151
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800152 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800153 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800154}
155
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800156// local add action. remote action is extracted from content object
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800157ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800158ActionLog::AddLocalActionUpdate (const std::string &filename,
159 const Hash &hash,
160 time_t wtime,
161 int mode,
162 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800163{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800164 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800165
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800166 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
167 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
168 sqlite3_int64 version;
169 CcnxCharbufPtr parent_device_name;
170 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800171
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800172 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800173
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800174 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
175 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800176
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800177 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800178 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800179 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800180 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800181 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800182 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800183 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800184 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800185 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800186 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800187
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800188 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
189
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800190 if (res != SQLITE_OK)
191 {
192 BOOST_THROW_EXCEPTION (Error::Db ()
193 << errmsg_info_str (sqlite3_errmsg (m_db))
194 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800195 }
196
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800197 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800198 sqlite3_bind_int64 (stmt, 2, seq_no);
199 sqlite3_bind_int (stmt, 3, 0);
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800200 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800201 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800202 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800203
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800204 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800205
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800206 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
207 sqlite3_bind_int64 (stmt, 9, wtime);
208 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800209 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800210 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800211
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800212 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800213 {
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800214 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_STATIC);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800215 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800216 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800217
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800218 ActionItemPtr item = make_shared<ActionItem> ();
219 item->set_action (ActionItem::UPDATE);
220 item->set_filename (filename);
221 item->set_version (version);
222 item->set_timestamp (action_time);
223 item->set_file_hash (hash.GetHash (), hash.GetHashBytes ());
224 // item->set_atime (atime);
225 item->set_mtime (wtime);
226 // item->set_ctime (ctime);
227 item->set_mode (mode);
228 item->set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800229
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800230 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800231 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800232 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800233
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800234 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
235 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800236 }
237
238 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800239
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800240
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800241 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800242 item->SerializeToString (&item_msg);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800243
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800244 // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
245 Name actionName = Name ("/")(m_syncLog->GetLocalName ())(m_appName)("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800246 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800247
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800248 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
249 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800250
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800251 // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800252
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800253 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
254 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800255
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800256 sqlite3_step (stmt);
257
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800258 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800259
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800260 sqlite3_finalize (stmt);
261
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800262 // I had a problem including directory_name assignment as part of the initial insert.
263 sqlite3_prepare_v2 (m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
264 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
265
266 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
267 sqlite3_bind_int64 (stmt, 2, seq_no);
268 sqlite3_step (stmt);
269 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
270
271 sqlite3_finalize (stmt);
272
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800273 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800274
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800275 // set complete for local file
276 m_fileState->SetFileComplete(filename);
277
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800278 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800279}
280
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800281// void
282// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
283// {
284// // not supported yet
285// BOOST_THROW_EXCEPTION (Error::Db ()
286// << errmsg_info_str ("Move operation is not yet supported"));
287// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800288
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800289ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800290ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800291{
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800292 _LOG_DEBUG ("Adding local action DELETE");
293
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800294 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800295
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800296 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
297 sqlite3_int64 version;
298 CcnxCharbufPtr parent_device_name;
299 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800300
301 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800302
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800303 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800304 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800305 {
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800306 _LOG_DEBUG ("Nothing to delete... [" << filename << "]");
307
308 // just in case, remove data from FileState
309 sqlite3_stmt *stmt;
310 sqlite3_prepare_v2 (m_db, "DELETE FROM FileState WHERE filename = ? ", -1, &stmt, 0);
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800311 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); // file
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800312
313 sqlite3_step (stmt);
314
315 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
316
317 sqlite3_finalize (stmt);
318
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800319 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800320 return ActionItemPtr ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800321 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800322 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800323
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800324 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800325
326 sqlite3_stmt *stmt;
327 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800328 "(device_name, seq_no, action, filename, version, action_timestamp, "
329 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800330 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800331 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800332 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800333 " ?, ?)", -1, &stmt, 0);
334
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800335 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800336 sqlite3_bind_int64 (stmt, 2, seq_no);
337 sqlite3_bind_int (stmt, 3, 1);
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800338 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_STATIC); // file
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800339
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800340 sqlite3_bind_int64 (stmt, 5, version);
341 sqlite3_bind_int64 (stmt, 6, action_time);
342
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800343 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800344 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800345
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800346 ActionItemPtr item = make_shared<ActionItem> ();
Alexander Afanasyev6d4befc2013-01-26 00:01:28 -0800347 item->set_action (ActionItem::DELETE);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800348 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 Afanasyevb6bc01a2013-01-02 23:34:20 -0800353
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800354 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800355 item->SerializeToString (&item_msg);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800356
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800357 // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
358 Name actionName = Name ("/")(m_syncLog->GetLocalName ())(m_appName)("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800359 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800360
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800361 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
362 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800363
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800364 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
365 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800366
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800367 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800368
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800369 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800370
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800371 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800372
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800373 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800374
375 sqlite3_finalize (stmt);
376
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800377 // I had a problem including directory_name assignment as part of the initial insert.
378 sqlite3_prepare_v2 (m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
379 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
380
381 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
382 sqlite3_bind_int64 (stmt, 2, seq_no);
383 sqlite3_step (stmt);
384 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
385
386 sqlite3_finalize (stmt);
387
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800388 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800389
390 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800391}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800392
393
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800394PcoPtr
395ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
396{
397 sqlite3_stmt *stmt;
398 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
399
400 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
401
402 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
403 sqlite3_bind_int64 (stmt, 2, seqno);
404
405 PcoPtr retval;
406 if (sqlite3_step (stmt) == SQLITE_ROW)
407 {
408 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
409 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
410 }
411 else
412 {
413 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
414 }
415 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
416 sqlite3_finalize (stmt);
417
418 return retval;
419}
420
421ActionItemPtr
422ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
423{
424 PcoPtr pco = LookupActionPco (deviceName, seqno);
425 if (!pco) return ActionItemPtr ();
426
427 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
428
429 return action;
430}
431
432Ccnx::PcoPtr
433ActionLog::LookupActionPco (const Ccnx::Name &actionName)
434{
435 sqlite3_stmt *stmt;
436 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
437
438 _LOG_DEBUG (actionName);
439 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
440
441 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
442
443 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
444
445 PcoPtr retval;
446 if (sqlite3_step (stmt) == SQLITE_ROW)
447 {
448 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
449 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
450 }
451 else
452 {
453 _LOG_TRACE ("No action found for name: " << actionName);
454 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800455 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800456 sqlite3_finalize (stmt);
457
458 return retval;
459}
460
461ActionItemPtr
462ActionLog::LookupAction (const Ccnx::Name &actionName)
463{
464 PcoPtr pco = LookupActionPco (actionName);
465 if (!pco) return ActionItemPtr ();
466
467 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
468
469 return action;
470}
471
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800472FileItemPtr
473ActionLog::LookupAction (const std::string &filename, sqlite3_int64 version, const Hash &filehash)
474{
475 sqlite3_stmt *stmt;
476 sqlite3_prepare_v2 (m_db,
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800477 "SELECT device_name, seq_no, strftime('%s', file_mtime), file_chmod, file_seg_num, file_hash "
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800478 " FROM ActionLog "
479 " WHERE action = 0 AND "
480 " filename=? AND "
481 " version=? AND "
482 " is_prefix (?, file_hash)=1", -1, &stmt, 0);
483 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
484
485 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
486 sqlite3_bind_int64 (stmt, 2, version);
487 sqlite3_bind_blob (stmt, 3, filehash.GetHash (), filehash.GetHashBytes (), SQLITE_STATIC);
488
489 FileItemPtr fileItem;
490
491 if (sqlite3_step (stmt) == SQLITE_ROW)
492 {
493 fileItem = make_shared<FileItem> ();
494 fileItem->set_filename (filename);
495 fileItem->set_device_name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0));
496 fileItem->set_seq_no (sqlite3_column_int64 (stmt, 1));
497 fileItem->set_mtime (sqlite3_column_int64 (stmt, 2));
498 fileItem->set_mode (sqlite3_column_int64 (stmt, 3));
499 fileItem->set_seg_num (sqlite3_column_int64 (stmt, 4));
500
501 fileItem->set_file_hash (sqlite3_column_blob (stmt, 5), sqlite3_column_bytes (stmt, 5));
502 }
503
504 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE || sqlite3_errcode (m_db) != SQLITE_ROW || sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
505
506 return fileItem;
507}
508
509
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800510ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800511ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
512{
513 if (!actionPco)
514 {
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800515 _LOG_ERROR ("actionPco is not valid");
516 return ActionItemPtr ();
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800517 }
518 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
519
520 if (!action)
521 {
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800522 _LOG_ERROR ("action cannot be decoded");
523 return ActionItemPtr ();
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800524 }
525
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800526 _LOG_DEBUG ("AddRemoteAction: [" << deviceName << "] seqno: " << seqno);
527
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800528 sqlite3_stmt *stmt;
529 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
530 "(device_name, seq_no, action, filename, version, action_timestamp, "
531 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
532 "parent_device_name, parent_seq_no, "
533 "action_name, action_content_object) "
534 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
535 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
536 " ?, ?, "
537 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800538 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800539
540 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
541 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
542 sqlite3_bind_int64 (stmt, 2, seqno);
543
544 sqlite3_bind_int (stmt, 3, action->action ());
545 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
546 sqlite3_bind_int64 (stmt, 5, action->version ());
547 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
548
549 if (action->action () == ActionItem::UPDATE)
550 {
551 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
552
553 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
554 sqlite3_bind_int64 (stmt, 9, action->mtime ());
555 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
556
557 sqlite3_bind_int (stmt, 11, action->mode ());
558 sqlite3_bind_int (stmt, 12, action->seg_num ());
559 }
560
561 if (action->has_parent_device_name ())
562 {
Alexander Afanasyev2b760d02013-02-07 13:47:29 -0800563 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_STATIC);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800564 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
565 }
566
567 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
568 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
569
570 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
571 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
572 sqlite3_step (stmt);
573
574 // if action needs to be applied to file state, the trigger will take care of it
575
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800576 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800577
578 sqlite3_finalize (stmt);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800579
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800580 // I had a problem including directory_name assignment as part of the initial insert.
581 sqlite3_prepare_v2 (m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
582 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
583
584 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
585 sqlite3_bind_int64 (stmt, 2, seqno);
586 sqlite3_step (stmt);
587 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
588
589 sqlite3_finalize (stmt);
590
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800591 return action;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800592}
593
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800594ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800595ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
596{
597 Name name = actionPco->name ();
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800598 // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800599
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800600 uint64_t seqno = name.getCompFromBackAsInt (0);
601 string sharedFolder = name.getCompFromBackAsString (1);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800602
603 if (sharedFolder != m_sharedFolderName)
604 {
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800605 _LOG_ERROR ("Action doesn't belong to this shared folder");
606 return ActionItemPtr ();
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800607 }
608
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800609 string action = name.getCompFromBackAsString (2);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800610
611 if (action != "action")
612 {
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800613 _LOG_ERROR ("not an action");
614 return ActionItemPtr ();
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800615 }
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800616
617 string appName = name.getCompFromBackAsString (3);
618 if (appName != m_appName)
619 {
620 _LOG_ERROR ("Action doesn't belong to this application");
621 return ActionItemPtr ();
622 }
623
624 Name deviceName = name.getPartialName (0, name.size ()-4);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800625
626 _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno);
627
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800628 return AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800629}
630
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800631sqlite3_int64
632ActionLog::LogSize ()
633{
634 sqlite3_stmt *stmt;
635 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
636
637 sqlite3_int64 retval = -1;
638 if (sqlite3_step (stmt) == SQLITE_ROW)
639 {
640 retval = sqlite3_column_int64 (stmt, 0);
641 }
642
643 return retval;
644}
645
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800646
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800647bool
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800648ActionLog::LookupActionsInFolderRecursively (const boost::function<void (const Ccnx::Name &name, sqlite3_int64 seq_no, const ActionItem &)> &visitor,
649 const std::string &folder, int offset/*=0*/, int limit/*=-1*/)
650{
651 _LOG_DEBUG ("LookupActionsInFolderRecursively: [" << folder << "]");
652
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800653 if (limit >= 0)
654 limit += 1; // to check if there is more data
655
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800656 sqlite3_stmt *stmt;
657 if (folder != "")
658 {
659 /// @todo Do something to improve efficiency of this query. Right now it is basically scanning the whole database
660
661 sqlite3_prepare_v2 (m_db,
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800662 "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), "
663 " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, "
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800664 " parent_device_name,parent_seq_no "
665 " FROM ActionLog "
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800666 " WHERE is_dir_prefix (?, directory)=1 "
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800667 " ORDER BY action_timestamp DESC "
668 " LIMIT ? OFFSET ?", -1, &stmt, 0); // there is a small ambiguity with is_prefix matching, but should be ok for now
669 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
670
671 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
672 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
673
674 sqlite3_bind_int (stmt, 2, limit);
675 sqlite3_bind_int (stmt, 3, offset);
676 }
677 else
678 {
679 sqlite3_prepare_v2 (m_db,
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800680 "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), "
681 " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, "
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800682 " parent_device_name,parent_seq_no "
683 " FROM ActionLog "
684 " ORDER BY action_timestamp DESC "
685 " LIMIT ? OFFSET ?", -1, &stmt, 0);
686 sqlite3_bind_int (stmt, 1, limit);
687 sqlite3_bind_int (stmt, 2, offset);
688 }
689
690 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
691
692 while (sqlite3_step (stmt) == SQLITE_ROW)
693 {
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800694 if (limit == 1)
695 break;
696
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800697 ActionItem action;
698
699 Ccnx::Name device_name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0));
700 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt, 1);
701 action.set_action (static_cast<ActionItem_ActionType> (sqlite3_column_int (stmt, 2)));
702 action.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 3)), sqlite3_column_bytes (stmt, 3));
703 std::string directory (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 4)), sqlite3_column_bytes (stmt, 4));
704 if (action.action () == 0)
705 {
706 action.set_version (sqlite3_column_int64 (stmt, 5));
707 action.set_timestamp (sqlite3_column_int64 (stmt, 6));
708 action.set_file_hash (sqlite3_column_blob (stmt, 7), sqlite3_column_bytes (stmt, 7));
709 action.set_mtime (sqlite3_column_int (stmt, 8));
710 action.set_mode (sqlite3_column_int (stmt, 9));
711 action.set_seg_num (sqlite3_column_int64 (stmt, 10));
712 }
713 if (sqlite3_column_bytes (stmt, 11) > 0)
714 {
715 action.set_parent_device_name (sqlite3_column_blob (stmt, 11), sqlite3_column_bytes (stmt, 11));
716 action.set_parent_seq_no (sqlite3_column_int64 (stmt, 12));
717 }
718
719 visitor (device_name, seq_no, action);
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800720 limit --;
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800721 }
722
723 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
724
725 sqlite3_finalize (stmt);
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800726
727 return (limit == 1); // more data is available
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800728}
729
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800730void
731ActionLog::LookupRecentFileActions(const boost::function<void (const string &, int, int)> &visitor, int limit)
732{
733 sqlite3_stmt *stmt;
734
735 sqlite3_prepare_v2 (m_db,
736 "SELECT AL.filename, AL.action"
737 " FROM ActionLog AL"
738 " JOIN "
739 " (SELECT filename, MAX(action_timestamp) AS action_timestamp "
740 " FROM ActionLog "
741 " GROUP BY filename ) AS GAL"
742 " ON AL.filename = GAL.filename AND AL.action_timestamp = GAL.action_timestamp "
743 " ORDER BY AL.action_timestamp DESC "
744 " LIMIT ?;",
745 -1, &stmt, 0);
746 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
747 sqlite3_bind_int(stmt, 1, limit);
748 int index = 0;
749 while (sqlite3_step(stmt) == SQLITE_ROW)
750 {
751 std::string filename(reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
752 int action = sqlite3_column_int (stmt, 1);
753 visitor(filename, action, index);
754 index++;
755 }
756
757 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
758
759 sqlite3_finalize (stmt);
760}
761
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800762
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800763///////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800764///////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800765///////////////////////////////////////////////////////////////////////////////////
766
767void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800768ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
769{
770 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
771
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -0800772 if (argc != 11)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800773 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800774 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800775 return;
776 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800777
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800778 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
779 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
780 int action = sqlite3_value_int (argv[2]);
781 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -0800782 sqlite3_int64 version = sqlite3_value_int64 (argv[4]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800783
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800784 _LOG_TRACE ("apply_function called with " << argc);
785 _LOG_TRACE ("device_name: " << Name (device_name)
786 << ", action: " << action
787 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800788
789 if (action == 0) // update
790 {
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -0800791 Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5]));
792 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
793 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
794 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8]));
795 int mode = sqlite3_value_int (argv[9]);
796 int seg_num = sqlite3_value_int (argv[10]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800797
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800798 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800799
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -0800800 the->m_fileState->UpdateFile (filename, version, hash, device_name, seq_no, atime, mtime, ctime, mode, seg_num);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800801
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800802 // no callback here
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800803 }
804 else if (action == 1) // delete
805 {
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800806 the->m_fileState->DeleteFile (filename);
Alexander Afanasyev0c545ed2013-01-26 00:12:32 -0800807
808 the->m_onFileRemoved (filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800809 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800810
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800811 sqlite3_result_null (context);
812}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800813
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800814