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

Skip to content

Commit d750813

Browse files
Rewrite generic mset to use newer methods
1 parent a4a0ed5 commit d750813

2 files changed

Lines changed: 32 additions & 82 deletions

File tree

redis.c

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,6 @@ PHP_REDIS_API void generic_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, char *sort,
16341634
}
16351635
}
16361636
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply);
1637-
16381637
}
16391638

16401639
/* {{{ proto array Redis::sortAsc(string key, string pattern, string get,
@@ -1842,95 +1841,50 @@ PHP_METHOD(Redis, move) {
18421841
}
18431842
/* }}} */
18441843

1845-
PHP_REDIS_API void
1846-
generic_mset(INTERNAL_FUNCTION_PARAMETERS, char *kw, ResultCallback fun) {
1847-
zval *object;
1844+
static
1845+
void generic_mset(INTERNAL_FUNCTION_PARAMETERS, char *kw, ResultCallback fun)
1846+
{
18481847
RedisSock *redis_sock;
1849-
1850-
char *cmd = NULL, *p = NULL;
1851-
int cmd_len = 0, argc = 0, kw_len = strlen(kw);
1852-
int step = 0; // 0: compute size; 1: copy strings.
1853-
zval *z_array;
1854-
1855-
HashTable *keytable;
1848+
smart_string cmd = {0};
1849+
zval *object, *z_array;
1850+
HashTable *htargs;
1851+
zend_string *zkey;
1852+
zval *zmem;
1853+
char buf[64];
1854+
size_t keylen;
1855+
ulong idx;
18561856

18571857
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
18581858
&object, redis_ce, &z_array) == FAILURE)
18591859
{
18601860
RETURN_FALSE;
18611861
}
18621862

1863-
if ((redis_sock = redis_sock_get(object TSRMLS_CC, 0)) == NULL) {
1864-
RETURN_FALSE;
1865-
}
1866-
1867-
if(zend_hash_num_elements(Z_ARRVAL_P(z_array)) == 0) {
1863+
/* Make sure we can get our socket, and we were not passed an empty array */
1864+
if ((redis_sock = redis_sock_get(object TSRMLS_CC, 0)) == NULL ||
1865+
zend_hash_num_elements(Z_ARRVAL_P(z_array)) == 0)
1866+
{
18681867
RETURN_FALSE;
18691868
}
18701869

1871-
for(step = 0; step < 2; ++step) {
1872-
if(step == 1) {
1873-
/* '*' + arg count + NL */
1874-
cmd_len += 1 + integer_length(1 + 2 * argc) + 2;
1875-
/* '$' + strlen(kw) + NL */
1876-
cmd_len += 1 + integer_length(kw_len) + 2;
1877-
/* kw + NL */
1878-
cmd_len += kw_len + 2;
1870+
/* Initialize our command */
1871+
htargs = Z_ARRVAL_P(z_array);
1872+
redis_cmd_init_sstr(&cmd, zend_hash_num_elements(htargs) * 2, kw, strlen(kw));
18791873

1880-
p = cmd = emalloc(cmd_len + 1);
1881-
p += sprintf(cmd, "*%d" _NL "$%d" _NL "%s" _NL, 1 + 2 * argc,
1882-
kw_len, kw);
1874+
ZEND_HASH_FOREACH_KEY_VAL(htargs, idx, zkey, zmem) {
1875+
/* Handle string or numeric keys */
1876+
if (zkey) {
1877+
redis_cmd_append_sstr_key(&cmd, zkey->val, zkey->len, redis_sock, NULL);
1878+
} else {
1879+
keylen = snprintf(buf, sizeof(buf), "%ld", (long)idx);
1880+
redis_cmd_append_sstr_key(&cmd, buf, (strlen_t)keylen, redis_sock, NULL);
18831881
}
18841882

1885-
ulong idx;
1886-
zend_string *zkey;
1887-
zval *z_value_p;
1888-
keytable = Z_ARRVAL_P(z_array);
1889-
ZEND_HASH_FOREACH_KEY_VAL(keytable, idx, zkey, z_value_p) {
1890-
char *key, *val;
1891-
strlen_t key_len;
1892-
strlen_t val_len;
1893-
int val_free, key_free;
1894-
char buf[32];
1895-
1896-
if (zkey) {
1897-
key = zkey->val;
1898-
key_len = zkey->len;
1899-
} else {
1900-
// Create string representation of our index
1901-
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
1902-
key = (char*)buf;
1903-
}
1904-
1905-
if(step == 0)
1906-
argc++; /* found a valid arg */
1907-
1908-
val_free = redis_serialize(redis_sock, z_value_p, &val, &val_len
1909-
TSRMLS_CC);
1910-
key_free = redis_key_prefix(redis_sock, &key, &key_len);
1911-
1912-
if(step == 0) { /* counting */
1913-
cmd_len += 1 + integer_length(key_len) + 2
1914-
+ key_len + 2
1915-
+ 1 + integer_length(val_len) + 2
1916-
+ val_len + 2;
1917-
} else {
1918-
p += sprintf(p, "$%d" _NL, key_len); /* key len */
1919-
memcpy(p, key, key_len); p += key_len; /* key */
1920-
memcpy(p, _NL, 2); p += 2;
1921-
1922-
p += sprintf(p, "$%d" _NL, val_len); /* val len */
1923-
memcpy(p, val, val_len); p += val_len; /* val */
1924-
memcpy(p, _NL, 2); p += 2;
1925-
}
1926-
1927-
if (val_free) efree(val);
1928-
if (key_free) efree(key);
1929-
} ZEND_HASH_FOREACH_END();
1930-
}
1931-
1932-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
1883+
/* Append our value */
1884+
redis_cmd_append_sstr_zval(&cmd, zmem, redis_sock TSRMLS_CC);
1885+
} ZEND_HASH_FOREACH_END();
19331886

1887+
REDIS_PROCESS_REQUEST(redis_sock, cmd.c, cmd.len);
19341888
IF_ATOMIC() {
19351889
fun(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
19361890
}
@@ -1939,16 +1893,14 @@ generic_mset(INTERNAL_FUNCTION_PARAMETERS, char *kw, ResultCallback fun) {
19391893

19401894
/* {{{ proto bool Redis::mset(array (key => value, ...)) */
19411895
PHP_METHOD(Redis, mset) {
1942-
generic_mset(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MSET",
1943-
redis_boolean_response);
1896+
generic_mset(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MSET", redis_boolean_response);
19441897
}
19451898
/* }}} */
19461899

19471900

19481901
/* {{{ proto bool Redis::msetnx(array (key => value, ...)) */
19491902
PHP_METHOD(Redis, msetnx) {
1950-
generic_mset(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MSETNX",
1951-
redis_1_response);
1903+
generic_mset(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MSETNX", redis_1_response);
19521904
}
19531905
/* }}} */
19541906

redis_commands.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,7 @@ int redis_key_varval_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
809809
{
810810
zval *z_args;
811811
smart_string cmdstr = {0};
812-
char *arg;
813-
int arg_free;
814-
strlen_t arg_len, i;
812+
strlen_t i;
815813
int argc = ZEND_NUM_ARGS();
816814

817815
// We at least need a key and one value

0 commit comments

Comments
 (0)