blob: c9f125a2507d19286fcdb0d043ca6b600909edc0 [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
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080037SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080038 : DbHelper (path)
39 , m_localName (localName)
40{
Zhenkai Zhue851b952013-01-13 22:29:57 -080041 UpdateDeviceSeqNo (localName, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080042
43 sqlite3_stmt *stmt;
44 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 -080045
46 Ccnx::CcnxCharbufPtr name = m_localName;
47 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080048
49 if (sqlite3_step (stmt) == SQLITE_ROW)
50 {
51 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
52 }
53 else
54 {
55 BOOST_THROW_EXCEPTION (Error::Db ()
56 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
57 }
58 sqlite3_finalize (stmt);
59}
60
Zhenkai Zhue851b952013-01-13 22:29:57 -080061void
62SyncLog::initYP(map<Name, Name> &yp)
63{
64 sqlite3_stmt *stmt;
65 sqlite3_prepare_v2(m_db, "SELECT device_name, last_known_locator FROM SyncNodes;", -1, &stmt, 0);
66
67 while (sqlite3_step(stmt) == SQLITE_ROW)
68 {
69 Name deviceName((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
70 Name locator;
71 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
72 {
73 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
74 }
75 yp.insert(make_pair(deviceName, locator));
76 }
77
78 sqlite3_finalize(stmt);
79}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080080
81sqlite3_int64
82SyncLog::GetNextLocalSeqNo ()
83{
84 sqlite3_stmt *stmt_seq;
85 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
86 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
87
88 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
89 {
90 BOOST_THROW_EXCEPTION (Error::Db ()
91 << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
92 }
93
94 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
95 sqlite3_finalize (stmt_seq);
96
97 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
98
99 return seq_no;
100}
101
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800102HashPtr
103SyncLog::RememberStateInStateLog ()
104{
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800105 WriteLock lock (m_stateUpdateMutex);
106
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800107 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
108
109 res += sqlite3_exec (m_db, "\
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800110INSERT INTO SyncLog \
111 (state_hash, last_update) \
112 SELECT \
113 hash(device_name, seq_no), datetime('now') \
114 FROM (SELECT * FROM SyncNodes \
115 ORDER BY device_name); \
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800116", 0,0,0);
117
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800118 // std::cout << sqlite3_errmsg (m_db) << std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800119 if (res != SQLITE_OK)
120 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800121 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800122 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800123 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800124 }
125
126 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
127
128 sqlite3_stmt *insertStmt;
129 res += sqlite3_prepare (m_db, "\
130INSERT INTO SyncStateNodes \
131 (state_id, device_id, seq_no) \
132 SELECT ?, device_id, seq_no \
133 FROM SyncNodes; \
134", -1, &insertStmt, 0);
135
136 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
137 sqlite3_step (insertStmt);
138
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800139 // std::cout << sqlite3_errmsg (m_db) << std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800140 if (res != SQLITE_OK)
141 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800142 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800143 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800144 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800145 }
146 sqlite3_finalize (insertStmt);
147
148 sqlite3_stmt *getHashStmt;
149 res += sqlite3_prepare (m_db, "\
150SELECT state_hash FROM SyncLog WHERE state_id = ?\
151", -1, &getHashStmt, 0);
152 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
153
154 HashPtr retval;
155 int stepRes = sqlite3_step (getHashStmt);
156 if (stepRes == SQLITE_ROW)
157 {
158 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
159 sqlite3_column_bytes (getHashStmt, 0));
160 }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800161 else
162 {
163 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
164
165 // std::cout << sqlite3_errmsg (m_db) << std::endl;
166 BOOST_THROW_EXCEPTION (Error::Db ()
167 << errmsg_info_str ("Not a valid hash in rememberStateInStateLog"));
168 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800169 sqlite3_finalize (getHashStmt);
170 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
171
172 if (res != SQLITE_OK)
173 {
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800174 sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800175 BOOST_THROW_EXCEPTION (Error::Db ()
176 << errmsg_info_str ("Some error with rememberStateInStateLog"));
177 }
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800178
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800179 return retval;
180}
181
182sqlite3_int64
183SyncLog::LookupSyncLog (const std::string &stateHash)
184{
185 return LookupSyncLog (*Hash::FromString (stateHash));
186}
187
188sqlite3_int64
189SyncLog::LookupSyncLog (const Hash &stateHash)
190{
191 sqlite3_stmt *stmt;
192 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
193 -1, &stmt, 0);
194
195 if (res != SQLITE_OK)
196 {
197 BOOST_THROW_EXCEPTION (Error::Db ()
198 << errmsg_info_str ("Cannot prepare statement"));
199 }
200
201 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
202 if (res != SQLITE_OK)
203 {
204 BOOST_THROW_EXCEPTION (Error::Db ()
205 << errmsg_info_str ("Cannot bind"));
206 }
207
208 sqlite3_int64 row = 0; // something bad
209
210 if (sqlite3_step (stmt) == SQLITE_ROW)
211 {
212 row = sqlite3_column_int64 (stmt, 0);
213 }
214
215 sqlite3_finalize (stmt);
216
217 return row;
218}
219
220void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800221SyncLog::UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800222{
223 sqlite3_stmt *stmt;
224 // update is performed using trigger
225 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
226 -1, &stmt, 0);
227
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800228 Ccnx::CcnxCharbufPtr nameBuf = name;
229 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800230 res += sqlite3_bind_int64 (stmt, 2, seqNo);
231 sqlite3_step (stmt);
232
233 if (res != SQLITE_OK)
234 {
235 BOOST_THROW_EXCEPTION (Error::Db ()
Zhenkai Zhue851b952013-01-13 22:29:57 -0800236 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800237 }
238 sqlite3_finalize (stmt);
239}
240
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800241void
242SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
243{
244 sqlite3_stmt *stmt;
245 // update is performed using trigger
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800246 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800247 -1, &stmt, 0);
248
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800249 res += sqlite3_bind_int64 (stmt, 1, seqNo);
250 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800251 sqlite3_step (stmt);
252
253 if (res != SQLITE_OK)
254 {
255 BOOST_THROW_EXCEPTION (Error::Db ()
256 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
257 }
258 sqlite3_finalize (stmt);
259}
260
Zhenkai Zhue851b952013-01-13 22:29:57 -0800261Name
262SyncLog::LookupLocator(const Name &deviceName)
263{
264 sqlite3_stmt *stmt;
265 sqlite3_prepare_v2 (m_db, "SELECT last_known_locator FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
266 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
267 sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
268 int res = sqlite3_step (stmt);
269 Name locator;
270 switch (res)
271 {
272 case SQLITE_ROW:
273 {
274 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
275 }
276 case SQLITE_DONE: break;
277 default:
278 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in LookupLocator()"));
279 }
280
281 sqlite3_finalize(stmt);
282
283 return locator;
284}
285
286void
287SyncLog::UpdateLocator(const Name &deviceName, const Name &locator)
288{
289 sqlite3_stmt *stmt;
290 sqlite3_prepare_v2 (m_db, "UPDATE SyncNodes SET last_known_locator=? WHERE device_name=?;", -1, &stmt, 0);
291 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
292 Ccnx::CcnxCharbufPtr locatorBuf = locator;
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800293 sqlite3_bind_blob (stmt, 1, locatorBuf->buf(), locatorBuf->length(), SQLITE_STATIC);
294 sqlite3_bind_blob (stmt, 2, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800295 int res = sqlite3_step (stmt);
296
297 if (res != SQLITE_OK && res != SQLITE_DONE)
298 {
299 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in UpdateLoactor()"));
300 }
301
302 sqlite3_finalize(stmt);
303}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800304
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800305SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800306SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
307{
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800308 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800309}
310
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800311SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800312SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
313{
314 sqlite3_stmt *stmt;
315
316 int res = sqlite3_prepare_v2 (m_db, "\
Zhenkai Zhue851b952013-01-13 22:29:57 -0800317SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800318 FROM (SELECT * \
319 FROM SyncStateNodes \
320 WHERE state_id=(SELECT state_id \
321 FROM SyncLog \
322 WHERE state_hash=:old_hash)) s_old \
323 LEFT JOIN (SELECT * \
324 FROM SyncStateNodes \
325 WHERE state_id=(SELECT state_id \
326 FROM SyncLog \
327 WHERE state_hash=:new_hash)) s_new \
328 \
329 ON s_old.device_id = s_new.device_id \
330 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
331 \
332 WHERE s_new.seq_no IS NULL OR \
333 s_old.seq_no != s_new.seq_no \
334 \
335UNION ALL \
336 \
Zhenkai Zhue851b952013-01-13 22:29:57 -0800337SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800338 FROM (SELECT * \
339 FROM SyncStateNodes \
340 WHERE state_id=(SELECT state_id \
341 FROM SyncLog \
342 WHERE state_hash=:new_hash )) s_new \
343 LEFT JOIN (SELECT * \
344 FROM SyncStateNodes \
345 WHERE state_id=(SELECT state_id \
346 FROM SyncLog \
347 WHERE state_hash=:old_hash)) s_old \
348 \
349 ON s_old.device_id = s_new.device_id \
350 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
351 \
352 WHERE s_old.seq_no IS NULL \
353", -1, &stmt, 0);
354
355 if (res != SQLITE_OK)
356 {
357 BOOST_THROW_EXCEPTION (Error::Db ()
358 << errmsg_info_str ("Some error with FindStateDifferences"));
359 }
360
361 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
362 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
363
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800364 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800365
366 // sqlite3_trace(m_db, xTrace, NULL);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800367
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800368 while (sqlite3_step (stmt) == SQLITE_ROW)
369 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800370 SyncState *state = msg->add_state ();
371
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800372 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800373
Zhenkai Zhue851b952013-01-13 22:29:57 -0800374 // locator is optional, so must check if it is null
375 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
376 {
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800377 state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1)), sqlite3_column_bytes (stmt, 1));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800378 }
379
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800380 if (sqlite3_column_type (stmt, 3) == SQLITE_NULL)
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800381 {
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800382 state->set_type (SyncState::DELETE);
383 }
384 else
385 {
386 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800387 state->set_type (SyncState::UPDATE);
388 state->set_seq (newSeqNo);
389 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800390
391 // std::cout << sqlite3_column_text (stmt, 0) <<
392 // ": from " << sqlite3_column_int64 (stmt, 1) <<
393 // " to " << sqlite3_column_int64 (stmt, 2) <<
394 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800395 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800396 sqlite3_finalize (stmt);
397
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800398 // sqlite3_trace(m_db, NULL, NULL);
399
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800400 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800401}