Menu

[r625]: / framework / trunk / session_sqlite_storage.cpp  Maximize  Restore  History

Download this file

320 lines (298 with data), 7.7 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
#include "session_sid.h"
#include "global_config.h"
#include "session_sqlite_storage.h"
#include "cppcms_error.h"
#include "posix_mutex.h"
#include <sqlite3.h>
#include <pthread.h>
#include <poll.h>
#include <boost/lexical_cast.hpp>
namespace cppcms {
namespace storage {
class sqlite {
sqlite3 *db;
pthread_mutex_t mutexes;
int write_ops;
time_t last_commit;
int deferred_commit_count;
int deferred_commit_time;
pthread_mutex_t mutex;
public:
void exec(char const *s)
{
char *err=NULL;
int res;
while((res=sqlite3_exec(db,s,NULL,NULL,&err))!=0) {
if(res==SQLITE_BUSY) {
sqlite3_free(err);
poll(NULL,0,5);
continue;
}
string err_msg=err;
sqlite3_free(err);
throw cppcms_error(err_msg);
}
}
sqlite (string db_name,
bool sync_,
int deferred_commit_count_,
int deferred_commit_time_) :
db(0),
deferred_commit_count(deferred_commit_count_),
deferred_commit_time (deferred_commit_time_)
{
pthread_mutex_init(&mutex,NULL);
try {
if(sqlite3_open(db_name.c_str(),&db)) {
string error=sqlite3_errmsg(db);
throw cppcms_error(error);
}
exec( "CREATE TABLE IF NOT EXISTS sessions ( "
" sid varchar(32) primary key not null, "
" data varchar(1) not null, "
" timeout integer not null "
")");
exec( "CREATE INDEX IF NOT EXISTS sessions_timeout "
" ON sessions(timeout)");
if(!sync_) {
exec( "PRAGMA synchronous=OFF");
}
write_ops=0;
last_commit=time(NULL);
exec("begin");
}
catch(...) {
if(db) sqlite3_close(db);
pthread_mutex_destroy(&mutex);
throw;
}
}
~sqlite()
{
try {
exec("commit");
}
catch(...) {
}
if(db) sqlite3_close(db);
if(deferred_commit_count > 0)
pthread_mutex_destroy(&mutex);
}
void check_commits()
{
long long int now=time(NULL);
if( write_ops >= deferred_commit_count
|| (deferred_commit_time > 0 && last_commit + deferred_commit_time < now))
{
char stat[128];
snprintf(stat,sizeof(stat),"DELETE FROM sessions WHERE timeout < %lld",now);
exec(stat);
exec("commit");
exec("begin");
last_commit=time(NULL);
write_ops=0;
}
}
void exec(char const *s,string const &sid,string const &data,int64_t timeout)
{
sqlite3_stmt *st;
if(sqlite3_prepare(db,s,-1,&st,NULL)!=0) {
throw cppcms_error(string("sqlite prepared statement:")+sqlite3_errmsg(db));
}
sqlite3_bind_text(st,1, sid.c_str(), sid.size(),SQLITE_STATIC);
sqlite3_bind_blob(st,2,data.c_str(),data.size(),SQLITE_STATIC);
sqlite3_bind_int64(st,3,timeout);
int res;
while((res=sqlite3_step(st))==SQLITE_BUSY){
poll(NULL,0,5);
}
if(res==SQLITE_DONE) {
sqlite3_finalize(st);
}
else {
throw cppcms_error(string("Insert error:")+sqlite3_errmsg(db));
}
}
bool select(string const &sid,time_t &timeout,string &data)
{
sqlite3_stmt *st;
char const *q="SELECT data,timeout FROM sessions WHERE sid=?";
if(sqlite3_prepare(db,q,-1,&st,NULL)!=0) {
throw cppcms_error(string("sqlite prepared statement:")+sqlite3_errmsg(db));
}
int res;
sqlite3_bind_text(st,1, sid.c_str(), sid.size(),SQLITE_STATIC);
while((res=sqlite3_step(st))==SQLITE_BUSY){
poll(NULL,0,5);
}
if(res==SQLITE_DONE) {
sqlite3_finalize(st);
return false;
}
else if(res==SQLITE_ROW) {
int64_t to=sqlite3_column_int64(st,1);
if(to < time(NULL)) {
sqlite3_finalize(st);
del(sid);
write_ops++;
return false;
}
size_t length=sqlite3_column_bytes(st,0);
data.assign((const char *)sqlite3_column_blob(st,0),length);
timeout=to;
sqlite3_finalize(st);
return true;
}
else {
throw cppcms_error(string("Insert error:")+sqlite3_errmsg(db));
}
}
void save(string const &sid,time_t timeout,string const &data)
{
mutex_lock lock(mutex);
exec("INSERT OR REPLACE INTO sessions VALUES(?,?,?)",sid,data,timeout);
write_ops++;
check_commits();
}
void del(string const &sid)
{
char q[128];
snprintf(q,sizeof(q),"DELETE FROM sessions WHERE sid='%s'",sid.c_str());
exec(q);
}
void remove(string const &sid)
{
mutex_lock lock(mutex);
del(sid);
write_ops++;
check_commits();
}
bool load(std::string const &sid,time_t *timeout,std::string &out)
{
mutex_lock lock(mutex);
time_t tout;
if(!select(sid,tout,out)) {
check_commits();
return false;
}
if(timeout) *timeout=tout;
check_commits();
return true;
}
};
sqlite &sqlite_N::db(string const &sid)
{
char buf[3]={ sid.at(10) , sid.at(15) , 0 };
int v;
sscanf(buf,"%x",&v);
v = v%size;
return *dbs.at(v);
}
sqlite_N::sqlite_N(string db,int n,bool sync,int def_comm,int def_to) :
size(n)
{
dbs.resize(n);
int i;
for(i=0;i<n;i++) {
string fname=db+"_"+boost::lexical_cast<string>(i);
dbs[i]=boost::shared_ptr<sqlite>(new sqlite(fname,sync,def_comm,def_to));
}
}
bool sqlite_N::load(std::string const &sid,time_t *timeout,std::string &out)
{
return db(sid).load(sid,timeout,out);
}
void sqlite_N::remove(string const &sid)
{
db(sid).remove(sid);
}
void sqlite_N::save(string const &sid,time_t timeout,string const &data)
{
db(sid).save(sid,timeout,data);
}
} // storage
#ifndef NO_BUILDER_INTERFACE
namespace {
// The database is created at startup
struct builder_thread {
boost::shared_ptr<storage::sqlite_N> db;
bool cache;
builder_thread(string dir,int n,bool sync,int dc,int dt,bool c) :
db(new storage::sqlite_N(dir,n,sync,dc,dt)),
cache(c)
{
}
boost::shared_ptr<session_api> operator()(worker_thread &w)
{
boost::shared_ptr<session_server_storage> storage(new session_sqlite_storage(db));
return boost::shared_ptr<session_api>(new session_sid(storage,cache));
}
};
// The database created *AFTER* forks + no deferred commits
struct builder_proc {
string dir;
bool sync;
int size;
bool cache;
builder_proc(string d,int n,bool s,bool c) : dir(d) , sync(s) , size(n), cache(c)
{
}
boost::shared_ptr<session_api> operator()(worker_thread &w)
{
boost::shared_ptr<storage::sqlite_N> db(new storage::sqlite_N(dir,size,sync,0,0));
boost::shared_ptr<session_server_storage> storage(new session_sqlite_storage(db));
return boost::shared_ptr<session_api>(new session_sid(storage,cache));
}
};
}
session_backend_factory session_sqlite_storage::factory(cppcms_config const &config)
{
string db=config.sval("session.sqlite_db");
int db_count=config.ival("session.sqlite_db_num",4);
if(db_count>8)
db_count=8;
if(db_count<0)
db_count=0;
db_count=1<<db_count;
string def="fork";
if(config.sval("server.mod","")=="thread")
def="thread";
string mod=config.sval("session.sqlite_mod",def);
bool cache=config.ival("session.server_enable_cache",0);
if(mod=="fork") {
bool sync=config.ival("session.sqlite_sync",0);
return builder_proc(db,db_count,sync,cache);
}
else if(mod=="thread") {
bool sync=config.ival("session.sqlite_sync",1);
int dc=config.ival("session.sqlite_commits",1000);
int dt=config.ival("session.sqlite_commit_timeout",5);
return builder_thread(db,db_count,sync,dc,dt,cache);
}
else {
throw cppcms_error("Unknown sqlite mode:"+mod);
}
}
#else // NO_BUILDER_INTERFACE
session_backend_factory session_sqlite_storage::factory(cppcms_config const &config)
{
throw runtime_error("session_sqlite_storage::factory should bot be used");
}
#endif
session_sqlite_storage::session_sqlite_storage(boost::shared_ptr<storage::sqlite_N> db_):
db(db_)
{
}
void session_sqlite_storage::save(std::string const &sid,time_t timeout,std::string const &in)
{
db->save(sid,timeout,in);
}
bool session_sqlite_storage::load(std::string const &sid,time_t *timeout,std::string &out)
{
return db->load(sid,timeout,out);
}
void session_sqlite_storage::remove(std::string const &sid)
{
return db->remove(sid);
}
} // cppcms
MongoDB Logo MongoDB