-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathtable.cpp
332 lines (262 loc) Β· 9.38 KB
/
table.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| @link https://www.swoole.com/ |
| @contact [email protected] |
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
| @Author Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "test_core.h"
#include "swoole_table.h"
using namespace swoole;
#include <exception>
#include <map>
struct exception_t : public std::exception {
int code;
std::string msg;
exception_t(std::string _msg, int _code) : std::exception() {
msg = _msg;
code = _code;
}
const char *what() const throw() {
return msg.c_str();
}
};
struct row_t {
std::string name;
long id;
double score;
};
class table_t {
private:
TableColumn *column_id;
TableColumn *column_name;
TableColumn *column_score;
Table *table;
public:
table_t(uint32_t rows_size, float conflict_proportion = 0.2) {
table = Table::make(rows_size, conflict_proportion);
if (!table) {
throw exception_t("alloc failed", swoole_get_last_error());
}
table->add_column("id", TableColumn::TYPE_INT, 0);
table->add_column("name", TableColumn::TYPE_STRING, 32);
table->add_column("score", TableColumn::TYPE_FLOAT, 0);
if (!table->create()) {
throw exception_t("create failed", swoole_get_last_error());
}
column_id = table->get_column("id");
column_name = table->get_column("name");
column_score = table->get_column("score");
}
bool set(const std::string &key, const row_t &value) {
TableRow *_rowlock = nullptr;
TableRow *row = table->set(key.c_str(), key.length(), &_rowlock, nullptr);
if (!row) {
_rowlock->unlock();
return false;
}
row->set_value(column_id, (void *) &value.id, sizeof(value.id));
row->set_value(column_name, (void *) value.name.c_str(), value.name.length());
row->set_value(column_score, (void *) &value.score, sizeof(value.score));
_rowlock->unlock();
return true;
}
row_t get(const std::string &key) {
row_t result;
TableRow *_rowlock = nullptr;
TableRow *row = table->get(key.c_str(), key.length(), &_rowlock);
if (row) {
memcpy(&result.id, row->data + column_id->index, sizeof(result.id));
memcpy(&result.score, row->data + column_score->index, sizeof(result.score));
TableStringLength l;
memcpy(&l, row->data + column_name->index, sizeof(l));
result.name = std::string(row->data + column_name->index + sizeof(l), l);
}
_rowlock->unlock();
return result;
}
bool del(const std::string &key) {
return table->del(key.c_str(), key.length());
}
bool exists(const std::string &key) {
TableRow *_rowlock = nullptr;
TableRow *row = table->get(key.c_str(), key.length(), &_rowlock);
_rowlock->unlock();
return row != nullptr;
}
size_t count() {
return table->count();
}
Table *ptr() {
return table;
}
~table_t() {
if (table) {
table->destroy();
}
}
};
TEST(table, create) {
table_t table(1024);
table.set("php", {"php", 1, 1.245});
table.set("java", {"java", 2, 3.1415926});
table.set("c++", {"c++", 3, 4.888});
ASSERT_EQ(table.count(), 3);
row_t r1 = table.get("java");
ASSERT_EQ(r1.id, 2);
ASSERT_EQ(r1.score, 3.1415926);
ASSERT_EQ(r1.name, std::string("java"));
ASSERT_TRUE(table.exists("php"));
ASSERT_TRUE(table.del("php"));
ASSERT_FALSE(table.exists("php"));
}
void start_iterator(Table *_ptr) {
_ptr->rewind();
auto count = 0;
while (true) {
_ptr->forward();
auto row = _ptr->current();
if (row->key_len == 0) {
break;
}
ASSERT_TRUE(_ptr->exists(row->key, row->key_len));
count++;
}
ASSERT_EQ(count, _ptr->count());
}
TEST(table, iterator) {
table_t table(1024);
table.set("php", {"php", 1, 1.245});
table.set("java", {"java", 2, 3.1415926});
table.set("c++", {"c++", 3, 4.888});
auto _ptr = table.ptr();
start_iterator(_ptr);
}
TEST(table, iterator_2) {
table_t table(1024);
auto _ptr = table.ptr();
_ptr->set_hash_func([](const char *key, size_t len) -> uint64_t { return 1; });
table.set("php", {"php", 1, 1.245});
table.set("java", {"java", 2, 3.1415926});
table.set("c++", {"c++", 3, 4.888});
start_iterator(_ptr);
}
static int test_table_size = 128;
static void create_table(table_t &table) {
auto ptr = table.ptr();
ptr->set_hash_func([](const char *key, size_t len) -> uint64_t { return 1; });
ASSERT_TRUE(table.set("php", {"php", 1, 1.245}));
ASSERT_TRUE(table.set("java", {"java", 2, 3.1415926}));
ASSERT_TRUE(table.set("c++", {"c++", 3, 4.888}));
ASSERT_TRUE(table.set("js", {"js", 9, 6565}));
ASSERT_TRUE(table.set("golang", {"golang", 4, 9.888}));
}
TEST(table, conflict1) {
table_t table(test_table_size);
ASSERT_FALSE(table.exists("swift"));
create_table(table);
auto ptr = table.ptr();
ASSERT_FALSE(table.exists("kotlin"));
ASSERT_TRUE(table.del("php"));
ASSERT_FALSE(table.exists("php"));
ASSERT_TRUE(table.set("rust", {"rust", 5, 9.888}));
ASSERT_TRUE(table.del("golang"));
ASSERT_FALSE(table.exists("golang"));
ASSERT_TRUE(table.set("erlang", {"erlang", 6, 12.888}));
ASSERT_TRUE(table.del("java"));
ASSERT_FALSE(table.exists("java"));
ASSERT_EQ(ptr->get_total_slice_num() - ptr->get_available_slice_num(), table.count() - 1);
}
TEST(table, conflict2) {
table_t table(test_table_size);
create_table(table);
auto ptr = table.ptr();
ASSERT_TRUE(table.del("java"));
ASSERT_FALSE(table.exists("java"));
ASSERT_TRUE(table.set("rust", {"rust", 5, 9.888}));
ASSERT_TRUE(table.del("golang"));
ASSERT_FALSE(table.exists("golang"));
ASSERT_TRUE(table.set("erlang", {"erlang", 6, 12.888}));
ASSERT_EQ(ptr->get_total_slice_num() - ptr->get_available_slice_num(), table.count() - 1);
}
TEST(table, conflict3) {
table_t table(test_table_size);
create_table(table);
auto ptr = table.ptr();
ASSERT_TRUE(table.del("golang"));
ASSERT_TRUE(table.set("erlang", {"erlang", 6, 12.888}));
ASSERT_TRUE(table.del("java"));
ASSERT_EQ(ptr->get_total_slice_num() - ptr->get_available_slice_num(), table.count() - 1);
}
TEST(table, conflict4) {
table_t table(test_table_size);
create_table(table);
auto ptr = table.ptr();
ASSERT_TRUE(table.del("c++"));
ASSERT_TRUE(table.set("rust", {"rust", 5, 9.888}));
ASSERT_TRUE(table.del("golang"));
ASSERT_TRUE(table.set("erlang", {"erlang", 6, 12.888}));
ASSERT_TRUE(table.del("java"));
ASSERT_EQ(ptr->get_total_slice_num() - ptr->get_available_slice_num(), table.count() - 1);
}
TEST(table, get_value) {
table_t table(test_table_size);
create_table(table);
auto ptr = table.ptr();
std::string key("php");
TableRow *_rowlock = nullptr;
TableRow *row = ptr->get(key.c_str(), key.length(), &_rowlock);
_rowlock->unlock();
TableColumn *column_id = ptr->get_column("id");
TableColumn *column_name = ptr->get_column("name");
TableColumn *column_score = ptr->get_column("score");
char *str = nullptr;
TableStringLength len = 0;
row->get_value(column_name, &str, &len);
ASSERT_STREQ(str, "php");
double dval = 0;
row->get_value(column_score, &dval);
ASSERT_EQ(dval, 1.245);
long lval = 0;
row->get_value(column_id, &lval);
ASSERT_EQ(lval, 1);
column_id->clear(row);
column_name->clear(row);
column_score->clear(row);
row->get_value(column_name, &str, &len);
ASSERT_STREQ(str, "php");
row->get_value(column_score, &dval);
ASSERT_EQ(dval, 0);
row->get_value(column_id, &lval);
ASSERT_EQ(lval, 0);
}
TEST(table, lock) {
table_t table(test_table_size);
create_table(table);
auto ptr = table.ptr();
std::string key("php");
TableRow *_rowlock = nullptr;
for (int i = 0; i <= 3; i++) {
std::thread t([&]() {
TableRow *row = ptr->get(key.c_str(), key.length(), &_rowlock);
TableColumn *column_name = ptr->get_column("name");
char *str = nullptr;
TableStringLength len = 0;
row->get_value(column_name, &str, &len);
ASSERT_STREQ(str, "php");
});
t.join();
}
_rowlock->unlock();
}