blob: 5bd5703a1056882523140576cff755f6f0c92bd7 [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"
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080023#include "logging.h"
Zhenkai Zhue851b952013-01-13 22:29:57 -080024#include <utility>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080025
26#include <boost/make_shared.hpp>
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080027#include <boost/thread.hpp>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080028
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080029INIT_LOGGER ("Sync.Log");
30
Alexander Afanasyeva199f972013-01-02 19:37:26 -080031using namespace boost;
32using namespace std;
Zhenkai Zhue851b952013-01-13 22:29:57 -080033using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080034
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080035static void xTrace (void*, const char* q)
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080036{
37 cout << q << endl;
38}
39
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080040const std::string INIT_DATABASE = "\
41CREATE TABLE \n\
42 SyncNodes( \n\
43 device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
44 device_name BLOB NOT NULL, \n\
45 description TEXT, \n\
46 seq_no INTEGER NOT NULL, \n\
47 last_known_locator BLOB, \n\
48 last_update TIMESTAMP \n\
49 ); \n\
50 \n\
51CREATE TRIGGER SyncNodesUpdater_trigger \n\
52 BEFORE INSERT ON SyncNodes \n\
53 FOR EACH ROW \n\
54 WHEN (SELECT device_id \n\
55 FROM SyncNodes \n\
56 WHERE device_name=NEW.device_name) \n\
57 IS NOT NULL \n\
58 BEGIN \n\
59 UPDATE SyncNodes \n\
60 SET seq_no=max(seq_no,NEW.seq_no) \n\
61 WHERE device_name=NEW.device_name; \n\
62 SELECT RAISE(IGNORE); \n\
63 END; \n\
64 \n\
65CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
66 \n\
67CREATE TABLE SyncLog( \n\
68 state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
69 state_hash BLOB NOT NULL UNIQUE, \n\
70 last_update TIMESTAMP NOT NULL \n\
71 ); \n\
72 \n\
73CREATE TABLE \n\
74 SyncStateNodes( \n\
75 id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
76 state_id INTEGER NOT NULL \n\
77 REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
78 device_id INTEGER NOT NULL \n\
79 REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
80 seq_no INTEGER NOT NULL \n\
81 ); \n\
82 \n\
83CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
84CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
85CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
86 \n\
87CREATE TRIGGER SyncLogGuard_trigger \n\
88 BEFORE INSERT ON SyncLog \n\
89 FOR EACH ROW \n\
90 WHEN (SELECT state_hash \n\
91 FROM SyncLog \n\
92 WHERE state_hash=NEW.state_hash) \n\
93 IS NOT NULL \n\
94 BEGIN \n\
95 DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
96 END; \n\
97";
98
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080099
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800100SyncLog::SyncLog (const boost::filesystem::path &path, const Ccnx::Name &localName)
101 : DbHelper (path / ".chronoshare", "sync-log.db")
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800102 , m_localName (localName)
103{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800104 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
105 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
106
Zhenkai Zhue851b952013-01-13 22:29:57 -0800107 UpdateDeviceSeqNo (localName, 0);
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800108
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800109 sqlite3_stmt *stmt;
110 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 -0800111
112 Ccnx::CcnxCharbufPtr name = m_localName;
113 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800114
115 if (sqlite3_step (stmt) == SQLITE_ROW)
116 {
117 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
118 }
119 else
120 {
121 BOOST_THROW_EXCEPTION (Error::Db ()
122 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
123 }
124 sqlite3_finalize (stmt);
125}
126
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800127sqlite3_int64
128SyncLog::GetNextLocalSeqNo ()
129{
130 sqlite3_stmt *stmt_seq;
131 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
132 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
133
134 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
135 {
136 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800137 << errmsg_info_str ("Impossible thing in SyncLog::GetNextLocalSeqNo"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800138 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800139
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800140 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800141
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800142 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800143 sqlite3_finalize (stmt_seq);
144
145 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800146
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800147 return seq_no;
148}
149
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800150HashPtr
151SyncLog::RememberStateInStateLog ()
152{
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800153 WriteLock lock (m_stateUpdateMutex);
154
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800155 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
156
157 res += sqlite3_exec (m_db, "\
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800158INSERT INTO SyncLog \
159 (state_hash, last_update) \
160 SELECT \
161 hash(device_name, seq_no), datetime('now') \
162 FROM (SELECT * FROM SyncNodes \
163 ORDER BY device_name); \
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800164", 0,0,0);
165
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800166 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, "DbError: " << sqlite3_errmsg (m_db));
167
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800168 if (res != SQLITE_OK)
169 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800170 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800171 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800172 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800173 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800174
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800175 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800176
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800177 sqlite3_stmt *insertStmt;
178 res += sqlite3_prepare (m_db, "\
179INSERT INTO SyncStateNodes \
180 (state_id, device_id, seq_no) \
181 SELECT ?, device_id, seq_no \
182 FROM SyncNodes; \
183", -1, &insertStmt, 0);
184
185 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
186 sqlite3_step (insertStmt);
187
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800188 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, "DbError: " << sqlite3_errmsg (m_db));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800189 if (res != SQLITE_OK)
190 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800191 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800192 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800193 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800194 }
195 sqlite3_finalize (insertStmt);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800196
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800197 sqlite3_stmt *getHashStmt;
198 res += sqlite3_prepare (m_db, "\
199SELECT state_hash FROM SyncLog WHERE state_id = ?\
200", -1, &getHashStmt, 0);
201 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
202
203 HashPtr retval;
204 int stepRes = sqlite3_step (getHashStmt);
205 if (stepRes == SQLITE_ROW)
206 {
207 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
208 sqlite3_column_bytes (getHashStmt, 0));
209 }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800210 else
211 {
212 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
213
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800214 _LOG_ERROR ("DbError: " << sqlite3_errmsg (m_db));
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800215 BOOST_THROW_EXCEPTION (Error::Db ()
216 << errmsg_info_str ("Not a valid hash in rememberStateInStateLog"));
217 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800218 sqlite3_finalize (getHashStmt);
219 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
220
221 if (res != SQLITE_OK)
222 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800223 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800224 BOOST_THROW_EXCEPTION (Error::Db ()
225 << errmsg_info_str ("Some error with rememberStateInStateLog"));
226 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800227
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800228 return retval;
229}
230
231sqlite3_int64
232SyncLog::LookupSyncLog (const std::string &stateHash)
233{
234 return LookupSyncLog (*Hash::FromString (stateHash));
235}
236
237sqlite3_int64
238SyncLog::LookupSyncLog (const Hash &stateHash)
239{
240 sqlite3_stmt *stmt;
241 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
242 -1, &stmt, 0);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800243
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800244 if (res != SQLITE_OK)
245 {
246 BOOST_THROW_EXCEPTION (Error::Db ()
247 << errmsg_info_str ("Cannot prepare statement"));
248 }
249
250 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
251 if (res != SQLITE_OK)
252 {
253 BOOST_THROW_EXCEPTION (Error::Db ()
254 << errmsg_info_str ("Cannot bind"));
255 }
256
257 sqlite3_int64 row = 0; // something bad
258
259 if (sqlite3_step (stmt) == SQLITE_ROW)
260 {
261 row = sqlite3_column_int64 (stmt, 0);
262 }
263
264 sqlite3_finalize (stmt);
265
266 return row;
267}
268
269void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800270SyncLog::UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800271{
272 sqlite3_stmt *stmt;
273 // update is performed using trigger
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800274 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800275 -1, &stmt, 0);
276
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800277 Ccnx::CcnxCharbufPtr nameBuf = name;
278 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800279 res += sqlite3_bind_int64 (stmt, 2, seqNo);
280 sqlite3_step (stmt);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800281
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800282 if (res != SQLITE_OK)
283 {
284 BOOST_THROW_EXCEPTION (Error::Db ()
Zhenkai Zhue851b952013-01-13 22:29:57 -0800285 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800286 }
287 sqlite3_finalize (stmt);
288}
289
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800290void
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800291SyncLog::UpdateLocalSeqNo (sqlite3_int64 seqNo)
292{
293 return UpdateDeviceSeqNo (m_localDeviceId, seqNo);
294}
295
296void
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800297SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
298{
299 sqlite3_stmt *stmt;
300 // update is performed using trigger
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800301 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800302 -1, &stmt, 0);
303
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800304 res += sqlite3_bind_int64 (stmt, 1, seqNo);
305 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800306 sqlite3_step (stmt);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800307
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800308 if (res != SQLITE_OK)
309 {
310 BOOST_THROW_EXCEPTION (Error::Db ()
311 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
312 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800313
314 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
315
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800316 sqlite3_finalize (stmt);
317}
318
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319Name
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800320SyncLog::LookupLocator (const Name &deviceName)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800321{
322 sqlite3_stmt *stmt;
323 sqlite3_prepare_v2 (m_db, "SELECT last_known_locator FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
324 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
325 sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
326 int res = sqlite3_step (stmt);
327 Name locator;
328 switch (res)
329 {
330 case SQLITE_ROW:
331 {
332 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
333 }
334 case SQLITE_DONE: break;
335 default:
336 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in LookupLocator()"));
337 }
338
339 sqlite3_finalize(stmt);
340
341 return locator;
342}
343
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800344Ccnx::Name
345SyncLog::LookupLocalLocator ()
346{
347 return LookupLocator (m_localName);
348}
349
Zhenkai Zhue851b952013-01-13 22:29:57 -0800350void
351SyncLog::UpdateLocator(const Name &deviceName, const Name &locator)
352{
353 sqlite3_stmt *stmt;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800354 sqlite3_prepare_v2 (m_db, "UPDATE SyncNodes SET last_known_locator=?,last_update=datetime('now') WHERE device_name=?;", -1, &stmt, 0);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800355 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
356 Ccnx::CcnxCharbufPtr locatorBuf = locator;
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800357 sqlite3_bind_blob (stmt, 1, locatorBuf->buf(), locatorBuf->length(), SQLITE_STATIC);
358 sqlite3_bind_blob (stmt, 2, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800359 int res = sqlite3_step (stmt);
360
361 if (res != SQLITE_OK && res != SQLITE_DONE)
362 {
363 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in UpdateLoactor()"));
364 }
365
366 sqlite3_finalize(stmt);
367}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800368
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800369void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800370SyncLog::UpdateLocalLocator (const Ccnx::Name &forwardingHint)
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800371{
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800372 if (m_localName.size () >= forwardingHint.size () &&
373 m_localName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
374 {
375 return UpdateLocator (m_localName, Name ("/")); // "directly" accesible
376 }
377 else
378 {
379 return UpdateLocator (m_localName, forwardingHint);
380 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800381}
382
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800383SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800384SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800385{
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800386 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash), includeOldSeq);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800387}
388
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800389SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800390SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800391{
392 sqlite3_stmt *stmt;
393
394 int res = sqlite3_prepare_v2 (m_db, "\
Zhenkai Zhue851b952013-01-13 22:29:57 -0800395SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800396 FROM (SELECT * \
397 FROM SyncStateNodes \
398 WHERE state_id=(SELECT state_id \
399 FROM SyncLog \
400 WHERE state_hash=:old_hash)) s_old \
401 LEFT JOIN (SELECT * \
402 FROM SyncStateNodes \
403 WHERE state_id=(SELECT state_id \
404 FROM SyncLog \
405 WHERE state_hash=:new_hash)) s_new \
406 \
407 ON s_old.device_id = s_new.device_id \
408 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
409 \
410 WHERE s_new.seq_no IS NULL OR \
411 s_old.seq_no != s_new.seq_no \
412 \
413UNION ALL \
414 \
Zhenkai Zhue851b952013-01-13 22:29:57 -0800415SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800416 FROM (SELECT * \
417 FROM SyncStateNodes \
418 WHERE state_id=(SELECT state_id \
419 FROM SyncLog \
420 WHERE state_hash=:new_hash )) s_new \
421 LEFT JOIN (SELECT * \
422 FROM SyncStateNodes \
423 WHERE state_id=(SELECT state_id \
424 FROM SyncLog \
425 WHERE state_hash=:old_hash)) s_old \
426 \
427 ON s_old.device_id = s_new.device_id \
428 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
429 \
430 WHERE s_old.seq_no IS NULL \
431", -1, &stmt, 0);
432
433 if (res != SQLITE_OK)
434 {
435 BOOST_THROW_EXCEPTION (Error::Db ()
436 << errmsg_info_str ("Some error with FindStateDifferences"));
437 }
438
439 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
440 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
441
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800442 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800443
444 // sqlite3_trace(m_db, xTrace, NULL);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800445
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800446 while (sqlite3_step (stmt) == SQLITE_ROW)
447 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800448 SyncState *state = msg->add_state ();
449
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800450 // set name
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800451 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800452
Zhenkai Zhue851b952013-01-13 22:29:57 -0800453 // locator is optional, so must check if it is null
454 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
455 {
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800456 state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1)), sqlite3_column_bytes (stmt, 1));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800457 }
458
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800459 // set old seq
460 if (includeOldSeq)
461 {
462 if (sqlite3_column_type (stmt, 2) == SQLITE_NULL)
463 {
464 // old seq is zero; we always have an initial action of zero seq
465 // other's do not need to fetch this action
466 state->set_old_seq(0);
467 }
468 else
469 {
470 sqlite3_int64 oldSeqNo = sqlite3_column_int64 (stmt, 2);
471 state->set_old_seq(oldSeqNo);
472 }
473 }
474
475 // set new seq
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800476 if (sqlite3_column_type (stmt, 3) == SQLITE_NULL)
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800477 {
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800478 state->set_type (SyncState::DELETE);
479 }
480 else
481 {
482 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800483 state->set_type (SyncState::UPDATE);
484 state->set_seq (newSeqNo);
485 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800486
487 // std::cout << sqlite3_column_text (stmt, 0) <<
488 // ": from " << sqlite3_column_int64 (stmt, 1) <<
489 // " to " << sqlite3_column_int64 (stmt, 2) <<
490 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800491 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800492 sqlite3_finalize (stmt);
493
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800494 // sqlite3_trace(m_db, NULL, NULL);
495
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800496 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800497}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800498
499sqlite3_int64
500SyncLog::SeqNo(const Name &name)
501{
502 sqlite3_stmt *stmt;
503 sqlite3_int64 seq = -1;
504 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
505 Ccnx::CcnxCharbufPtr nameBuf = name;
506 sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
507 if (sqlite3_step (stmt) == SQLITE_ROW)
508 {
509 seq = sqlite3_column_int64 (stmt, 0);
510 }
511
512 return seq;
513}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800514
515sqlite3_int64
516SyncLog::LogSize ()
517{
518 sqlite3_stmt *stmt;
519 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SyncLog", -1, &stmt, 0);
520
521 sqlite3_int64 retval = -1;
522 if (sqlite3_step (stmt) == SQLITE_ROW)
523 {
524 retval = sqlite3_column_int64 (stmt, 0);
525 }
526
527 return retval;
528}
529