diff --git a/ext-src/swoole_table.cc b/ext-src/swoole_table.cc index 8c612761f83..35108f20719 100644 --- a/ext-src/swoole_table.cc +++ b/ext-src/swoole_table.cc @@ -23,24 +23,20 @@ using namespace swoole; static inline void php_swoole_table_row2array(Table *table, TableRow *row, zval *return_value) { array_init(return_value); - TableStringLength vlen = 0; - double dval = 0; - long lval = 0; - for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) { TableColumn *col = *i; if (col->type == TableColumn::TYPE_STRING) { - memcpy(&vlen, row->data + col->index, sizeof(TableStringLength)); - add_assoc_stringl_ex(return_value, - col->name.c_str(), - col->name.length(), - row->data + col->index + sizeof(TableStringLength), - vlen); + TableStringLength len = 0; + char *str = nullptr; + row->get_value(col, &str, &len); + add_assoc_stringl_ex(return_value, col->name.c_str(), col->name.length(), str, len); } else if (col->type == TableColumn::TYPE_FLOAT) { - memcpy(&dval, row->data + col->index, sizeof(dval)); + double dval = 0; + row->get_value(col, &dval); add_assoc_double_ex(return_value, col->name.c_str(), col->name.length(), dval); } else if (col->type == TableColumn::TYPE_INT) { - memcpy(&lval, row->data + col->index, sizeof(lval)); + long lval = 0; + row->get_value(col, &lval); add_assoc_long_ex(return_value, col->name.c_str(), col->name.length(), lval); } else { abort(); @@ -50,23 +46,24 @@ static inline void php_swoole_table_row2array(Table *table, TableRow *row, zval static inline void php_swoole_table_get_field_value( Table *table, TableRow *row, zval *return_value, char *field, uint16_t field_len) { - TableStringLength vlen = 0; - double dval = 0; - long lval = 0; - TableColumn *col = table->get_column(std::string(field, field_len)); if (!col) { ZVAL_FALSE(return_value); return; } if (col->type == TableColumn::TYPE_STRING) { - memcpy(&vlen, row->data + col->index, sizeof(TableStringLength)); - ZVAL_STRINGL(return_value, row->data + col->index + sizeof(TableStringLength), vlen); + TableStringLength len = 0; + char *str = nullptr; + row->get_value(col, &str, &len); + ZVAL_STRINGL(return_value, str, len); } else if (col->type == TableColumn::TYPE_FLOAT) { - memcpy(&dval, row->data + col->index, sizeof(dval)); + double dval = 0; + row->get_value(col, &dval); + add_assoc_double_ex(return_value, col->name.c_str(), col->name.length(), dval); ZVAL_DOUBLE(return_value, dval); } else if (col->type == TableColumn::TYPE_INT) { - memcpy(&lval, row->data + col->index, sizeof(lval)); + long lval = 0; + row->get_value(col, &lval); ZVAL_LONG(return_value, lval); } else { abort(); @@ -515,9 +512,7 @@ static PHP_METHOD(swoole_table, incr) { } if (out_flags & SW_TABLE_FLAG_NEW_ROW) { - for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) { - (*i)->clear(row); - } + table->clear_row(row); } if (column->type == TableColumn::TYPE_STRING) { @@ -577,9 +572,7 @@ static PHP_METHOD(swoole_table, decr) { } if (out_flags & SW_TABLE_FLAG_NEW_ROW) { - for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) { - (*i)->clear(row); - } + table->clear_row(row); } if (column->type == TableColumn::TYPE_STRING) { diff --git a/include/swoole_table.h b/include/swoole_table.h index df984f4b1df..8998e8eefd4 100644 --- a/include/swoole_table.h +++ b/include/swoole_table.h @@ -67,6 +67,9 @@ struct TableRow { } void set_value(TableColumn *col, void *value, size_t vlen); + void get_value(TableColumn *col, double *dval); + void get_value(TableColumn *col, long *lval); + void get_value(TableColumn *col, char **strval, TableStringLength *strlen); }; struct TableIterator { @@ -216,6 +219,12 @@ class Table { } } + void clear_row(TableRow *row) { + for (auto i = column_list->begin(); i != column_list->end(); i++) { + (*i)->clear(row); + } + } + void init_row(TableRow *new_row, const char *key, int keylen) { sw_memset_zero(new_row, sizeof(TableRow)); memcpy(new_row->key, key, keylen); diff --git a/src/memory/table.cc b/src/memory/table.cc index 8170ad06e03..1af16bc827a 100644 --- a/src/memory/table.cc +++ b/src/memory/table.cc @@ -419,4 +419,17 @@ void TableRow::set_value(TableColumn *col, void *value, size_t vlen) { } } +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