Repository navigation
Expand file tree
/
Copy pathdatabase.cpp
More file actions
298 lines (263 loc) · 8.56 KB
/
Copy pathdatabase.cpp
File metadata and controls
298 lines (263 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <iostream>
#include <string>
#include <unistd.h>
#include "database.h"
#define check(r) if((r) != SQLITE_OK) { goto sqlerror; }
DatabaseError::DatabaseError(const std::string &msg)
: m_Message(msg)
{
}
DatabaseError::~DatabaseError() throw()
{
}
const char *DatabaseError::what() const throw()
{
return m_Message.c_str();
}
Database::Database(const char *filepath, Generator &generator)
throw(DatabaseError)
: m_Generator(generator)
{
if(sqlite3_open(filepath, &m_DB) != SQLITE_OK)
throw DatabaseError(std::string("Can't open ") + filepath);
sqlite3_busy_timeout(m_DB, 50);
// Checks whether the database is already setup
bool database_setup = false;
if(sqlite3_prepare_v2(m_DB, "SELECT state FROM generator_state;",
-1, &m_stmtGetState, NULL) == SQLITE_OK)
{
int ret = sqlite3_step(m_stmtGetState);
if(ret == SQLITE_ROW)
{
sqlite3_int64 state = sqlite3_column_int(m_stmtGetState, 0);
database_setup = true;
std::cerr << "Database initialized, generator state: " << state
<< std::endl;
ret = sqlite3_step(m_stmtGetState);
}
if(ret != SQLITE_DONE)
goto sqlerror;
sqlite3_reset(m_stmtGetState);
}
if(!database_setup)
{
// Creates the tables
const char *sql[] = {
"CREATE TABLE generator_state("
" state INTEGER NOT NULL"
" );",
"CREATE TABLE urls("
" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
" our_url TEXT NOT NULL,"
" target_url TEXT NOT NULL,"
" author_ip TEXT NULL,"
" author_date TEXT NOT NULL,"
" views INTEGER NOT NULL"
" );",
"CREATE INDEX urls_our_url_idx ON urls(our_url);",
"INSERT INTO generator_state(state) VALUES(0);"
};
for(const char *sql_line : sql)
check(sqlite3_exec(m_DB, sql_line, NULL, NULL, NULL));
std::cerr << "Database created, generator state: 0" << std::endl;
}
if(!database_setup)
check(sqlite3_prepare_v2(m_DB, "SELECT state FROM generator_state;",
-1, &m_stmtGetState, NULL));
check(sqlite3_prepare_v2(
m_DB,
"UPDATE generator_state SET state=?;",
-1, &m_stmtSetState, NULL));
check(sqlite3_prepare_v2(
m_DB,
"INSERT INTO urls(our_url, target_url, "
"author_ip, author_date, views) "
"VALUES(?, ?, ?, datetime(), 0);",
-1, &m_stmtInsertURL, NULL));
check(sqlite3_prepare_v2(
m_DB,
"SELECT target_url FROM urls WHERE our_url=?;",
-1, &m_stmtGetURL, NULL));
check(sqlite3_prepare_v2(
m_DB,
"UPDATE urls SET views=views+1 WHERE our_url=?;",
-1, &m_stmtIncrementURLViews, NULL));
check(sqlite3_prepare_v2(
m_DB,
"SELECT views FROM urls WHERE our_url=?;",
-1, &m_stmtGetViews, NULL));
check(sqlite3_prepare_v2(
m_DB,
"BEGIN IMMEDIATE;",
-1, &m_stmtBegin, NULL));
check(sqlite3_prepare_v2(
m_DB,
"COMMIT;",
-1, &m_stmtCommit, NULL));
check(sqlite3_prepare_v2(
m_DB,
"ROLLBACK;",
-1, &m_stmtRollback, NULL));
return;
sqlerror:
throw DatabaseError("Couldn't initialize database");
}
Database::~Database()
{
sqlite3_finalize(m_stmtGetState);
sqlite3_finalize(m_stmtSetState);
sqlite3_finalize(m_stmtInsertURL);
sqlite3_finalize(m_stmtGetURL);
sqlite3_finalize(m_stmtIncrementURLViews);
sqlite3_finalize(m_stmtGetViews);
sqlite3_finalize(m_stmtBegin);
sqlite3_finalize(m_stmtCommit);
sqlite3_finalize(m_stmtRollback);
sqlite3_close(m_DB);
}
void Database::storeURL(const std::string &our_url,
const std::string &their_url,
const std::string &their_address) throw(DatabaseError)
{
check(sqlite3_bind_text(m_stmtInsertURL, 1,
our_url.c_str(), our_url.size(),
SQLITE_TRANSIENT));
check(sqlite3_bind_text(m_stmtInsertURL, 2,
their_url.c_str(), their_url.size(),
SQLITE_TRANSIENT));
check(sqlite3_bind_text(m_stmtInsertURL, 3,
their_address.c_str(), their_address.size(),
SQLITE_TRANSIENT));
if(sqlite3_step(m_stmtInsertURL) != SQLITE_DONE)
goto sqlerror;
sqlite3_reset(m_stmtInsertURL);
return;
sqlerror:
throw DatabaseError("Couldn't insert a URL");
}
void Database::incrementViews(const std::string &our_url) throw(DatabaseError)
{
check(sqlite3_bind_text(m_stmtIncrementURLViews, 1,
our_url.c_str(), our_url.size(),
SQLITE_TRANSIENT));
if(sqlite3_step(m_stmtIncrementURLViews) == SQLITE_DONE)
{
sqlite3_reset(m_stmtIncrementURLViews);
return;
}
sqlerror:
throw DatabaseError("Couldn't increment a URL's view count");
}
std::string Database::resolveURL(const std::string &our_url,
bool increment_views) throw(DatabaseError)
{
int ret;
check(sqlite3_bind_text(m_stmtGetURL, 1,
our_url.c_str(), our_url.size(),
SQLITE_TRANSIENT));
ret = sqlite3_step(m_stmtGetURL);
if(ret == SQLITE_DONE)
{
sqlite3_reset(m_stmtGetURL);
return "";
}
else if(ret == SQLITE_ROW)
{
std::string result((const char*)sqlite3_column_text(m_stmtGetURL, 0),
sqlite3_column_bytes(m_stmtGetURL, 0));
sqlite3_reset(m_stmtGetURL);
if(increment_views)
incrementViews(our_url);
return result;
}
sqlerror:
throw DatabaseError("Couldn't resolve a URL");
}
unsigned int Database::getViews(const std::string &our_url)
throw(DatabaseError)
{
int ret;
check(sqlite3_bind_text(m_stmtGetViews, 1,
our_url.c_str(), our_url.size(),
SQLITE_TRANSIENT));
ret = sqlite3_step(m_stmtGetViews);
if(ret == SQLITE_DONE)
{
sqlite3_reset(m_stmtGetViews);
return 0;
}
else if(ret == SQLITE_ROW)
{
unsigned int result = sqlite3_column_int(m_stmtGetViews, 0);
sqlite3_reset(m_stmtGetViews);
return result;
}
sqlerror:
throw DatabaseError("Couldn't get a URL's' view count");
}
sqlite3_int64 Database::getState() throw(DatabaseError)
{
if(sqlite3_step(m_stmtGetState) == SQLITE_ROW)
{
sqlite3_int64 result = sqlite3_column_int64(m_stmtGetState, 0);
if(sqlite3_step(m_stmtGetState) != SQLITE_DONE)
goto sqlerror;
sqlite3_reset(m_stmtGetState);
return result;
}
sqlerror:
throw DatabaseError("Couldn't get the generator state");
}
void Database::setState(sqlite3_int64 state) throw(DatabaseError)
{
check(sqlite3_bind_int64(m_stmtSetState, 1, state));
if(sqlite3_step(m_stmtSetState) != SQLITE_DONE)
goto sqlerror;
sqlite3_reset(m_stmtSetState);
return;
sqlerror:
throw DatabaseError("Couldn't set the generator state");
}
Key Database::nextState() throw(DatabaseError)
{
for(int wait = 0; wait <= 20; wait += 5)
{
if(wait > 0)
usleep(wait * 1000);
switch(sqlite3_step(m_stmtBegin))
{
case SQLITE_BUSY:
check(sqlite3_reset(m_stmtBegin));
continue;
case SQLITE_DONE:
check(sqlite3_reset(m_stmtBegin));
break;
default:
goto sqlerror;
}
Key state = getState();
state = m_Generator.generate(state);
check(sqlite3_bind_int64(m_stmtSetState, 1, state));
int ret = sqlite3_step(m_stmtSetState);
check(sqlite3_reset(m_stmtSetState));
if(ret == SQLITE_DONE)
{
ret = sqlite3_step(m_stmtCommit);
check(sqlite3_reset(m_stmtCommit));
}
switch(ret)
{
case SQLITE_DONE:
return state;
case SQLITE_BUSY:
if(sqlite3_step(m_stmtRollback) != SQLITE_DONE)
goto sqlerror;
check(sqlite3_reset(m_stmtRollback));
continue;
default:
goto sqlerror;
}
}
sqlerror:
throw DatabaseError("Couldn't advance the generator state");
}