Menu

[r938]: / framework / branches / refactoring / thread_cache.cpp  Maximize  Restore  History

Download this file

283 lines (260 with data), 7.4 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
#define CPPCMS_SOURCE
#include "config.h"
#include "thread_cache.h"
#ifdef CPPCMS_USE_EXTERNAL_BOOST
# include <boost/thread.hpp>
# include <boost/format.hpp>
#else // Internal Boost
# include <cppcms_boost/thread.hpp>
# include <cppcms_boost/format.hpp>
namespace boost = cppcms_boost;
#endif
using namespace std;
namespace cppcms {
namespace impl {
class thread_cache : public base_cache {
boost::mutex lru_mutex;
boost::shared_mutex access_lock;
struct container {
string data;
typedef std::map<string,container>::iterator pointer;
list<pointer>::iterator lru;
list<multimap<string,pointer>::iterator> triggers;
multimap<time_t,pointer>::iterator timeout;
};
typedef container::pointer pointer;
std::map<string,container> primary;
multimap<string,pointer> triggers;
typedef multimap<string,pointer>::iterator triggers_ptr;
multimap<time_t,pointer> timeout;
typedef multimap<time_t,pointer>::iterator timeout_ptr;
list<pointer> lru;
typedef list<pointer>::iterator lru_ptr;
unsigned limit;
string const *get(string const &key,set<string> *triggers);
void delete_node(pointer p);
void print_all();
bool debug_mode;
int fd;
public:
void set_debug_mode(int fd) { debug_mode=true; this->fd=fd; };
thread_cache(unsigned pages=0) : limit(pages) {
debug_mode=false;
};
void set_size(unsigned l) { limit=l; };
virtual bool fetch(string const &key,string &a,std::set<std::string> *tags);
virtual void rise(string const &trigger);
virtual void clear();
virtual void stats(unsigned &keys,unsigned &triggers);
virtual void store(string const &key,std::string const &a,set<string> const &triggers,time_t timeout);
virtual ~thread_cache();
}; // thread cache
thread_cache::~thread_cache()
{
}
std::string const *thread_cache::get(string const &key,set<string> *triggers)
{
pointer p;
time_t now;
time(&now);
if(debug_mode) print_all();
if((p=primary.find(key))==primary.end() || p->second.timeout->first < now) {
if(debug_mode) {
string res;
if(p==primary.end()) {
res=str(boost::format("Not found [%1%]\n") % key);
}
else {
res=str(boost::format("Found [%1%] but timeout of %2% seconds\n")
% key % (now - p->second.timeout->first));
}
write(fd,res.c_str(),res.size());
}
return NULL;
}
if(triggers) {
list<triggers_ptr>::iterator tp;
for(tp=p->second.triggers.begin();tp!=p->second.triggers.end();tp++) {
triggers->insert((*tp)->first);
}
}
{
boost::unique_lock<boost::mutex> lock(lru_mutex);
lru.erase(p->second.lru);
lru.push_front(p);
p->second.lru=lru.begin();
}
if(debug_mode){
string res=(boost::format("Fetched [%1%] triggers:") % key).str();
list<triggers_ptr>::iterator tp;
for(tp=p->second.triggers.begin();
tp!=p->second.triggers.end();tp++)
{
res+=(*tp)->first;
res+=" ";
}
res+="\n";
write(fd,res.c_str(),res.size());
}
return &(p->second.data);
}
bool thread_cache::fetch(string const &key,std::string &a,set<string> *tags)
{
boost::shared_lock<boost::shared_mutex> lock(access_lock);
string const *r=get(key,tags);
if(!r) return false;
a = *r;
return true;
}
void thread_cache::clear()
{
boost::unique_lock<boost::shared_mutex> lock(access_lock);
timeout.clear();
lru.clear();
primary.clear();
triggers.clear();
}
void thread_cache::stats(unsigned &keys,unsigned &triggers)
{
boost::shared_lock<boost::shared_mutex> lock(access_lock);
keys=primary.size();
triggers=this->triggers.size();
}
void thread_cache::rise(string const &trigger)
{
boost::unique_lock<boost::shared_mutex> lock(access_lock);
if(debug_mode) print_all();
pair<triggers_ptr,triggers_ptr> range=triggers.equal_range(trigger);
triggers_ptr p;
list<pointer> kill_list;
for(p=range.first;p!=range.second;p++) {
kill_list.push_back(p->second);
}
list<pointer>::iterator lptr;
if(debug_mode){
string out=str(boost::format("Trigger [%1%] dropping: ") % trigger);
write(fd,out.c_str(),out.size());
}
for(lptr=kill_list.begin();lptr!=kill_list.end();lptr++) {
if(debug_mode) {
write(fd,(*lptr)->first.c_str(),(*lptr)->first.size());
write(fd," ",1);
}
delete_node(*lptr);
}
if(debug_mode)
write(fd,"\n",1);
}
void thread_cache::store(string const &key,std::string const &a,set<string> const &triggers_in,time_t timeout_in)
{
boost::unique_lock<boost::shared_mutex> lock(access_lock);
if(debug_mode) print_all();
pointer main;
if(debug_mode) {
string res;
res=str(boost::format("Storing key [%1%], triggers:") % key);
for(set<string>::iterator ps=triggers_in.begin(),pe=triggers_in.end();ps!=pe;ps++) {
res+=*ps;
res+=" ";
}
res+="\n";
write(fd,res.c_str(),res.size());
}
main=primary.find(key);
if(main==primary.end() && primary.size()>=limit && limit>0) {
if(debug_mode) {
char const *msg="Not found, size limit\n";
write(fd,msg,strlen(msg));
}
time_t now;
time(&now);
if(timeout.begin()->first<now) {
main=timeout.begin()->second;
if(debug_mode) {
string res;
res=str(boost::format("Deleting timeout node [%1%] with "
"delta of %2% seconds\n") % main->first
% (now - main->second.timeout->first));
write(fd,res.c_str(),res.size());
}
}
else {
main=lru.back();
if(debug_mode) {
string res;
res=str(boost::format("Deleting LRU [%1%]\n") % main->first);
write(fd,res.c_str(),res.size());
}
}
}
if(main!=primary.end())
delete_node(main);
pair<pointer,bool> res=primary.insert(pair<string,container>(key,container()));
main=res.first;
container &cont=main->second;
cont.data=a;
lru.push_front(main);
cont.lru=lru.begin();
cont.timeout=timeout.insert(pair<time_t,pointer>(timeout_in,main));
if(triggers_in.find(key)==triggers_in.end()){
cont.triggers.push_back(triggers.insert(pair<string,pointer>(key,main)));
}
set<string>::const_iterator si;
for(si=triggers_in.begin();si!=triggers_in.end();si++) {
cont.triggers.push_back(triggers.insert(pair<string,pointer>(*si,main)));
}
}
void thread_cache::delete_node(pointer p)
{
lru.erase(p->second.lru);
timeout.erase(p->second.timeout);
list<triggers_ptr>::iterator i;
for(i=p->second.triggers.begin();i!=p->second.triggers.end();i++) {
triggers.erase(*i);
}
primary.erase(p);
}
void thread_cache::print_all()
{
string res;
res+="Printing stored keys\n";
unsigned N_triggers=0;
unsigned N_keys=0;
time_t now;
time(&now);
for(pointer p=primary.begin();p!=primary.end();p++) {
N_keys++;
res+=str(boost::format("%1%: timeount in %2% sec, triggers:") % p->first
% (p->second.timeout->first - now));
for(list<triggers_ptr>::iterator p1=p->second.triggers.begin(),
p2=p->second.triggers.end();
p2!=p1;p1++)
{
N_triggers++;
res+=(*p1)->first;
res+=" ";
}
res+="\n";
}
res+="LRU order:";
for(list<pointer>::iterator pl=lru.begin();pl!=lru.end();pl++) {
res+=(*pl)->first;
res+=" ";
}
res+="\n";
if(N_keys!=timeout.size() || N_keys!=lru.size() || N_triggers!=triggers.size()){
res+=str(boost::format("Internal error #prim=%1%, #lru=%2%, "
"#prim.triggers=%3% #triggers=%4%\n")
% N_keys % lru.size() % N_triggers % triggers.size());
}
else {
res+=str(boost::format("#Keys=%1% #Triggers=%2%\n") % N_keys % N_triggers);
}
write(fd,res.c_str(),res.size());
}
intrusive_ptr<base_cache> thread_cache_factory(unsigned items)
{
return new thread_cache(items);
}
} // impl
} // cppcms
MongoDB Logo MongoDB