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

Skip to content

Commit 658ee37

Browse files
committed
ZEND_HASH_FOREACH_PTR
1 parent e672f40 commit 658ee37

4 files changed

Lines changed: 44 additions & 61 deletions

File tree

cluster_library.c

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ extern zend_class_entry *redis_cluster_exception_ce;
1212
static void cluster_dump_nodes(redisCluster *c) {
1313
redisClusterNode *p;
1414
15-
for(zend_hash_internal_pointer_reset(c->nodes);
16-
zend_hash_has_more_elements(c->nodes)==SUCCESS;
17-
zend_hash_move_forward(c->nodes))
18-
{
19-
if ((p = zend_hash_get_current_data_ptr(c->nodes)) == NULL) {
15+
ZEND_HASH_FOREACH_PTR(c->nodes, p) {
16+
if (p == NULL) {
2017
continue;
2118
}
2219
@@ -25,7 +22,7 @@ static void cluster_dump_nodes(redisCluster *c) {
2522
p->slot);
2623
2724
php_printf("\n");
28-
}
25+
} ZEND_HASH_FOREACH_END();
2926
}
3027
3128
static void cluster_log(char *fmt, ...)
@@ -939,30 +936,24 @@ PHP_REDIS_API int cluster_map_keyspace(redisCluster *c TSRMLS_DC) {
939936
int mapped=0;
940937

941938
// Iterate over seeds until we can get slots
942-
for(zend_hash_internal_pointer_reset(c->seeds);
943-
!mapped && zend_hash_has_more_elements(c->seeds) == SUCCESS;
944-
zend_hash_move_forward(c->seeds))
945-
{
946-
// Grab the redis_sock for this seed
947-
if ((seed = zend_hash_get_current_data_ptr(c->seeds)) == NULL) {
948-
continue;
949-
}
950-
939+
ZEND_HASH_FOREACH_PTR(c->seeds, seed) {
951940
// Attempt to connect to this seed node
952-
if (redis_sock_connect(seed TSRMLS_CC) != 0) {
941+
if (seed == NULL || redis_sock_connect(seed TSRMLS_CC) != 0) {
953942
continue;
954943
}
955944

956945
// Parse out cluster nodes. Flag mapped if we are valid
957946
slots = cluster_get_slots(seed TSRMLS_CC);
958-
if(slots) mapped = !cluster_map_slots(c, slots);
959-
960-
// Bin anything mapped, if we failed somewhere
961-
if(!mapped && slots) {
962-
memset(c->master, 0, sizeof(redisClusterNode*)*REDIS_CLUSTER_SLOTS);
947+
if (slots) {
948+
mapped = !cluster_map_slots(c, slots);
949+
// Bin anything mapped, if we failed somewhere
950+
if (!mapped) {
951+
memset(c->master, 0, sizeof(redisClusterNode*)*REDIS_CLUSTER_SLOTS);
952+
}
963953
}
964954
redis_sock_disconnect(seed TSRMLS_CC);
965-
}
955+
if (mapped) break;
956+
} ZEND_HASH_FOREACH_END();
966957

967958
// Clean up slots reply if we got one
968959
if(slots) cluster_free_reply(slots, 1);
@@ -1081,13 +1072,11 @@ static int cluster_check_response(redisCluster *c, REDIS_REPLY_TYPE *reply_type
10811072
PHP_REDIS_API void cluster_disconnect(redisCluster *c TSRMLS_DC) {
10821073
redisClusterNode *node;
10831074

1084-
for(zend_hash_internal_pointer_reset(c->nodes);
1085-
(node = zend_hash_get_current_data_ptr(c->nodes)) != NULL;
1086-
zend_hash_move_forward(c->nodes))
1087-
{
1075+
ZEND_HASH_FOREACH_PTR(c->nodes, node) {
1076+
if (node == NULL) break;
10881077
redis_sock_disconnect(node->sock TSRMLS_CC);
10891078
node->sock->lazy_connect = 1;
1090-
}
1079+
} ZEND_HASH_FOREACH_END();
10911080
}
10921081

10931082
/* This method attempts to write our command at random to the master and any
@@ -1219,17 +1208,9 @@ static int cluster_sock_write(redisCluster *c, const char *cmd, size_t sz,
12191208
if(direct) return -1;
12201209

12211210
/* Fall back by attempting the request against every known node */
1222-
for(zend_hash_internal_pointer_reset(c->nodes);
1223-
zend_hash_has_more_elements(c->nodes)==SUCCESS;
1224-
zend_hash_move_forward(c->nodes))
1225-
{
1226-
/* Grab node */
1227-
if ((seed_node = zend_hash_get_current_data_ptr(c->nodes)) == NULL) {
1228-
continue;
1229-
}
1230-
1211+
ZEND_HASH_FOREACH_PTR(c->nodes, seed_node) {
12311212
/* Skip this node if it's the one that failed, or if it's a slave */
1232-
if(seed_node->sock == redis_sock || seed_node->slave) continue;
1213+
if (seed_node == NULL || seed_node->sock == redis_sock || seed_node->slave) continue;
12331214

12341215
/* Connect to this node if we haven't already */
12351216
CLUSTER_LAZY_CONNECT(seed_node->sock);
@@ -1240,7 +1221,7 @@ static int cluster_sock_write(redisCluster *c, const char *cmd, size_t sz,
12401221
c->cmd_sock = seed_node->sock;
12411222
return 0;
12421223
}
1243-
}
1224+
} ZEND_HASH_FOREACH_END();
12441225

12451226
/* We were unable to write to any node in our cluster */
12461227
return -1;

common.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ typedef struct {
6363
) { \
6464
_val = zend_hash_get_current_data_ex(ht, &_hpos); \
6565

66+
#define ZEND_HASH_FOREACH_PTR(ht, _ptr) do { \
67+
HashPosition _hpos; \
68+
for (zend_hash_internal_pointer_reset_ex(ht, &_hpos); \
69+
zend_hash_has_more_elements_ex(ht, &_hpos) == SUCCESS; \
70+
zend_hash_move_forward_ex(ht, &_hpos) \
71+
) { \
72+
_ptr = zend_hash_get_current_data_ptr_ex(ht, &_hpos); \
73+
6674
#define ZEND_HASH_FOREACH_END() \
6775
} \
6876
} while(0)
@@ -126,15 +134,16 @@ zend_hash_get_current_data(HashTable *ht)
126134
}
127135

128136
static zend_always_inline void *
129-
zend_hash_get_current_data_ptr(HashTable *ht)
137+
zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
130138
{
131139
void **ptr;
132140

133-
if (zend_hash_get_current_data_ex(ht, (void **)&ptr, NULL) == SUCCESS) {
141+
if (zend_hash_get_current_data_ex(ht, (void **)&ptr, pos) == SUCCESS) {
134142
return *ptr;
135143
}
136144
return NULL;
137145
}
146+
#define zend_hash_get_current_data_ptr(ht) zend_hash_get_current_data_ptr_ex(ht, NULL)
138147

139148
static int (*_zend_hash_index_find)(const HashTable *, ulong, void **) = &zend_hash_index_find;
140149
#define zend_hash_index_find(ht, h) inline_zend_hash_index_find(ht, h)

redis_cluster.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,8 @@ PHP_METHOD(RedisCluster, keys) {
10371037
c->readonly = CLUSTER_IS_ATOMIC(c);
10381038

10391039
/* Iterate over our known nodes */
1040-
for(zend_hash_internal_pointer_reset(c->nodes);
1041-
(node = zend_hash_get_current_data_ptr(c->nodes)) != NULL;
1042-
zend_hash_move_forward(c->nodes))
1043-
{
1040+
ZEND_HASH_FOREACH_PTR(c->nodes, node) {
1041+
if (node == NULL) break;
10441042
if (cluster_send_slot(c, node->slot, cmd, cmd_len, TYPE_MULTIBULK
10451043
TSRMLS_CC)<0)
10461044
{
@@ -1072,7 +1070,7 @@ PHP_METHOD(RedisCluster, keys) {
10721070

10731071
/* Free response, don't free data */
10741072
cluster_free_reply(resp, 0);
1075-
}
1073+
} ZEND_HASH_FOREACH_END();
10761074

10771075
efree(cmd);
10781076

@@ -1969,10 +1967,8 @@ PHP_METHOD(RedisCluster, _masters) {
19691967

19701968
array_init(z_ret);
19711969

1972-
for(zend_hash_internal_pointer_reset(c->nodes);
1973-
(node = zend_hash_get_current_data_ptr(c->nodes)) != NULL;
1974-
zend_hash_move_forward(c->nodes))
1975-
{
1970+
ZEND_HASH_FOREACH_PTR(c->nodes, node) {
1971+
if (node == NULL) break;
19761972
host = node->sock->host;
19771973
port = node->sock->port;
19781974

@@ -1985,7 +1981,7 @@ PHP_METHOD(RedisCluster, _masters) {
19851981
add_next_index_stringl(z_sub, host, strlen(host));
19861982
add_next_index_long(z_sub, port);
19871983
add_next_index_zval(z_ret, z_sub);
1988-
}
1984+
} ZEND_HASH_FOREACH_END();
19891985

19901986
RETVAL_ZVAL(z_ret, 1, 0);
19911987
}
@@ -2073,18 +2069,17 @@ PHP_METHOD(RedisCluster, watch) {
20732069
}
20742070

20752071
// Iterate over each node we'll be sending commands to
2076-
for(zend_hash_internal_pointer_reset(ht_dist);
2077-
zend_hash_get_current_key(ht_dist, NULL, &slot) == HASH_KEY_IS_LONG;
2078-
zend_hash_move_forward(ht_dist))
2079-
{
2072+
ZEND_HASH_FOREACH_PTR(ht_dist, dl) {
20802073
// Grab the clusterDistList pointer itself
2081-
if ((dl = zend_hash_get_current_data_ptr(ht_dist)) == NULL) {
2074+
if (dl == NULL) {
20822075
zend_throw_exception(redis_cluster_exception_ce,
20832076
"Internal error in a PHP HashTable", 0 TSRMLS_CC);
20842077
cluster_dist_free(ht_dist);
20852078
efree(z_args);
20862079
efree(cmd.c);
20872080
RETURN_FALSE;
2081+
} else if (zend_hash_get_current_key(ht_dist, NULL, &slot) != HASH_KEY_IS_LONG) {
2082+
break;
20882083
}
20892084

20902085
// Construct our watch command for this node
@@ -2104,7 +2099,7 @@ PHP_METHOD(RedisCluster, watch) {
21042099

21052100
// Zero out our command buffer
21062101
cmd.len = 0;
2107-
}
2102+
} ZEND_HASH_FOREACH_END();
21082103

21092104
// Cleanup
21102105
cluster_dist_free(ht_dist);

redis_cluster.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@
5353
/* Reset anything flagged as MULTI */
5454
#define CLUSTER_RESET_MULTI(c) \
5555
redisClusterNode *_node; \
56-
for(zend_hash_internal_pointer_reset(c->nodes); \
57-
(_node = zend_hash_get_current_data_ptr(c->nodes)) != NULL; \
58-
zend_hash_move_forward(c->nodes)) \
59-
{ \
56+
ZEND_HASH_FOREACH_PTR(c->nodes, _node) { \
57+
if (_node == NULL) break; \
6058
_node->sock->watching = 0; \
6159
_node->sock->mode = ATOMIC; \
62-
} \
60+
} ZEND_HASH_FOREACH_END(); \
6361
c->flags->watching = 0; \
6462
c->flags->mode = ATOMIC; \
6563

0 commit comments

Comments
 (0)