Skip to content

Instantly share code, notes, and snippets.

@michael-grunder
Created April 27, 2017 05:09
Show Gist options
  • Select an option

  • Save michael-grunder/54015a9879b7f2bd140eef70bc477b9a to your computer and use it in GitHub Desktop.

Select an option

Save michael-grunder/54015a9879b7f2bd140eef70bc477b9a to your computer and use it in GitHub Desktop.
PHP_REDIS_API void
generic_mset_old(INTERNAL_FUNCTION_PARAMETERS, char *kw, ResultCallback fun) {
zval *object;
RedisSock *redis_sock;
char *cmd = NULL, *p = NULL;
int cmd_len = 0, argc = 0, kw_len = strlen(kw);
int step = 0; // 0: compute size; 1: copy strings.
zval *z_array;
HashTable *keytable;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
&object, redis_ce, &z_array) == FAILURE)
{
RETURN_FALSE;
}
if ((redis_sock = redis_sock_get(object TSRMLS_CC, 0)) == NULL) {
RETURN_FALSE;
}
if(zend_hash_num_elements(Z_ARRVAL_P(z_array)) == 0) {
RETURN_FALSE;
}
for(step = 0; step < 2; ++step) {
if(step == 1) {
/* '*' + arg count + NL */
cmd_len += 1 + integer_length(1 + 2 * argc) + 2;
/* '$' + strlen(kw) + NL */
cmd_len += 1 + integer_length(kw_len) + 2;
/* kw + NL */
cmd_len += kw_len + 2;
p = cmd = emalloc(cmd_len + 1);
p += sprintf(cmd, "*%d" _NL "$%d" _NL "%s" _NL, 1 + 2 * argc,
kw_len, kw);
}
ulong idx;
zend_string *zkey;
zval *z_value_p;
keytable = Z_ARRVAL_P(z_array);
ZEND_HASH_FOREACH_KEY_VAL(keytable, idx, zkey, z_value_p) {
char *key, *val;
strlen_t key_len;
strlen_t val_len;
int val_free, key_free;
char buf[32];
if (zkey) {
key = zkey->val;
key_len = zkey->len;
} else {
// Create string representation of our index
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
}
if(step == 0)
argc++; /* found a valid arg */
val_free = redis_serialize(redis_sock, z_value_p, &val, &val_len
TSRMLS_CC);
key_free = redis_key_prefix(redis_sock, &key, &key_len);
if(step == 0) { /* counting */
cmd_len += 1 + integer_length(key_len) + 2
+ key_len + 2
+ 1 + integer_length(val_len) + 2
+ val_len + 2;
} else {
p += sprintf(p, "$%d" _NL, key_len); /* key len */
memcpy(p, key, key_len); p += key_len; /* key */
memcpy(p, _NL, 2); p += 2;
p += sprintf(p, "$%d" _NL, val_len); /* val len */
memcpy(p, val, val_len); p += val_len; /* val */
memcpy(p, _NL, 2); p += 2;
}
if (val_free) efree(val);
if (key_free) efree(key);
} ZEND_HASH_FOREACH_END();
}
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
IF_ATOMIC() {
fun(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
}
REDIS_PROCESS_RESPONSE(fun);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment