blob: f49c69eda824a2162ac2e2d7e2d8d4129c6b72ba [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
25using namespace boost;
26using namespace std;
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080027using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080028
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080029INIT_LOGGER ("ActionLog");
30
31const std::string INIT_DATABASE = "\
32CREATE TABLE ActionLog ( \n\
33 device_id INTEGER NOT NULL, \n\
34 seq_no INTEGER NOT NULL, \n\
35 \n\
36 action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
37 filename TEXT NOT NULL, \n\
38 \n\
39 version INTEGER NOT NULL, \n\
40 action_timestamp TIMESTAMP NOT NULL, \n\
41 \n\
42 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
43 file_atime TIMESTAMP, \n\
44 file_mtime TIMESTAMP, \n\
45 file_ctime TIMESTAMP, \n\
46 file_chmod INTEGER, \n\
47 file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\
48 \n\
49 parent_device_id INTEGER, \n\
50 parent_seq_no INTEGER, \n\
51 \n\
52 action_name TEXT, \n\
53 action_content_object BLOB, \n\
54 \n\
55 PRIMARY KEY (device_id, seq_no), \n\
56 \n\
57 FOREIGN KEY (parent_device_id, parent_seq_no) \n\
58 REFERENCES ActionLog (device_id, seq_no) \n\
59 ON UPDATE RESTRICT \n\
60 ON DELETE SET NULL \n\
61); \n\
62 \n\
63CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
64CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
65CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
66 \n\
67CREATE TRIGGER ActionLogInsert_trigger \n\
68 AFTER INSERT ON ActionLog \n\
69 FOR EACH ROW \n\
70 WHEN (SELECT device_id \n\
71 FROM ActionLog \n\
72 WHERE filename=NEW.filename AND \n\
73 version > NEW.version) IS NULL AND \n\
74 (SELECT a.device_id \n\
75 FROM ActionLog a \n\
76 LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\
77 WHERE filename=NEW.filename AND \n\
78 version = NEW.version AND \n\
79 a.device_id != NEW.device_id AND \n\
80 s.device_name > (SELECT device_name \n\
81 FROM SyncNodes \n\
82 WHERE device_id=NEW.device_id)) IS NULL \n\
83 BEGIN \n\
84 SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \
85 NEW.device_id, NEW.seq_no, \
86 NEW.action,NEW.filename,NEW.file_hash, \
87 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
88 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
89 END; \n\
90 \n\
91CREATE TABLE FileState ( \n\
92 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
93 filename TEXT NOT NULL, \n\
94 device_id INTEGER NOT NULL, \n\
95 seq_no INTEGER NOT NULL, \n\
96 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
97 file_atime TIMESTAMP, \n\
98 file_mtime TIMESTAMP, \n\
99 file_ctime TIMESTAMP, \n\
100 file_chmod INTEGER, \n\
101 file_seg_num INTEGER, \n\
102 \n\
103 PRIMARY KEY (type, filename) \n\
104); \n\
105 \n\
106CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\
107";
108
109
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800110ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800111 SyncLogPtr syncLog,
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800112 const std::string &localName, const std::string &sharedFolder)
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800113 : DbHelper (path)
114 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800115 , m_ccnx (ccnx)
116 , m_sharedFolderName (sharedFolder)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800117{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800118 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
119 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
120
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800121 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
122 ActionLog::apply_action_xFun,
123 0, 0);
124 if (res != SQLITE_OK)
125 {
126 BOOST_THROW_EXCEPTION (Error::Db ()
127 << errmsg_info_str ("Cannot create function ``apply_action''"));
128 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800129}
130
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800131tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800132ActionLog::GetExistingRecord (const std::string &filename)
133{
134 // check if something already exists
135 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800136 int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
137 "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800138 "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800139
140 if (res != SQLITE_OK)
141 {
142 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800143 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800144 }
145
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800146 // with parent with version number + 1
147 sqlite3_int64 version = 0;
148 sqlite3_int64 parent_device_id = -1;
149 sqlite3_int64 parent_seq_no = -1;
150 string parent_device_name;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800151
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800152 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800153 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800154 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800155 version = sqlite3_column_int64 (stmt, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800156
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800157 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
158 {
159 parent_device_id = sqlite3_column_int64 (stmt, 1);
160 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyev707f6df2013-01-04 22:46:15 -0800161 parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 4)), sqlite3_column_bytes (stmt, 4));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800162 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800163 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800164
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800165 sqlite3_finalize (stmt);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800166 return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800167}
168
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800169// local add action. remote action is extracted from content object
170void
171ActionLog::AddActionUpdate (const std::string &filename,
172 const Hash &hash,
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800173 time_t wtime,
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800174 int mode,
175 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800176{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800177 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800178
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800179 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800180 sqlite3_int64 version = 0;
181 sqlite3_int64 parent_device_id = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800182 string parent_device_name;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800183 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800184
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800185 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800186
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800187 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800188
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800189 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800190 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
191 "(device_id, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800192 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800193 "parent_device_id, parent_seq_no, "
194 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800195 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
196 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800197 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800198 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800199
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800200 // cout << "INSERT INTO ActionLog "
201 // "(device_id, seq_no, action, filename, version, action_timestamp, "
202 // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
203 // "parent_device_id, parent_seq_no) "
204 // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
205 // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
206 // " ?, ?)" << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800207
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800208 if (res != SQLITE_OK)
209 {
210 BOOST_THROW_EXCEPTION (Error::Db ()
211 << errmsg_info_str (sqlite3_errmsg (m_db))
212 );
213 // << errmsg_info_str ("Some error with prepare AddActionUpdate"));
214 }
215
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800216
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800217 sqlite3_bind_int64 (stmt, 1, m_syncLog->GetLocalSyncNodeId ());
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800218 sqlite3_bind_int64 (stmt, 2, seq_no);
219 sqlite3_bind_int (stmt, 3, 0);
220 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800221 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800222 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800223
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800224 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800225
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800226 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
227 sqlite3_bind_int64 (stmt, 9, wtime);
228 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800229 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800230 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800231
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800232 if (parent_device_id > 0 && parent_seq_no > 0)
233 {
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800234 sqlite3_bind_int64 (stmt, 13, parent_device_id);
235 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800236 }
237 else
238 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800239 sqlite3_bind_null (stmt, 13);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800240 sqlite3_bind_null (stmt, 14);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800241 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800242
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800243 // missing part: creating ContentObject for the action !!!
244
245 ActionItem item;
246 item.set_action (ActionItem::UPDATE);
247 item.set_filename (filename);
248 item.set_version (version);
249 item.set_timestamp (action_time);
250 item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800251 // item.set_atime (atime);
252 item.set_mtime (wtime);
253 // item.set_ctime (ctime);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800254 item.set_mode (mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800255 item.set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800256
257 if (parent_device_id > 0 && parent_seq_no > 0)
258 {
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800259 cout << Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
260 parent_device_name.size ()) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800261
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800262 item.set_parent_device_name (parent_device_name);
263 item.set_parent_seq_no (parent_seq_no);
264 }
265
266 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800267
268 string item_msg;
269 item.SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800270 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800271
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800272 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
273 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800274
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800275 sqlite3_bind_blob (stmt, 14, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
276 sqlite3_bind_blob (stmt, 15, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800277
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800278 sqlite3_step (stmt);
279
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800280 sqlite3_finalize (stmt);
281
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800282 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800283}
284
285void
286ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
287{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800288 // not supported yet
289 BOOST_THROW_EXCEPTION (Error::Db ()
290 << errmsg_info_str ("Move operation is not yet supported"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800291}
292
293void
294ActionLog::AddActionDelete (const std::string &filename)
295{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800296 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800297
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800298 sqlite3_int64 version = 0;
299 sqlite3_int64 parent_device_id = -1;
300 string parent_device_name;
301 sqlite3_int64 parent_seq_no = -1;
302
303 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800304
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800305 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
306 if (parent_device_id < 0) // no records exist or file was already deleted
307 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800308 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800309 return;
310 }
311
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800312 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800313
314 sqlite3_stmt *stmt;
315 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
316 "(device_id, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800317 "parent_device_id, parent_seq_no, "
318 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800319 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800320 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800321 " ?, ?)", -1, &stmt, 0);
322
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800323 sqlite3_bind_int64 (stmt, 1, m_syncLog->GetLocalSyncNodeId ());
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800324 sqlite3_bind_int64 (stmt, 2, seq_no);
325 sqlite3_bind_int (stmt, 3, 1);
326 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
327 sqlite3_bind_int64 (stmt, 5, version);
328 sqlite3_bind_int64 (stmt, 6, action_time);
329
330 sqlite3_bind_int64 (stmt, 7, parent_device_id);
331 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800332
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800333
334 ActionItem item;
335 item.set_action (ActionItem::UPDATE);
336 item.set_filename (filename);
337 item.set_version (version);
338 item.set_timestamp (action_time);
339 item.set_parent_device_name (parent_device_name);
340 item.set_parent_seq_no (parent_seq_no);
341
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800342 string item_msg;
343 item.SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800344 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800345
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800346 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
347 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800348
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800349 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
350 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800351
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800352 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800353
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800354 // cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
355 // parent_device_name.size ()) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800356
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800357 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800358
359 sqlite3_finalize (stmt);
360
361 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800362}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800363
364
365void
366ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
367{
368 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
369
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800370 if (argc != 11)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800371 {
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800372 sqlite3_result_error (context, "``apply_action'' expects 11 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800373 return;
374 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800375
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800376 // cout << "apply_function called with " << argc << endl;
377
378 // cout << "device_name: " << sqlite3_value_text (argv[0]) << endl;
379 // cout << "action: " << sqlite3_value_int (argv[1]) << endl;
380 // cout << "filename: " << sqlite3_value_text (argv[2]) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800381
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800382 string device_name (reinterpret_cast<const char*> (sqlite3_value_blob (argv[0])), sqlite3_value_bytes (argv[0]));
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800383 sqlite3_int64 device_id = sqlite3_value_int64 (argv[1]);
384 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[2]);
385 int action = sqlite3_value_int (argv[3]);
386 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[4]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800387
388 if (action == 0) // update
389 {
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800390 Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5]));
391 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
392 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
393 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8]));
394 int mode = sqlite3_value_int (argv[9]);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800395 int seg_num = sqlite3_value_int (argv[10]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800396
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800397 cout << "Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash << endl;
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800398
399 sqlite3_stmt *stmt;
400 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800401 "SET "
402 "device_id=?, seq_no=?, "
403 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800404 "file_atime=datetime(?, 'unixepoch'),"
405 "file_mtime=datetime(?, 'unixepoch'),"
406 "file_ctime=datetime(?, 'unixepoch'),"
407 "file_chmod=? "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800408 "file_seg_num=? "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800409 "WHERE type=0 AND filename=?", -1, &stmt, 0);
410
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800411 sqlite3_bind_int64 (stmt, 1, device_id);
412 sqlite3_bind_int64 (stmt, 2, seq_no);
413 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
414 sqlite3_bind_int64 (stmt, 4, atime);
415 sqlite3_bind_int64 (stmt, 5, mtime);
416 sqlite3_bind_int64 (stmt, 6, ctime);
417 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800418 sqlite3_bind_int (stmt, 8, seg_num);
419 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800420
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800421 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800422
423 // cout << sqlite3_errmsg (the->m_db) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800424
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800425 sqlite3_finalize (stmt);
426
427 int affected_rows = sqlite3_changes (the->m_db);
428 if (affected_rows == 0) // file didn't exist
429 {
430 sqlite3_stmt *stmt;
431 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800432 "(type,filename,device_id,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod) "
433 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800434 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", -1, &stmt, 0);
435
436 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800437 sqlite3_bind_int64 (stmt, 2, device_id);
438 sqlite3_bind_int64 (stmt, 3, seq_no);
439 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
440 sqlite3_bind_int64 (stmt, 5, atime);
441 sqlite3_bind_int64 (stmt, 6, mtime);
442 sqlite3_bind_int64 (stmt, 7, ctime);
443 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800444
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800445 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800446 // cout << sqlite3_errmsg (the->m_db) << endl;
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800447 sqlite3_finalize (stmt);
448 }
449 }
450 else if (action == 1) // delete
451 {
452 sqlite3_stmt *stmt;
453 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
454 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
455
456 cout << "Delete " << filename << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800457
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800458 sqlite3_step (stmt);
459 sqlite3_finalize (stmt);
460 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800461
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800462 sqlite3_result_null (context);
463}