-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathtable.cc
451 lines (393 loc) Β· 12.6 KB
/
table.cc
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "swoole_table.h"
namespace swoole {
Table *Table::make(uint32_t rows_size, float conflict_proportion) {
if (rows_size >= 0x80000000) {
rows_size = 0x80000000;
} else {
uint32_t i = 6;
while ((1U << i) < rows_size) {
i++;
}
rows_size = 1 << i;
}
if (conflict_proportion > 1.0) {
conflict_proportion = 1.0;
} else if (conflict_proportion < SW_TABLE_CONFLICT_PROPORTION) {
conflict_proportion = SW_TABLE_CONFLICT_PROPORTION;
}
auto table = (Table *) sw_mem_pool()->alloc(sizeof(Table));
if (table == nullptr) {
return nullptr;
}
table->mutex = new Mutex(Mutex::PROCESS_SHARED);
table->iterator = nullptr;
table->column_map = new std::unordered_map<std::string, TableColumn *>;
table->column_list = new std::vector<TableColumn *>;
table->size = rows_size;
table->mask = rows_size - 1;
table->conflict_proportion = conflict_proportion;
#ifdef SW_TABLE_USE_PHP_HASH
table->hash_func = swoole_hash_php;
#else
table->hash_func = swoole_hash_austin;
#endif
return table;
}
bool Table::add_column(const std::string &_name, enum TableColumn::Type _type, size_t _size) {
if (_type < TableColumn::TYPE_INT || _type > TableColumn::TYPE_STRING) {
swoole_warning("unknown column type");
return false;
}
auto col = new TableColumn(_name, _type, _size);
col->index = item_size;
item_size += col->size;
column_map->emplace(_name, col);
column_list->push_back(col);
return true;
}
size_t Table::calc_memory_size() const {
/**
* table size + conflict size
*/
size_t _row_num = size * (1 + conflict_proportion);
/*
* header + data
*/
size_t _row_memory_size = sizeof(TableRow) + item_size;
/**
* row data & header
*/
size_t _memory_size = _row_num * _row_memory_size;
/**
* memory pool for conflict rows
*/
_memory_size += FixedPool::sizeof_struct_impl() + ((_row_num - size) * FixedPool::sizeof_struct_slice());
/**
* for iterator, Iterate through all the elements
*/
_memory_size += size * sizeof(TableRow *);
swoole_trace("_memory_size=%lu, _row_num=%lu, _row_memory_size=%lu", _memory_size, _row_num, _row_memory_size);
return _memory_size;
}
size_t Table::get_memory_size() const {
return memory_size;
}
uint32_t Table::get_available_slice_num() {
lock();
uint32_t num = pool->get_number_of_spare_slice();
unlock();
return num;
}
uint32_t Table::get_total_slice_num() {
return pool->get_number_of_total_slice();
}
bool Table::create() {
if (created) {
return false;
}
size_t _memory_size = calc_memory_size();
size_t _row_memory_size = sizeof(TableRow) + item_size;
void *_memory = sw_shm_malloc(_memory_size);
if (_memory == nullptr) {
return false;
}
memory = _memory;
rows = (TableRow **) _memory;
_memory = (char *) _memory + size * sizeof(TableRow *);
_memory_size -= size * sizeof(TableRow *);
for (size_t i = 0; i < size; i++) {
rows[i] = (TableRow *) ((char *) _memory + (_row_memory_size * i));
memset(rows[i], 0, sizeof(TableRow));
}
_memory = (char *) _memory + _row_memory_size * size;
_memory_size -= _row_memory_size * size;
pool = new FixedPool(_row_memory_size, _memory, _memory_size, true);
iterator = new TableIterator(_row_memory_size);
memory_size = _memory_size;
created = true;
return true;
}
void Table::destroy() {
#ifdef SW_TABLE_DEBUG
printf("swoole_table: size=%ld, conflict_count=%d, conflict_max_level=%d, insert_count=%d\n",
size,
conflict_count,
conflict_max_level,
insert_count);
#endif
auto i = column_map->begin();
while (i != column_map->end()) {
delete i->second;
column_map->erase(i++);
}
delete column_map;
delete column_list;
delete iterator;
delete pool;
if (memory) {
sw_shm_free(memory);
}
memory = nullptr;
delete mutex;
sw_mem_pool()->free(this);
}
void TableRow::lock() {
sw_atomic_t *lock = &lock_;
uint32_t i, n;
long t = 0;
while (1) {
if (*lock == 0 && sw_atomic_cmp_set(lock, 0, 1)) {
_success:
lock_pid = SwooleG.pid;
return;
}
if (SW_CPU_NUM > 1) {
for (n = 1; n < SW_SPINLOCK_LOOP_N; n <<= 1) {
for (i = 0; i < n; i++) {
sw_atomic_cpu_pause();
}
if (*lock == 0 && sw_atomic_cmp_set(lock, 0, 1)) {
goto _success;
}
}
}
/**
* The process occupied by the resource no longer exists,
* indicating that OOM occurred during the locking process,
* forced to unlock
*/
if (kill(lock_pid, 0) < 0 && errno == ESRCH) {
*lock = 1;
swoole_warning("lock process[%d] not exists, force unlock", lock_pid);
goto _success;
}
/**
* Mark time
*/
if (t == 0) {
t = swoole::time<std::chrono::milliseconds>(true);
}
/**
* The deadlock time exceeds 2 seconds (SW_TABLE_FORCE_UNLOCK_TIME),
* indicating that the lock process has OOM,
* and the PID has been reused, forcing the unlock
*/
else if ((swoole::time<std::chrono::milliseconds>(true) - t) > SW_TABLE_FORCE_UNLOCK_TIME) {
*lock = 1;
swoole_warning("timeout, force unlock");
goto _success;
}
sw_yield();
}
}
void Table::forward() {
iterator->lock();
for (; iterator->absolute_index < size; iterator->absolute_index++) {
TableRow *row = get_by_index(iterator->absolute_index);
if (row == nullptr) {
continue;
}
row->lock();
if (row->next == nullptr) {
iterator->absolute_index++;
memcpy(iterator->current_, row, iterator->row_memory_size_);
row->unlock();
iterator->unlock();
return;
} else {
uint32_t i = 0;
TableRow *tmp_row = row;
for (;; i++) {
if (tmp_row == nullptr) {
iterator->collision_index = 0;
break;
}
if (i == iterator->collision_index) {
iterator->collision_index++;
memcpy(iterator->current_, tmp_row, iterator->row_memory_size_);
row->unlock();
iterator->unlock();
return;
}
tmp_row = tmp_row->next;
}
}
row->unlock();
}
sw_memset_zero(iterator->current_, sizeof(TableRow));
iterator->unlock();
}
TableRow *Table::get(const char *key, uint16_t keylen, TableRow **rowlock) {
check_key_length(&keylen);
TableRow *row = hash(key, keylen);
*rowlock = row;
row->lock();
for (;;) {
if (sw_mem_equal(row->key, row->key_len, key, keylen)) {
if (!row->active) {
row = nullptr;
}
break;
} else if (row->next == nullptr) {
row = nullptr;
break;
} else {
row = row->next;
}
}
return row;
}
TableRow *Table::set(const char *key, uint16_t keylen, TableRow **rowlock, int *out_flags) {
check_key_length(&keylen);
TableRow *row = hash(key, keylen);
*rowlock = row;
row->lock();
int _out_flags = 0;
uint32_t _conflict_level = 1;
if (row->active) {
for (;;) {
if (sw_mem_equal(row->key, row->key_len, key, keylen)) {
break;
} else if (row->next == nullptr) {
conflict_count++;
if (_conflict_level > conflict_max_level) {
conflict_max_level = _conflict_level;
}
TableRow *new_row = alloc_row();
if (!new_row) {
return nullptr;
}
init_row(new_row, key, keylen);
_out_flags |= SW_TABLE_FLAG_NEW_ROW;
row->next = new_row;
row = new_row;
break;
} else {
row = row->next;
_out_flags |= SW_TABLE_FLAG_CONFLICT;
_conflict_level++;
}
}
} else {
init_row(row, key, keylen);
_out_flags |= SW_TABLE_FLAG_NEW_ROW;
}
if (out_flags) {
*out_flags = _out_flags;
}
if (_out_flags & SW_TABLE_FLAG_NEW_ROW) {
sw_atomic_fetch_add(&(insert_count), 1);
} else {
sw_atomic_fetch_add(&(update_count), 1);
}
return row;
}
bool Table::del(const char *key, uint16_t keylen) {
check_key_length(&keylen);
TableRow *row = hash(key, keylen);
// no exists
if (!row->active) {
return false;
}
TableRow *tmp, *prev = nullptr;
row->lock();
if (row->next == nullptr) {
if (sw_mem_equal(row->key, row->key_len, key, keylen)) {
row->clear();
goto _delete_element;
} else {
goto _not_exists;
}
} else {
tmp = row;
while (tmp) {
if (sw_mem_equal(tmp->key, tmp->key_len, key, keylen)) {
break;
}
prev = tmp;
tmp = tmp->next;
}
if (tmp == nullptr) {
_not_exists:
row->unlock();
return false;
}
// when the deleting element is root, should move the first element's data to root,
// and remove the element from the collision list.
if (tmp == row) {
tmp = tmp->next;
row->next = tmp->next;
memcpy(row->key, tmp->key, tmp->key_len + 1);
row->key_len = tmp->key_len;
memcpy(row->data, tmp->data, item_size);
} else {
prev->next = tmp->next;
}
free_row(tmp);
}
_delete_element:
sw_atomic_fetch_add(&(delete_count), 1);
sw_atomic_fetch_sub(&(row_num), 1);
row->unlock();
return true;
}
void TableColumn::clear(TableRow *row) {
if (type == TYPE_STRING) {
row->set_value(this, nullptr, 0);
} else if (type == TYPE_FLOAT) {
double _value = 0;
row->set_value(this, &_value, 0);
} else {
long _value = 0;
row->set_value(this, &_value, 0);
}
}
void TableRow::set_value(TableColumn *col, void *value, size_t vlen) {
switch (col->type) {
case TableColumn::TYPE_INT:
memcpy(data + col->index, value, sizeof(long));
break;
case TableColumn::TYPE_FLOAT:
memcpy(data + col->index, value, sizeof(double));
break;
default:
if (vlen > (col->size - sizeof(TableStringLength))) {
swoole_warning("[key=%s,field=%s]string value is too long", key, col->name.c_str());
vlen = col->size - sizeof(TableStringLength);
}
if (value == nullptr) {
vlen = 0;
}
memcpy(data + col->index, &vlen, sizeof(TableStringLength));
if (vlen > 0) {
memcpy(data + col->index + sizeof(TableStringLength), value, vlen);
}
break;
}
}
void TableRow::get_value(TableColumn *col, double *dval) {
memcpy(dval, data + col->index, sizeof(*dval));
}
void TableRow::get_value(TableColumn *col, long *lval) {
memcpy(lval, data + col->index, sizeof(*lval));
}
void TableRow::get_value(TableColumn *col, char **value, TableStringLength *len) {
memcpy(len, data + col->index, sizeof(*len));
*value = data + col->index + sizeof(*len);
}
} // namespace swoole