-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
336 lines (278 loc) · 7.14 KB
/
Copy pathdb.cpp
File metadata and controls
336 lines (278 loc) · 7.14 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
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
#include "db.h"
#include<sstream>
#include<cassert>
#include<algorithm>
#include "curl.h"
#include "util.h"
namespace tba{
std::ostream& operator<<(std::ostream& o,Fetch_result const& a){
o<<"Fetche_results( ";
#define X(A,B) o<<""#B<<":"<<a.B<<" ";
FETCH_RESULT(X)
#undef X
return o<<")";
}
static int callback2(void *data,int arg,char **argv,char **azColname);
Sqlite::Sqlite(const char *filename){
int rc=sqlite3_open(filename,&db);
if(rc){
std::ostringstream ss;
ss<<"Could not open database: "<<filename;
throw std::runtime_error{ss.str()};
}
}
Sqlite::~Sqlite(){
sqlite3_close(db);
}
Sqlite::operator sqlite3 *()const{ return db; }
std::vector<Row> Sqlite::query(std::string const& s){
std::vector<Row> r;
r.reserve(1);
char *err_msg;
int rc=sqlite3_exec(db,s.c_str(),callback2,&r,&err_msg);
if(rc!=SQLITE_OK){
std::ostringstream ss;
ss<<"SQL error:"<<err_msg<<" tried running: "<<s;
sqlite3_free(err_msg);
throw std::runtime_error{ss.str()};
}
return r;
}
static int callback2(void *data,int argc,char **argv,char **azColname){
std::vector<Row> &r=*(std::vector<Row>*)data;
Row row;
row.reserve(argc);
for(int i=0;i<argc;i++){
row.push_back(std::make_pair(azColname[i],argv[i]));
}
r.push_back(std::move(row));
return 0;
}
using Date=std::string;
Fetch_result Fetcher::fetch(URL url,std::optional<ETag> etag_in)const{
auto g=tba::get_url(
url,
[&](){
std::vector<std::string> r;
r|={"X-TBA-Auth-Key: "+auth_key};
if(etag_in){
r|="ETag: "+*etag_in;
}
return r;
}()
);
auto get_header=[&](std::string name)->std::optional<std::string>{
auto f=std::find_if(g.headers.begin(),g.headers.end(),[=](auto const& p){ return p.first==name; });
if(f!=g.headers.end()){
return f->second;
}
return std::nullopt;
};
auto date=[&]()->std::optional<std::string>{
auto f2=std::find_if(g.headers.begin(),g.headers.end(),[](auto const& p){ return p.first=="Date" || p.first=="date"; });
if(f2!=g.headers.end()){
return f2->second;
}
return std::nullopt;
}();
auto etag_out=[&]()->std::optional<std::string>{
auto f=std::find_if(g.headers.begin(),g.headers.end(),[](auto const& p){ return p.first=="ETag"; });
if(f!=g.headers.end()){
return f->second;
}
return std::nullopt;
}();
assert(date);
return Fetch_result(
date,
etag_out,
get_header("Cache-Control"),
g.data
);
/*{
std::ostringstream ss;
ss<<"fetching:"<<url<<"\n";
//ss<<"got:"<<g.headers<<"\n";
ss<<"got:\n";
for(auto elem:g.headers) ss<<elem<<"\n";
ss<<"Error: Did not find Last-Modified header";
throw std::runtime_error{ss.str()};
}*/
}
Cache_policy::Cache_policy(Type type1):type_(type1){}
Cache_policy::Type Cache_policy::type()const{ return type_; }
HTTP_Date const& Cache_policy::date()const{
assert(type()==Type::DATE);
return date_;
}
Cache_policy Cache_policy::any(){ return Cache_policy{Type::ANY}; }
Cache_policy Cache_policy::date(Date date){
Cache_policy r{Type::DATE};
r.date_=date;
return r;
}
Cache_policy Cache_policy::none(){ return Cache_policy{Type::NONE}; }
Cache::Cache(const char *filename):db(filename),policy(Cache_policy::any()){
db.query("CREATE TABLE IF NOT EXISTS cache (url STRING UNIQUE NOT NULL,date INT NOT NULL,body VARCHAR NOT NULL);");
db.query(
"CREATE TABLE IF NOT EXISTS cache2 ("
//could make this enforce uniqueness, but want to be able to keep track of how data changes
//so queries will have to ask for the most recent.
"url TEXT NOT NULL,"
//because sqlite does not include a real date/time type
//this will be UNIX time, in relative to GMT.
"date INTEGER NOT NULL,"
//supposedly included in all the queries, so hopefully the non-null is ok.
"etag TEXT NOT NULL,"
"cache_control TEXT NOT NULL,"
"body TEXT NOT NULL"
") STRICT;" //strict not used originally because it didn't exist yet.
);
}
Cache::Cache():Cache("cache.db"){
}
void Cache::update(URL url,Fetch_result a){
assert(a.date);
//const char* sql = "INSERT INTO cache (url, date, etag, body) VALUES (?, ?, ?)";
//const char *sql="UPDATE cache2 SET date=?, body=?, WHERE url=?";
//nyi
update(url,std::pair<Date,Data>(*a.date,a.data));
}
void Cache::update(URL url,std::pair<Date,Data> p){
const char *sql="UPDATE cache SET date=?, body=? WHERE url=?";
sqlite3_stmt *stmt;
{
int r=sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
if(r!=SQLITE_OK){
std::stringstream ss;
ss<<"sqlite3: Failed to prepate statement: "<<sqlite3_errstr(r);
throw ss.str();
}
}
auto bind=[&](int index,std::string const& s){
int r=sqlite3_bind_text(stmt,index,s.c_str(),s.size(),SQLITE_STATIC);
if(r!=SQLITE_OK){
throw "sqlite3_bind_text";
}
};
bind(1,p.first);
bind(2,p.second);
bind(3,url);
{
int r=sqlite3_step(stmt);
if(r!=SQLITE_DONE){
std::stringstream ss;
ss<<"sqlite3: Execution failed: "<<sqlite3_errstr(r);
throw ss.str();
}
}
{
auto r=sqlite3_finalize(stmt);
if(r!=SQLITE_OK){
throw "sqlite3_finalize";
}
}
}
void Cache::add(URL url,std::pair<Date,Data> p){
auto date=p.first;
auto body=p.second;
const char* sql = "INSERT INTO cache (url, date, body) VALUES (?, ?, ?)";
sqlite3_stmt *stmt;
{
int r = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (r != SQLITE_OK) {
std::stringstream ss;
ss<< "Failed to prepare statement: " << sqlite3_errstr(r);
throw ss.str();
}
}
//Note the 1-based index
auto bind=[&](int index,std::string const& s){
int r=sqlite3_bind_text(stmt,index,s.c_str(),s.size(),SQLITE_STATIC);
if(r!=SQLITE_OK){
throw "sqlite3 failed";
}
};
bind(1,url);
bind(2,date);
bind(3,body);
{
int r=sqlite3_step(stmt);
if (r!=SQLITE_DONE){
std::stringstream ss;
ss<<"sqlite3: Execution failed: "<<sqlite3_errstr(r);
throw ss.str();
}
}
{
auto r=sqlite3_finalize(stmt);
if(r!=SQLITE_OK){
throw "sqlite3 error";
}
}
}
void Cache::add(URL url,Fetch_result fr){
if(!fr.date){
TBA_PRINT(fr);
}
assert(fr.date);
return add(
url,
make_pair(*fr.date,fr.data)
);
}
struct Mutex_lock{
std::mutex &mutex;
Mutex_lock(std::mutex &m):mutex(m){
m.lock();
}
~Mutex_lock(){
mutex.unlock();
}
};
std::optional<std::pair<Date,Data>> Cache::fetch(URL url){
switch(policy.type()){
case Cache_policy::Type::NONE:
return {};
case Cache_policy::Type::ANY:
break;
default:
TBA_NYI
}
Mutex_lock lock(mutex);
std::ostringstream ss;
ss<<"SELECT date,body FROM cache WHERE url=\'"<<url<<"\'";
auto r=db.query(ss.str());
if(r.size()==0){
return {};
}
if(r.size()==1){
auto &x=r[0];
assert(x.size()==2);
auto &date=x[0].second;
auto &body=x[1].second;
//cout<<"date:"<<date<<"\n";
//cout<<"body:"<<body<<"\n";
assert(date);
assert(body);
return make_pair(std::move(*date),std::move(*body));
}
assert(0);TBA_NYI
}
std::pair<Date,Data> Cached_fetcher::fetch(URL url){
auto c=cache.fetch(url);
if(c) return std::move(*c);
auto f=fetcher.fetch(url,std::nullopt);
cache.add(url,f);
TBA_PRINT(url);
assert(f.date);
return make_pair(*f.date,f.data);
}
Nonempty_string::Nonempty_string(std::string s1):s(std::move(s1)){}
std::string const& Nonempty_string::str()const{
return s;
}
std::string operator+(const char *s1,Nonempty_string const& s2){
return s1+s2.str();
}
}