-
Notifications
You must be signed in to change notification settings - Fork 0
/
hn2sqlite.cpp
470 lines (413 loc) · 12.5 KB
/
hn2sqlite.cpp
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* hn2mbox -- convert HackerNews stories and comments from JSON to a sqlite
* database
*
* See https://github.com/jesrui/hn2sqlite and
* https://github.com/sytelus/HackerNewsData
*/
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <sqlite3.h>
#include <string>
#include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#define printd(...) fprintf(stderr, __VA_ARGS__)
//#define printd(...)
using namespace std;
using namespace rapidjson;
static const int32_t kDbVersion = 1;
static_assert(kDbVersion != 0, "Database version can't be 0");
// Either a story or a comment
struct Item {
string created_at;
string title;
string url;
string author;
int points;
string story_text;
string comment_text;
unsigned num_comments;
unsigned story_id;
string story_title;
string story_url;
unsigned parent_id;
unsigned created_at_i;
unsigned objectID;
};
// the Item field being currently parsed
enum class Element
{
none,
created_at,
title,
url,
author,
points,
story_text,
comment_text,
num_comments,
story_id,
story_title,
story_url,
parent_id,
created_at_i,
objectID,
};
int getDBUserVersion(sqlite3 *db, int32_t *user_version)
{
struct CB {
static int callback(void *u, int argc, char **argv,
char **azColName)
{
auto user_version = (int32_t*)u;
*user_version = argc >= 0 ? atoi(argv[0]) : -1;
return 0;
}
};
char *zErrMsg = NULL;
int rc = sqlite3_exec(db, "PRAGMA user_version;", CB::callback, user_version,
&zErrMsg);
if( rc!=SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
return rc;
}
int setDBUserVersion(sqlite3 *db, int32_t user_version)
{
char q[100];
snprintf(q, sizeof q, "PRAGMA user_version= %d", user_version);
char *zErrMsg = NULL;
int rc = sqlite3_exec(db, q, NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
return rc;
}
int setupDatabase(sqlite3 *db)
{
int32_t user_version = 0;
int rc = getDBUserVersion(db, &user_version);
if (rc != SQLITE_OK)
return rc;
fprintf(stderr, "DB user_version=%d\n", user_version);
if (user_version == kDbVersion) // assume database is already initialized
return SQLITE_OK;
if (user_version != 0) { // assume incompatible database schema
rc = SQLITE_SCHEMA;
fprintf(stderr, "can't init database: %s\n", sqlite3_errstr(rc));
return rc;
}
rc = setDBUserVersion(db, kDbVersion);
if (rc != SQLITE_OK)
return rc;
#if 0
CREATE TABLE IF NOT EXISTS stories (
objectID INTEGER PRIMARY KEY,
created_at TEXT,
title TEXT,
url TEXT,
author TEXT,
points INTEGER,
story_text TEXT,
comment_text TEXT,
num_comments INTEGER,
story_id INTEGER,
story_title TEXT,
story_url TEXT,
parent_id INTEGER,
created_at_i INTEGER);
#endif
const char sql_setup_script[] = R"END_SCRIPT(
CREATE TABLE IF NOT EXISTS stories (
objectID INTEGER PRIMARY KEY,
title TEXT,
url TEXT,
author TEXT,
points INTEGER,
story_text TEXT,
num_comments INTEGER,
created_at_i INTEGER);
CREATE TABLE IF NOT EXISTS comments (
objectID INTEGER PRIMARY KEY,
author TEXT,
points INTEGER,
comment_text TEXT,
story_id INTEGER,
parent_id INTEGER,
created_at_i INTEGER);
)END_SCRIPT";
char *zErrMsg = NULL;
rc = sqlite3_exec(db, sql_setup_script, NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
return rc;
}
return SQLITE_OK;
}
int insertStory(sqlite3 *db, const Item &item)
{
assert(item.parent_id == 0);
// printd("%s objectID=%u\n", __func__, item.objectID);
sqlite3_stmt *stmt;
const char q[] = R"END_SCRIPT(
insert into stories (
objectID,
title,
url,
author,
points,
story_text,
num_comments,
created_at_i)
values (?1,?2,?3,?4,?5,?6,?7,?8);";
)END_SCRIPT";
int rc = sqlite3_prepare_v2(db, q, sizeof q, &stmt, NULL);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 1, item.objectID);
if (rc)
goto out;
rc = sqlite3_bind_text(stmt, 2, item.title.c_str(), -1, SQLITE_TRANSIENT);
if (rc)
goto out;
rc = sqlite3_bind_text(stmt, 3, item.url.c_str(), -1, SQLITE_TRANSIENT);
if (rc)
goto out;
rc = sqlite3_bind_text(stmt, 4, item.author.c_str(), -1, SQLITE_TRANSIENT);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 5, item.points);
if (rc)
goto out;
rc = sqlite3_bind_text(stmt, 6, item.story_text.c_str(), -1, SQLITE_TRANSIENT);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 7, item.num_comments);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 8, item.created_at_i);
if (rc)
goto out;
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
// fprintf(stderr, "can't exec statement: %s\n", sqlite3_errstr(rc));
goto out;
}
rc = SQLITE_OK;
out:
sqlite3_finalize(stmt);
return rc;
}
int insertComment(sqlite3 *db, const Item &item)
{
assert(item.parent_id != 0);
// printd("%s objectID=%u\n", __func__, item.objectID);
sqlite3_stmt *stmt;
const char q[] = R"END_SCRIPT(
insert into comments (
objectID,
author,
points,
comment_text,
story_id,
parent_id,
created_at_i)
values (?1,?2,?3,?4,?5,?6,?7);";
)END_SCRIPT";
int rc = sqlite3_prepare_v2(db, q, sizeof q, &stmt, NULL);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 1, item.objectID);
if (rc)
goto out;
rc = sqlite3_bind_text(stmt, 2, item.author.c_str(), -1, SQLITE_TRANSIENT);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 3, item.points);
if (rc)
goto out;
rc = sqlite3_bind_text(stmt, 4, item.comment_text.c_str(), -1, SQLITE_TRANSIENT);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 5, item.story_id);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 6, item.parent_id);
if (rc)
goto out;
rc = sqlite3_bind_int(stmt, 7, item.created_at_i);
if (rc)
goto out;
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
// fprintf(stderr, "can't exec statement: %s\n", sqlite3_errstr(rc));
goto out;
}
rc = SQLITE_OK;
out:
sqlite3_finalize(stmt);
return rc;
}
template<typename Encoding = UTF8<>>
struct ItemsHandler {
typedef typename Encoding::Ch Ch;
ItemsHandler(sqlite3 *db)
: db(db), element(Element::none), parent(noparent), level(0), item {}
{}
void Default() {}
void Null() { element = Element::none; }
void Bool(bool) { Default(); }
void Int(int i) { Int64(i); }
void Uint(unsigned i) { Int64(i); }
void Int64(int64_t i)
{
if (parent == _highlightResult)
return;
switch (element) {
case Element::points: item.points = i; break;
case Element::num_comments: item.num_comments = i; break;
case Element::story_id: item.story_id = i; break;
case Element::parent_id: item.parent_id = i; break;
case Element::created_at_i: item.created_at_i = i; break;
default: break;
}
element = Element::none;
}
void Uint64(uint64_t) { Default(); }
void Double(double) { Default(); }
void String(const Ch* str, SizeType length, bool copy)
{
if (parent == _highlightResult)
return;
if (element == Element::none) {
if (strcmp("hits", str) == 0)
parent = hits;
else if (strcmp("_highlightResult", str) == 0)
parent = _highlightResult;
else if (strcmp("created_at", str) == 0)
element = Element::created_at;
else if (strcmp("title", str) == 0)
element = Element::title;
else if (strcmp("url", str) == 0)
element = Element::url;
else if (strcmp("author", str) == 0)
element = Element::author;
else if (strcmp("points", str) == 0)
element = Element::points;
else if (strcmp("story_text", str) == 0)
element = Element::story_text;
else if (strcmp("comment_text", str) == 0)
element = Element::comment_text;
else if (strcmp("num_comments", str) == 0)
element = Element::num_comments;
else if (strcmp("story_id", str) == 0)
element = Element::story_id;
else if (strcmp("story_title", str) == 0)
element = Element::story_title;
else if (strcmp("story_url", str) == 0)
element = Element::story_url;
else if (strcmp("parent_id", str) == 0)
element = Element::parent_id;
else if (strcmp("created_at_i", str) == 0)
element = Element::created_at_i;
else if (strcmp("objectID", str) == 0)
element = Element::objectID;
return;
}
if (element == Element::none)
return;
// minimal string normalization
string s = str;
replace(s.begin(), s.end(), '\n', ' ');
remove(s.begin(), s.end(), '\r');
switch (element) {
case Element::created_at: item.created_at = s; break;
case Element::title: item.title = s; break;
case Element::url: item.url = s; break;
case Element::author: item.author = s; break;
case Element::story_text: item.story_text = s; break;
case Element::comment_text: item.comment_text = s; break;
case Element::story_title: item.story_title = s; break;
case Element::story_url: item.story_url = s; break;
case Element::objectID: item.objectID = atoi(s.c_str()); break;
default: break;
}
element = Element::none;
}
void StartObject() { ++level; }
void EndObject(SizeType)
{
if (--level == 1) {
parent = hits;
int rc = item.parent_id != 0
? insertComment(db, item): insertStory(db, item);
if (rc)
fprintf(stderr, "Can't insert objectID=%u: %s\n",
item.objectID, sqlite3_errmsg(db));
item = {};
}
}
void StartArray() { Default(); }
void EndArray(SizeType) { Default(); }
sqlite3 *db;
Element element;
enum {
noparent,
hits,
_highlightResult,
} parent;
int level;
Item item;
};
int main(int argc, char* argv[])
{
if (argc < 2) {
fprintf(stderr, "no database specified\n");
exit(1);
}
sqlite3 *db;
int rc = sqlite3_open(argv[1], &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rc = setupDatabase(db);
if (rc) {
sqlite3_close(db);
return 1;
}
Reader reader;
char readBuffer[65536];
FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
ItemsHandler<> handler(db);
char *zErrMsg = NULL;
rc = sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &zErrMsg);
if (rc) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
return 1;
}
bool ok = reader.Parse<kParseValidateEncodingFlag>(is, handler);
if (!ok) {
fprintf(stderr, "\nError(%u): %s\n",
(unsigned)reader.GetErrorOffset(), reader.GetParseError());
}
rc = sqlite3_exec(db, "END TRANSACTION", NULL, NULL, &zErrMsg);
if (rc) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return ok ? 0 : 1;
}