-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathArcusDao.hpp
255 lines (216 loc) · 7.05 KB
/
ArcusDao.hpp
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
/*
* Copyright 2012-2014 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _ARCUSDAO_H_
#define _ARCUSDAO_H_
#include "arcus.hpp"
#include <iostream>
#include <time.h>
namespace dao
{
/**
* Cacheable interface
*/
class cacheable
{
public:
cacheable(arcus::base_client *client)
: _client(client)
{
}
~cacheable()
{
}
bool del()
{
memcached_return_t rc;
return _client->del(_key, &rc);
}
virtual bool get() = 0;
virtual bool insert() = 0;
protected:
arcus::base_client *_client;
std::string _key;
};
/**
* DOMAIN : USERINFO
*/
class userinfo : public cacheable
{
public:
/* TODO */
const static int32_t USERINFO_EXPIRETIME = 60;
const static int32_t USERINFO_FLAGS = 0;
const static int32_t USERINFO_NUM_OF_FIELDS = 7;
public:
std::string mMID;
std::string mProxyIP;
unsigned short mProxyPort;
std::string mKey;
std::string mToken;
bool mTerm;
bool mCancel;
public:
userinfo(arcus::base_client *client, std::string prefix, std::string key)
: cacheable(client)
{
_key = prefix + ":userinfo_" + key;
}
bool insert()
{
const uint64_t bkeys[] = { 0, 1, 2, 3, 4, 5, 6 };
const char *values[] = {
mMID.c_str(),
mProxyIP.c_str(),
(const char*)&mProxyPort,
mKey.c_str(),
mToken.c_str(),
(const char*)&mTerm,
(const char*)&mCancel
};
size_t value_lengths[] = {
mMID.length(),
mProxyIP.length(),
sizeof(mProxyPort),
mKey.length(),
mToken.length(),
sizeof(mTerm),
sizeof(mCancel)
};
memcached_coll_create_attrs_st create_attrs;
memcached_coll_create_attrs_init(&create_attrs, USERINFO_FLAGS, USERINFO_EXPIRETIME, 100);
memcached_return_t rc;
bool result = _client->insert(_key, USERINFO_NUM_OF_FIELDS, bkeys, values, value_lengths, &create_attrs, &rc);
return result;
}
bool get()
{
memcached_return_t rc;
bool success = false;
arcus::cached_data *data = _client->get(_key, 0, USERINFO_NUM_OF_FIELDS-1, &rc);
if (data) {
if (data->size() == USERINFO_NUM_OF_FIELDS) {
mMID = std::string(data->value(0));
mProxyIP = std::string(data->value(1));
mProxyPort = *(unsigned short*)(data->value(2));
mKey = std::string(data->value(3));
mToken = std::string(data->value(4));
mTerm = *(bool*)(data->value(5));
mCancel = *(bool*)(data->value(6));
success = true;
} else {
_client->del(_key, &rc);
success = false;
}
delete data;
}
return success;
}
bool update()
{
memcached_return_t rc;
return _client->update(_key, 1, mProxyIP.c_str(), mProxyIP.length(), &rc);
}
};
/**
* DOMAIN : MESGINFO
*/
class mesginfo : public cacheable
{
public:
const static int32_t MESGINFO_EXPIRETIME = 600;
const static int32_t MESGINFO_FLAGS = 0;
const static int32_t MESGINFO_NUM_OF_FIELDS = 9;
public:
std::string sid;
std::string state;
std::string type;
std::string from;
std::string to;
struct timeval starttime;
struct timeval endtime;
std::string srcip;
std::string dstip;
public:
mesginfo(arcus::base_client *client, std::string prefix, std::string key)
: cacheable(client)
{
_key = prefix + ":mesginfo_" + key;
}
bool insert()
{
const uint64_t bkeys[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
const char *values[] = {
sid.c_str(),
state.c_str(),
type.c_str(),
from.c_str(),
to.c_str(),
(const char *)&starttime,
(const char *)&endtime,
srcip.c_str(),
dstip.c_str()
};
size_t value_lengths[] = {
sid.length(),
state.length(),
type.length(),
from.length(),
to.length(),
sizeof(struct timeval),
sizeof(struct timeval),
sizeof(struct timeval),
srcip.length(),
dstip.length()
};
memcached_coll_create_attrs_st create_attrs;
memcached_coll_create_attrs_init(&create_attrs, MESGINFO_FLAGS, MESGINFO_EXPIRETIME, 100);
memcached_return_t rc;
bool result = _client->insert(_key, MESGINFO_NUM_OF_FIELDS, bkeys, values, value_lengths, &create_attrs, &rc);
return result;
}
bool update()
{
//TODO
return false;
}
bool get()
{
memcached_return_t rc;
bool success = false;
arcus::cached_data *data = _client->get(_key, 0, 10, &rc);
if (data) {
if (data->size() == MESGINFO_NUM_OF_FIELDS) {
sid = std::string(data->value(0));
state = std::string(data->value(1));
type = std::string(data->value(2));
from = std::string(data->value(3));
to = std::string(data->value(4));
starttime = *(struct timeval *)data->value(5);
endtime = *(struct timeval *)data->value(6);
srcip = std::string(data->value(7));
dstip = std::string(data->value(8));
success = true;
} else {
_client->del(_key, &rc);
success = false;
}
delete data;
}
return success;
}
};
} // namespace dao
#endif