blob: b9f4209eacc629fa448f580dac4977fc8d5f8c32 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyeva199f972013-01-02 19:37:26 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyeva199f972013-01-02 19:37:26 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyeva199f972013-01-02 19:37:26 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyeva199f972013-01-02 19:37:26 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyeva199f972013-01-02 19:37:26 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "sync-log.hpp"
22#include "logging.hpp"
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
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080028INIT_LOGGER ("Sync.Log");
29
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030using namespace boost;
31using namespace std;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070032using namespace Ndnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080033
Alexander Afanasyevd7245812013-02-13 21:06:57 -080034// static void xTrace (void*, const char* q)
35// {
36// cout << q << endl;
37// }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080038
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080039const std::string INIT_DATABASE = "\
40CREATE TABLE \n\
41 SyncNodes( \n\
42 device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
43 device_name BLOB NOT NULL, \n\
44 description TEXT, \n\
45 seq_no INTEGER NOT NULL, \n\
46 last_known_locator BLOB, \n\
47 last_update TIMESTAMP \n\
48 ); \n\
49 \n\
50CREATE TRIGGER SyncNodesUpdater_trigger \n\
51 BEFORE INSERT ON SyncNodes \n\
52 FOR EACH ROW \n\
53 WHEN (SELECT device_id \n\
54 FROM SyncNodes \n\
55 WHERE device_name=NEW.device_name) \n\
56 IS NOT NULL \n\
57 BEGIN \n\
58 UPDATE SyncNodes \n\
59 SET seq_no=max(seq_no,NEW.seq_no) \n\
60 WHERE device_name=NEW.device_name; \n\
61 SELECT RAISE(IGNORE); \n\
62 END; \n\
63 \n\
64CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
65 \n\
66CREATE TABLE SyncLog( \n\
67 state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
68 state_hash BLOB NOT NULL UNIQUE, \n\
69 last_update TIMESTAMP NOT NULL \n\
70 ); \n\
71 \n\
72CREATE TABLE \n\
73 SyncStateNodes( \n\
74 id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
75 state_id INTEGER NOT NULL \n\
76 REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
77 device_id INTEGER NOT NULL \n\
78 REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
79 seq_no INTEGER NOT NULL \n\
80 ); \n\
81 \n\
82CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
83CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
84CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
85 \n\
86CREATE TRIGGER SyncLogGuard_trigger \n\
87 BEFORE INSERT ON SyncLog \n\
88 FOR EACH ROW \n\
89 WHEN (SELECT state_hash \n\
90 FROM SyncLog \n\
91 WHERE state_hash=NEW.state_hash) \n\
92 IS NOT NULL \n\
93 BEGIN \n\
94 DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
95 END; \n\
96";
97
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080098
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070099SyncLog::SyncLog (const boost::filesystem::path &path, const Ndnx::Name &localName)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800100 : DbHelper (path / ".chronoshare", "sync-log.db")
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800101 , m_localName (localName)
102{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800103 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
104 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
105
Zhenkai Zhue851b952013-01-13 22:29:57 -0800106 UpdateDeviceSeqNo (localName, 0);
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800107
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800108 sqlite3_stmt *stmt;
109 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 -0800110
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700111 Ndnx::NdnxCharbufPtr name = m_localName;
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800112 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800113
114 if (sqlite3_step (stmt) == SQLITE_ROW)
115 {
116 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
117 }
118 else
119 {
120 BOOST_THROW_EXCEPTION (Error::Db ()
121 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
122 }
123 sqlite3_finalize (stmt);
124}
125
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800126sqlite3_int64
127SyncLog::GetNextLocalSeqNo ()
128{
129 sqlite3_stmt *stmt_seq;
130 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
131 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
132
133 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
134 {
135 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800136 << errmsg_info_str ("Impossible thing in SyncLog::GetNextLocalSeqNo"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800137 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800138
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800139 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800140
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800141 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800142 sqlite3_finalize (stmt_seq);
143
144 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800145
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800146 return seq_no;
147}
148
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800149HashPtr
150SyncLog::RememberStateInStateLog ()
151{
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800152 WriteLock lock (m_stateUpdateMutex);
153
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800154 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
155
156 res += sqlite3_exec (m_db, "\
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800157INSERT INTO SyncLog \
158 (state_hash, last_update) \
159 SELECT \
160 hash(device_name, seq_no), datetime('now') \
161 FROM (SELECT * FROM SyncNodes \
162 ORDER BY device_name); \
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800163", 0,0,0);
164
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800165 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, "DbError: " << sqlite3_errmsg (m_db));
166
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800167 if (res != SQLITE_OK)
168 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800169 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800170 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800171 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800172 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800173
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800174 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800175
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800176 sqlite3_stmt *insertStmt;
177 res += sqlite3_prepare (m_db, "\
178INSERT INTO SyncStateNodes \
179 (state_id, device_id, seq_no) \
180 SELECT ?, device_id, seq_no \
181 FROM SyncNodes; \
182", -1, &insertStmt, 0);
183
184 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
185 sqlite3_step (insertStmt);
186
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800187 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, "DbError: " << sqlite3_errmsg (m_db));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800188 if (res != SQLITE_OK)
189 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800190 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800191 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800192 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800193 }
194 sqlite3_finalize (insertStmt);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800195
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800196 sqlite3_stmt *getHashStmt;
197 res += sqlite3_prepare (m_db, "\
198SELECT state_hash FROM SyncLog WHERE state_id = ?\
199", -1, &getHashStmt, 0);
200 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
201
202 HashPtr retval;
203 int stepRes = sqlite3_step (getHashStmt);
204 if (stepRes == SQLITE_ROW)
205 {
206 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
207 sqlite3_column_bytes (getHashStmt, 0));
208 }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800209 else
210 {
211 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
212
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800213 _LOG_ERROR ("DbError: " << sqlite3_errmsg (m_db));
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800214 BOOST_THROW_EXCEPTION (Error::Db ()
215 << errmsg_info_str ("Not a valid hash in rememberStateInStateLog"));
216 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800217 sqlite3_finalize (getHashStmt);
218 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
219
220 if (res != SQLITE_OK)
221 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800222 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800223 BOOST_THROW_EXCEPTION (Error::Db ()
224 << errmsg_info_str ("Some error with rememberStateInStateLog"));
225 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800226
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800227 return retval;
228}
229
230sqlite3_int64
231SyncLog::LookupSyncLog (const std::string &stateHash)
232{
233 return LookupSyncLog (*Hash::FromString (stateHash));
234}
235
236sqlite3_int64
237SyncLog::LookupSyncLog (const Hash &stateHash)
238{
239 sqlite3_stmt *stmt;
240 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
241 -1, &stmt, 0);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800242
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800243 if (res != SQLITE_OK)
244 {
245 BOOST_THROW_EXCEPTION (Error::Db ()
246 << errmsg_info_str ("Cannot prepare statement"));
247 }
248
249 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
250 if (res != SQLITE_OK)
251 {
252 BOOST_THROW_EXCEPTION (Error::Db ()
253 << errmsg_info_str ("Cannot bind"));
254 }
255
256 sqlite3_int64 row = 0; // something bad
257
258 if (sqlite3_step (stmt) == SQLITE_ROW)
259 {
260 row = sqlite3_column_int64 (stmt, 0);
261 }
262
263 sqlite3_finalize (stmt);
264
265 return row;
266}
267
268void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700269SyncLog::UpdateDeviceSeqNo (const Ndnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800270{
271 sqlite3_stmt *stmt;
272 // update is performed using trigger
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800273 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800274 -1, &stmt, 0);
275
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700276 Ndnx::NdnxCharbufPtr nameBuf = name;
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800277 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800278 res += sqlite3_bind_int64 (stmt, 2, seqNo);
279 sqlite3_step (stmt);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800280
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800281 if (res != SQLITE_OK)
282 {
283 BOOST_THROW_EXCEPTION (Error::Db ()
Zhenkai Zhue851b952013-01-13 22:29:57 -0800284 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800285 }
286 sqlite3_finalize (stmt);
287}
288
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800289void
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800290SyncLog::UpdateLocalSeqNo (sqlite3_int64 seqNo)
291{
292 return UpdateDeviceSeqNo (m_localDeviceId, seqNo);
293}
294
295void
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800296SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
297{
298 sqlite3_stmt *stmt;
299 // update is performed using trigger
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800300 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800301 -1, &stmt, 0);
302
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800303 res += sqlite3_bind_int64 (stmt, 1, seqNo);
304 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800305 sqlite3_step (stmt);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800306
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800307 if (res != SQLITE_OK)
308 {
309 BOOST_THROW_EXCEPTION (Error::Db ()
310 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
311 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800312
313 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
314
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800315 sqlite3_finalize (stmt);
316}
317
Zhenkai Zhue851b952013-01-13 22:29:57 -0800318Name
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800319SyncLog::LookupLocator (const Name &deviceName)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800320{
321 sqlite3_stmt *stmt;
322 sqlite3_prepare_v2 (m_db, "SELECT last_known_locator FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700323 Ndnx::NdnxCharbufPtr nameBuf = deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800324 sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
325 int res = sqlite3_step (stmt);
326 Name locator;
327 switch (res)
328 {
329 case SQLITE_ROW:
330 {
331 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
332 }
333 case SQLITE_DONE: break;
334 default:
335 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in LookupLocator()"));
336 }
337
338 sqlite3_finalize(stmt);
339
340 return locator;
341}
342
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700343Ndnx::Name
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800344SyncLog::LookupLocalLocator ()
345{
346 return LookupLocator (m_localName);
347}
348
Zhenkai Zhue851b952013-01-13 22:29:57 -0800349void
350SyncLog::UpdateLocator(const Name &deviceName, const Name &locator)
351{
352 sqlite3_stmt *stmt;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800353 sqlite3_prepare_v2 (m_db, "UPDATE SyncNodes SET last_known_locator=?,last_update=datetime('now') WHERE device_name=?;", -1, &stmt, 0);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700354 Ndnx::NdnxCharbufPtr nameBuf = deviceName;
355 Ndnx::NdnxCharbufPtr locatorBuf = locator;
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800356 sqlite3_bind_blob (stmt, 1, locatorBuf->buf(), locatorBuf->length(), SQLITE_STATIC);
357 sqlite3_bind_blob (stmt, 2, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800358 int res = sqlite3_step (stmt);
359
360 if (res != SQLITE_OK && res != SQLITE_DONE)
361 {
362 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in UpdateLoactor()"));
363 }
364
365 sqlite3_finalize(stmt);
366}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800367
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800368void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700369SyncLog::UpdateLocalLocator (const Ndnx::Name &forwardingHint)
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800370{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800371 return UpdateLocator (m_localName, forwardingHint);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800372}
373
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800374SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800375SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800376{
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800377 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash), includeOldSeq);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800378}
379
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800380SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800381SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800382{
383 sqlite3_stmt *stmt;
384
385 int res = sqlite3_prepare_v2 (m_db, "\
Zhenkai Zhue851b952013-01-13 22:29:57 -0800386SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800387 FROM (SELECT * \
388 FROM SyncStateNodes \
389 WHERE state_id=(SELECT state_id \
390 FROM SyncLog \
391 WHERE state_hash=:old_hash)) s_old \
392 LEFT JOIN (SELECT * \
393 FROM SyncStateNodes \
394 WHERE state_id=(SELECT state_id \
395 FROM SyncLog \
396 WHERE state_hash=:new_hash)) s_new \
397 \
398 ON s_old.device_id = s_new.device_id \
399 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
400 \
401 WHERE s_new.seq_no IS NULL OR \
402 s_old.seq_no != s_new.seq_no \
403 \
404UNION ALL \
405 \
Zhenkai Zhue851b952013-01-13 22:29:57 -0800406SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800407 FROM (SELECT * \
408 FROM SyncStateNodes \
409 WHERE state_id=(SELECT state_id \
410 FROM SyncLog \
411 WHERE state_hash=:new_hash )) s_new \
412 LEFT JOIN (SELECT * \
413 FROM SyncStateNodes \
414 WHERE state_id=(SELECT state_id \
415 FROM SyncLog \
416 WHERE state_hash=:old_hash)) s_old \
417 \
418 ON s_old.device_id = s_new.device_id \
419 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
420 \
421 WHERE s_old.seq_no IS NULL \
422", -1, &stmt, 0);
423
424 if (res != SQLITE_OK)
425 {
426 BOOST_THROW_EXCEPTION (Error::Db ()
427 << errmsg_info_str ("Some error with FindStateDifferences"));
428 }
429
430 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
431 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
432
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800433 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800434
435 // sqlite3_trace(m_db, xTrace, NULL);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800436
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800437 while (sqlite3_step (stmt) == SQLITE_ROW)
438 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800439 SyncState *state = msg->add_state ();
440
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800441 // set name
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800442 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800443
Zhenkai Zhue851b952013-01-13 22:29:57 -0800444 // locator is optional, so must check if it is null
445 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
446 {
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800447 state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1)), sqlite3_column_bytes (stmt, 1));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800448 }
449
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800450 // set old seq
451 if (includeOldSeq)
452 {
453 if (sqlite3_column_type (stmt, 2) == SQLITE_NULL)
454 {
455 // old seq is zero; we always have an initial action of zero seq
456 // other's do not need to fetch this action
457 state->set_old_seq(0);
458 }
459 else
460 {
461 sqlite3_int64 oldSeqNo = sqlite3_column_int64 (stmt, 2);
462 state->set_old_seq(oldSeqNo);
463 }
464 }
465
466 // set new seq
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800467 if (sqlite3_column_type (stmt, 3) == SQLITE_NULL)
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800468 {
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800469 state->set_type (SyncState::DELETE);
470 }
471 else
472 {
473 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800474 state->set_type (SyncState::UPDATE);
475 state->set_seq (newSeqNo);
476 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800477
478 // std::cout << sqlite3_column_text (stmt, 0) <<
479 // ": from " << sqlite3_column_int64 (stmt, 1) <<
480 // " to " << sqlite3_column_int64 (stmt, 2) <<
481 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800482 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800483 sqlite3_finalize (stmt);
484
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800485 // sqlite3_trace(m_db, NULL, NULL);
486
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800487 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800488}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800489
490sqlite3_int64
491SyncLog::SeqNo(const Name &name)
492{
493 sqlite3_stmt *stmt;
494 sqlite3_int64 seq = -1;
495 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700496 Ndnx::NdnxCharbufPtr nameBuf = name;
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800497 sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
498 if (sqlite3_step (stmt) == SQLITE_ROW)
499 {
500 seq = sqlite3_column_int64 (stmt, 0);
501 }
502
503 return seq;
504}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800505
506sqlite3_int64
507SyncLog::LogSize ()
508{
509 sqlite3_stmt *stmt;
510 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SyncLog", -1, &stmt, 0);
511
512 sqlite3_int64 retval = -1;
513 if (sqlite3_step (stmt) == SQLITE_ROW)
514 {
515 retval = sqlite3_column_int64 (stmt, 0);
516 }
517
518 return retval;
519}
520