blob: ca6adf73f549ce2f9d52604199fc8c6d326b3f66 [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"
23
24#include <boost/make_shared.hpp>
25
26using namespace boost;
27using namespace std;
28
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080030 : DbHelper (path)
31 , m_localName (localName)
32{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080033 SyncLog::RememberStateInStateLog ();
34
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080035 UpdateDeviceSeqno (localName, 0);
36
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
55
56sqlite3_int64
57SyncLog::GetNextLocalSeqNo ()
58{
59 sqlite3_stmt *stmt_seq;
60 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
61 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
62
63 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
64 {
65 BOOST_THROW_EXCEPTION (Error::Db ()
66 << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
67 }
68
69 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
70 sqlite3_finalize (stmt_seq);
71
72 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
73
74 return seq_no;
75}
76
77
Alexander Afanasyeva199f972013-01-02 19:37:26 -080078HashPtr
79SyncLog::RememberStateInStateLog ()
80{
81 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
82
83 res += sqlite3_exec (m_db, "\
84INSERT INTO SyncLog \
85 (state_hash, last_update) \
86 SELECT \
87 hash(device_name, seq_no), datetime('now') \
88 FROM SyncNodes \
89 ORDER BY device_name; \
90", 0,0,0);
91
92 if (res != SQLITE_OK)
93 {
94 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080095 << errmsg_info_str (sqlite3_errmsg(m_db)));
Alexander Afanasyeva199f972013-01-02 19:37:26 -080096 }
97
98 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
99
100 sqlite3_stmt *insertStmt;
101 res += sqlite3_prepare (m_db, "\
102INSERT INTO SyncStateNodes \
103 (state_id, device_id, seq_no) \
104 SELECT ?, device_id, seq_no \
105 FROM SyncNodes; \
106", -1, &insertStmt, 0);
107
108 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
109 sqlite3_step (insertStmt);
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 sqlite3_finalize (insertStmt);
117
118 sqlite3_stmt *getHashStmt;
119 res += sqlite3_prepare (m_db, "\
120SELECT state_hash FROM SyncLog WHERE state_id = ?\
121", -1, &getHashStmt, 0);
122 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
123
124 HashPtr retval;
125 int stepRes = sqlite3_step (getHashStmt);
126 if (stepRes == SQLITE_ROW)
127 {
128 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
129 sqlite3_column_bytes (getHashStmt, 0));
130 }
131 sqlite3_finalize (getHashStmt);
132 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
133
134 if (res != SQLITE_OK)
135 {
136 BOOST_THROW_EXCEPTION (Error::Db ()
137 << errmsg_info_str ("Some error with rememberStateInStateLog"));
138 }
139
140 return retval;
141}
142
143sqlite3_int64
144SyncLog::LookupSyncLog (const std::string &stateHash)
145{
146 return LookupSyncLog (*Hash::FromString (stateHash));
147}
148
149sqlite3_int64
150SyncLog::LookupSyncLog (const Hash &stateHash)
151{
152 sqlite3_stmt *stmt;
153 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
154 -1, &stmt, 0);
155
156 if (res != SQLITE_OK)
157 {
158 BOOST_THROW_EXCEPTION (Error::Db ()
159 << errmsg_info_str ("Cannot prepare statement"));
160 }
161
162 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
163 if (res != SQLITE_OK)
164 {
165 BOOST_THROW_EXCEPTION (Error::Db ()
166 << errmsg_info_str ("Cannot bind"));
167 }
168
169 sqlite3_int64 row = 0; // something bad
170
171 if (sqlite3_step (stmt) == SQLITE_ROW)
172 {
173 row = sqlite3_column_int64 (stmt, 0);
174 }
175
176 sqlite3_finalize (stmt);
177
178 return row;
179}
180
181void
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800182SyncLog::UpdateDeviceSeqno (const Ccnx::Name &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800183{
184 sqlite3_stmt *stmt;
185 // update is performed using trigger
186 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
187 -1, &stmt, 0);
188
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800189 Ccnx::CcnxCharbufPtr nameBuf = name;
190 res += sqlite3_bind_blob (stmt, 1, nameBuf->buf (), nameBuf->length (), SQLITE_STATIC);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800191 res += sqlite3_bind_int64 (stmt, 2, seqNo);
192 sqlite3_step (stmt);
193
194 if (res != SQLITE_OK)
195 {
196 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800197 << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800198 }
199 sqlite3_finalize (stmt);
200}
201
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800202void
203SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
204{
205 sqlite3_stmt *stmt;
206 // update is performed using trigger
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800207 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800208 -1, &stmt, 0);
209
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800210 res += sqlite3_bind_int64 (stmt, 1, seqNo);
211 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800212 sqlite3_step (stmt);
213
214 if (res != SQLITE_OK)
215 {
216 BOOST_THROW_EXCEPTION (Error::Db ()
217 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
218 }
219 sqlite3_finalize (stmt);
220}
221
222
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800223SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800224SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
225{
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800226 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800227}
228
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800229SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800230SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
231{
232 sqlite3_stmt *stmt;
233
234 int res = sqlite3_prepare_v2 (m_db, "\
235SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
236 FROM (SELECT * \
237 FROM SyncStateNodes \
238 WHERE state_id=(SELECT state_id \
239 FROM SyncLog \
240 WHERE state_hash=:old_hash)) s_old \
241 LEFT JOIN (SELECT * \
242 FROM SyncStateNodes \
243 WHERE state_id=(SELECT state_id \
244 FROM SyncLog \
245 WHERE state_hash=:new_hash)) s_new \
246 \
247 ON s_old.device_id = s_new.device_id \
248 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
249 \
250 WHERE s_new.seq_no IS NULL OR \
251 s_old.seq_no != s_new.seq_no \
252 \
253UNION ALL \
254 \
255SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
256 FROM (SELECT * \
257 FROM SyncStateNodes \
258 WHERE state_id=(SELECT state_id \
259 FROM SyncLog \
260 WHERE state_hash=:new_hash )) s_new \
261 LEFT JOIN (SELECT * \
262 FROM SyncStateNodes \
263 WHERE state_id=(SELECT state_id \
264 FROM SyncLog \
265 WHERE state_hash=:old_hash)) s_old \
266 \
267 ON s_old.device_id = s_new.device_id \
268 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
269 \
270 WHERE s_old.seq_no IS NULL \
271", -1, &stmt, 0);
272
273 if (res != SQLITE_OK)
274 {
275 BOOST_THROW_EXCEPTION (Error::Db ()
276 << errmsg_info_str ("Some error with FindStateDifferences"));
277 }
278
279 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
280 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
281
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800282 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
283
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800284 while (sqlite3_step (stmt) == SQLITE_ROW)
285 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800286 SyncState *state = msg->add_state ();
287
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800288 state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0)));
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800289
290 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2);
291 if (newSeqNo > 0)
292 {
293 state->set_type (SyncState::UPDATE);
294 state->set_seq (newSeqNo);
295 }
296 else
297 state->set_type (SyncState::DELETE);
298
299 // std::cout << sqlite3_column_text (stmt, 0) <<
300 // ": from " << sqlite3_column_int64 (stmt, 1) <<
301 // " to " << sqlite3_column_int64 (stmt, 2) <<
302 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800303 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800304 sqlite3_finalize (stmt);
305
306 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800307}