blob: 8375142ede2d600f8ea0b6d1661cd2d394d478ce [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"
Zhenkai Zhue851b952013-01-13 22:29:57 -080023#include <utility>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080024
25#include <boost/make_shared.hpp>
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080026#include <boost/thread.hpp>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027
28using namespace boost;
29using namespace std;
Zhenkai Zhue851b952013-01-13 22:29:57 -080030using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080031
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080032void xTrace (void*, const char* q)
33{
34 cout << q << endl;
35}
36
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080037
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080038SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080039 : DbHelper (path)
40 , m_localName (localName)
41{
Zhenkai Zhue851b952013-01-13 22:29:57 -080042 UpdateDeviceSeqNo (localName, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080043
44 sqlite3_stmt *stmt;
45 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 -080046
47 Ccnx::CcnxCharbufPtr name = m_localName;
48 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080049
50 if (sqlite3_step (stmt) == SQLITE_ROW)
51 {
52 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
53 }
54 else
55 {
56 BOOST_THROW_EXCEPTION (Error::Db ()
57 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
58 }
59 sqlite3_finalize (stmt);
60}
61
Zhenkai Zhue851b952013-01-13 22:29:57 -080062void
63SyncLog::initYP(map<Name, Name> &yp)
64{
65 sqlite3_stmt *stmt;
66 sqlite3_prepare_v2(m_db, "SELECT device_name, last_known_locator FROM SyncNodes;", -1, &stmt, 0);
67
68 while (sqlite3_step(stmt) == SQLITE_ROW)
69 {
70 Name deviceName((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
71 Name locator;
72 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
73 {
74 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
75 }
76 yp.insert(make_pair(deviceName, locator));
77 }
78
79 sqlite3_finalize(stmt);
80}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080081
82sqlite3_int64
83SyncLog::GetNextLocalSeqNo ()
84{
85 sqlite3_stmt *stmt_seq;
86 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
87 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
88
89 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
90 {
91 BOOST_THROW_EXCEPTION (Error::Db ()
92 << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
93 }
94
95 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
96 sqlite3_finalize (stmt_seq);
97
98 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
99
100 return seq_no;
101}
102
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800103HashPtr
104SyncLog::RememberStateInStateLog ()
105{
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800106 WriteLock lock (m_stateUpdateMutex);
107
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800108 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
109
110 res += sqlite3_exec (m_db, "\
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800111INSERT INTO SyncLog \
112 (state_hash, last_update) \
113 SELECT \
114 hash(device_name, seq_no), datetime('now') \
115 FROM (SELECT * FROM SyncNodes \
116 ORDER BY device_name); \
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800117", 0,0,0);
118
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800119 // std::cout << sqlite3_errmsg (m_db) << std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800120 if (res != SQLITE_OK)
121 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800122 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800123 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800124 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800125 }
126
127 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
128
129 sqlite3_stmt *insertStmt;
130 res += sqlite3_prepare (m_db, "\
131INSERT INTO SyncStateNodes \
132 (state_id, device_id, seq_no) \
133 SELECT ?, device_id, seq_no \
134 FROM SyncNodes; \
135", -1, &insertStmt, 0);
136
137 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
138 sqlite3_step (insertStmt);
139
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800140 // std::cout << sqlite3_errmsg (m_db) << std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800141 if (res != SQLITE_OK)
142 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800143 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800144 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800145 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800146 }
147 sqlite3_finalize (insertStmt);
148
149 sqlite3_stmt *getHashStmt;
150 res += sqlite3_prepare (m_db, "\
151SELECT state_hash FROM SyncLog WHERE state_id = ?\
152", -1, &getHashStmt, 0);
153 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
154
155 HashPtr retval;
156 int stepRes = sqlite3_step (getHashStmt);
157 if (stepRes == SQLITE_ROW)
158 {
159 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
160 sqlite3_column_bytes (getHashStmt, 0));
161 }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800162 else
163 {
164 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
165
166 // std::cout << sqlite3_errmsg (m_db) << std::endl;
167 BOOST_THROW_EXCEPTION (Error::Db ()
168 << errmsg_info_str ("Not a valid hash in rememberStateInStateLog"));
169 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800170 sqlite3_finalize (getHashStmt);
171 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
172
173 if (res != SQLITE_OK)
174 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800175 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800176 BOOST_THROW_EXCEPTION (Error::Db ()
177 << errmsg_info_str ("Some error with rememberStateInStateLog"));
178 }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800179
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800180 return retval;
181}
182
183sqlite3_int64
184SyncLog::LookupSyncLog (const std::string &stateHash)
185{
186 return LookupSyncLog (*Hash::FromString (stateHash));
187}
188
189sqlite3_int64
190SyncLog::LookupSyncLog (const Hash &stateHash)
191{
192 sqlite3_stmt *stmt;
193 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
194 -1, &stmt, 0);
195
196 if (res != SQLITE_OK)
197 {
198 BOOST_THROW_EXCEPTION (Error::Db ()
199 << errmsg_info_str ("Cannot prepare statement"));
200 }
201
202 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
203 if (res != SQLITE_OK)
204 {
205 BOOST_THROW_EXCEPTION (Error::Db ()
206 << errmsg_info_str ("Cannot bind"));
207 }
208
209 sqlite3_int64 row = 0; // something bad
210
211 if (sqlite3_step (stmt) == SQLITE_ROW)
212 {
213 row = sqlite3_column_int64 (stmt, 0);
214 }
215
216 sqlite3_finalize (stmt);
217
218 return row;
219}
220
221void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800222SyncLog::UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800223{
224 sqlite3_stmt *stmt;
225 // update is performed using trigger
226 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
227 -1, &stmt, 0);
228
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800229 Ccnx::CcnxCharbufPtr nameBuf = name;
230 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800231 res += sqlite3_bind_int64 (stmt, 2, seqNo);
232 sqlite3_step (stmt);
233
234 if (res != SQLITE_OK)
235 {
236 BOOST_THROW_EXCEPTION (Error::Db ()
Zhenkai Zhue851b952013-01-13 22:29:57 -0800237 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800238 }
239 sqlite3_finalize (stmt);
240}
241
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800242void
243SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
244{
245 sqlite3_stmt *stmt;
246 // update is performed using trigger
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800247 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800248 -1, &stmt, 0);
249
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800250 res += sqlite3_bind_int64 (stmt, 1, seqNo);
251 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800252 sqlite3_step (stmt);
253
254 if (res != SQLITE_OK)
255 {
256 BOOST_THROW_EXCEPTION (Error::Db ()
257 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
258 }
259 sqlite3_finalize (stmt);
260}
261
Zhenkai Zhue851b952013-01-13 22:29:57 -0800262Name
263SyncLog::LookupLocator(const Name &deviceName)
264{
265 sqlite3_stmt *stmt;
266 sqlite3_prepare_v2 (m_db, "SELECT last_known_locator FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
267 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
268 sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
269 int res = sqlite3_step (stmt);
270 Name locator;
271 switch (res)
272 {
273 case SQLITE_ROW:
274 {
275 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
276 }
277 case SQLITE_DONE: break;
278 default:
279 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in LookupLocator()"));
280 }
281
282 sqlite3_finalize(stmt);
283
284 return locator;
285}
286
287void
288SyncLog::UpdateLocator(const Name &deviceName, const Name &locator)
289{
290 sqlite3_stmt *stmt;
291 sqlite3_prepare_v2 (m_db, "UPDATE SyncNodes SET last_known_locator=? WHERE device_name=?;", -1, &stmt, 0);
292 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
293 Ccnx::CcnxCharbufPtr locatorBuf = locator;
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800294 sqlite3_bind_blob (stmt, 1, locatorBuf->buf(), locatorBuf->length(), SQLITE_STATIC);
295 sqlite3_bind_blob (stmt, 2, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800296 int res = sqlite3_step (stmt);
297
298 if (res != SQLITE_OK && res != SQLITE_DONE)
299 {
300 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in UpdateLoactor()"));
301 }
302
303 sqlite3_finalize(stmt);
304}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800305
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800306SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800307SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
308{
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800309 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800310}
311
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800312SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800313SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
314{
315 sqlite3_stmt *stmt;
316
317 int res = sqlite3_prepare_v2 (m_db, "\
Zhenkai Zhue851b952013-01-13 22:29:57 -0800318SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800319 FROM (SELECT * \
320 FROM SyncStateNodes \
321 WHERE state_id=(SELECT state_id \
322 FROM SyncLog \
323 WHERE state_hash=:old_hash)) s_old \
324 LEFT JOIN (SELECT * \
325 FROM SyncStateNodes \
326 WHERE state_id=(SELECT state_id \
327 FROM SyncLog \
328 WHERE state_hash=:new_hash)) s_new \
329 \
330 ON s_old.device_id = s_new.device_id \
331 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
332 \
333 WHERE s_new.seq_no IS NULL OR \
334 s_old.seq_no != s_new.seq_no \
335 \
336UNION ALL \
337 \
Zhenkai Zhue851b952013-01-13 22:29:57 -0800338SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800339 FROM (SELECT * \
340 FROM SyncStateNodes \
341 WHERE state_id=(SELECT state_id \
342 FROM SyncLog \
343 WHERE state_hash=:new_hash )) s_new \
344 LEFT JOIN (SELECT * \
345 FROM SyncStateNodes \
346 WHERE state_id=(SELECT state_id \
347 FROM SyncLog \
348 WHERE state_hash=:old_hash)) s_old \
349 \
350 ON s_old.device_id = s_new.device_id \
351 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
352 \
353 WHERE s_old.seq_no IS NULL \
354", -1, &stmt, 0);
355
356 if (res != SQLITE_OK)
357 {
358 BOOST_THROW_EXCEPTION (Error::Db ()
359 << errmsg_info_str ("Some error with FindStateDifferences"));
360 }
361
362 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
363 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
364
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800365 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800366
367 // sqlite3_trace(m_db, xTrace, NULL);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800368
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800369 while (sqlite3_step (stmt) == SQLITE_ROW)
370 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800371 SyncState *state = msg->add_state ();
372
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800373 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800374
Zhenkai Zhue851b952013-01-13 22:29:57 -0800375 // locator is optional, so must check if it is null
376 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
377 {
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800378 state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1)), sqlite3_column_bytes (stmt, 1));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800379 }
380
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800381 if (sqlite3_column_type (stmt, 3) == SQLITE_NULL)
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800382 {
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800383 state->set_type (SyncState::DELETE);
384 }
385 else
386 {
387 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800388 state->set_type (SyncState::UPDATE);
389 state->set_seq (newSeqNo);
390 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800391
392 // std::cout << sqlite3_column_text (stmt, 0) <<
393 // ": from " << sqlite3_column_int64 (stmt, 1) <<
394 // " to " << sqlite3_column_int64 (stmt, 2) <<
395 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800396 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800397 sqlite3_finalize (stmt);
398
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800399 // sqlite3_trace(m_db, NULL, NULL);
400
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800401 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800402}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800403
404sqlite3_int64
405SyncLog::SeqNo(const Name &name)
406{
407 sqlite3_stmt *stmt;
408 sqlite3_int64 seq = -1;
409 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
410 Ccnx::CcnxCharbufPtr nameBuf = name;
411 sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
412 if (sqlite3_step (stmt) == SQLITE_ROW)
413 {
414 seq = sqlite3_column_int64 (stmt, 0);
415 }
416
417 return seq;
418}