Skip to content

Commit b8cce7c

Browse files
committed
fix code style, fix #4006
1 parent f142924 commit b8cce7c

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

ext-src/swoole_redis_coro.cc

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,30 @@ ZEND_END_ARG_INFO()
847847
#define IS_EX_PX_ARG(a) (IS_EX_ARG(a) || IS_PX_ARG(a))
848848
#define IS_NX_XX_ARG(a) (IS_NX_ARG(a) || IS_XX_ARG(a))
849849

850+
struct RedisClient {
851+
redisContext *context;
852+
struct {
853+
bool auth;
854+
long db_num;
855+
bool subscribe;
856+
} session;
857+
double connect_timeout;
858+
double timeout;
859+
bool serialize;
860+
bool defer;
861+
uint8_t reconnect_interval;
862+
uint8_t reconnected_count;
863+
bool auth;
864+
bool compatibility_mode;
865+
long database;
866+
zval *zobject;
867+
zval _zobject;
868+
zend_object std;
869+
};
870+
850871
#define SW_REDIS_COMMAND_CHECK \
851872
Coroutine::get_current_safe(); \
852-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
873+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
853874

854875
#define SW_REDIS_COMMAND_ARGV_FILL(str, str_len) \
855876
argvlen[i] = str_len; \
@@ -893,43 +914,16 @@ ZEND_END_ARG_INFO()
893914
efree(argv); \
894915
}
895916

896-
typedef struct {
897-
redisContext *context;
898-
struct {
899-
bool auth;
900-
long db_num;
901-
bool subscribe;
902-
} session;
903-
double connect_timeout;
904-
double timeout;
905-
bool serialize;
906-
bool defer;
907-
uint8_t reconnect_interval;
908-
uint8_t reconnected_count;
909-
bool auth;
910-
bool compatibility_mode;
911-
long database;
912-
zval *zobject;
913-
zval _zobject;
914-
zend_object std;
915-
} swRedisClient;
916-
917-
typedef struct {
918-
zval _value;
919-
zval *value;
920-
swRedisClient *redis;
921-
} swRedis_result;
922-
923917
enum { SW_REDIS_MODE_MULTI, SW_REDIS_MODE_PIPELINE };
924918

925-
static void swoole_redis_coro_parse_result(swRedisClient *redis, zval *return_value, redisReply *reply);
919+
static void swoole_redis_coro_parse_result(RedisClient *redis, zval *return_value, redisReply *reply);
926920

927-
static sw_inline swRedisClient *php_swoole_redis_coro_fetch_object(zend_object *obj) {
928-
return (swRedisClient *) ((char *) obj - swoole_redis_coro_handlers.offset);
921+
static sw_inline RedisClient *php_swoole_redis_coro_fetch_object(zend_object *obj) {
922+
return (RedisClient *) ((char *) obj - swoole_redis_coro_handlers.offset);
929923
}
930924

931-
static sw_inline swRedisClient *php_swoole_get_redis_client(zval *zobject) {
932-
swRedisClient *redis = (swRedisClient *) php_swoole_redis_coro_fetch_object(Z_OBJ_P(zobject));
925+
static sw_inline RedisClient *php_swoole_get_redis_client(zval *zobject) {
926+
RedisClient *redis = (RedisClient *) php_swoole_redis_coro_fetch_object(Z_OBJ_P(zobject));
933927
if (UNEXPECTED(!redis)) {
934928
php_swoole_fatal_error(E_ERROR, "you must call Redis constructor first");
935929
}
@@ -943,7 +937,7 @@ static sw_inline Socket *swoole_redis_coro_get_socket(redisContext *context) {
943937
return nullptr;
944938
}
945939

946-
static sw_inline bool swoole_redis_coro_close(swRedisClient *redis) {
940+
static sw_inline bool swoole_redis_coro_close(RedisClient *redis) {
947941
if (redis->context) {
948942
int sockfd = redis->context->fd;
949943
Socket *socket = swoole_redis_coro_get_socket(redis->context);
@@ -963,7 +957,7 @@ static sw_inline bool swoole_redis_coro_close(swRedisClient *redis) {
963957
}
964958

965959
static void php_swoole_redis_coro_free_object(zend_object *object) {
966-
swRedisClient *redis = php_swoole_redis_coro_fetch_object(object);
960+
RedisClient *redis = php_swoole_redis_coro_fetch_object(object);
967961

968962
if (redis && redis->context) {
969963
swoole_redis_coro_close(redis);
@@ -973,7 +967,7 @@ static void php_swoole_redis_coro_free_object(zend_object *object) {
973967
}
974968

975969
static zend_object *php_swoole_redis_coro_create_object(zend_class_entry *ce) {
976-
swRedisClient *redis = (swRedisClient *) zend_object_alloc(sizeof(swRedisClient), ce);
970+
RedisClient *redis = (RedisClient *) zend_object_alloc(sizeof(RedisClient), ce);
977971
zend_object_std_init(&redis->std, ce);
978972
object_properties_init(&redis->std, ce);
979973
redis->std.handlers = &swoole_redis_coro_handlers;
@@ -1027,12 +1021,12 @@ static sw_inline void swoole_redis_handle_assoc_array_result(zval *return_value,
10271021
RETVAL_ZVAL(&zret, 1, 1);
10281022
}
10291023

1030-
static bool redis_auth(swRedisClient *redis, char *pw, size_t pw_len);
1031-
static bool redis_select_db(swRedisClient *redis, long db_number);
1024+
static bool redis_auth(RedisClient *redis, char *pw, size_t pw_len);
1025+
static bool redis_select_db(RedisClient *redis, long db_number);
10321026
static void redis_request(
1033-
swRedisClient *redis, int argc, char **argv, size_t *argvlen, zval *return_value, bool retry = false);
1027+
RedisClient *redis, int argc, char **argv, size_t *argvlen, zval *return_value, bool retry = false);
10341028

1035-
static bool swoole_redis_coro_connect(swRedisClient *redis) {
1029+
static bool swoole_redis_coro_connect(RedisClient *redis) {
10361030
zval *zobject = redis->zobject;
10371031
redisContext *context;
10381032
Socket *socket;
@@ -1134,7 +1128,7 @@ static bool swoole_redis_coro_connect(swRedisClient *redis) {
11341128
return true;
11351129
}
11361130

1137-
static sw_inline bool swoole_redis_coro_keep_liveness(swRedisClient *redis) {
1131+
static sw_inline bool swoole_redis_coro_keep_liveness(RedisClient *redis) {
11381132
Socket *socket = nullptr;
11391133
if (!redis->context || !(socket = swoole_redis_coro_get_socket(redis->context)) || !socket->check_liveness()) {
11401134
if (socket) {
@@ -1161,7 +1155,7 @@ static sw_inline bool swoole_redis_coro_keep_liveness(swRedisClient *redis) {
11611155
return true;
11621156
}
11631157

1164-
static bool redis_auth(swRedisClient *redis, char *pw, size_t pw_len) {
1158+
static bool redis_auth(RedisClient *redis, char *pw, size_t pw_len) {
11651159
int i = 0;
11661160
size_t argvlen[2];
11671161
char *argv[2];
@@ -1178,7 +1172,7 @@ static bool redis_auth(swRedisClient *redis, char *pw, size_t pw_len) {
11781172
return ret;
11791173
}
11801174

1181-
static bool redis_select_db(swRedisClient *redis, long db_number) {
1175+
static bool redis_select_db(RedisClient *redis, long db_number) {
11821176
int i = 0;
11831177
size_t argvlen[2];
11841178
char *argv[2];
@@ -1198,7 +1192,7 @@ static bool redis_select_db(swRedisClient *redis, long db_number) {
11981192
}
11991193

12001194
static void redis_request(
1201-
swRedisClient *redis, int argc, char **argv, size_t *argvlen, zval *return_value, bool retry) {
1195+
RedisClient *redis, int argc, char **argv, size_t *argvlen, zval *return_value, bool retry) {
12021196
redisReply *reply = nullptr;
12031197
if (!swoole_redis_coro_keep_liveness(redis)) {
12041198
ZVAL_FALSE(return_value);
@@ -1261,8 +1255,7 @@ static void redis_request(
12611255
}
12621256
}
12631257
}
1264-
int i;
1265-
for (i = 0; i < argc; i++) {
1258+
SW_LOOP_N(argc) {
12661259
efree(argv[i]);
12671260
}
12681261
}
@@ -1907,7 +1900,7 @@ void php_swoole_redis_coro_minit(int module_number) {
19071900
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_redis_coro, sw_zend_class_unset_property_deny);
19081901
SW_SET_CLASS_CREATE_WITH_ITS_OWN_HANDLERS(swoole_redis_coro);
19091902
SW_SET_CLASS_CUSTOM_OBJECT(
1910-
swoole_redis_coro, php_swoole_redis_coro_create_object, php_swoole_redis_coro_free_object, swRedisClient, std);
1903+
swoole_redis_coro, php_swoole_redis_coro_create_object, php_swoole_redis_coro_free_object, RedisClient, std);
19111904

19121905
zend_declare_property_string(swoole_redis_coro_ce, ZEND_STRL("host"), "", ZEND_ACC_PUBLIC);
19131906
zend_declare_property_long(swoole_redis_coro_ce, ZEND_STRL("port"), 0, ZEND_ACC_PUBLIC);
@@ -1938,7 +1931,7 @@ void php_swoole_redis_coro_minit(int module_number) {
19381931
SW_REGISTER_LONG_CONSTANT("SWOOLE_REDIS_ERR_ALLOC", SW_REDIS_ERR_ALLOC);
19391932
}
19401933

1941-
static void swoole_redis_coro_set_options(swRedisClient *redis, zval *zoptions, bool backward_compatibility = false) {
1934+
static void swoole_redis_coro_set_options(RedisClient *redis, zval *zoptions, bool backward_compatibility = false) {
19421935
zval *zsettings =
19431936
sw_zend_read_and_convert_property_array(swoole_redis_coro_ce, redis->zobject, ZEND_STRL("setting"), 0);
19441937
HashTable *vht = Z_ARRVAL_P(zoptions);
@@ -1979,7 +1972,7 @@ static void swoole_redis_coro_set_options(swRedisClient *redis, zval *zoptions,
19791972
}
19801973

19811974
static PHP_METHOD(swoole_redis_coro, __construct) {
1982-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
1975+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
19831976
zval *zsettings = sw_zend_read_and_convert_property_array(swoole_redis_coro_ce, ZEND_THIS, ZEND_STRL("setting"), 0);
19841977
zval *zset = nullptr;
19851978

@@ -2044,7 +2037,7 @@ static PHP_METHOD(swoole_redis_coro, connect) {
20442037
}
20452038

20462039
static PHP_METHOD(swoole_redis_coro, getAuth) {
2047-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
2040+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
20482041
if (redis->session.auth) {
20492042
zval *ztmp = sw_zend_read_and_convert_property_array(swoole_redis_coro_ce, ZEND_THIS, ZEND_STRL("setting"), 0);
20502043
if (php_swoole_array_get_value(Z_ARRVAL_P(ztmp), "password", ztmp)) {
@@ -2056,7 +2049,7 @@ static PHP_METHOD(swoole_redis_coro, getAuth) {
20562049
}
20572050

20582051
static PHP_METHOD(swoole_redis_coro, getDBNum) {
2059-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
2052+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
20602053
if (!redis->context) {
20612054
RETURN_FALSE;
20622055
}
@@ -2069,7 +2062,7 @@ static PHP_METHOD(swoole_redis_coro, getOptions) {
20692062
}
20702063

20712064
static PHP_METHOD(swoole_redis_coro, setOptions) {
2072-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
2065+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
20732066
zval *zoptions;
20742067

20752068
ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -2082,13 +2075,13 @@ static PHP_METHOD(swoole_redis_coro, setOptions) {
20822075
}
20832076

20842077
static PHP_METHOD(swoole_redis_coro, getDefer) {
2085-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
2078+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
20862079

20872080
RETURN_BOOL(redis->defer);
20882081
}
20892082

20902083
static PHP_METHOD(swoole_redis_coro, setDefer) {
2091-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
2084+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
20922085
zend_bool defer = 1;
20932086

20942087
if (redis->session.subscribe) {
@@ -2165,7 +2158,7 @@ static PHP_METHOD(swoole_redis_coro, recv) {
21652158
}
21662159

21672160
static PHP_METHOD(swoole_redis_coro, close) {
2168-
swRedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
2161+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
21692162
RETURN_BOOL(swoole_redis_coro_close(redis));
21702163
}
21712164

@@ -3890,6 +3883,11 @@ static PHP_METHOD(swoole_redis_coro, hMGet) {
38903883

38913884
static PHP_METHOD(swoole_redis_coro, hExists) {
38923885
sw_redis_command_key_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRL("HEXISTS"));
3886+
3887+
RedisClient *redis = php_swoole_get_redis_client(ZEND_THIS);
3888+
if (redis->compatibility_mode && ZVAL_IS_LONG(return_value)) {
3889+
RETURN_BOOL(zval_get_long(return_value) );
3890+
}
38933891
}
38943892

38953893
static PHP_METHOD(swoole_redis_coro, publish) {
@@ -4406,7 +4404,7 @@ static PHP_METHOD(swoole_redis_coro, script) {
44064404
}
44074405
}
44084406

4409-
static void swoole_redis_coro_parse_result(swRedisClient *redis, zval *return_value, redisReply *reply) {
4407+
static void swoole_redis_coro_parse_result(RedisClient *redis, zval *return_value, redisReply *reply) {
44104408
int j;
44114409
zval _val, *val = &_val;
44124410

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
swoole_redis_coro: hExists
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../../include/bootstrap.php';
8+
9+
const KEY = 'hkey';
10+
11+
Co\run(function() {
12+
$redis = new Swoole\Coroutine\Redis();
13+
$redis->setOptions(['compatibility_mode' => true]);
14+
$redis->connect(REDIS_SERVER_HOST, REDIS_SERVER_PORT);
15+
16+
$redis->delete(KEY);
17+
$redis->hSet(KEY, 'field', 'val1');
18+
19+
Assert::true($redis->hExists(KEY, 'field') === true);
20+
Assert::true($redis->hExists(KEY, 'field_not_found') === false);
21+
});
22+
?>
23+
--EXPECT--

0 commit comments

Comments
 (0)