@@ -693,52 +693,6 @@ ra_is_write_cmd(RedisArray *ra, const char *cmd, int cmd_len) {
693693 return !ret ;
694694}
695695
696- /* list keys from array index */
697- static long
698- ra_rehash_scan (zval * z_redis , char * * * keys , int * * key_lens , const char * cmd , const char * arg TSRMLS_DC ) {
699-
700- long count , i ;
701- zval z_fun_smembers , z_ret , z_arg [1 ], * z_data_p ;
702- HashTable * h_keys ;
703- char * key ;
704- int key_len ;
705-
706- /* arg */
707- ZVAL_STRING (& z_arg [0 ], arg );
708-
709- /* run SMEMBERS */
710- ZVAL_STRING (& z_fun_smembers , cmd );
711- call_user_function (& redis_ce -> function_table , z_redis , & z_fun_smembers , & z_ret , 1 , z_arg );
712- zval_dtor (& z_fun_smembers );
713- zval_dtor (& z_arg [0 ]);
714- if (Z_TYPE (z_ret ) != IS_ARRAY ) { /* failure */
715- zval_dtor (& z_ret );
716- return -1 ; /* TODO: log error. */
717- }
718- h_keys = Z_ARRVAL (z_ret );
719-
720- /* allocate key array */
721- count = zend_hash_num_elements (h_keys );
722- * keys = emalloc (count * sizeof (char * ));
723- * key_lens = emalloc (count * sizeof (int ));
724-
725- i = 0 ;
726- ZEND_HASH_FOREACH_VAL (h_keys , z_data_p ) {
727- key = Z_STRVAL_P (z_data_p );
728- key_len = Z_STRLEN_P (z_data_p );
729-
730- /* copy key and length */
731- (* keys )[i ] = estrndup (key , key_len );
732- (* key_lens )[i ] = key_len ;
733- i ++ ;
734- } ZEND_HASH_FOREACH_END ();
735-
736- /* cleanup */
737- zval_dtor (& z_ret );
738-
739- return count ;
740- }
741-
742696/* run TYPE to find the type */
743697static zend_bool
744698ra_get_key_type (zval * z_redis , const char * key , int key_len , zval * z_from , long * res TSRMLS_DC ) {
@@ -1184,42 +1138,52 @@ static void
11841138ra_rehash_server (RedisArray * ra , zval * z_redis , const char * hostname , zend_bool b_index ,
11851139 zend_fcall_info * z_cb , zend_fcall_info_cache * z_cb_cache TSRMLS_DC ) {
11861140
1187- char * * keys ;
1188- long count , i ;
1189- int * key_lens , target_pos ;
1190- zval * z_target ;
1141+ HashTable * h_keys ;
1142+ long count = 0 ;
1143+ zval z_fun , z_ret , z_argv , * z_ele ;
11911144
1192- /* list all keys */
1193- if (b_index ) {
1194- count = ra_rehash_scan (z_redis , & keys , & key_lens , "SMEMBERS" , PHPREDIS_INDEX_NAME TSRMLS_CC );
1195- } else {
1196- count = ra_rehash_scan (z_redis , & keys , & key_lens , "KEYS" , "*" TSRMLS_CC );
1197- }
1145+ /* list all keys */
1146+ if (b_index ) {
1147+ ZVAL_STRING (& z_fun , "SMEMBERS" );
1148+ ZVAL_STRING (& z_argv , PHPREDIS_INDEX_NAME );
1149+ } else {
1150+ ZVAL_STRING (& z_fun , "KEYS" );
1151+ ZVAL_STRING (& z_argv , "*" );
1152+ }
1153+ ZVAL_NULL (& z_ret );
1154+ call_user_function (& redis_ce -> function_table , z_redis , & z_fun , & z_ret , 1 , & z_argv );
1155+ zval_dtor (& z_argv );
1156+ zval_dtor (& z_fun );
11981157
1199- if (count < 0 ) return ;
1158+ if (Z_TYPE (z_ret ) == IS_ARRAY ) {
1159+ h_keys = Z_ARRVAL (z_ret );
1160+ count = zend_hash_num_elements (h_keys );
1161+ }
1162+
1163+ if (!count ) {
1164+ zval_dtor (& z_ret );
1165+ return ;
1166+ }
12001167
12011168 /* callback */
12021169 if (z_cb && z_cb_cache ) {
12031170 zval_rehash_callback (z_cb , z_cb_cache , hostname , count TSRMLS_CC );
12041171 }
12051172
1206- /* for each key, redistribute */
1207- for (i = 0 ; i < count ; ++ i ) {
1173+ /* for each key, redistribute */
1174+ ZEND_HASH_FOREACH_VAL (h_keys , z_ele ) {
1175+ int pos = 0 ;
1176+ /* check that we're not moving to the same node. */
1177+ zval * z_target = ra_find_node (ra , Z_STRVAL_P (z_ele ), Z_STRLEN_P (z_ele ), & pos TSRMLS_CC );
12081178
1209- /* check that we're not moving to the same node. */
1210- z_target = ra_find_node (ra , keys [i ], key_lens [i ], & target_pos TSRMLS_CC );
1211-
1212- if (z_target && strcmp (hostname , ra -> hosts [target_pos ])) { /* different host */
1213- /* php_printf("move [%s] from [%s] to [%s]\n", keys[i], hostname, ra->hosts[target_pos]); */
1214- ra_move_key (keys [i ], key_lens [i ], z_redis , z_target TSRMLS_CC );
1215- }
1179+ if (z_target && strcmp (hostname , ra -> hosts [pos ])) { /* different host */
1180+ ra_move_key (Z_STRVAL_P (z_ele ), Z_STRLEN_P (z_ele ), z_redis , z_target TSRMLS_CC );
1181+ }
12161182
1217- /* cleanup */
1218- efree (keys [i ]);
1219- }
1183+ } ZEND_HASH_FOREACH_END ();
12201184
1221- efree ( keys );
1222- efree ( key_lens );
1185+ /* cleanup */
1186+ zval_dtor ( & z_ret );
12231187}
12241188
12251189void
0 commit comments