-
Notifications
You must be signed in to change notification settings - Fork 16
/
MemStorage.Tc
90 lines (82 loc) · 1.78 KB
/
MemStorage.Tc
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
#include "libconfig.h++"
#include "logging.h"
#include "MemStorage.h"
MemStorage::MemStorage(log4cpp::Appender *app)
{
LOG.setAdditivity(false);
LOG.setAppender(app);
}
MemStorage::~MemStorage()
{
}
tamed void MemStorage::get(ID_Value key, cb_blob ret_blob) {
tvars{
MemStorage::mem_it it;
ptr<blob> ret_b;
blob to_ret;
u_int i;
}
it = mem_data.find(key);
if(it == mem_data.end()) {
TRIGGER(ret_blob, NULL);
} else {
to_ret.setsize(it->second.len);
for(i=0; i<it->second.len; i++) {
to_ret[i] = it->second.data[i];
}
ret_b = New refcounted<blob>(to_ret);
TRIGGER(ret_blob, ret_b);
}
}
tamed void MemStorage::set(ID_Value key, const blob* data, cbb ret_blob) {
tvars {
bool del_val;
MemStorage::mem_type to_add;
u_int i;
}
twait { del(key, mkevent(del_val)); }
to_add.len = data->size();
to_add.data = new unsigned char[to_add.len];
for(i=0; i<to_add.len; i++) {
to_add.data[i] = (*data)[i];
}
mem_data[key] = to_add;
TRIGGER(ret_blob, true);
}
tamed void MemStorage::add(ID_Value key, const blob* data, cbb ret_blob) {
tvars{
MemStorage::mem_it it;
bool set_val;
}
it = mem_data.find(key);
if(it == mem_data.end()) {
twait { set(key, data, mkevent(set_val)); }
TRIGGER(ret_blob, true);
} else {
TRIGGER(ret_blob, false);
}
}
tamed void MemStorage::replace(ID_Value key, const blob* data, cbb ret_blob) {
tvars{
MemStorage::mem_it it;
bool set_val;
}
it = mem_data.find(key);
if(it == mem_data.end()) {
TRIGGER(ret_blob, false);
} else {
twait { set(key, data, mkevent(set_val)); }
TRIGGER(ret_blob, true);
}
}
tamed void MemStorage::del(ID_Value key, cbb ret_bool) {
tvars{
MemStorage::mem_it it;
}
it = mem_data.find(key);
if(it != mem_data.end()) {
delete it->second.data;
mem_data.erase(key);
}
TRIGGER(ret_bool, true);
}