Permalink
Browse files

Re-added HMGET + doc & unit tests.

  • Loading branch information...
1 parent a8738d7 commit c67e2ddf7405e827435cad1d14cc06eb96811a84 @nicolasff nicolasff committed Sep 20, 2010
Showing with 253 additions and 104 deletions.
  1. +16 −0 README.markdown
  2. +12 −8 common.h
  3. +54 −10 library.c
  4. +11 −10 library.h
  5. +1 −0 php_redis.h
  6. +154 −76 redis.c
  7. +5 −0 tests/TestRedis.php
View
@@ -1780,3 +1780,19 @@ $redis->delete('user:1');
$redis->hMset('user:1', array('name' => 'Joe', 'salary' => 2000));
$redis->hIncrBy('user:1', 'salary', 100); // Joe earns 100 more now.
</pre>
+
+## hMGet
+##### Description
+Retirieve the values associated to the specified fields in the hash.
+##### Parameters
+*key*
+*memberKeys* Array
+##### Return value
+*Array* An array of elements, the values of the specified fields in the hash, with the hash keys as array keys.
+##### Examples
+<pre>
+$redis->delete('h');
+$redis->hSet('h', 'field1', 'value1');
+$redis->hSet('h', 'field2', 'value2');
+$redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */
+</pre>
View
@@ -67,9 +67,10 @@
RETURN_FALSE; \
}
-#define REDIS_SAVE_CALLBACK(callback) IF_MULTI_OR_PIPELINE() { \
+#define REDIS_SAVE_CALLBACK(callback, closure_context) IF_MULTI_OR_PIPELINE() { \
fold_item *f1 = malloc(sizeof(fold_item)); \
f1->fun = (void *)callback; \
+ f1->ctx = closure_context; \
f1->next = NULL; \
fold_item *current = redis_sock->current;\
if(current) current->next = f1; \
@@ -79,18 +80,18 @@
}\
}
-#define REDIS_ELSE_IF_MULTI(function) \
+#define REDIS_ELSE_IF_MULTI(function, closure_context) \
else if(redis_sock->mode == MULTI) { \
if(redis_response_enqueued(redis_sock TSRMLS_CC) == 1) {\
- REDIS_SAVE_CALLBACK(function); \
+ REDIS_SAVE_CALLBACK(function, closure_context); \
RETURN_ZVAL(getThis(), 1, 0);\
} else {\
RETURN_FALSE;\
}\
}
-#define REDIS_ELSE_IF_PIPELINE(function) else IF_PIPELINE() { \
- REDIS_SAVE_CALLBACK(function); \
+#define REDIS_ELSE_IF_PIPELINE(function, closure_context) else IF_PIPELINE() { \
+ REDIS_SAVE_CALLBACK(function, closure_context); \
RETURN_ZVAL(getThis(), 1, 0);\
}
@@ -104,14 +105,17 @@ else if(redis_sock->mode == MULTI) { \
efree(cmd); \
}
-#define REDIS_PROCESS_RESPONSE(function) \
- REDIS_ELSE_IF_MULTI(function) \
- REDIS_ELSE_IF_PIPELINE(function);
+#define REDIS_PROCESS_RESPONSE_CLOSURE(function, closure_context) \
+ REDIS_ELSE_IF_MULTI(function, closure_context) \
+ REDIS_ELSE_IF_PIPELINE(function, closure_context);
+
+#define REDIS_PROCESS_RESPONSE(function) REDIS_PROCESS_RESPONSE_CLOSURE(function, NULL)
typedef enum {ATOMIC, MULTI, PIPELINE} redis_mode;
typedef struct fold_item {
zval * (*fun)(INTERNAL_FUNCTION_PARAMETERS, void *, ...);
+ void *ctx;
struct fold_item *next;
} fold_item;
View
@@ -356,7 +356,7 @@ redis_cmd_format(char **ret, char *format, ...) {
}
}
-PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
char *response;
int response_len;
@@ -375,7 +375,7 @@ PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *
}
}
-PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
char *response;
int response_len;
char ret;
@@ -405,7 +405,7 @@ PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
}
}
-PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
char *response;
int response_len;
char ret;
@@ -475,7 +475,7 @@ PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
}
}
-PHPAPI void redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI void redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
char *response;
int response_len;
@@ -507,7 +507,7 @@ PHPAPI void redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redi
}
}
-PHPAPI void redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval * z_tab) {
+PHPAPI void redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval * z_tab, void *ctx) {
char *response;
int response_len;
@@ -575,16 +575,16 @@ PHPAPI int redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PA
return 0;
}
-PHPAPI int redis_sock_read_multibulk_reply_zipped(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI int redis_sock_read_multibulk_reply_zipped(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
return redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, z_tab, 1);
}
-PHPAPI int redis_sock_read_multibulk_reply_zipped_strings(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI int redis_sock_read_multibulk_reply_zipped_strings(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
return redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, z_tab, 0);
}
-PHPAPI void redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI void redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
char *response;
int response_len;
@@ -616,7 +616,7 @@ PHPAPI void redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock
}
}
-PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab) {
+PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx) {
char *response;
int response_len;
@@ -771,7 +771,7 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
/**
* redis_sock_read_multibulk_reply
*/
-PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab)
+PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
{
char inbuf[1024], *response;
int response_len;
@@ -822,6 +822,50 @@ redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *re
}
/**
+ * redis_sock_read_multibulk_reply_assoc
+ */
+PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
+{
+ char inbuf[1024], *response;
+ int response_len;
+
+ zval **z_keys = ctx;
+
+ redis_check_eof(redis_sock TSRMLS_CC);
+ php_stream_gets(redis_sock->stream, inbuf, 1024);
+
+ if(inbuf[0] != '*') {
+ return -1;
+ }
+ int i, numElems = atoi(inbuf+1);
+ zval *z_multi_result;
+ MAKE_STD_ZVAL(z_multi_result);
+ array_init(z_multi_result); /* pre-allocate array for multi's results. */
+
+ for(i = 0; i < numElems; ++i) {
+ response = redis_sock_read(redis_sock, &response_len TSRMLS_CC);
+ if(response != NULL) {
+ add_assoc_stringl_ex(z_multi_result, Z_STRVAL_P(z_keys[i]), 1+Z_STRLEN_P(z_keys[i]), response, response_len, 1);
+ } else {
+ add_assoc_bool_ex(z_multi_result, Z_STRVAL_P(z_keys[i]), 1+Z_STRLEN_P(z_keys[i]), 0);
+ }
+ zval_dtor(z_keys[i]);
+ efree(z_keys[i]);
+ }
+ efree(z_keys);
+
+ zval *object = getThis();
+ IF_MULTI_OR_PIPELINE() {
+ add_next_index_zval(z_tab, z_multi_result);
+ }
+
+ *return_value = *z_multi_result;
+ //zval_copy_ctor(return_value);
+ // efree(z_multi_result);
+ return 0;
+}
+
+/**
* redis_sock_write
*/
PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz TSRMLS_DC)
View
@@ -5,23 +5,24 @@ int redis_cmd_format(char **ret, char *format, ...);
PHPAPI char * redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC);
-PHPAPI void redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
-PHPAPI void redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval* z_tab);
-PHPAPI void redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
-PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
-PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
-PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
-PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
+PHPAPI void redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI void redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval* z_tab, void *ctx);
+PHPAPI void redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, long timeout);
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);
PHPAPI zval *redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock);
PHPAPI char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes);
-PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *_z_tab);
+PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *_z_tab, void *ctx);
PHPAPI int redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, int numElems);
-PHPAPI int redis_sock_read_multibulk_reply_zipped(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
-PHPAPI int redis_sock_read_multibulk_reply_zipped_strings(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
+PHPAPI int redis_sock_read_multibulk_reply_zipped(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI int redis_sock_read_multibulk_reply_zipped_strings(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz);
PHPAPI void redis_check_eof(RedisSock *redis_sock TSRMLS_DC);
//PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC);
View
@@ -110,6 +110,7 @@ PHP_METHOD(Redis, hGetAll);
PHP_METHOD(Redis, hExists);
PHP_METHOD(Redis, hIncrBy);
PHP_METHOD(Redis, hMset);
+PHP_METHOD(Redis, hMget);
PHP_METHOD(Redis, multi);
PHP_METHOD(Redis, discard);
Oops, something went wrong. Retry.

0 comments on commit c67e2dd

Please sign in to comment.