Skip to content

Commit 016423c

Browse files
committed
swoole_table-0.1-beta release
1 parent 1b82a45 commit 016423c

File tree

10 files changed

+141
-70
lines changed

10 files changed

+141
-70
lines changed

examples/table.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
<?php
2-
echo "test\n";
32
$table = new swoole_table(1024);
43
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
54
$table->column('name', swoole_table::TYPE_STRING, 64);
65
$table->column('num', swoole_table::TYPE_FLOAT);
76
$table->create();
87

9-
$table->add('[email protected]', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
8+
$worker = new swoole_process('child1', false, false);
9+
$worker->start();
1010

11-
$table->lock();
12-
$table->add('[email protected]', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
13-
$table->add('[email protected]', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
14-
$table->unlock();
11+
//child
12+
function child1($worker)
13+
{
14+
global $table;
15+
$table->set('[email protected]', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
16+
$table->set('[email protected]', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
17+
$table->set('[email protected]', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
18+
sleep(100000);
19+
}
1520

16-
var_dump($table->get('[email protected]'));
21+
//master
22+
sleep(1);
23+
$s = microtime(true);
24+
for($i =0; $i < 1000000; $i++)
25+
{
26+
$arr = $table->get('[email protected]');
27+
}
28+
echo "use: ".((microtime(true) - $s) * 1000)."ms\n";
29+
//var_dump($table->get('[email protected]'));
30+
sleep(100000);

include/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,6 @@ static inline uint64_t swoole_hash_php(char *key, uint32_t len)
195195

196196
#define CRC_STRING_MAXLEN 256
197197

198-
uint64_t swoole_crc32(char *data, uint32_t size);
198+
uint32_t swoole_crc32(char *data, uint32_t size);
199199

200200
#endif /* SW_HASH_H_ */

include/swoole.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ typedef struct _swMemoryPool
534534
* FixedPool, random alloc/free fixed size memory
535535
*/
536536
swMemoryPool* swFixedPool_new(uint32_t slice_num, uint32_t slice_size, uint8_t shared);
537-
538537
swMemoryPool* swFixedPool_new2(uint32_t slice_size, void *memory, size_t size);
539538

540539
/**

include/table.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,20 @@
2626
typedef struct _swTableRow
2727
{
2828
sw_atomic_t lock;
29+
30+
/**
31+
* string crc32
32+
*/
2933
uint32_t crc32;
34+
35+
/**
36+
* 1:used, 0:empty
37+
*/
3038
uint8_t active;
39+
40+
/**
41+
* next slot
42+
*/
3143
struct _swTableRow *next;
3244
char data[0];
3345
} swTableRow;
@@ -79,15 +91,16 @@ swTable* swTable_new(uint32_t rows_size);
7991
int swTable_create(swTable *table);
8092
void swTable_free(swTable *table);
8193
int swTableColumn_add(swTable *table, char *name, int len, int type, int size);
82-
swTableRow* swTableRow_add(swTable *table, char *row_key, int klen);
94+
swTableRow* swTableRow_set(swTable *table, char *key, int keylen);
8395
swTableRow* swTableRow_get(swTable *table, char *key, int keylen);
96+
int swTableRow_del(swTable *table, char *key, int keylen);
8497

8598
static sw_inline swTableColumn* swTableColumn_get(swTable *table, char *column_key, int keylen)
8699
{
87100
return swHashMap_find(table->columns, column_key, keylen);
88101
}
89102

90-
static sw_inline void swTableRow_set(swTableRow *row, swTableColumn * col, void *value, int vlen)
103+
static sw_inline void swTableRow_set_value(swTableRow *row, swTableColumn * col, void *value, int vlen)
91104
{
92105
switch(col->type)
93106
{

php_swoole.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ PHP_METHOD(swoole_buffer, clear);
256256
PHP_METHOD(swoole_table, __construct);
257257
PHP_METHOD(swoole_table, column);
258258
PHP_METHOD(swoole_table, create);
259-
PHP_METHOD(swoole_table, add);
259+
PHP_METHOD(swoole_table, set);
260260
PHP_METHOD(swoole_table, get);
261-
261+
PHP_METHOD(swoole_table, del);
262262
PHP_METHOD(swoole_table, lock);
263263
PHP_METHOD(swoole_table, unlock);
264264

src/core/hashmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static unsigned int crc32_tab[] = {
409409
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
410410
};
411411

412-
static inline unsigned int crc32(char *buf, unsigned int size)
412+
static inline uint32_t crc32(char *buf, unsigned int size)
413413
{
414414
const char *p;
415415
register int crc = 0;
@@ -423,7 +423,7 @@ static inline unsigned int crc32(char *buf, unsigned int size)
423423
return crc ^ ~0U;
424424
}
425425

426-
uint64_t swoole_crc32(char *data, uint32_t size)
426+
uint32_t swoole_crc32(char *data, uint32_t size)
427427
{
428428
if (size < CRC_STRING_MAXLEN)
429429
{

src/memory/FixedPool.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ swMemoryPool* swFixedPool_new2(uint32_t slice_size, void *memory, size_t size)
9696

9797
swMemoryPool *pool = memory;
9898
memory += sizeof(swMemoryPool);
99+
bzero(pool, sizeof(swMemoryPool));
99100

100101
pool->object = object;
101102
pool->alloc = swFixedPool_alloc;
102-
pool->free = NULL;
103-
pool->destroy = NULL;
103+
pool->free = swFixedPool_free;
104+
pool->destroy = swFixedPool_destroy;
104105

105106
object->memory = memory;
106107

src/memory/Table.c

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,100 +87,127 @@ int swTableColumn_add(swTable *table, char *name, int len, int type, int size)
8787
int swTable_create(swTable *table)
8888
{
8989
uint32_t row_num = table->size * (1 + SW_TABLE_CONFLICT_PROPORTION);
90-
size_t memory_size = row_num * (sizeof(swTableRow) + table->item_size);
90+
uint32_t row_memory_size = sizeof(swTableRow) + table->item_size;
9191

92+
size_t memory_size = row_num * row_memory_size;
9293
void *memory = sw_shm_malloc(memory_size);
9394
if (memory == NULL)
9495
{
9596
return SW_ERR;
9697
}
9798
table->memory = memory;
9899
table->rows = memory;
99-
memory += sizeof(swTableRow) * table->size;
100-
memory_size -= sizeof(swTableRow) * table->size;
101100

102101
int i;
103102
for (i = 0; i < table->size; i++)
104103
{
105-
table->rows[i] = memory + (sizeof(swTableRow) + table->item_size) * i;
104+
table->rows[i] = memory + (row_memory_size * i);
106105
}
107-
memory += (sizeof(swTableRow) + table->item_size) * table->size;
108-
memory_size -= (sizeof(swTableRow) + table->item_size) * table->size;
109-
table->pool = swFixedPool_new2(table->item_size, memory, memory_size);
106+
memory += row_memory_size * table->size;
107+
memory_size -= row_memory_size * table->size;
108+
table->pool = swFixedPool_new2(row_memory_size, memory, memory_size);
110109
return SW_OK;
111110
}
112111

113112
void swTable_free(swTable *table)
114113
{
115-
116-
//TODO free columns
117-
// if (table->item_size > 0)
118-
// {
119-
//
120-
// }
121114
sw_shm_free(table->memory);
122115
}
123116

124-
125117
static sw_inline swTableRow* swTable_hash(swTable *table, char *key, int keylen)
126118
{
127119
uint64_t hashv = swoole_hash_austin(key, keylen);
128120
uint32_t index = hashv & (table->size - 1);
129121
return table->rows[index];
130122
}
131123

132-
swTableRow* swTableRow_add(swTable *table, char *key, int keylen)
124+
swTableRow* swTableRow_set(swTable *table, char *key, int keylen)
125+
{
126+
swTableRow *row = swTable_hash(table, key, keylen);
127+
uint32_t crc32 = swoole_crc32(key, keylen);
128+
129+
sw_spinlock(&row->lock);
130+
if (row->active)
131+
{
132+
for (;;)
133+
{
134+
if (row->crc32 == crc32)
135+
{
136+
break;
137+
}
138+
else if (row->next == NULL)
139+
{
140+
swTableRow *new_row = table->pool->alloc(table->pool, 0);
141+
row->next = new_row;
142+
row = new_row;
143+
break;
144+
}
145+
else
146+
{
147+
row = row->next;
148+
}
149+
}
150+
}
151+
row->crc32 = crc32;
152+
row->active = 1;
153+
swTrace("row=%p, crc32=%u, key=%s\n", row, crc32, key);
154+
sw_spinlock_release(&row->lock);
155+
return row;
156+
}
157+
158+
swTableRow* swTableRow_get(swTable *table, char *key, int keylen)
133159
{
134160
swTableRow *row = swTable_hash(table, key, keylen);
161+
uint32_t crc32 = swoole_crc32(key, keylen);
135162

163+
swTrace("row=%p, crc32=%u, key=%s\n", row, crc32, key);
136164
sw_spinlock(&row->lock);
137-
while(1)
165+
for (;;)
138166
{
139-
//empty slot
140-
if (row->active == 0)
167+
if (row->crc32 == crc32)
141168
{
142169
break;
143170
}
144-
else if (row->next)
171+
else if (row->next == NULL)
145172
{
146-
row = row->next;
173+
row = NULL;
174+
break;
147175
}
148176
else
149177
{
150-
swTableRow *new_row = table->pool->alloc(table->pool, 0);
151-
row->next = new_row;
152-
row = new_row;
153-
break;
178+
row = row->next;
154179
}
155180
}
156-
row->active = 1;
157181
sw_spinlock_release(&row->lock);
158182
return row;
159183
}
160184

161-
swTableRow* swTableRow_get(swTable *table, char *key, int keylen)
185+
int swTableRow_del(swTable *table, char *key, int keylen)
162186
{
163187
swTableRow *row = swTable_hash(table, key, keylen);
188+
uint32_t crc32 = swoole_crc32(key, keylen);
164189

165-
// for(;;)
166-
// {
167-
// //empty slot
168-
// if (row->active == 0)
169-
// {
170-
// break;
171-
// }
172-
// else if (row->next)
173-
// {
174-
// row = row->next;
175-
// }
176-
// else
177-
// {
178-
// swTableRow *new_row = swTable_alloc(table);
179-
// row->next = new_row;
180-
// row = new_row;
181-
// break;
182-
// }
183-
// }
184-
return row;
190+
sw_spinlock(&row->lock);
191+
while (row->active)
192+
{
193+
for (;;)
194+
{
195+
if (row->crc32 == crc32)
196+
{
197+
table->pool->free(table->pool, row);
198+
break;
199+
}
200+
else if (row->next == NULL)
201+
{
202+
return SW_ERR;
203+
}
204+
else
205+
{
206+
row = row->next;
207+
}
208+
}
209+
}
210+
row->active = 0;
211+
sw_spinlock_release(&row->lock);
212+
return SW_OK;
185213
}
186-

swoole.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,9 @@ const zend_function_entry swoole_table_methods[] =
422422
PHP_ME(swoole_table, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
423423
PHP_ME(swoole_table, column, NULL, ZEND_ACC_PUBLIC)
424424
PHP_ME(swoole_table, create, NULL, ZEND_ACC_PUBLIC)
425-
PHP_ME(swoole_table, add, NULL, ZEND_ACC_PUBLIC)
425+
PHP_ME(swoole_table, set, NULL, ZEND_ACC_PUBLIC)
426426
PHP_ME(swoole_table, get, NULL, ZEND_ACC_PUBLIC)
427+
PHP_ME(swoole_table, del, NULL, ZEND_ACC_PUBLIC)
427428
PHP_ME(swoole_table, lock, NULL, ZEND_ACC_PUBLIC)
428429
PHP_ME(swoole_table, unlock, NULL, ZEND_ACC_PUBLIC)
429430
PHP_FE_END

swoole_table.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ PHP_METHOD(swoole_table, create)
108108
RETURN_TRUE;
109109
}
110110

111-
PHP_METHOD(swoole_table, add)
111+
PHP_METHOD(swoole_table, set)
112112
{
113113
zval *array;
114114
char *key;
@@ -120,7 +120,7 @@ PHP_METHOD(swoole_table, add)
120120
}
121121

122122
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
123-
swTableRow *row = swTableRow_add(table, key, keylen);
123+
swTableRow *row = swTableRow_set(table, key, keylen);
124124
swTableColumn *col;
125125
zval *v;
126126
char *k;
@@ -142,17 +142,17 @@ PHP_METHOD(swoole_table, add)
142142
else if (col->type == SW_TABLE_STRING)
143143
{
144144
convert_to_string(v);
145-
swTableRow_set(row, col, Z_STRVAL_P(v), Z_STRLEN_P(v));
145+
swTableRow_set_value(row, col, Z_STRVAL_P(v), Z_STRLEN_P(v));
146146
}
147147
else if (col->type == SW_TABLE_FLOAT)
148148
{
149149
convert_to_double(v);
150-
swTableRow_set(row, col, &Z_DVAL_P(v), 0);
150+
swTableRow_set_value(row, col, &Z_DVAL_P(v), 0);
151151
}
152152
else
153153
{
154154
convert_to_long(v);
155-
swTableRow_set(row, col, &Z_LVAL_P(v), 0);
155+
swTableRow_set_value(row, col, &Z_LVAL_P(v), 0);
156156
}
157157
} while (p);
158158
}
@@ -215,6 +215,22 @@ PHP_METHOD(swoole_table, get)
215215
}
216216
}
217217

218+
PHP_METHOD(swoole_table, del)
219+
{
220+
char *key;
221+
int keylen;
222+
223+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &keylen) == FAILURE)
224+
{
225+
RETURN_FALSE;
226+
}
227+
228+
array_init(return_value);
229+
230+
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
231+
swTableRow_del(table, key, keylen);
232+
}
233+
218234
PHP_METHOD(swoole_table, lock)
219235
{
220236
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);

0 commit comments

Comments
 (0)