blob: 4b0984a41d131eea788887fd4bc6dbad0b43ca66 [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) 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 "sync-log.h"
23
24#include <boost/make_shared.hpp>
25
26using namespace boost;
27using namespace std;
28
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080030 : DbHelper (path)
31 , m_localName (localName)
32{
33 UpdateDeviceSeqno (localName, 0);
34
35 sqlite3_stmt *stmt;
36 int res = sqlite3_prepare_v2 (m_db, "SELECT device_id, seq_no FROM SyncNodes WHERE device_name=?", -1, &stmt, 0);
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080037
38 Ccnx::CcnxCharbufPtr name = m_localName;
39 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080040
41 if (sqlite3_step (stmt) == SQLITE_ROW)
42 {
43 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
44 }
45 else
46 {
47 BOOST_THROW_EXCEPTION (Error::Db ()
48 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
49 }
50 sqlite3_finalize (stmt);
51}
52
53
54sqlite3_int64
55SyncLog::GetNextLocalSeqNo ()
56{
57 sqlite3_stmt *stmt_seq;
58 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
59 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
60
61 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
62 {
63 BOOST_THROW_EXCEPTION (Error::Db ()
64 << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
65 }
66
67 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
68 sqlite3_finalize (stmt_seq);
69
70 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
71
72 return seq_no;
73}
74
75
Alexander Afanasyeva199f972013-01-02 19:37:26 -080076HashPtr
77SyncLog::RememberStateInStateLog ()
78{
79 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
80
81 res += sqlite3_exec (m_db, "\
82INSERT INTO SyncLog \
83 (state_hash, last_update) \
84 SELECT \
85 hash(device_name, seq_no), datetime('now') \
86 FROM SyncNodes \
87 ORDER BY device_name; \
88", 0,0,0);
89
90 if (res != SQLITE_OK)
91 {
92 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080093 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -080094 }
95
96 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
97
98 sqlite3_stmt *insertStmt;
99 res += sqlite3_prepare (m_db, "\
100INSERT INTO SyncStateNodes \
101 (state_id, device_id, seq_no) \
102 SELECT ?, device_id, seq_no \
103 FROM SyncNodes; \
104", -1, &insertStmt, 0);
105
106 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
107 sqlite3_step (insertStmt);
108
109 if (res != SQLITE_OK)
110 {
111 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800112 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800113 }
114 sqlite3_finalize (insertStmt);
115
116 sqlite3_stmt *getHashStmt;
117 res += sqlite3_prepare (m_db, "\
118SELECT state_hash FROM SyncLog WHERE state_id = ?\
119", -1, &getHashStmt, 0);
120 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
121
122 HashPtr retval;
123 int stepRes = sqlite3_step (getHashStmt);
124 if (stepRes == SQLITE_ROW)
125 {
126 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
127 sqlite3_column_bytes (getHashStmt, 0));
128 }
129 sqlite3_finalize (getHashStmt);
130 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
131
132 if (res != SQLITE_OK)
133 {
134 BOOST_THROW_EXCEPTION (Error::Db ()
135 << errmsg_info_str ("Some error with rememberStateInStateLog"));
136 }
137
138 return retval;
139}
140
141sqlite3_int64
142SyncLog::LookupSyncLog (const std::string &stateHash)
143{
144 return LookupSyncLog (*Hash::FromString (stateHash));
145}
146
147sqlite3_int64
148SyncLog::LookupSyncLog (const Hash &stateHash)
149{
150 sqlite3_stmt *stmt;
151 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
152 -1, &stmt, 0);
153
154 if (res != SQLITE_OK)
155 {
156 BOOST_THROW_EXCEPTION (Error::Db ()
157 << errmsg_info_str ("Cannot prepare statement"));
158 }
159
160 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
161 if (res != SQLITE_OK)
162 {
163 BOOST_THROW_EXCEPTION (Error::Db ()
164 << errmsg_info_str ("Cannot bind"));
165 }
166
167 sqlite3_int64 row = 0; // something bad
168
169 if (sqlite3_step (stmt) == SQLITE_ROW)
170 {
171 row = sqlite3_column_int64 (stmt, 0);
172 }
173
174 sqlite3_finalize (stmt);
175
176 return row;
177}
178
179void
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800180SyncLog::UpdateDeviceSeqno (const Ccnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800181{
182 sqlite3_stmt *stmt;
183 // update is performed using trigger
184 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
185 -1, &stmt, 0);
186
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800187 Ccnx::CcnxCharbufPtr nameBuf = name;
188 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800189 res += sqlite3_bind_int64 (stmt, 2, seqNo);
190 sqlite3_step (stmt);
191
192 if (res != SQLITE_OK)
193 {
194 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800195 << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800196 }
197 sqlite3_finalize (stmt);
198}
199
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800200void
201SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
202{
203 sqlite3_stmt *stmt;
204 // update is performed using trigger
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800205 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800206 -1, &stmt, 0);
207
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800208 res += sqlite3_bind_int64 (stmt, 1, seqNo);
209 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800210 sqlite3_step (stmt);
211
212 if (res != SQLITE_OK)
213 {
214 BOOST_THROW_EXCEPTION (Error::Db ()
215 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
216 }
217 sqlite3_finalize (stmt);
218}
219
220
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800221SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800222SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
223{
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800224 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800225}
226
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800227SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800228SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
229{
230 sqlite3_stmt *stmt;
231
232 int res = sqlite3_prepare_v2 (m_db, "\
233SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
234 FROM (SELECT * \
235 FROM SyncStateNodes \
236 WHERE state_id=(SELECT state_id \
237 FROM SyncLog \
238 WHERE state_hash=:old_hash)) s_old \
239 LEFT JOIN (SELECT * \
240 FROM SyncStateNodes \
241 WHERE state_id=(SELECT state_id \
242 FROM SyncLog \
243 WHERE state_hash=:new_hash)) s_new \
244 \
245 ON s_old.device_id = s_new.device_id \
246 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
247 \
248 WHERE s_new.seq_no IS NULL OR \
249 s_old.seq_no != s_new.seq_no \
250 \
251UNION ALL \
252 \
253SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
254 FROM (SELECT * \
255 FROM SyncStateNodes \
256 WHERE state_id=(SELECT state_id \
257 FROM SyncLog \
258 WHERE state_hash=:new_hash )) s_new \
259 LEFT JOIN (SELECT * \
260 FROM SyncStateNodes \
261 WHERE state_id=(SELECT state_id \
262 FROM SyncLog \
263 WHERE state_hash=:old_hash)) s_old \
264 \
265 ON s_old.device_id = s_new.device_id \
266 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
267 \
268 WHERE s_old.seq_no IS NULL \
269", -1, &stmt, 0);
270
271 if (res != SQLITE_OK)
272 {
273 BOOST_THROW_EXCEPTION (Error::Db ()
274 << errmsg_info_str ("Some error with FindStateDifferences"));
275 }
276
277 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
278 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
279
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800280 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
281
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800282 while (sqlite3_step (stmt) == SQLITE_ROW)
283 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800284 SyncState *state = msg->add_state ();
285
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800286 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800287
288 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2);
289 if (newSeqNo > 0)
290 {
291 state->set_type (SyncState::UPDATE);
292 state->set_seq (newSeqNo);
293 }
294 else
295 state->set_type (SyncState::DELETE);
296
297 // std::cout << sqlite3_column_text (stmt, 0) <<
298 // ": from " << sqlite3_column_int64 (stmt, 1) <<
299 // " to " << sqlite3_column_int64 (stmt, 2) <<
300 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800301 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800302 sqlite3_finalize (stmt);
303
304 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800305}