Skip to content

Commit

Permalink
swoole_table-0.1-beta release
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 20, 2014
1 parent 1b82a45 commit 016423c
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 70 deletions.
28 changes: 21 additions & 7 deletions examples/table.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
<?php
echo "test\n";
$table = new swoole_table(1024);
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
$table->column('name', swoole_table::TYPE_STRING, 64);
$table->column('num', swoole_table::TYPE_FLOAT);
$table->create();

$table->add('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
$worker = new swoole_process('child1', false, false);
$worker->start();

$table->lock();
$table->add('350749960@qq.com', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
$table->add('hello@qq.com', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
$table->unlock();
//child
function child1($worker)
{
global $table;
$table->set('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
$table->set('350749960@qq.com', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
$table->set('hello@qq.com', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
sleep(100000);
}

var_dump($table->get('350749960@qq.com'));
//master
sleep(1);
$s = microtime(true);
for($i =0; $i < 1000000; $i++)
{
$arr = $table->get('350749960@qq.com');
}
echo "use: ".((microtime(true) - $s) * 1000)."ms\n";
//var_dump($table->get('350749960@qq.com'));
sleep(100000);
2 changes: 1 addition & 1 deletion include/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ static inline uint64_t swoole_hash_php(char *key, uint32_t len)

#define CRC_STRING_MAXLEN 256

uint64_t swoole_crc32(char *data, uint32_t size);
uint32_t swoole_crc32(char *data, uint32_t size);

#endif /* SW_HASH_H_ */
1 change: 0 additions & 1 deletion include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ typedef struct _swMemoryPool
* FixedPool, random alloc/free fixed size memory
*/
swMemoryPool* swFixedPool_new(uint32_t slice_num, uint32_t slice_size, uint8_t shared);

swMemoryPool* swFixedPool_new2(uint32_t slice_size, void *memory, size_t size);

/**
Expand Down
17 changes: 15 additions & 2 deletions include/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@
typedef struct _swTableRow
{
sw_atomic_t lock;

/**
* string crc32
*/
uint32_t crc32;

/**
* 1:used, 0:empty
*/
uint8_t active;

/**
* next slot
*/
struct _swTableRow *next;
char data[0];
} swTableRow;
Expand Down Expand Up @@ -79,15 +91,16 @@ swTable* swTable_new(uint32_t rows_size);
int swTable_create(swTable *table);
void swTable_free(swTable *table);
int swTableColumn_add(swTable *table, char *name, int len, int type, int size);
swTableRow* swTableRow_add(swTable *table, char *row_key, int klen);
swTableRow* swTableRow_set(swTable *table, char *key, int keylen);
swTableRow* swTableRow_get(swTable *table, char *key, int keylen);
int swTableRow_del(swTable *table, char *key, int keylen);

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

static sw_inline void swTableRow_set(swTableRow *row, swTableColumn * col, void *value, int vlen)
static sw_inline void swTableRow_set_value(swTableRow *row, swTableColumn * col, void *value, int vlen)
{
switch(col->type)
{
Expand Down
4 changes: 2 additions & 2 deletions php_swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ PHP_METHOD(swoole_buffer, clear);
PHP_METHOD(swoole_table, __construct);
PHP_METHOD(swoole_table, column);
PHP_METHOD(swoole_table, create);
PHP_METHOD(swoole_table, add);
PHP_METHOD(swoole_table, set);
PHP_METHOD(swoole_table, get);

PHP_METHOD(swoole_table, del);
PHP_METHOD(swoole_table, lock);
PHP_METHOD(swoole_table, unlock);

Expand Down
4 changes: 2 additions & 2 deletions src/core/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ static unsigned int crc32_tab[] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};

static inline unsigned int crc32(char *buf, unsigned int size)
static inline uint32_t crc32(char *buf, unsigned int size)
{
const char *p;
register int crc = 0;
Expand All @@ -423,7 +423,7 @@ static inline unsigned int crc32(char *buf, unsigned int size)
return crc ^ ~0U;
}

uint64_t swoole_crc32(char *data, uint32_t size)
uint32_t swoole_crc32(char *data, uint32_t size)
{
if (size < CRC_STRING_MAXLEN)
{
Expand Down
5 changes: 3 additions & 2 deletions src/memory/FixedPool.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ swMemoryPool* swFixedPool_new2(uint32_t slice_size, void *memory, size_t size)

swMemoryPool *pool = memory;
memory += sizeof(swMemoryPool);
bzero(pool, sizeof(swMemoryPool));

pool->object = object;
pool->alloc = swFixedPool_alloc;
pool->free = NULL;
pool->destroy = NULL;
pool->free = swFixedPool_free;
pool->destroy = swFixedPool_destroy;

object->memory = memory;

Expand Down
121 changes: 74 additions & 47 deletions src/memory/Table.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,100 +87,127 @@ int swTableColumn_add(swTable *table, char *name, int len, int type, int size)
int swTable_create(swTable *table)
{
uint32_t row_num = table->size * (1 + SW_TABLE_CONFLICT_PROPORTION);
size_t memory_size = row_num * (sizeof(swTableRow) + table->item_size);
uint32_t row_memory_size = sizeof(swTableRow) + table->item_size;

size_t memory_size = row_num * row_memory_size;
void *memory = sw_shm_malloc(memory_size);
if (memory == NULL)
{
return SW_ERR;
}
table->memory = memory;
table->rows = memory;
memory += sizeof(swTableRow) * table->size;
memory_size -= sizeof(swTableRow) * table->size;

int i;
for (i = 0; i < table->size; i++)
{
table->rows[i] = memory + (sizeof(swTableRow) + table->item_size) * i;
table->rows[i] = memory + (row_memory_size * i);
}
memory += (sizeof(swTableRow) + table->item_size) * table->size;
memory_size -= (sizeof(swTableRow) + table->item_size) * table->size;
table->pool = swFixedPool_new2(table->item_size, memory, memory_size);
memory += row_memory_size * table->size;
memory_size -= row_memory_size * table->size;
table->pool = swFixedPool_new2(row_memory_size, memory, memory_size);
return SW_OK;
}

void swTable_free(swTable *table)
{

//TODO free columns
// if (table->item_size > 0)
// {
//
// }
sw_shm_free(table->memory);
}


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

swTableRow* swTableRow_add(swTable *table, char *key, int keylen)
swTableRow* swTableRow_set(swTable *table, char *key, int keylen)
{
swTableRow *row = swTable_hash(table, key, keylen);
uint32_t crc32 = swoole_crc32(key, keylen);

sw_spinlock(&row->lock);
if (row->active)
{
for (;;)
{
if (row->crc32 == crc32)
{
break;
}
else if (row->next == NULL)
{
swTableRow *new_row = table->pool->alloc(table->pool, 0);
row->next = new_row;
row = new_row;
break;
}
else
{
row = row->next;
}
}
}
row->crc32 = crc32;
row->active = 1;
swTrace("row=%p, crc32=%u, key=%s\n", row, crc32, key);
sw_spinlock_release(&row->lock);
return row;
}

swTableRow* swTableRow_get(swTable *table, char *key, int keylen)
{
swTableRow *row = swTable_hash(table, key, keylen);
uint32_t crc32 = swoole_crc32(key, keylen);

swTrace("row=%p, crc32=%u, key=%s\n", row, crc32, key);
sw_spinlock(&row->lock);
while(1)
for (;;)
{
//empty slot
if (row->active == 0)
if (row->crc32 == crc32)
{
break;
}
else if (row->next)
else if (row->next == NULL)
{
row = row->next;
row = NULL;
break;
}
else
{
swTableRow *new_row = table->pool->alloc(table->pool, 0);
row->next = new_row;
row = new_row;
break;
row = row->next;
}
}
row->active = 1;
sw_spinlock_release(&row->lock);
return row;
}

swTableRow* swTableRow_get(swTable *table, char *key, int keylen)
int swTableRow_del(swTable *table, char *key, int keylen)
{
swTableRow *row = swTable_hash(table, key, keylen);
uint32_t crc32 = swoole_crc32(key, keylen);

// for(;;)
// {
// //empty slot
// if (row->active == 0)
// {
// break;
// }
// else if (row->next)
// {
// row = row->next;
// }
// else
// {
// swTableRow *new_row = swTable_alloc(table);
// row->next = new_row;
// row = new_row;
// break;
// }
// }
return row;
sw_spinlock(&row->lock);
while (row->active)
{
for (;;)
{
if (row->crc32 == crc32)
{
table->pool->free(table->pool, row);
break;
}
else if (row->next == NULL)
{
return SW_ERR;
}
else
{
row = row->next;
}
}
}
row->active = 0;
sw_spinlock_release(&row->lock);
return SW_OK;
}

3 changes: 2 additions & 1 deletion swoole.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ const zend_function_entry swoole_table_methods[] =
PHP_ME(swoole_table, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_table, column, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, create, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, add, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, set, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, del, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, lock, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, unlock, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
Expand Down
26 changes: 21 additions & 5 deletions swoole_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ PHP_METHOD(swoole_table, create)
RETURN_TRUE;
}

PHP_METHOD(swoole_table, add)
PHP_METHOD(swoole_table, set)
{
zval *array;
char *key;
Expand All @@ -120,7 +120,7 @@ PHP_METHOD(swoole_table, add)
}

swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow *row = swTableRow_add(table, key, keylen);
swTableRow *row = swTableRow_set(table, key, keylen);
swTableColumn *col;
zval *v;
char *k;
Expand All @@ -142,17 +142,17 @@ PHP_METHOD(swoole_table, add)
else if (col->type == SW_TABLE_STRING)
{
convert_to_string(v);
swTableRow_set(row, col, Z_STRVAL_P(v), Z_STRLEN_P(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(row, col, &Z_DVAL_P(v), 0);
swTableRow_set_value(row, col, &Z_DVAL_P(v), 0);
}
else
{
convert_to_long(v);
swTableRow_set(row, col, &Z_LVAL_P(v), 0);
swTableRow_set_value(row, col, &Z_LVAL_P(v), 0);
}
} while (p);
}
Expand Down Expand Up @@ -215,6 +215,22 @@ PHP_METHOD(swoole_table, get)
}
}

PHP_METHOD(swoole_table, del)
{
char *key;
int keylen;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &keylen) == FAILURE)
{
RETURN_FALSE;
}

array_init(return_value);

swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
swTableRow_del(table, key, keylen);
}

PHP_METHOD(swoole_table, lock)
{
swTable *table = php_swoole_table_get(getThis() TSRMLS_CC);
Expand Down

0 comments on commit 016423c

Please sign in to comment.