Skip to content

Commit

Permalink
fix table bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Dec 17, 2020
1 parent bcd4f60 commit 0d5e72e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 98 deletions.
32 changes: 3 additions & 29 deletions include/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ typedef struct
enum swoole_table_type
{
SW_TABLE_INT = 1,
SW_TABLE_INT8,
SW_TABLE_INT16,
SW_TABLE_INT32,
#ifdef __x86_64__
SW_TABLE_INT64,
#endif
SW_TABLE_FLOAT,
SW_TABLE_STRING,
};
Expand Down Expand Up @@ -169,32 +163,12 @@ typedef uint32_t swTable_string_length_t;

static sw_inline void swTableRow_set_value(swTableRow *row, swTableColumn * col, void *value, size_t vlen)
{
int8_t _i8;
int16_t _i16;
int32_t _i32;
#ifdef __x86_64__
int64_t _i64;
#endif

switch(col->type)
{
case SW_TABLE_INT8:
_i8 = *(int8_t *) value;
memcpy(row->data + col->index, &_i8, 1);
break;
case SW_TABLE_INT16:
_i16 = *(int16_t *) value;
memcpy(row->data + col->index, &_i16, 2);
break;
case SW_TABLE_INT32:
_i32 = *(int32_t *) value;
memcpy(row->data + col->index, &_i32, 4);
break;
#ifdef __x86_64__
case SW_TABLE_INT64:
_i64 = *(int64_t *) value;
memcpy(row->data + col->index, &_i64, 8);
case SW_TABLE_INT:
memcpy(row->data + col->index, value, sizeof(long));
break;
#endif
case SW_TABLE_FLOAT:
memcpy(row->data + col->index, value, sizeof(double));
break;
Expand Down
35 changes: 8 additions & 27 deletions src/memory/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,20 @@ int swTableColumn_add(swTable *table, const char *name, int len, int type, int s
switch(type)
{
case SW_TABLE_INT:
switch(size)
{
case 1:
col->size = 1;
col->type = SW_TABLE_INT8;
break;
case 2:
col->size = 2;
col->type = SW_TABLE_INT16;
break;
#ifdef __x86_64__
case 8:
col->size = 8;
col->type = SW_TABLE_INT64;
break;
#endif
default:
col->size = 4;
col->type = SW_TABLE_INT32;
break;
}
col->size = sizeof(long);
break;
case SW_TABLE_FLOAT:
col->size = sizeof(double);
col->type = SW_TABLE_FLOAT;
break;
case SW_TABLE_STRING:
col->size = size + sizeof(swTable_string_length_t);
col->type = SW_TABLE_STRING;
break;
default:
swWarn("unkown column type");
swTableColumn_free(col);
return SW_ERR;
}
col->type = type;
col->index = table->item_size;
table->item_size += col->size;
++table->column_num;
Expand Down Expand Up @@ -369,6 +348,8 @@ swTableRow* swTableRow_set(swTable *table, const char *key, int keylen, swTableR
}
//add row_num
bzero(new_row, sizeof(swTableRow));
bzero((char *)new_row + sizeof(swTableRow), table->item_size);

sw_atomic_fetch_add(&(table->row_num), 1);
row->next = new_row;
row = new_row;
Expand All @@ -389,6 +370,7 @@ swTableRow* swTableRow_set(swTable *table, const char *key, int keylen, swTableR
insert_count ++;
#endif
sw_atomic_fetch_add(&(table->row_num), 1);
bzero((char *)row + sizeof(swTableRow), table->item_size);
}

memcpy(row->key, key, keylen);
Expand All @@ -400,8 +382,7 @@ swTableRow* swTableRow_set(swTable *table, const char *key, int keylen, swTableR

static inline void swTable_clear_row(swTable *table, swTableRow *row)
{
sw_memset_zero((char*) &row->lock_pid, sizeof(*row) - offsetof(swTableRow, lock_pid));
bzero(row + sizeof(swTableRow), table->item_size);
bzero((char*) &row->lock_pid, sizeof(*row) - offsetof(swTableRow, lock_pid));
}

int swTableRow_del(swTable *table, char *key, int keylen)
Expand All @@ -423,7 +404,7 @@ int swTableRow_del(swTable *table, char *key, int keylen)
{
if (sw_mem_equal(row->key, row->key_len, key, keylen))
{
swTable_clear_row(row);
swTable_clear_row(table, row);
goto _delete_element;
}
else
Expand Down Expand Up @@ -468,7 +449,7 @@ int swTableRow_del(swTable *table, char *key, int keylen)
prev->next = tmp->next;
}
table->lock.lock(&table->lock);
swTable_clear_row(tmp);
swTable_clear_row(table, tmp);
table->pool->free(table->pool, tmp);
table->lock.unlock(&table->lock);
}
Expand Down
52 changes: 10 additions & 42 deletions swoole_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ static inline void php_swoole_table_row2array(swTable *table, swTableRow *row, z

swTableColumn *col = NULL;
swTable_string_length_t vlen = 0;
double dval = 0;
int64_t lval = 0;
char *k;

swHashMap_rewind(table->columns);
Expand All @@ -43,39 +41,24 @@ static inline void php_swoole_table_row2array(swTable *table, swTableRow *row, z
}
else if (col->type == SW_TABLE_FLOAT)
{
double dval = 0;
memcpy(&dval, row->data + col->index, sizeof(dval));
add_assoc_double_ex(return_value, col->name->str, col->name->length, dval);
}
else
{
switch (col->type)
{
case SW_TABLE_INT8:
memcpy(&lval, row->data + col->index, 1);
add_assoc_long_ex(return_value, col->name->str, col->name->length, (int8_t) lval);
break;
case SW_TABLE_INT16:
memcpy(&lval, row->data + col->index, 2);
add_assoc_long_ex(return_value, col->name->str, col->name->length, (int16_t) lval);
break;
case SW_TABLE_INT32:
memcpy(&lval, row->data + col->index, 4);
add_assoc_long_ex(return_value, col->name->str, col->name->length, (int32_t) lval);
break;
default:
memcpy(&lval, row->data + col->index, 8);
add_assoc_long_ex(return_value, col->name->str, col->name->length, lval);
break;
}
long lval = 0;
memcpy(&lval, row->data + col->index, sizeof(lval));
add_assoc_long_ex(return_value, col->name->str, col->name->length, lval);
}
}
}

static inline void php_swoole_table_get_field_value(swTable *table, swTableRow *row, zval *return_value, char *field, uint16_t field_len)
{
swTable_string_length_t vlen = 0;
double dval = 0;
int64_t lval = 0;



swTableColumn *col = (swTableColumn *) swHashMap_find(table->columns, field, field_len);
if (!col)
Expand All @@ -90,30 +73,15 @@ static inline void php_swoole_table_get_field_value(swTable *table, swTableRow *
}
else if (col->type == SW_TABLE_FLOAT)
{
double dval = 0;
memcpy(&dval, row->data + col->index, sizeof(dval));
ZVAL_DOUBLE(return_value, dval);
}
else
{
switch (col->type)
{
case SW_TABLE_INT8:
memcpy(&lval, row->data + col->index, 1);
ZVAL_LONG(return_value, (int8_t) lval);
break;
case SW_TABLE_INT16:
memcpy(&lval, row->data + col->index, 2);
ZVAL_LONG(return_value, (int16_t) lval);
break;
case SW_TABLE_INT32:
memcpy(&lval, row->data + col->index, 4);
ZVAL_LONG(return_value, (int32_t) lval);
break;
default:
memcpy(&lval, row->data + col->index, 8);
ZVAL_LONG(return_value, lval);
break;
}
int64_t lval = 0;
memcpy(&lval, row->data + col->index, sizeof(lval));
ZVAL_LONG(return_value, lval);
}
}

Expand Down

0 comments on commit 0d5e72e

Please sign in to comment.