blob: f3ba3d0fdc1f65739509faa41fafcea4ce019346 [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>
26
27using namespace boost;
28using namespace std;
Zhenkai Zhue851b952013-01-13 22:29:57 -080029using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080031SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080032 : DbHelper (path)
33 , m_localName (localName)
34{
Zhenkai Zhue851b952013-01-13 22:29:57 -080035 UpdateDeviceSeqNo (localName, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080036
37 sqlite3_stmt *stmt;
38 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 -080039
40 Ccnx::CcnxCharbufPtr name = m_localName;
41 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080042
43 if (sqlite3_step (stmt) == SQLITE_ROW)
44 {
45 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
46 }
47 else
48 {
49 BOOST_THROW_EXCEPTION (Error::Db ()
50 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
51 }
52 sqlite3_finalize (stmt);
53}
54
Zhenkai Zhue851b952013-01-13 22:29:57 -080055void
56SyncLog::initYP(map<Name, Name> &yp)
57{
58 sqlite3_stmt *stmt;
59 sqlite3_prepare_v2(m_db, "SELECT device_name, last_known_locator FROM SyncNodes;", -1, &stmt, 0);
60
61 while (sqlite3_step(stmt) == SQLITE_ROW)
62 {
63 Name deviceName((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
64 Name locator;
65 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
66 {
67 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
68 }
69 yp.insert(make_pair(deviceName, locator));
70 }
71
72 sqlite3_finalize(stmt);
73}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080074
75sqlite3_int64
76SyncLog::GetNextLocalSeqNo ()
77{
78 sqlite3_stmt *stmt_seq;
79 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
80 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
81
82 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
83 {
84 BOOST_THROW_EXCEPTION (Error::Db ()
85 << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
86 }
87
88 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
89 sqlite3_finalize (stmt_seq);
90
91 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
92
93 return seq_no;
94}
95
96
Alexander Afanasyeva199f972013-01-02 19:37:26 -080097HashPtr
98SyncLog::RememberStateInStateLog ()
99{
100 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
101
102 res += sqlite3_exec (m_db, "\
103INSERT INTO SyncLog \
104 (state_hash, last_update) \
105 SELECT \
106 hash(device_name, seq_no), datetime('now') \
107 FROM SyncNodes \
108 ORDER BY device_name; \
109", 0,0,0);
110
111 if (res != SQLITE_OK)
112 {
113 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800114 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800115 }
116
117 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
118
119 sqlite3_stmt *insertStmt;
120 res += sqlite3_prepare (m_db, "\
121INSERT INTO SyncStateNodes \
122 (state_id, device_id, seq_no) \
123 SELECT ?, device_id, seq_no \
124 FROM SyncNodes; \
125", -1, &insertStmt, 0);
126
127 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
128 sqlite3_step (insertStmt);
129
130 if (res != SQLITE_OK)
131 {
132 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800133 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800134 }
135 sqlite3_finalize (insertStmt);
136
137 sqlite3_stmt *getHashStmt;
138 res += sqlite3_prepare (m_db, "\
139SELECT state_hash FROM SyncLog WHERE state_id = ?\
140", -1, &getHashStmt, 0);
141 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
142
143 HashPtr retval;
144 int stepRes = sqlite3_step (getHashStmt);
145 if (stepRes == SQLITE_ROW)
146 {
147 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
148 sqlite3_column_bytes (getHashStmt, 0));
149 }
150 sqlite3_finalize (getHashStmt);
151 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
152
153 if (res != SQLITE_OK)
154 {
155 BOOST_THROW_EXCEPTION (Error::Db ()
156 << errmsg_info_str ("Some error with rememberStateInStateLog"));
157 }
158
159 return retval;
160}
161
162sqlite3_int64
163SyncLog::LookupSyncLog (const std::string &stateHash)
164{
165 return LookupSyncLog (*Hash::FromString (stateHash));
166}
167
168sqlite3_int64
169SyncLog::LookupSyncLog (const Hash &stateHash)
170{
171 sqlite3_stmt *stmt;
172 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
173 -1, &stmt, 0);
174
175 if (res != SQLITE_OK)
176 {
177 BOOST_THROW_EXCEPTION (Error::Db ()
178 << errmsg_info_str ("Cannot prepare statement"));
179 }
180
181 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
182 if (res != SQLITE_OK)
183 {
184 BOOST_THROW_EXCEPTION (Error::Db ()
185 << errmsg_info_str ("Cannot bind"));
186 }
187
188 sqlite3_int64 row = 0; // something bad
189
190 if (sqlite3_step (stmt) == SQLITE_ROW)
191 {
192 row = sqlite3_column_int64 (stmt, 0);
193 }
194
195 sqlite3_finalize (stmt);
196
197 return row;
198}
199
200void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800201SyncLog::UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800202{
203 sqlite3_stmt *stmt;
204 // update is performed using trigger
205 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
206 -1, &stmt, 0);
207
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800208 Ccnx::CcnxCharbufPtr nameBuf = name;
209 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800210 res += sqlite3_bind_int64 (stmt, 2, seqNo);
211 sqlite3_step (stmt);
212
213 if (res != SQLITE_OK)
214 {
215 BOOST_THROW_EXCEPTION (Error::Db ()
Zhenkai Zhue851b952013-01-13 22:29:57 -0800216 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800217 }
218 sqlite3_finalize (stmt);
219}
220
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800221void
222SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
223{
224 sqlite3_stmt *stmt;
225 // update is performed using trigger
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800226 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800227 -1, &stmt, 0);
228
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800229 res += sqlite3_bind_int64 (stmt, 1, seqNo);
230 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800231 sqlite3_step (stmt);
232
233 if (res != SQLITE_OK)
234 {
235 BOOST_THROW_EXCEPTION (Error::Db ()
236 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
237 }
238 sqlite3_finalize (stmt);
239}
240
Zhenkai Zhue851b952013-01-13 22:29:57 -0800241Name
242SyncLog::LookupLocator(const Name &deviceName)
243{
244 sqlite3_stmt *stmt;
245 sqlite3_prepare_v2 (m_db, "SELECT last_known_locator FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
246 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
247 sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
248 int res = sqlite3_step (stmt);
249 Name locator;
250 switch (res)
251 {
252 case SQLITE_ROW:
253 {
254 locator = Name((const unsigned char *)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
255 }
256 case SQLITE_DONE: break;
257 default:
258 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in LookupLocator()"));
259 }
260
261 sqlite3_finalize(stmt);
262
263 return locator;
264}
265
266void
267SyncLog::UpdateLocator(const Name &deviceName, const Name &locator)
268{
269 sqlite3_stmt *stmt;
270 sqlite3_prepare_v2 (m_db, "UPDATE SyncNodes SET last_known_locator=? WHERE device_name=?;", -1, &stmt, 0);
271 Ccnx::CcnxCharbufPtr nameBuf = deviceName;
272 Ccnx::CcnxCharbufPtr locatorBuf = locator;
273 sqlite3_bind_blob (stmt, 1, nameBuf->buf(), nameBuf->length(), SQLITE_STATIC);
274 sqlite3_bind_blob (stmt, 2, locatorBuf->buf(), locatorBuf->length(), SQLITE_STATIC);
275 int res = sqlite3_step (stmt);
276
277 if (res != SQLITE_OK && res != SQLITE_DONE)
278 {
279 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Error in UpdateLoactor()"));
280 }
281
282 sqlite3_finalize(stmt);
283}
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800284
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800285SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800286SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
287{
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800288 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800289}
290
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800291SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800292SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
293{
294 sqlite3_stmt *stmt;
295
296 int res = sqlite3_prepare_v2 (m_db, "\
Zhenkai Zhue851b952013-01-13 22:29:57 -0800297SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800298 FROM (SELECT * \
299 FROM SyncStateNodes \
300 WHERE state_id=(SELECT state_id \
301 FROM SyncLog \
302 WHERE state_hash=:old_hash)) s_old \
303 LEFT JOIN (SELECT * \
304 FROM SyncStateNodes \
305 WHERE state_id=(SELECT state_id \
306 FROM SyncLog \
307 WHERE state_hash=:new_hash)) s_new \
308 \
309 ON s_old.device_id = s_new.device_id \
310 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
311 \
312 WHERE s_new.seq_no IS NULL OR \
313 s_old.seq_no != s_new.seq_no \
314 \
315UNION ALL \
316 \
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=:new_hash )) s_new \
323 LEFT JOIN (SELECT * \
324 FROM SyncStateNodes \
325 WHERE state_id=(SELECT state_id \
326 FROM SyncLog \
327 WHERE state_hash=:old_hash)) s_old \
328 \
329 ON s_old.device_id = s_new.device_id \
330 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
331 \
332 WHERE s_old.seq_no IS NULL \
333", -1, &stmt, 0);
334
335 if (res != SQLITE_OK)
336 {
337 BOOST_THROW_EXCEPTION (Error::Db ()
338 << errmsg_info_str ("Some error with FindStateDifferences"));
339 }
340
341 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
342 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
343
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800344 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
345
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800346 while (sqlite3_step (stmt) == SQLITE_ROW)
347 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800348 SyncState *state = msg->add_state ();
349
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800350 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800351
Zhenkai Zhue851b952013-01-13 22:29:57 -0800352 // locator is optional, so must check if it is null
353 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
354 {
355 state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1)));
356 }
357
358 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800359 if (newSeqNo > 0)
360 {
361 state->set_type (SyncState::UPDATE);
362 state->set_seq (newSeqNo);
363 }
364 else
365 state->set_type (SyncState::DELETE);
366
367 // std::cout << sqlite3_column_text (stmt, 0) <<
368 // ": from " << sqlite3_column_int64 (stmt, 1) <<
369 // " to " << sqlite3_column_int64 (stmt, 2) <<
370 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800371 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800372 sqlite3_finalize (stmt);
373
374 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800375}