blob: 41166e74a5c58977cf987246cdcfec0b64520b8a [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{
31}
32
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080033tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080034ActionLog::GetExistingRecord (const std::string &filename)
35{
36 // check if something already exists
37 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080038 int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
39 "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
40 "WHERE filename=? ORDER BY a.version DESC,a.device_id DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080041
42 if (res != SQLITE_OK)
43 {
44 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080045 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080046 }
47
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080048 // with parent with version number + 1
49 sqlite3_int64 version = 0;
50 sqlite3_int64 parent_device_id = -1;
51 sqlite3_int64 parent_seq_no = -1;
52 string parent_device_name;
53
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080054 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080055 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080056 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080057 version = sqlite3_column_int64 (stmt, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080058
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080059 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
60 {
61 parent_device_id = sqlite3_column_int64 (stmt, 1);
62 parent_seq_no = sqlite3_column_int64 (stmt, 2);
63 parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_text (stmt, 4)));
64 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080065 }
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080066
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080067 sqlite3_finalize (stmt);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080068 return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080069}
70
Alexander Afanasyeva199f972013-01-02 19:37:26 -080071// local add action. remote action is extracted from content object
72void
73ActionLog::AddActionUpdate (const std::string &filename,
74 const Hash &hash,
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080075 time_t atime, time_t mtime, time_t ctime,
Alexander Afanasyeva199f972013-01-02 19:37:26 -080076 int mode)
77{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080078 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080079
80 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
81 sqlite3_int64 version = 0;
82 sqlite3_int64 parent_device_id = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080083 string parent_device_name;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080084 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080085
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080086 sqlite3_int64 action_time = time (0);
87
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080088 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080089
90 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080091 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
92 "(device_id, seq_no, action, filename, version, action_timestamp, "
93 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
94 "parent_device_id, parent_seq_no) "
95 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
96 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
97 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080098
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080099 // cout << "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 // " ?, ?)" << endl;
106
107 if (res != SQLITE_OK)
108 {
109 BOOST_THROW_EXCEPTION (Error::Db ()
110 << errmsg_info_str (sqlite3_errmsg (m_db))
111 );
112 // << errmsg_info_str ("Some error with prepare AddActionUpdate"));
113 }
114
115
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800116 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
117 sqlite3_bind_int64 (stmt, 2, seq_no);
118 sqlite3_bind_int (stmt, 3, 0);
119 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800120 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800121 sqlite3_bind_int64 (stmt, 6, action_time);
122
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800123 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
124
125 sqlite3_bind_int64 (stmt, 8, atime);
126 sqlite3_bind_int64 (stmt, 9, mtime);
127 sqlite3_bind_int64 (stmt, 10, ctime);
128 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800129
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800130 if (parent_device_id > 0 && parent_seq_no > 0)
131 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800132 sqlite3_bind_int64 (stmt, 12, parent_device_id);
133 sqlite3_bind_int64 (stmt, 13, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800134 }
135 else
136 {
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800137 sqlite3_bind_null (stmt, 12);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800138 sqlite3_bind_null (stmt, 13);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800139 }
140
141 sqlite3_step (stmt);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800142
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800143 // missing part: creating ContentObject for the action !!!
144
145 ActionItem item;
146 item.set_action (ActionItem::UPDATE);
147 item.set_filename (filename);
148 item.set_version (version);
149 item.set_timestamp (action_time);
150 item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800151 item.set_atime (atime);
152 item.set_mtime (mtime);
153 item.set_ctime (ctime);
154 item.set_mode (mode);
155
156 if (parent_device_id > 0 && parent_seq_no > 0)
157 {
158 item.set_parent_device_name (parent_device_name);
159 item.set_parent_seq_no (parent_seq_no);
160 }
161
162 // assign name to the action, serialize action, and create content object
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800163
164 sqlite3_finalize (stmt);
165
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800166 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800167}
168
169void
170ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
171{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800172 // not supported yet
173 BOOST_THROW_EXCEPTION (Error::Db ()
174 << errmsg_info_str ("Move operation is not yet supported"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800175}
176
177void
178ActionLog::AddActionDelete (const std::string &filename)
179{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800180 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
181
182 sqlite3_int64 version = 0;
183 sqlite3_int64 parent_device_id = -1;
184 string parent_device_name;
185 sqlite3_int64 parent_seq_no = -1;
186
187 sqlite3_int64 action_time = time (0);
188
189 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
190 if (parent_device_id < 0) // no records exist or file was already deleted
191 {
192 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
193 return;
194 }
195
196 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
197
198 sqlite3_stmt *stmt;
199 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
200 "(device_id, seq_no, action, filename, version, action_timestamp, "
201 "parent_device_id, parent_seq_no) "
202 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
203 " ?, ?)", -1, &stmt, 0);
204
205 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
206 sqlite3_bind_int64 (stmt, 2, seq_no);
207 sqlite3_bind_int (stmt, 3, 1);
208 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
209 sqlite3_bind_int64 (stmt, 5, version);
210 sqlite3_bind_int64 (stmt, 6, action_time);
211
212 sqlite3_bind_int64 (stmt, 7, parent_device_id);
213 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
214
215 sqlite3_step (stmt);
216
217 // missing part: creating ContentObject for the action !!!
218
219 ActionItem item;
220 item.set_action (ActionItem::UPDATE);
221 item.set_filename (filename);
222 item.set_version (version);
223 item.set_timestamp (action_time);
224 item.set_parent_device_name (parent_device_name);
225 item.set_parent_seq_no (parent_seq_no);
226
227 // assign name to the action, serialize action, and create content object
228
229 sqlite3_finalize (stmt);
230
231 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800232}