blob: 6bd03aad959adf8d7c225c24ccaffff1880e3fcf [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 Afanasyev66f4c492013-01-20 23:32:50 -080043
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080044 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
Alexander Afanasyevdac84922013-01-20 23:32:17 -080062// void
63// SyncLog::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);
Zhenkai Zhue851b952013-01-13 22:29:57 -080067
Alexander Afanasyevdac84922013-01-20 23:32:17 -080068// 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// }
Zhenkai Zhue851b952013-01-13 22:29:57 -080078
Alexander Afanasyevdac84922013-01-20 23:32:17 -080079// 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 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -080094
95 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080096 sqlite3_finalize (stmt_seq);
97
98 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080099
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800100 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
Zhenkai Zhub330aed2013-01-17 13:29:37 -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 }
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800126
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800127 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800128
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800129 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);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800148
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800149 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 Afanasyevdac84922013-01-20 23:32:17 -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);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800195
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800196 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
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800226 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800227 -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);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800233
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800234 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 Afanasyevdac84922013-01-20 23:32:17 -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);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800253
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800254 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 Afanasyevdac84922013-01-20 23:32:17 -0800306void
307SyncLog::UpdateLocalLocator (const Ccnx::Name &locator)
308{
309 return UpdateLocator (m_localName, locator);
310}
311
312
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800313SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800314SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800315{
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800316 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash), includeOldSeq);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800317}
318
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800319SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800320SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800321{
322 sqlite3_stmt *stmt;
323
324 int res = sqlite3_prepare_v2 (m_db, "\
Zhenkai Zhue851b952013-01-13 22:29:57 -0800325SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800326 FROM (SELECT * \
327 FROM SyncStateNodes \
328 WHERE state_id=(SELECT state_id \
329 FROM SyncLog \
330 WHERE state_hash=:old_hash)) s_old \
331 LEFT JOIN (SELECT * \
332 FROM SyncStateNodes \
333 WHERE state_id=(SELECT state_id \
334 FROM SyncLog \
335 WHERE state_hash=:new_hash)) s_new \
336 \
337 ON s_old.device_id = s_new.device_id \
338 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
339 \
340 WHERE s_new.seq_no IS NULL OR \
341 s_old.seq_no != s_new.seq_no \
342 \
343UNION ALL \
344 \
Zhenkai Zhue851b952013-01-13 22:29:57 -0800345SELECT sn.device_name, sn.last_known_locator, s_old.seq_no, s_new.seq_no\
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800346 FROM (SELECT * \
347 FROM SyncStateNodes \
348 WHERE state_id=(SELECT state_id \
349 FROM SyncLog \
350 WHERE state_hash=:new_hash )) s_new \
351 LEFT JOIN (SELECT * \
352 FROM SyncStateNodes \
353 WHERE state_id=(SELECT state_id \
354 FROM SyncLog \
355 WHERE state_hash=:old_hash)) s_old \
356 \
357 ON s_old.device_id = s_new.device_id \
358 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
359 \
360 WHERE s_old.seq_no IS NULL \
361", -1, &stmt, 0);
362
363 if (res != SQLITE_OK)
364 {
365 BOOST_THROW_EXCEPTION (Error::Db ()
366 << errmsg_info_str ("Some error with FindStateDifferences"));
367 }
368
369 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
370 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
371
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800372 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800373
374 // sqlite3_trace(m_db, xTrace, NULL);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800375
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800376 while (sqlite3_step (stmt) == SQLITE_ROW)
377 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800378 SyncState *state = msg->add_state ();
379
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800380 // set name
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800381 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800382
Zhenkai Zhue851b952013-01-13 22:29:57 -0800383 // locator is optional, so must check if it is null
384 if (sqlite3_column_type(stmt, 1) == SQLITE_BLOB)
385 {
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800386 state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1)), sqlite3_column_bytes (stmt, 1));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800387 }
388
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800389 // set old seq
390 if (includeOldSeq)
391 {
392 if (sqlite3_column_type (stmt, 2) == SQLITE_NULL)
393 {
394 // old seq is zero; we always have an initial action of zero seq
395 // other's do not need to fetch this action
396 state->set_old_seq(0);
397 }
398 else
399 {
400 sqlite3_int64 oldSeqNo = sqlite3_column_int64 (stmt, 2);
401 state->set_old_seq(oldSeqNo);
402 }
403 }
404
405 // set new seq
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800406 if (sqlite3_column_type (stmt, 3) == SQLITE_NULL)
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800407 {
Alexander Afanasyev49ab6f82013-01-15 16:58:13 -0800408 state->set_type (SyncState::DELETE);
409 }
410 else
411 {
412 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 3);
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800413 state->set_type (SyncState::UPDATE);
414 state->set_seq (newSeqNo);
415 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800416
417 // std::cout << sqlite3_column_text (stmt, 0) <<
418 // ": from " << sqlite3_column_int64 (stmt, 1) <<
419 // " to " << sqlite3_column_int64 (stmt, 2) <<
420 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800421 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800422 sqlite3_finalize (stmt);
423
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -0800424 // sqlite3_trace(m_db, NULL, NULL);
425
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800426 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800427}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800428
429sqlite3_int64
430SyncLog::SeqNo(const Name &name)
431{
432 sqlite3_stmt *stmt;
433 sqlite3_int64 seq = -1;
434 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_name=?;", -1, &stmt, 0);
435 Ccnx::CcnxCharbufPtr nameBuf = name;
436 sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
437 if (sqlite3_step (stmt) == SQLITE_ROW)
438 {
439 seq = sqlite3_column_int64 (stmt, 0);
440 }
441
442 return seq;
443}