Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90eb047

Browse files
Refactor HMSET command.
1 parent 6d10448 commit 90eb047

1 file changed

Lines changed: 18 additions & 57 deletions

File tree

redis_commands.c

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,76 +2644,37 @@ int redis_hmget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
26442644
int redis_hmset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
26452645
char **cmd, int *cmd_len, short *slot, void **ctx)
26462646
{
2647-
char *key;
2648-
int key_free, count;
2649-
size_t key_len;
2650-
zend_ulong idx;
2651-
zval *z_arr;
2652-
HashTable *ht_vals;
26532647
smart_string cmdstr = {0};
2654-
zend_string *zkey;
2655-
zval *z_val;
2648+
zend_string *key = NULL;
2649+
HashTable *ht = NULL;
2650+
uint32_t fields;
2651+
zend_ulong idx;
2652+
zval *zv;
26562653

2657-
// Parse args
2658-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa", &key, &key_len,
2659-
&z_arr) == FAILURE)
2660-
{
2661-
return FAILURE;
2662-
}
2654+
ZEND_PARSE_PARAMETERS_START(2, 2)
2655+
Z_PARAM_STR(key)
2656+
Z_PARAM_ARRAY_HT(ht)
2657+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
26632658

2664-
// We can abort if we have no fields
2665-
if ((count = zend_hash_num_elements(Z_ARRVAL_P(z_arr))) == 0) {
2659+
fields = zend_hash_num_elements(ht);
2660+
if (fields == 0)
26662661
return FAILURE;
2667-
}
2668-
2669-
// Prefix our key
2670-
key_free = redis_key_prefix(redis_sock, &key, &key_len);
2671-
2672-
// Grab our array as a HashTable
2673-
ht_vals = Z_ARRVAL_P(z_arr);
2674-
2675-
// Initialize our HMSET command (key + 2x each array entry), add key
2676-
redis_cmd_init_sstr(&cmdstr, 1+(count*2), ZEND_STRL("HMSET"));
2677-
redis_cmd_append_sstr(&cmdstr, key, key_len);
26782662

2679-
// Start traversing our key => value array
2680-
ZEND_HASH_FOREACH_KEY_VAL(ht_vals, idx, zkey, z_val) {
2681-
char *mem, *val, kbuf[40];
2682-
size_t val_len;
2683-
int val_free;
2684-
unsigned int mem_len;
2663+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (2 * fields), "HMSET");
2664+
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
26852665

2686-
// If the hash key is an integer, convert it to a string
2687-
if (zkey) {
2688-
mem_len = ZSTR_LEN(zkey);
2689-
mem = ZSTR_VAL(zkey);
2666+
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, zv) {
2667+
if (key) {
2668+
redis_cmd_append_sstr_zstr(&cmdstr, key);
26902669
} else {
2691-
mem_len = snprintf(kbuf, sizeof(kbuf), ZEND_LONG_FMT, idx);
2692-
mem = (char*)kbuf;
2670+
redis_cmd_append_sstr_long(&cmdstr, idx);
26932671
}
2694-
2695-
// Serialize value (if directed)
2696-
val_free = redis_pack(redis_sock, z_val, &val, &val_len);
2697-
2698-
// Append the key and value to our command
2699-
redis_cmd_append_sstr(&cmdstr, mem, mem_len);
2700-
redis_cmd_append_sstr(&cmdstr, val, val_len);
2701-
2702-
// Free our value if we serialized it
2703-
if (val_free) efree(val);
2672+
redis_cmd_append_sstr_zval(&cmdstr, zv, redis_sock);
27042673
} ZEND_HASH_FOREACH_END();
27052674

2706-
// Set slot if directed
2707-
CMD_SET_SLOT(slot,key,key_len);
2708-
2709-
// Free our key if we prefixed it
2710-
if (key_free) efree(key);
2711-
2712-
// Push return pointers
27132675
*cmd_len = cmdstr.len;
27142676
*cmd = cmdstr.c;
27152677

2716-
// Success!
27172678
return SUCCESS;
27182679
}
27192680

0 commit comments

Comments
 (0)