Menu

[r1523]: / cppdb / trunk / backend.cpp  Maximize  Restore  History

Download this file

154 lines (137 with data), 2.9 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
#include "backend.h"
#include "utils.h"
#include "pool.h"
#include <map>
#include <list>
namespace cppdb {
namespace backend {
void statement::dispose(statement *p)
{
if(!p)
return;
statements_cache *cache = p->cache_;
p->cache_ = 0;
if(cache)
cache->put(p);
else
delete p;
}
void connection::dispose(connection *c)
{
if(!c)
return;
pool *p = c->pool_;
c->pool_ = 0;
if(p) {
p->put(c);
}
else
delete c;
}
struct statements_cache::data {
data() :
size(0),
max_size(0)
{
}
struct entry;
typedef std::map<std::string,entry> statements_type;
typedef std::list<statements_type::iterator> lru_type;
struct entry {
ref_ptr<statement> stat;
lru_type::iterator lru_ptr;
};
statements_type statements;
lru_type lru;
size_t size;
size_t max_size;
void insert(ref_ptr<statement> st)
{
statements_type::iterator p;
if((p=statements.find(st->sql_query()))!=statements.end()) {
p->second.stat = st;
lru.erase(p->second.lru_ptr);
lru.push_front(p);
p->second.lru_ptr = lru.begin();
}
else {
if(size > 0 && size >= max_size) {
statements.erase(lru.back());
lru.pop_back();
size--;
}
std::pair<statements_type::iterator,bool> ins =
statements.insert(std::make_pair(st->sql_query(),entry()));
p = ins.first;
p->second.stat = st;
lru.push_front(p);
p->second.lru_ptr = lru.begin();
size ++;
}
}
ref_ptr<statement> fetch(std::string const &query)
{
ref_ptr<statement> st;
statements_type::iterator p = statements.find(query);
if(p==statements.end())
return st;
st=p->second.stat;
lru.erase(p->second.lru_ptr);
statements.erase(p);
return st;
}
}; // data
statements_cache::statements_cache()
{
}
void statements_cache::set_size(size_t n)
{
if(n!=0 && !active()) {
d.reset(new data());
d->max_size = n;
}
}
void statements_cache::put(statement *p_in)
{
if(!active()) {
delete p_in;
}
ref_ptr<statement> p(p_in);
d->insert(p);
}
ref_ptr<statement> statements_cache::fetch(std::string const &q)
{
if(!active())
return 0;
return d->fetch(q);
}
statements_cache::~statements_cache()
{
}
bool statements_cache::active()
{
return d.get()!=0;
}
ref_ptr<statement> connection::prepare(std::string const &q)
{
ref_ptr<statement> st;
if(!cache_.active()) {
st = real_prepare(q);
return st;
}
st = cache_.fetch(q);
if(!st)
st = real_prepare(q);
st->cache(&cache_);
return st;
}
connection::connection(connection_info const &info) :
pool_(0)
{
int cache_size = info.get("@stmt_cache_size",64);
if(cache_size > 0) {
cache_.set_size(cache_size);
}
}
} // backend
} // cppdb
MongoDB Logo MongoDB