forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_table.c
412 lines (353 loc) · 12.2 KB
/
swoole_table.c
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
/*
+----------------------------------------------------------------------+
| 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 "php_swoole.h"
#include "include/table.h"
zend_class_entry swoole_table_ce;
zend_class_entry *swoole_table_class_entry_ptr;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_construct, 0, 0, 1)
ZEND_ARG_INFO(0, table_size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_column, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_set, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_get, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_count, 0, 0, 0)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_table_del, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
const zend_function_entry swoole_table_methods[] =
{
PHP_ME(swoole_table, __construct, arginfo_swoole_table_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_table, column, arginfo_swoole_table_column, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, create, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, set, arginfo_swoole_table_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, get, arginfo_swoole_table_get, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, rewind, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, next, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, current, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, key, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, valid, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, count, arginfo_swoole_table_count, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, del, arginfo_swoole_table_del, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, lock, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, unlock, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static sw_inline swTable* php_swoole_table_get(zval *object TSRMLS_DC)
{
zval **zres;
swTable *table = NULL;
if (zend_hash_find(Z_OBJPROP_P(object), SW_STRL("_table"), (void **) &zres) == SUCCESS)
{
ZEND_FETCH_RESOURCE_NO_RETURN(table, swTable*, zres, -1, SW_RES_TABLE_NAME, le_swoole_table);
}
assert(table != NULL);
return table;
}
static void php_swoole_table_row2array(swTable *table, swTableRow *row, zval *return_value)
{
array_init(return_value);
swTableColumn *col = NULL;
swTable_string_length_t vlen = 0;
double dval = 0;
int64_t lval = 0;
char *k;
sw_spinlock(&row->lock);
while(1)
{
col = swHashMap_each(table->columns, &k);
if (col == NULL)
{
break;
}
if (col->type == SW_TABLE_STRING)
{
memcpy(&vlen, row->data + col->index, sizeof(swTable_string_length_t));
add_assoc_stringl_ex(return_value, col->name->str, col->name->length + 1, row->data + col->index + sizeof(swTable_string_length_t), vlen, 1);
}
else if (col->type == SW_TABLE_FLOAT)
{
memcpy(&dval, row->data + col->index, sizeof(dval));
add_assoc_double_ex(return_value, col->name->str, col->name->length + 1, dval);
}
else
{
switch (col->type)
{
case SW_TABLE_INT8:
memcpy(&lval, row->data + col->index, 1);
break;
case SW_TABLE_INT16:
memcpy(&lval, row->data + col->index, 2);
break;
case SW_TABLE_INT32:
memcpy(&lval, row->data + col->index, 4);
break;
default:
memcpy(&lval, row->data + col->index, 8);
break;
}
add_assoc_long_ex(return_value, col->name->str, col->name->length + 1, lval);
}
}
sw_spinlock_release(&row->lock);
}
void swoole_destory_table(zend_resource *rsrc TSRMLS_DC)
{
swTable *table = (swTable *) rsrc->ptr;
if (table)
{
swTable_free(table);
}
}
void swoole_table_init(int module_number TSRMLS_DC)
{
INIT_CLASS_ENTRY(swoole_table_ce, "swoole_table", swoole_table_methods);
swoole_table_class_entry_ptr = zend_register_internal_class(&swoole_table_ce TSRMLS_CC);
zend_class_implements(swoole_table_class_entry_ptr TSRMLS_CC, 2, spl_ce_Iterator, spl_ce_Countable);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("TYPE_INT")-1, SW_TABLE_INT TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("TYPE_STRING")-1, SW_TABLE_STRING TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("TYPE_FLOAT")-1, SW_TABLE_FLOAT TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_GT")-1, SW_TABLE_FIND_GT TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_LT")-1, SW_TABLE_FIND_LT TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_EQ")-1, SW_TABLE_FIND_EQ TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_NEQ")-1, SW_TABLE_FIND_NEQ TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_LEFTLIKE")-1, SW_TABLE_FIND_LEFTLIKE TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_RIGHTLIKE")-1, SW_TABLE_FIND_RIGHTLIKE TSRMLS_CC);
zend_declare_class_constant_long(swoole_table_class_entry_ptr, SW_STRL("FIND_LIKE")-1, SW_TABLE_FIND_LIKE TSRMLS_CC);
}
void swoole_table_column_free(swTableColumn *col)
{
swString_free(col->name);
}
PHP_METHOD(swoole_table, __construct)
{
long table_size;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &table_size) == FAILURE)
{
RETURN_FALSE;
}
if (table_size < 1)
{
RETURN_FALSE;
}
#ifdef ZTS
if (sw_thread_ctx == NULL)
{
TSRMLS_SET_CTX(sw_thread_ctx);
}
#endif
swTable *table = swTable_new(table_size);
zval *zres;
MAKE_STD_ZVAL(zres);
ZEND_REGISTER_RESOURCE(zres, table, le_swoole_table);
zend_update_property(swoole_table_class_entry_ptr, getThis(), ZEND_STRL("_table"), zres TSRMLS_CC);
zval_ptr_dtor(&zres);
}
PHP_METHOD(swoole_table, column)
{
char *name;
int len;
long type;
long size;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &name, &len, &type, &size) == FAILURE)
{
RETURN_FALSE;
}
if (type == SW_TABLE_STRING && size < 1)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "string length must be more than 0.");
RETURN_FALSE;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableColumn_add(table, name, len, type, size);
RETURN_TRUE;
}
PHP_METHOD(swoole_table, create)
{
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTable_create(table);
RETURN_TRUE;
}
PHP_METHOD(swoole_table, set)
{
zval *array;
char *key;
int keylen;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &key, &keylen, &array) == FAILURE)
{
RETURN_FALSE;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow *row = swTableRow_set(table, key, keylen);
if (!row)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate memory.");
RETURN_FALSE;
}
swTableColumn *col;
zval *v;
char *k;
int klen;
Bucket *p = Z_ARRVAL_P(array)->pListHead;
sw_atomic_t *lock = &row->lock;
sw_spinlock(lock);
do
{
v = p->pDataPtr;
k = (char *) p->arKey;
klen = p->nKeyLength - 1;
p = p->pListNext;
col = swTableColumn_get(table, k, klen);
if (col == NULL)
{
continue;
}
else if (col->type == SW_TABLE_STRING)
{
convert_to_string(v);
swTableRow_set_value(row, col, Z_STRVAL_P(v), Z_STRLEN_P(v));
}
else if (col->type == SW_TABLE_FLOAT)
{
convert_to_double(v);
swTableRow_set_value(row, col, &Z_DVAL_P(v), 0);
}
else
{
convert_to_long(v);
swTableRow_set_value(row, col, &Z_LVAL_P(v), 0);
}
} while (p);
sw_spinlock_release(lock);
RETURN_TRUE;
}
PHP_METHOD(swoole_table, get)
{
char *key;
int keylen;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &keylen) == FAILURE)
{
RETURN_FALSE;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow *row = swTableRow_get(table, key, keylen);
if (!row)
{
RETURN_FALSE;
}
php_swoole_table_row2array(table, row, return_value);
}
PHP_METHOD(swoole_table, rewind)
{
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTable_iterator_rewind(table);
}
PHP_METHOD(swoole_table, current)
{
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow *row = swTable_iterator_current(table);
php_swoole_table_row2array(table, row, return_value);
}
PHP_METHOD(swoole_table, key)
{
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow *row = swTable_iterator_current(table);
RETURN_LONG(row->crc32);
}
PHP_METHOD(swoole_table, next)
{
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTable_iterator_forward(table);
}
PHP_METHOD(swoole_table, valid)
{
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow *row = swTable_iterator_current(table);
RETURN_BOOL(row != NULL);
}
PHP_METHOD(swoole_table, count)
{
#define COUNT_NORMAL 0
#define COUNT_RECURSIVE 1
long mode = COUNT_NORMAL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE)
{
return;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
if (mode == COUNT_NORMAL)
{
RETURN_LONG(table->row_num);
}
else
{
RETURN_LONG(table->row_num * table->column_num);
}
}
PHP_METHOD(swoole_table, del)
{
char *key;
int keylen;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &keylen) == FAILURE)
{
RETURN_FALSE;
}
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
SW_CHECK_RETURN(swTableRow_del(table, key, keylen));
}
PHP_METHOD(swoole_table, lock)
{
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
SW_LOCK_CHECK_RETURN(table->lock.lock(&table->lock));
}
PHP_METHOD(swoole_table, unlock)
{
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
SW_LOCK_CHECK_RETURN(table->lock.unlock(&table->lock));
}