blob: 6254e1e732037eb2f61e13fa70d98e086fb0e116 [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 Afanasyev0995f322013-01-22 13:16:46 -080057 PRIMARY KEY (device_name, seq_no), \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080058 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080059 FOREIGN KEY (parent_device_name, parent_seq_no) \n\
60 REFERENCES ActionLog (device_name, seq_no) \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080061 ON UPDATE RESTRICT \n\
62 ON DELETE SET NULL \n\
63); \n\
64 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080065CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
66CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080067CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
68 \n\
69CREATE TRIGGER ActionLogInsert_trigger \n\
70 AFTER INSERT ON ActionLog \n\
71 FOR EACH ROW \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080072 WHEN (SELECT device_name \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080073 FROM ActionLog \n\
74 WHERE filename=NEW.filename AND \n\
75 version > NEW.version) IS NULL AND \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080076 (SELECT device_name \n\
77 FROM ActionLog \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080078 WHERE filename=NEW.filename AND \n\
79 version = NEW.version AND \n\
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080080 device_name > NEW.device_name) IS NULL \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080081 BEGIN \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080082 SELECT apply_action (NEW.device_name, NEW.seq_no, \
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080083 NEW.action,NEW.filename,NEW.file_hash, \
84 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
85 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
86 END; \n\
87 \n\
88CREATE TABLE FileState ( \n\
89 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
90 filename TEXT NOT NULL, \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080091 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080092 seq_no INTEGER NOT NULL, \n\
93 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
94 file_atime TIMESTAMP, \n\
95 file_mtime TIMESTAMP, \n\
96 file_ctime TIMESTAMP, \n\
97 file_chmod INTEGER, \n\
98 file_seg_num INTEGER, \n\
99 \n\
100 PRIMARY KEY (type, filename) \n\
101); \n\
102 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800103CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800104";
105
106
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800107ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800108 SyncLogPtr syncLog,
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800109 const std::string &sharedFolder)
110 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800111 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800112 , m_ccnx (ccnx)
113 , m_sharedFolderName (sharedFolder)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800114{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800115 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
116 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
117
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800118 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
119 ActionLog::apply_action_xFun,
120 0, 0);
121 if (res != SQLITE_OK)
122 {
123 BOOST_THROW_EXCEPTION (Error::Db ()
124 << errmsg_info_str ("Cannot create function ``apply_action''"));
125 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800126}
127
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800128tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
129ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800130{
131 // check if something already exists
132 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800133 int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action "
134 "FROM ActionLog "
135 "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800136
137 if (res != SQLITE_OK)
138 {
139 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800140 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800141 }
142
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800143 sqlite3_int64 version = -1;
144 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800145 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800146
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800147 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800148 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800149 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800150 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800151
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800152 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
153 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800154 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800155 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800156 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800157 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800158
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800159 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800160 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800161}
162
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800163// local add action. remote action is extracted from content object
164void
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800165ActionLog::AddLocalActionUpdate (const std::string &filename,
166 const Hash &hash,
167 time_t wtime,
168 int mode,
169 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800170{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800171 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800172
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800173 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
174 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
175 sqlite3_int64 version;
176 CcnxCharbufPtr parent_device_name;
177 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800178
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800179 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800180
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800181 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
182 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800183
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800184 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800185 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800186 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800187 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800188 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800189 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800190 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800191 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800192 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800193 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800194
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800195 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
196
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800197 if (res != SQLITE_OK)
198 {
199 BOOST_THROW_EXCEPTION (Error::Db ()
200 << errmsg_info_str (sqlite3_errmsg (m_db))
201 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800202 }
203
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800204 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800205 sqlite3_bind_int64 (stmt, 2, seq_no);
206 sqlite3_bind_int (stmt, 3, 0);
207 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800208 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800209 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800210
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800211 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800212
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800213 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
214 sqlite3_bind_int64 (stmt, 9, wtime);
215 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800216 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800217 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800218
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800219 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800220 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800221 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800222 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800223 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800224
225 ActionItem item;
226 item.set_action (ActionItem::UPDATE);
227 item.set_filename (filename);
228 item.set_version (version);
229 item.set_timestamp (action_time);
230 item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800231 // item.set_atime (atime);
232 item.set_mtime (wtime);
233 // item.set_ctime (ctime);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800234 item.set_mode (mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800235 item.set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800236
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800237 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800238 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800239 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800240
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800241 item.set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800242 item.set_parent_seq_no (parent_seq_no);
243 }
244
245 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800246
247 string item_msg;
248 item.SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800249 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800250 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800251
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800252 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
253 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800254
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800255 _LOG_DEBUG (" >>>>>>> " << namePtr->buf () << " " << namePtr->length ());
256
257 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
258 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800259
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800260 sqlite3_step (stmt);
261
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800262 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
263
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800264 sqlite3_finalize (stmt);
265
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800266 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800267}
268
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800269// void
270// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
271// {
272// // not supported yet
273// BOOST_THROW_EXCEPTION (Error::Db ()
274// << errmsg_info_str ("Move operation is not yet supported"));
275// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800276
277void
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800278ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800279{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800280 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800281
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800282 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
283 sqlite3_int64 version;
284 CcnxCharbufPtr parent_device_name;
285 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800286
287 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800288
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800289 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800290 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800291 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800292 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800293 return;
294 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800295 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800296
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800297 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800298
299 sqlite3_stmt *stmt;
300 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800301 "(device_name, seq_no, action, filename, version, action_timestamp, "
302 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800303 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800304 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800305 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800306 " ?, ?)", -1, &stmt, 0);
307
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800308 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800309 sqlite3_bind_int64 (stmt, 2, seq_no);
310 sqlite3_bind_int (stmt, 3, 1);
311 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
312 sqlite3_bind_int64 (stmt, 5, version);
313 sqlite3_bind_int64 (stmt, 6, action_time);
314
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800315 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800316 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800317
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800318
319 ActionItem item;
320 item.set_action (ActionItem::UPDATE);
321 item.set_filename (filename);
322 item.set_version (version);
323 item.set_timestamp (action_time);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800324 item.set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800325 item.set_parent_seq_no (parent_seq_no);
326
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800327 string item_msg;
328 item.SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800329 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800330
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800331 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
332 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800333
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800334 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
335 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800336
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800337 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800338
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800339 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
340
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800341 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800342
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800343 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800344
345 sqlite3_finalize (stmt);
346
347 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800348}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800349
350
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800351PcoPtr
352ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
353{
354 sqlite3_stmt *stmt;
355 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
356
357 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
358
359 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
360 sqlite3_bind_int64 (stmt, 2, seqno);
361
362 PcoPtr retval;
363 if (sqlite3_step (stmt) == SQLITE_ROW)
364 {
365 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
366 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
367 }
368 else
369 {
370 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
371 }
372 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
373 sqlite3_finalize (stmt);
374
375 return retval;
376}
377
378ActionItemPtr
379ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
380{
381 PcoPtr pco = LookupActionPco (deviceName, seqno);
382 if (!pco) return ActionItemPtr ();
383
384 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
385
386 return action;
387}
388
389Ccnx::PcoPtr
390ActionLog::LookupActionPco (const Ccnx::Name &actionName)
391{
392 sqlite3_stmt *stmt;
393 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
394
395 _LOG_DEBUG (actionName);
396 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
397
398 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
399
400 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
401
402 PcoPtr retval;
403 if (sqlite3_step (stmt) == SQLITE_ROW)
404 {
405 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
406 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
407 }
408 else
409 {
410 _LOG_TRACE ("No action found for name: " << actionName);
411 }
412 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
413 sqlite3_finalize (stmt);
414
415 return retval;
416}
417
418ActionItemPtr
419ActionLog::LookupAction (const Ccnx::Name &actionName)
420{
421 PcoPtr pco = LookupActionPco (actionName);
422 if (!pco) return ActionItemPtr ();
423
424 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
425
426 return action;
427}
428
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800429void
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800430ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
431{
432 if (!actionPco)
433 {
434 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid"));
435 }
436 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
437
438 if (!action)
439 {
440 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded"));
441 }
442
443 sqlite3_stmt *stmt;
444 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
445 "(device_name, seq_no, action, filename, version, action_timestamp, "
446 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
447 "parent_device_name, parent_seq_no, "
448 "action_name, action_content_object) "
449 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
450 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
451 " ?, ?, "
452 " ?, ?);", -1, &stmt, 0);
453
454 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
455 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
456 sqlite3_bind_int64 (stmt, 2, seqno);
457
458 sqlite3_bind_int (stmt, 3, action->action ());
459 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
460 sqlite3_bind_int64 (stmt, 5, action->version ());
461 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
462
463 if (action->action () == ActionItem::UPDATE)
464 {
465 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
466
467 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
468 sqlite3_bind_int64 (stmt, 9, action->mtime ());
469 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
470
471 sqlite3_bind_int (stmt, 11, action->mode ());
472 sqlite3_bind_int (stmt, 12, action->seg_num ());
473 }
474
475 if (action->has_parent_device_name ())
476 {
477 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT);
478 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
479 }
480
481 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
482 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
483
484 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
485 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
486 sqlite3_step (stmt);
487
488 // if action needs to be applied to file state, the trigger will take care of it
489
490 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
491
492 sqlite3_finalize (stmt);
493}
494
495void
496ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
497{
498 Name name = actionPco->name ();
499 // <device_name>/"action"/<shared_folder_name_one_component>/<seqno>
500
501}
502
503///////////////////////////////////////////////////////////////////////////////////
504// SHOULD BE MOVED TO SEPARATE FILESTATE CLASS "EVENTUALLY"
505///////////////////////////////////////////////////////////////////////////////////
506
507void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800508ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
509{
510 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
511
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800512 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800513 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800514 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800515 return;
516 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800517
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800518 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
519 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
520 int action = sqlite3_value_int (argv[2]);
521 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800522
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800523 _LOG_TRACE ("apply_function called with " << argc);
524 _LOG_TRACE ("device_name: " << Name (device_name)
525 << ", action: " << action
526 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800527
528 if (action == 0) // update
529 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800530 Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4]));
531 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5]));
532 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
533 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
534 int mode = sqlite3_value_int (argv[8]);
535 int seg_num = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800536
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800537 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800538
539 sqlite3_stmt *stmt;
540 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800541 "SET "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800542 "device_name=?, seq_no=?, "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800543 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800544 "file_atime=datetime(?, 'unixepoch'),"
545 "file_mtime=datetime(?, 'unixepoch'),"
546 "file_ctime=datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800547 "file_chmod=?, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800548 "file_seg_num=? "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800549 "WHERE type=0 AND filename=?", -1, &stmt, 0);
550
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800551 sqlite3_bind_blob (stmt, 1, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800552 sqlite3_bind_int64 (stmt, 2, seq_no);
553 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
554 sqlite3_bind_int64 (stmt, 4, atime);
555 sqlite3_bind_int64 (stmt, 5, mtime);
556 sqlite3_bind_int64 (stmt, 6, ctime);
557 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800558 sqlite3_bind_int (stmt, 8, seg_num);
559 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800560
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800561 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800562
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800563 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK,
564 sqlite3_errmsg (the->m_db));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800565
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800566 sqlite3_finalize (stmt);
567
568 int affected_rows = sqlite3_changes (the->m_db);
569 if (affected_rows == 0) // file didn't exist
570 {
571 sqlite3_stmt *stmt;
572 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800573 "(type,filename,device_name,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod,file_seg_num) "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800574 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800575 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?)", -1, &stmt, 0);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800576
577 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800578 sqlite3_bind_blob (stmt, 2, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800579 sqlite3_bind_int64 (stmt, 3, seq_no);
580 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
581 sqlite3_bind_int64 (stmt, 5, atime);
582 sqlite3_bind_int64 (stmt, 6, mtime);
583 sqlite3_bind_int64 (stmt, 7, ctime);
584 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800585 sqlite3_bind_int (stmt, 9, seg_num);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800586
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800587 sqlite3_step (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800588 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK,
589 sqlite3_errmsg (the->m_db));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800590 sqlite3_finalize (stmt);
591 }
592 }
593 else if (action == 1) // delete
594 {
595 sqlite3_stmt *stmt;
596 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
597 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
598
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800599 _LOG_DEBUG ("Delete " << filename);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800600
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800601 sqlite3_step (stmt);
602 sqlite3_finalize (stmt);
603 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800604
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800605 sqlite3_result_null (context);
606}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800607
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800608/**
609 * @todo Implement checking modification time and permissions
610 */
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800611bool
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800612ActionLog::KnownFileState(const std::string &filename, const Hash &hash
613 /*, time_t mtime, int chmod*/)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800614{
615 sqlite3_stmt *stmt;
616 sqlite3_prepare_v2 (m_db, "SELECT * FROM FileState WHERE filename = ? AND file_hash = ?;", -1, &stmt, 0);
617 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
618 sqlite3_bind_blob(stmt, 2, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800619
620 bool retval = false;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800621 if (sqlite3_step (stmt) == SQLITE_ROW)
622 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800623 retval = true;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800624 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800625 sqlite3_finalize (stmt);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800626
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800627 return retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800628}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800629
630sqlite3_int64
631ActionLog::LogSize ()
632{
633 sqlite3_stmt *stmt;
634 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
635
636 sqlite3_int64 retval = -1;
637 if (sqlite3_step (stmt) == SQLITE_ROW)
638 {
639 retval = sqlite3_column_int64 (stmt, 0);
640 }
641
642 return retval;
643}