blob: 677904ac30e5ef3faa2925c6e4e09c69fdbf6a8b [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 Afanasyev433ecda2013-01-02 22:13:45 -080029SyncLog::SyncLog (const std::string &path, const std::string &localName)
30 : 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);
39 sqlite3_bind_text (stmt, 1, m_localName.c_str (), m_localName.size (), SQLITE_STATIC);
40
41 if (sqlite3_step (stmt) == SQLITE_ROW)
42 {
43 m_localDeviceId = sqlite3_column_int64 (stmt, 0);
44 }
45 else
46 {
47 BOOST_THROW_EXCEPTION (Error::Db ()
48 << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
49 }
50 sqlite3_finalize (stmt);
51}
52
53
54sqlite3_int64
55SyncLog::GetNextLocalSeqNo ()
56{
57 sqlite3_stmt *stmt_seq;
58 sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
59 sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
60
61 if (sqlite3_step (stmt_seq) != SQLITE_ROW)
62 {
63 BOOST_THROW_EXCEPTION (Error::Db ()
64 << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
65 }
66
67 sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1;
68 sqlite3_finalize (stmt_seq);
69
70 UpdateDeviceSeqNo (m_localDeviceId, seq_no);
71
72 return seq_no;
73}
74
75
Alexander Afanasyeva199f972013-01-02 19:37:26 -080076HashPtr
77SyncLog::RememberStateInStateLog ()
78{
79 int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
80
81 res += sqlite3_exec (m_db, "\
82INSERT INTO SyncLog \
83 (state_hash, last_update) \
84 SELECT \
85 hash(device_name, seq_no), datetime('now') \
86 FROM SyncNodes \
87 ORDER BY device_name; \
88", 0,0,0);
89
90 if (res != SQLITE_OK)
91 {
92 BOOST_THROW_EXCEPTION (Error::Db ()
93 << errmsg_info_str ("1"));
94 }
95
96 sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
97
98 sqlite3_stmt *insertStmt;
99 res += sqlite3_prepare (m_db, "\
100INSERT INTO SyncStateNodes \
101 (state_id, device_id, seq_no) \
102 SELECT ?, device_id, seq_no \
103 FROM SyncNodes; \
104", -1, &insertStmt, 0);
105
106 res += sqlite3_bind_int64 (insertStmt, 1, rowId);
107 sqlite3_step (insertStmt);
108
109 if (res != SQLITE_OK)
110 {
111 BOOST_THROW_EXCEPTION (Error::Db ()
112 << errmsg_info_str ("4"));
113 }
114 sqlite3_finalize (insertStmt);
115
116 sqlite3_stmt *getHashStmt;
117 res += sqlite3_prepare (m_db, "\
118SELECT state_hash FROM SyncLog WHERE state_id = ?\
119", -1, &getHashStmt, 0);
120 res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
121
122 HashPtr retval;
123 int stepRes = sqlite3_step (getHashStmt);
124 if (stepRes == SQLITE_ROW)
125 {
126 retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
127 sqlite3_column_bytes (getHashStmt, 0));
128 }
129 sqlite3_finalize (getHashStmt);
130 res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
131
132 if (res != SQLITE_OK)
133 {
134 BOOST_THROW_EXCEPTION (Error::Db ()
135 << errmsg_info_str ("Some error with rememberStateInStateLog"));
136 }
137
138 return retval;
139}
140
141sqlite3_int64
142SyncLog::LookupSyncLog (const std::string &stateHash)
143{
144 return LookupSyncLog (*Hash::FromString (stateHash));
145}
146
147sqlite3_int64
148SyncLog::LookupSyncLog (const Hash &stateHash)
149{
150 sqlite3_stmt *stmt;
151 int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
152 -1, &stmt, 0);
153
154 if (res != SQLITE_OK)
155 {
156 BOOST_THROW_EXCEPTION (Error::Db ()
157 << errmsg_info_str ("Cannot prepare statement"));
158 }
159
160 res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
161 if (res != SQLITE_OK)
162 {
163 BOOST_THROW_EXCEPTION (Error::Db ()
164 << errmsg_info_str ("Cannot bind"));
165 }
166
167 sqlite3_int64 row = 0; // something bad
168
169 if (sqlite3_step (stmt) == SQLITE_ROW)
170 {
171 row = sqlite3_column_int64 (stmt, 0);
172 }
173
174 sqlite3_finalize (stmt);
175
176 return row;
177}
178
179void
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800180SyncLog::UpdateDeviceSeqno (const std::string &name, sqlite3_int64 seqNo)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800181{
182 sqlite3_stmt *stmt;
183 // update is performed using trigger
184 int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
185 -1, &stmt, 0);
186
187 res += sqlite3_bind_text (stmt, 1, name.c_str (), name.size (), SQLITE_STATIC);
188 res += sqlite3_bind_int64 (stmt, 2, seqNo);
189 sqlite3_step (stmt);
190
191 if (res != SQLITE_OK)
192 {
193 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800194 << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800195 }
196 sqlite3_finalize (stmt);
197}
198
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800199void
200SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
201{
202 sqlite3_stmt *stmt;
203 // update is performed using trigger
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800204 int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800205 -1, &stmt, 0);
206
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800207 res += sqlite3_bind_int64 (stmt, 1, seqNo);
208 res += sqlite3_bind_int64 (stmt, 2, deviceId);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800209 sqlite3_step (stmt);
210
211 if (res != SQLITE_OK)
212 {
213 BOOST_THROW_EXCEPTION (Error::Db ()
214 << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
215 }
216 sqlite3_finalize (stmt);
217}
218
219
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800220SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800221SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
222{
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800223 return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800224}
225
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800226SyncStateMsgPtr
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800227SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
228{
229 sqlite3_stmt *stmt;
230
231 int res = sqlite3_prepare_v2 (m_db, "\
232SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
233 FROM (SELECT * \
234 FROM SyncStateNodes \
235 WHERE state_id=(SELECT state_id \
236 FROM SyncLog \
237 WHERE state_hash=:old_hash)) s_old \
238 LEFT JOIN (SELECT * \
239 FROM SyncStateNodes \
240 WHERE state_id=(SELECT state_id \
241 FROM SyncLog \
242 WHERE state_hash=:new_hash)) s_new \
243 \
244 ON s_old.device_id = s_new.device_id \
245 JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
246 \
247 WHERE s_new.seq_no IS NULL OR \
248 s_old.seq_no != s_new.seq_no \
249 \
250UNION ALL \
251 \
252SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
253 FROM (SELECT * \
254 FROM SyncStateNodes \
255 WHERE state_id=(SELECT state_id \
256 FROM SyncLog \
257 WHERE state_hash=:new_hash )) s_new \
258 LEFT JOIN (SELECT * \
259 FROM SyncStateNodes \
260 WHERE state_id=(SELECT state_id \
261 FROM SyncLog \
262 WHERE state_hash=:old_hash)) s_old \
263 \
264 ON s_old.device_id = s_new.device_id \
265 JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
266 \
267 WHERE s_old.seq_no IS NULL \
268", -1, &stmt, 0);
269
270 if (res != SQLITE_OK)
271 {
272 BOOST_THROW_EXCEPTION (Error::Db ()
273 << errmsg_info_str ("Some error with FindStateDifferences"));
274 }
275
276 res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
277 res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
278
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800279 SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
280
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800281 while (sqlite3_step (stmt) == SQLITE_ROW)
282 {
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800283 SyncState *state = msg->add_state ();
284
285 state->set_name (reinterpret_cast<const char*> (sqlite3_column_text (stmt, 0)));
286
287 sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2);
288 if (newSeqNo > 0)
289 {
290 state->set_type (SyncState::UPDATE);
291 state->set_seq (newSeqNo);
292 }
293 else
294 state->set_type (SyncState::DELETE);
295
296 // std::cout << sqlite3_column_text (stmt, 0) <<
297 // ": from " << sqlite3_column_int64 (stmt, 1) <<
298 // " to " << sqlite3_column_int64 (stmt, 2) <<
299 // std::endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800300 }
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800301 sqlite3_finalize (stmt);
302
303 return msg;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800304}