blob: 293a79a1fe5dbce83f317eed15c67dacfe76ebfe [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 Afanasyev433ecda2013-01-02 22:13:45 -080023#include <action-item.pb.h>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080024
25using namespace boost;
26using namespace std;
27
28ActionLog::ActionLog (const std::string &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080029 : SyncLog (path, localName)
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030{
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080031 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
32 ActionLog::apply_action_xFun,
33 0, 0);
34 if (res != SQLITE_OK)
35 {
36 BOOST_THROW_EXCEPTION (Error::Db ()
37 << errmsg_info_str ("Cannot create function ``apply_action''"));
38 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -080039}
40
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080041tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080042ActionLog::GetExistingRecord (const std::string &filename)
43{
44 // check if something already exists
45 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080046 int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
47 "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080048 "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080049
50 if (res != SQLITE_OK)
51 {
52 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080053 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080054 }
55
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080056 // with parent with version number + 1
57 sqlite3_int64 version = 0;
58 sqlite3_int64 parent_device_id = -1;
59 sqlite3_int64 parent_seq_no = -1;
60 string parent_device_name;
61
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080062 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080063 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080064 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080065 version = sqlite3_column_int64 (stmt, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080066
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080067 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
68 {
69 parent_device_id = sqlite3_column_int64 (stmt, 1);
70 parent_seq_no = sqlite3_column_int64 (stmt, 2);
71 parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_text (stmt, 4)));
72 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080073 }
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080074
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080075 sqlite3_finalize (stmt);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080076 return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080077}
78
Alexander Afanasyeva199f972013-01-02 19:37:26 -080079// local add action. remote action is extracted from content object
80void
81ActionLog::AddActionUpdate (const std::string &filename,
82 const Hash &hash,
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080083 time_t atime, time_t mtime, time_t ctime,
Alexander Afanasyeva199f972013-01-02 19:37:26 -080084 int mode)
85{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080086 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080087
88 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
89 sqlite3_int64 version = 0;
90 sqlite3_int64 parent_device_id = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080091 string parent_device_name;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080092 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080093
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080094 sqlite3_int64 action_time = time (0);
95
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080096 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080097
98 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080099 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
100 "(device_id, seq_no, action, filename, version, action_timestamp, "
101 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
102 "parent_device_id, parent_seq_no) "
103 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
104 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
105 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800106
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800107 // cout << "INSERT INTO ActionLog "
108 // "(device_id, seq_no, action, filename, version, action_timestamp, "
109 // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
110 // "parent_device_id, parent_seq_no) "
111 // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
112 // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
113 // " ?, ?)" << endl;
114
115 if (res != SQLITE_OK)
116 {
117 BOOST_THROW_EXCEPTION (Error::Db ()
118 << errmsg_info_str (sqlite3_errmsg (m_db))
119 );
120 // << errmsg_info_str ("Some error with prepare AddActionUpdate"));
121 }
122
123
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800124 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
125 sqlite3_bind_int64 (stmt, 2, seq_no);
126 sqlite3_bind_int (stmt, 3, 0);
127 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800128 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800129 sqlite3_bind_int64 (stmt, 6, action_time);
130
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800131 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
132
133 sqlite3_bind_int64 (stmt, 8, atime);
134 sqlite3_bind_int64 (stmt, 9, mtime);
135 sqlite3_bind_int64 (stmt, 10, ctime);
136 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800137
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800138 if (parent_device_id > 0 && parent_seq_no > 0)
139 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800140 sqlite3_bind_int64 (stmt, 12, parent_device_id);
141 sqlite3_bind_int64 (stmt, 13, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800142 }
143 else
144 {
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800145 sqlite3_bind_null (stmt, 12);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800146 sqlite3_bind_null (stmt, 13);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800147 }
148
149 sqlite3_step (stmt);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800150
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800151 // missing part: creating ContentObject for the action !!!
152
153 ActionItem item;
154 item.set_action (ActionItem::UPDATE);
155 item.set_filename (filename);
156 item.set_version (version);
157 item.set_timestamp (action_time);
158 item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800159 item.set_atime (atime);
160 item.set_mtime (mtime);
161 item.set_ctime (ctime);
162 item.set_mode (mode);
163
164 if (parent_device_id > 0 && parent_seq_no > 0)
165 {
166 item.set_parent_device_name (parent_device_name);
167 item.set_parent_seq_no (parent_seq_no);
168 }
169
170 // assign name to the action, serialize action, and create content object
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800171
172 sqlite3_finalize (stmt);
173
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800174 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800175}
176
177void
178ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
179{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800180 // not supported yet
181 BOOST_THROW_EXCEPTION (Error::Db ()
182 << errmsg_info_str ("Move operation is not yet supported"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800183}
184
185void
186ActionLog::AddActionDelete (const std::string &filename)
187{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800188 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
189
190 sqlite3_int64 version = 0;
191 sqlite3_int64 parent_device_id = -1;
192 string parent_device_name;
193 sqlite3_int64 parent_seq_no = -1;
194
195 sqlite3_int64 action_time = time (0);
196
197 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
198 if (parent_device_id < 0) // no records exist or file was already deleted
199 {
200 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
201 return;
202 }
203
204 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
205
206 sqlite3_stmt *stmt;
207 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
208 "(device_id, seq_no, action, filename, version, action_timestamp, "
209 "parent_device_id, parent_seq_no) "
210 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
211 " ?, ?)", -1, &stmt, 0);
212
213 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
214 sqlite3_bind_int64 (stmt, 2, seq_no);
215 sqlite3_bind_int (stmt, 3, 1);
216 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
217 sqlite3_bind_int64 (stmt, 5, version);
218 sqlite3_bind_int64 (stmt, 6, action_time);
219
220 sqlite3_bind_int64 (stmt, 7, parent_device_id);
221 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
222
223 sqlite3_step (stmt);
224
225 // missing part: creating ContentObject for the action !!!
226
227 ActionItem item;
228 item.set_action (ActionItem::UPDATE);
229 item.set_filename (filename);
230 item.set_version (version);
231 item.set_timestamp (action_time);
232 item.set_parent_device_name (parent_device_name);
233 item.set_parent_seq_no (parent_seq_no);
234
235 // assign name to the action, serialize action, and create content object
236
237 sqlite3_finalize (stmt);
238
239 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800240}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800241
242
243void
244ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
245{
246 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
247
248 cout << "apply_function called with " << argc << endl;
249
250 cout << "device_name: " << sqlite3_value_text (argv[0]) << endl;
251 cout << "action: " << sqlite3_value_int (argv[1]) << endl;
252 cout << "filename: " << sqlite3_value_text (argv[2]) << endl;
253
254 sqlite3_result_null (context);
255}