blob: 7b6c0910926459753ed3ad50f236f8470c609cc3 [file] [log] [blame]
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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
Alexander Afanasyev242f8772013-01-01 23:26:31 -080022#include "db-helper.h"
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080023// #include "sync-log.h"
Alexander Afanasyevae43c502012-12-29 17:26:37 -080024#include <boost/make_shared.hpp>
25#include <boost/ref.hpp>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080026#include <boost/throw_exception.hpp>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080027
Alexander Afanasyevae43c502012-12-29 17:26:37 -080028using namespace boost;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080029
30const std::string INIT_DATABASE = "\
Alexander Afanasyev8811b352013-01-02 12:51:15 -080031PRAGMA foreign_keys = ON; \n\
32 \n\
33CREATE TABLE \n\
34 SyncNodes( \n\
35 device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
36 device_name TEXT NOT NULL, \n\
37 description TEXT, \n\
38 seq_no INTEGER NOT NULL, \n\
39 last_known_tdi TEXT, \n\
40 last_update TIMESTAMP \n\
41 ); \n\
42 \n\
43CREATE TRIGGER SyncNodesUpdater_trigger \n\
44 BEFORE INSERT ON SyncNodes \n\
45 FOR EACH ROW \n\
46 WHEN (SELECT device_id \n\
47 FROM SyncNodes \n\
48 WHERE device_name=NEW.device_name) \n\
49 IS NOT NULL \n\
50 BEGIN \n\
51 UPDATE SyncNodes \n\
52 SET seq_no=max(seq_no,NEW.seq_no) \n\
53 WHERE device_name=NEW.device_name; \n\
54 SELECT RAISE(IGNORE); \n\
55 END; \n\
56 \n\
57CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
58 \n\
59CREATE TABLE SyncLog( \n\
60 state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
61 state_hash BLOB NOT NULL UNIQUE, \n\
62 last_update TIMESTAMP NOT NULL \n\
63 ); \n\
64 \n\
65CREATE TABLE \n\
66 SyncStateNodes( \n\
67 id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
68 state_id INTEGER NOT NULL \n\
69 REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
70 device_id INTEGER NOT NULL \n\
71 REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
72 seq_no INTEGER NOT NULL \n\
73 ); \n\
74 \n\
75CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
76CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
77CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
78 \n\
79CREATE TRIGGER SyncLogGuard_trigger \n\
80 BEFORE INSERT ON SyncLog \n\
81 FOR EACH ROW \n\
82 WHEN (SELECT state_hash \n\
83 FROM SyncLog \n\
84 WHERE state_hash=NEW.state_hash) \n\
85 IS NOT NULL \n\
86 BEGIN \n\
87 DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
88 END; \n\
89 \n\
90CREATE TABLE ActionLog ( \n\
91 device_id INTEGER NOT NULL, \n\
92 seq_no INTEGER NOT NULL, \n\
93 \n\
94 action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
95 filename TEXT NOT NULL, \n\
96 \n\
97 version INTEGER NOT NULL, \n\
98 action_timestamp TIMESTAMP NOT NULL, \n\
99 \n\
100 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
101 file_atime TIMESTAMP, \n\
102 file_mtime TIMESTAMP, \n\
103 file_ctime TIMESTAMP, \n\
104 file_chmod INTEGER, \n\
105 \n\
106 parent_device_id INTEGER, \n\
107 parent_seq_no INTEGER, \n\
108 \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800109 action_name TEXT, \n\
110 action_content_object BLOB, \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800111 \n\
112 PRIMARY KEY (device_id, seq_no), \n\
113 \n\
114 FOREIGN KEY (parent_device_id, parent_seq_no) \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800115 REFERENCES ActionLog (device_id, seq_no) \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800116 ON UPDATE RESTRICT \n\
117 ON DELETE SET NULL \n\
118); \n\
119 \n\
120CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
121CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
122CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800123 \n\
124CREATE TRIGGER ActionLogInsert_trigger \n\
125 AFTER INSERT ON ActionLog \n\
126 FOR EACH ROW \n\
127 WHEN (SELECT device_id \n\
128 FROM ActionLog \n\
129 WHERE filename=NEW.filename AND \n\
130 version > NEW.version) IS NULL AND \n\
131 (SELECT a.device_id \n\
132 FROM ActionLog a \n\
133 LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\
134 WHERE filename=NEW.filename AND \n\
135 version = NEW.version AND \n\
136 a.device_id != NEW.device_id AND \n\
137 s.device_name > (SELECT device_name \n\
138 FROM SyncNodes \n\
139 WHERE device_id=NEW.device_id)) IS NULL \n\
140 BEGIN \n\
141 SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \
142 NEW.action,NEW.filename,NEW.file_hash, \
143 NEW.file_atime,NEW.file_mtime,NEW.file_ctime, \
144 NEW.file_chmod); /* function that applies action and adds record the FileState */ \n \
145 END; \n\
146 \n\
147CREATE TABLE FileState ( \n\
148 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
149 filename TEXT NOT NULL, \n\
150 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
151 file_atime TIMESTAMP, \n\
152 file_mtime TIMESTAMP, \n\
153 file_ctime TIMESTAMP, \n\
154 file_chmod INTEGER, \n\
155 \n\
156 PRIMARY KEY (type, filename) \n\
157); \n\
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800158";
159
160DbHelper::DbHelper (const std::string &path)
161{
162 int res = sqlite3_open((path+"chronoshare.db").c_str (), &m_db);
163 if (res != SQLITE_OK)
164 {
165 BOOST_THROW_EXCEPTION (Error::Db ()
166 << errmsg_info_str ("Cannot open/create dabatabase: [" + path + "chronoshare.db" + "]"));
167 }
168
169 res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800170 DbHelper::hash_xStep, DbHelper::hash_xFinal);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800171 if (res != SQLITE_OK)
172 {
173 BOOST_THROW_EXCEPTION (Error::Db ()
174 << errmsg_info_str ("Cannot create function ``hash''"));
175 }
176
177 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
178 // for now, just attempt to create everything
179
180 char *errmsg = 0;
181 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800182 if (res != SQLITE_OK && errmsg != 0)
183 {
184 std::cerr << "DEBUG: " << errmsg << std::endl;
185 sqlite3_free (errmsg);
186 }
187}
188
189DbHelper::~DbHelper ()
190{
191 int res = sqlite3_close (m_db);
192 if (res != SQLITE_OK)
193 {
194 // complain
195 }
196}
197
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800198void
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800199DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
200{
201 if (argc != 2)
202 {
203 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
204 sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
205 return;
206 }
207 if (sqlite3_value_type (argv[0]) != SQLITE_TEXT ||
208 sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
209 {
210 // _LOG_ERROR ("Hash expects (text,integer) parameters");
211 sqlite3_result_error (context, "Hash expects (text,integer) parameters", -1);
212 return;
213 }
214
215 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
216
217 if (hash_context == 0)
218 {
219 sqlite3_result_error_nomem (context);
220 return;
221 }
222
223 if (*hash_context == 0)
224 {
225 *hash_context = EVP_MD_CTX_create ();
226 EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
227 }
228
229 int nameBytes = sqlite3_value_bytes (argv[0]);
230 const unsigned char *name = sqlite3_value_text (argv[0]);
231 sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
232
233 EVP_DigestUpdate (*hash_context, name, nameBytes);
234 EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
235}
236
237void
238DbHelper::hash_xFinal (sqlite3_context *context)
239{
240 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
241
242 if (hash_context == 0)
243 {
244 sqlite3_result_error_nomem (context);
245 return;
246 }
247
248 if (*hash_context == 0) // no rows
249 {
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800250 char charNullResult = 0;
251 sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800252 return;
253 }
254
255 unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
256 unsigned int hashLength = 0;
257
258 int ok = EVP_DigestFinal_ex (*hash_context,
259 hash, &hashLength);
260
261 sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
262 delete [] hash;
263
264 EVP_MD_CTX_destroy (*hash_context);
265}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800266
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800267
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800268