From b695a9c8071e252b86eec52560f4e1fd164589f0 Mon Sep 17 00:00:00 2001 From: "bostin.wang" Date: Thu, 6 May 2021 18:51:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=BE=85=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=9A=84redis=20command=E4=B8=BAtodo=5Fcmd;=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0bitpos/bitop/setbit/getbit/incrby/decrby/incr?= =?UTF-8?q?byfloat/setrange/getrange/mset/msetnx/eval=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- e2e/tests/redis.php | 19 ++ src/sky_plugin_redis.cc | 428 +++++++++++++++++++++++++++++++++++----- src/sky_plugin_redis.h | 22 +++ 3 files changed, 421 insertions(+), 48 deletions(-) diff --git a/e2e/tests/redis.php b/e2e/tests/redis.php index 184a28e6..d3d25ef1 100644 --- a/e2e/tests/redis.php +++ b/e2e/tests/redis.php @@ -3,6 +3,7 @@ function testRedis() { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); + $redis->select(1); $key = 'skywalking'; // strings @@ -10,23 +11,41 @@ function testRedis() { $redis->bitcount($key); $redis->bitcount($key, 0); $redis->bitcount($key, 0, 1); + $redis->bitpos($key, 0, 1, 2); + $redis->bitop('AND', $key, $key . '_01', $key . '_02', $key . '_03'); + $redis->setbit($key, 1, 1); + $redis->setbit($key, 1, 0); + $redis->setbit($key, 1, true); + $redis->setbit($key, 1, false); + $redis->getbit($key, 1); $redis->decr($key); $redis->get($key); $redis->getSet($key, "test"); $redis->incr($key); + $redis->incrBy($key, 10); + $redis->decrBy($key, 10); + $redis->incrByFloat($key, 10.5); $redis->setnx($key, "test"); $redis->strlen($key); $redis->set($key, "test"); $redis->set($key, "test", 100); $redis->set($key, "test", ["nx", "ex" => 200]); $redis->setex($key, 300, "test"); + $redis->setrange($key, 1, "222"); + $redis->getrange($key, 1, 4); // multiple keys $redis->mget([$key .'_1', $key . '_2', $key . '_3']); $redis->getMultiple([$key .'_1', $key . '_2', $key . '_3']); + // multiple key-value + $redis->mset([$key . '_1' => '111', $key . '_2' => '222', $key . '_3' => '333']); + $redis->msetnx([$key . '_1' => '111', $key . '_2' => '222', $key . '_3' => '333']); + // uncertain keys $redis->eval('return {1,2,3};'); + $redis->eval('return {1,2,3};', ['val_01', 'val_02']); + $redis->eval('return {1,2,3};', [$key . '_01', $key . '_02', 'val_01', 'val_02'], 2); $redis->del($key); $redis->del($key, $key . '_1'); $redis->del([$key, $key . '_1', $key . '_2']); diff --git a/src/sky_plugin_redis.cc b/src/sky_plugin_redis.cc index 21c9af30..3dbc39e7 100644 --- a/src/sky_plugin_redis.cc +++ b/src/sky_plugin_redis.cc @@ -15,59 +15,215 @@ #include "sky_plugin_redis.h" std::map commands = { - {"APPEND", sky_plugin_redis_key_value_cmd}, + // connection + {"SELECT", sky_plugin_redis_select_cmd}, + {"ECHO", sky_plugin_redis_key_cmd}, + {"PING", sky_plugin_redis_pure_cmd}, + {"PIPELINE", sky_plugin_redis_pure_cmd}, - {"BITCOUNT", sky_plugin_redis_bit_count_cmd}, - {"BITFIELD", sky_plugin_redis_todo_cmd}, - {"BITOP", sky_plugin_redis_todo_cmd}, - {"BITPOS", sky_plugin_redis_todo_cmd}, + // server + // @todo + // strings + {"APPEND", sky_plugin_redis_key_value_cmd}, + {"BITCOUNT", sky_plugin_redis_bit_count_cmd}, + {"BITOP", sky_plugin_redis_uncertain_keys_cmd}, + {"BITPOS", sky_plugin_redis_bit_pos_cmd}, {"DECR", sky_plugin_redis_key_cmd}, - {"DECRBY", sky_plugin_redis_todo_cmd}, - + {"DECRBY", sky_plugin_redis_key_int_cmd}, {"GET", sky_plugin_redis_key_cmd}, - {"GETBIT", sky_plugin_redis_todo_cmd}, - {"GETRANGE", sky_plugin_redis_todo_cmd}, + {"GETBIT", sky_plugin_redis_key_int_cmd}, + {"GETRANGE", sky_plugin_redis_get_range_cmd}, {"GETSET", sky_plugin_redis_key_value_cmd}, - {"INCR", sky_plugin_redis_key_cmd}, - {"INCRBY", sky_plugin_redis_todo_cmd}, - {"INCRBYFLOAT", sky_plugin_redis_todo_cmd}, - + {"INCRBY", sky_plugin_redis_key_int_cmd}, + {"INCRBYFLOAT", sky_plugin_redis_key_float_cmd}, {"MGET", sky_plugin_redis_multi_key_cmd}, - {"MSET", sky_plugin_redis_todo_cmd}, - {"MSETNX", sky_plugin_redis_todo_cmd}, - {"PSETEX", sky_plugin_redis_todo_cmd}, {"GETMULTIPLE", sky_plugin_redis_multi_key_cmd}, - + {"MSET", sky_plugin_redis_multi_key_value_cmd}, + {"MSETNX", sky_plugin_redis_multi_key_value_cmd}, {"SET", sky_plugin_redis_set_cmd}, - {"SETBIT", sky_plugin_redis_todo_cmd}, + {"SETBIT", sky_plugin_redis_set_bit_cmd}, {"SETEX", sky_plugin_redis_setex_cmd}, + {"PSETEX", sky_plugin_redis_psetex_cmd}, {"SETNX", sky_plugin_redis_key_value_cmd}, - {"SETRANGE", sky_plugin_redis_todo_cmd}, - - {"STRALGO", sky_plugin_redis_todo_cmd}, + {"SETRANGE", sky_plugin_redis_set_range_cmd}, {"STRLEN", sky_plugin_redis_key_cmd}, - {"EVAL", sky_plugin_redis_key_cmd}, - + // keys {"DEL", sky_plugin_redis_uncertain_keys_cmd}, {"DELETE", sky_plugin_redis_uncertain_keys_cmd}, {"UNLINK", sky_plugin_redis_uncertain_keys_cmd}, + {"DUMP", sky_plugin_redis_todo_cmd}, {"EXISTS", sky_plugin_redis_uncertain_keys_cmd}, - - // empty commands - {"PING", sky_plugin_redis_todo_cmd}, - {"PIPELINE", sky_plugin_redis_todo_cmd}, - {"MULTI", sky_plugin_redis_todo_cmd}, - {"DISCARD", sky_plugin_redis_todo_cmd}, - {"EXEC", sky_plugin_redis_todo_cmd}, - {"EXPIRE", sky_plugin_redis_key_ttl_cmd}, + {"SETTIMEOUT", sky_plugin_redis_key_ttl_cmd}, {"PEXPIRE", sky_plugin_redis_key_ttl_cmd}, {"EXPIREAT", sky_plugin_redis_key_ttl_cmd}, {"PEXPIREAT", sky_plugin_redis_key_ttl_cmd}, - {"SETTIMEOUT", sky_plugin_redis_key_ttl_cmd}, + {"KEYS", sky_plugin_redis_todo_cmd}, + {"GETKEYS", sky_plugin_redis_todo_cmd}, + {"SCAN", sky_plugin_redis_todo_cmd}, + {"MIGRATE", sky_plugin_redis_todo_cmd}, + {"MOVE", sky_plugin_redis_todo_cmd}, + {"OBJECT", sky_plugin_redis_todo_cmd}, + {"PERSIST", sky_plugin_redis_todo_cmd}, + {"RANDOMKEY", sky_plugin_redis_todo_cmd}, + {"RENAME", sky_plugin_redis_todo_cmd}, + {"RENAMEKEY", sky_plugin_redis_todo_cmd}, + {"RENAMENX", sky_plugin_redis_todo_cmd}, + {"TYPE", sky_plugin_redis_todo_cmd}, + {"SORT", sky_plugin_redis_todo_cmd}, + {"TTL", sky_plugin_redis_todo_cmd}, + {"PTTL", sky_plugin_redis_todo_cmd}, + {"RESTORE", sky_plugin_redis_todo_cmd}, + + // hashes + {"HDEL", sky_plugin_redis_todo_cmd}, + {"HEXISTS", sky_plugin_redis_todo_cmd}, + {"HGET", sky_plugin_redis_todo_cmd}, + {"HGETALL", sky_plugin_redis_todo_cmd}, + {"HINCRBY", sky_plugin_redis_todo_cmd}, + {"HINCRBYFLOAT",sky_plugin_redis_todo_cmd}, + {"HKEYS", sky_plugin_redis_todo_cmd}, + {"HLEN", sky_plugin_redis_todo_cmd}, + {"HMGET", sky_plugin_redis_todo_cmd}, + {"HMSET", sky_plugin_redis_todo_cmd}, + {"HSET", sky_plugin_redis_todo_cmd}, + {"HSETNX", sky_plugin_redis_todo_cmd}, + {"HVALS", sky_plugin_redis_todo_cmd}, + {"HSCAN", sky_plugin_redis_todo_cmd}, + {"HSTRLEN", sky_plugin_redis_todo_cmd}, + + // lists + {"BLPOP", sky_plugin_redis_todo_cmd}, + {"BRPOP", sky_plugin_redis_todo_cmd}, + {"BRPOPLPUSH", sky_plugin_redis_todo_cmd}, + {"LINDEX", sky_plugin_redis_todo_cmd}, + {"LGET", sky_plugin_redis_todo_cmd}, + {"LINSERT", sky_plugin_redis_todo_cmd}, + {"LLEN", sky_plugin_redis_todo_cmd}, + {"LSIZE", sky_plugin_redis_todo_cmd}, + {"LPOP", sky_plugin_redis_todo_cmd}, + {"LPUSH", sky_plugin_redis_todo_cmd}, + {"LPUSHX", sky_plugin_redis_todo_cmd}, + {"LRANGE", sky_plugin_redis_todo_cmd}, + {"LGETRANGE", sky_plugin_redis_todo_cmd}, + {"LREM", sky_plugin_redis_todo_cmd}, + {"LREMOVE", sky_plugin_redis_todo_cmd}, + {"LSET", sky_plugin_redis_todo_cmd}, + {"LTRIM", sky_plugin_redis_todo_cmd}, + {"LISTTRIM", sky_plugin_redis_todo_cmd}, + {"RPOP", sky_plugin_redis_todo_cmd}, + {"RPOPLPUSH", sky_plugin_redis_todo_cmd}, + {"RPUSH", sky_plugin_redis_todo_cmd}, + {"RPUSHX", sky_plugin_redis_todo_cmd}, + + // sets + {"SADD", sky_plugin_redis_todo_cmd}, + {"SCARD", sky_plugin_redis_todo_cmd}, + {"SSIZE", sky_plugin_redis_todo_cmd}, + {"SDIFF", sky_plugin_redis_todo_cmd}, + {"SDIFFSTORE", sky_plugin_redis_todo_cmd}, + {"SINTER", sky_plugin_redis_todo_cmd}, + {"SINTERSTORE", sky_plugin_redis_todo_cmd}, + {"SISMEMBER", sky_plugin_redis_todo_cmd}, + {"SCONTAINS", sky_plugin_redis_todo_cmd}, + {"SMEMBERS", sky_plugin_redis_todo_cmd}, + {"SGETMEMBERS", sky_plugin_redis_todo_cmd}, + {"SMOVE", sky_plugin_redis_todo_cmd}, + {"SPOP", sky_plugin_redis_todo_cmd}, + {"SRANDMEMBER", sky_plugin_redis_todo_cmd}, + {"SREM", sky_plugin_redis_todo_cmd}, + {"SREMOVE", sky_plugin_redis_todo_cmd}, + {"SUNION", sky_plugin_redis_todo_cmd}, + {"SUNIONSTORE", sky_plugin_redis_todo_cmd}, + {"SSCAN", sky_plugin_redis_todo_cmd}, + + // sorted sets + {"BZPOP", sky_plugin_redis_todo_cmd}, + {"ZADD", sky_plugin_redis_todo_cmd}, + {"ZCARD", sky_plugin_redis_todo_cmd}, + {"ZSIZE", sky_plugin_redis_todo_cmd}, + {"ZCOUNT", sky_plugin_redis_todo_cmd}, + {"ZINCRBY", sky_plugin_redis_todo_cmd}, + {"ZINTERSTORE", sky_plugin_redis_todo_cmd}, + {"ZINTER", sky_plugin_redis_todo_cmd}, + {"ZPOP", sky_plugin_redis_todo_cmd}, + {"ZRANGE", sky_plugin_redis_todo_cmd}, + {"ZRANGEBYSCORE", sky_plugin_redis_todo_cmd}, + {"ZREVRANGEBYSCORE", sky_plugin_redis_todo_cmd}, + {"ZRANGEBYLEX", sky_plugin_redis_todo_cmd}, + {"ZRANK", sky_plugin_redis_todo_cmd}, + {"ZREVRANK", sky_plugin_redis_todo_cmd}, + {"ZREM", sky_plugin_redis_todo_cmd}, + {"ZDLETE", sky_plugin_redis_todo_cmd}, + {"ZREMOVE", sky_plugin_redis_todo_cmd}, + {"ZREMRANGEBYRANK", sky_plugin_redis_todo_cmd}, + {"ZDELETERANGEBYRANK", sky_plugin_redis_todo_cmd}, + {"ZREMRANGEBYSCORE", sky_plugin_redis_todo_cmd}, + {"ZDELETERANGEBYSCORE", sky_plugin_redis_todo_cmd}, + {"ZREMOVERANGEBYSCORE", sky_plugin_redis_todo_cmd}, + {"ZREVRANGE", sky_plugin_redis_todo_cmd}, + {"ZSCORE", sky_plugin_redis_todo_cmd}, + {"ZUNIONSTORE", sky_plugin_redis_todo_cmd}, + {"ZUNION", sky_plugin_redis_todo_cmd}, + {"ZSCAN", sky_plugin_redis_todo_cmd}, + + // hyperLogLogs + {"PFADD", sky_plugin_redis_todo_cmd}, + {"PFCOUNT", sky_plugin_redis_todo_cmd}, + {"PFMERGE", sky_plugin_redis_todo_cmd}, + + // geocoding + {"GEOADD", sky_plugin_redis_todo_cmd}, + {"GEOHASH", sky_plugin_redis_todo_cmd}, + {"GEOPOS", sky_plugin_redis_todo_cmd}, + {"GEODIST", sky_plugin_redis_todo_cmd}, + {"GEORADIUS", sky_plugin_redis_todo_cmd}, + {"GEORADIUSBYMEMBER", sky_plugin_redis_todo_cmd}, + + // streams + {"XACK", sky_plugin_redis_todo_cmd}, + {"XADD", sky_plugin_redis_todo_cmd}, + {"XCLAIM", sky_plugin_redis_todo_cmd}, + {"XDEL", sky_plugin_redis_todo_cmd}, + {"XGROUP", sky_plugin_redis_todo_cmd}, + {"XINFO", sky_plugin_redis_todo_cmd}, + {"XLEN", sky_plugin_redis_todo_cmd}, + {"XPENDING", sky_plugin_redis_todo_cmd}, + {"XRANGE", sky_plugin_redis_todo_cmd}, + {"XREAD", sky_plugin_redis_todo_cmd}, + {"XREADGROUP", sky_plugin_redis_todo_cmd}, + {"XREVRANGE", sky_plugin_redis_todo_cmd}, + {"XTRIM", sky_plugin_redis_todo_cmd}, + + // pub/sub + {"PSUBSCRIBE", sky_plugin_redis_todo_cmd}, + {"PUBLISH", sky_plugin_redis_todo_cmd}, + {"SUBSCRIBE", sky_plugin_redis_todo_cmd}, + {"PUBSUB", sky_plugin_redis_todo_cmd}, + + + // generic + {"RAWCOMMAND", sky_plugin_redis_todo_cmd}, + + + // transactions + {"MULTI", sky_plugin_redis_pure_cmd}, + {"EXEC", sky_plugin_redis_pure_cmd}, + {"DISCARD", sky_plugin_redis_pure_cmd}, + {"WATCH", sky_plugin_redis_todo_cmd}, + {"UNWATCH", sky_plugin_redis_todo_cmd}, + + + // scripting + {"EVAL", sky_plugin_redis_eval_cmd}, + {"EVALSHA", sky_plugin_redis_eval_cmd}, + {"SCRIPT", sky_plugin_redis_todo_cmd}, + {"CLIENT", sky_plugin_redis_todo_cmd}, + {"GETLASTERROR", sky_plugin_redis_todo_cmd}, + {"CLEARLASTERROR", sky_plugin_redis_todo_cmd}, }; @@ -140,6 +296,17 @@ std::string sky_plugin_redis_key_cmd(zend_execute_data *execute_data, std::strin return ""; } +std::string sky_plugin_redis_select_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 1) { + zval *db = ZEND_CALL_ARG(execute_data, 1); + if (Z_TYPE_P(db) == IS_LONG) { + return cmd + " " + std::to_string(Z_LVAL_P(db)); + } + } + return ""; +} + std::string sky_plugin_redis_key_value_cmd(zend_execute_data *execute_data, std::string cmd) { uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); if (arg_count == 2) { @@ -163,10 +330,10 @@ std::string sky_plugin_redis_set_cmd(zend_execute_data *execute_data, std::strin zval *optional = ZEND_CALL_ARG(execute_data, 3); if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(value) == IS_STRING) { - std::string _cmd = cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::string(Z_STRVAL_P(value)); + cmd += " " + std::string(Z_STRVAL_P(key)) + " " + std::string(Z_STRVAL_P(value)); if (Z_TYPE_P(optional) == IS_LONG) { - return _cmd + " " + std::to_string(Z_LVAL_P(optional)); + cmd += " " + std::to_string(Z_LVAL_P(optional)); } if (Z_TYPE_P(optional) == IS_ARRAY) { @@ -175,13 +342,13 @@ std::string sky_plugin_redis_set_cmd(zend_execute_data *execute_data, std::strin zval *val; ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(optional), nk, sk, val) { if (sk == NULL && Z_TYPE_P(val) == IS_STRING) { - _cmd += " " + std::string(Z_STRVAL_P(val)); + cmd += " " + std::string(Z_STRVAL_P(val)); } else if (Z_TYPE_P(val) == IS_LONG) { - _cmd += " " + std::string(ZSTR_VAL(sk)) + " " + std::to_string(Z_LVAL_P(val)); + cmd += " " + std::string(ZSTR_VAL(sk)) + " " + std::to_string(Z_LVAL_P(val)); } } ZEND_HASH_FOREACH_END(); - return _cmd; } + return cmd; } } return ""; @@ -192,10 +359,113 @@ std::string sky_plugin_redis_setex_cmd(zend_execute_data *execute_data, std::str if (arg_count == 3) { zval *key = ZEND_CALL_ARG(execute_data, 1); zval *ttl = ZEND_CALL_ARG(execute_data, 2); + zval *val = ZEND_CALL_ARG(execute_data, 3); + + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(ttl) == IS_LONG && Z_TYPE_P(val) == IS_STRING) { + return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(ttl)) + " " + std::string(Z_STRVAL_P(val)); + } + } + return ""; +} + +std::string sky_plugin_redis_multi_key_value_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 1) { + zval *key_values = ZEND_CALL_ARG(execute_data, 1); + if (Z_TYPE_P(key_values) == IS_ARRAY) { + zend_ulong nk; + zend_string *sk; + zval *val; + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(key_values), nk, sk, val) { + if (sk != NULL && Z_TYPE_P(val) == IS_STRING) { + cmd += " " + std::string(ZSTR_VAL(sk)) + " " + std::string(Z_STRVAL_P(val)); + } + } ZEND_HASH_FOREACH_END(); + return cmd; + } + } + return ""; +} + +std::string sky_plugin_redis_psetex_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 3) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *ttl = ZEND_CALL_ARG(execute_data, 2); + zval *val = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(ttl) == IS_LONG && Z_TYPE_P(val) == IS_STRING) { + return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(ttl)) + " " + std::string(Z_STRVAL_P(val)); + } + } + return ""; +} + +std::string sky_plugin_redis_key_int_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 2) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *delta = ZEND_CALL_ARG(execute_data, 2); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(delta) == IS_LONG) { + return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(delta)); + } + } + return ""; +} + +std::string sky_plugin_redis_get_range_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 3) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *start = ZEND_CALL_ARG(execute_data, 2); + zval *end = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(start) == IS_LONG && Z_TYPE_P(end) == IS_LONG) { + return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(start)) + " " + std::to_string(Z_LVAL_P(end)); + } + } + return ""; +} + +std::string sky_plugin_redis_set_bit_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 3) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *offset = ZEND_CALL_ARG(execute_data, 2); + zval *val = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(offset) == IS_LONG && (Z_TYPE_P(val) == IS_LONG || Z_TYPE_P(val) == IS_TRUE || Z_TYPE_P(val) == IS_FALSE)) { + cmd += " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(offset)); + if (Z_TYPE_P(val) == IS_LONG) { + cmd += " " + std::to_string(Z_LVAL_P(val)); + } else if (Z_TYPE_P(val) == IS_TRUE) { + cmd += " 1"; + } else if (Z_TYPE_P(val) == IS_FALSE) { + cmd += " 0"; + } + return cmd; + } + } + return ""; +} + +std::string sky_plugin_redis_set_range_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 3) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *offset = ZEND_CALL_ARG(execute_data, 2); zval *value = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(offset) == IS_LONG && Z_TYPE_P(value) == IS_STRING) { + return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(offset)) + " " + std::string(Z_STRVAL_P(value)); + } + } + return ""; +} - if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(ttl) == IS_LONG && Z_TYPE_P(value) == IS_STRING) { - return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(ttl)) + " " + std::string(Z_STRVAL_P(value)); +std::string sky_plugin_redis_key_float_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count == 2) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *delta = ZEND_CALL_ARG(execute_data, 2); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(delta) == IS_DOUBLE) { + return cmd + " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_DVAL_P(delta)); } } return ""; @@ -206,15 +476,14 @@ std::string sky_plugin_redis_multi_key_cmd(zend_execute_data *execute_data, std: if (arg_count == 1) { zval *keys = ZEND_CALL_ARG(execute_data, 1); if (Z_TYPE_P(keys) == IS_ARRAY) { - std::string _cmd = cmd; zend_ulong index; zval *key; ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(keys), index, key) { if (Z_TYPE_P(key) == IS_STRING) { - _cmd += " " + std::string(Z_STRVAL_P(key)); + cmd += " " + std::string(Z_STRVAL_P(key)); } } ZEND_HASH_FOREACH_END(); - return _cmd; + return cmd; } } return ""; @@ -241,28 +510,26 @@ std::string sky_plugin_redis_uncertain_keys_cmd(zend_execute_data *execute_data, return cmd + " " + std::string(Z_STRVAL_P(keys)); } if (Z_TYPE_P(keys) == IS_ARRAY) { - std::string _cmd = cmd; zend_ulong index; zval *key; ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(keys), index, key) { if (Z_TYPE_P(key) == IS_STRING) { - _cmd += " " + std::string(Z_STRVAL_P(key)); + cmd += " " + std::string(Z_STRVAL_P(key)); } } ZEND_HASH_FOREACH_END(); - return _cmd; + return cmd; } } if (arg_count > 1) { uint32_t i = 0; - std::string _cmd = cmd; for (i = 1; i <= arg_count; i++) { zval *key = ZEND_CALL_ARG(execute_data, i); if (Z_TYPE_P(key) == IS_STRING) { - _cmd += " " + std::string(Z_STRVAL_P(key)); + cmd += " " + std::string(Z_STRVAL_P(key)); } } - return _cmd; + return cmd; } return ""; @@ -272,6 +539,10 @@ std::string sky_plugin_redis_todo_cmd(zend_execute_data *execute_data, std::stri return ""; } +std::string sky_plugin_redis_pure_cmd(zend_execute_data *execute_data, std::string cmd) { + return ""; +} + std::string sky_plugin_redis_bit_count_cmd(zend_execute_data *execute_data, std::string cmd) { uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); @@ -295,3 +566,64 @@ std::string sky_plugin_redis_bit_count_cmd(zend_execute_data *execute_data, std: } return ""; } + +std::string sky_plugin_redis_bit_pos_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count >= 2) { + zval *key = ZEND_CALL_ARG(execute_data, 1); + zval *bit = ZEND_CALL_ARG(execute_data, 2); + if (Z_TYPE_P(key) == IS_STRING && Z_TYPE_P(bit) == IS_LONG) { + cmd += " " + std::string(Z_STRVAL_P(key)) + " " + std::to_string(Z_LVAL_P(bit)); + if (arg_count >= 3) { + zval *start = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(start) == IS_LONG) { + cmd += " " + std::to_string(Z_LVAL_P(start)); + } + } + if (arg_count >= 4) { + zval *end = ZEND_CALL_ARG(execute_data, 4); + if (Z_TYPE_P(end) == IS_LONG) { + cmd += " " + std::to_string(Z_LVAL_P(end)); + } + } + return cmd; + } + } + return ""; +} + +std::string sky_plugin_redis_eval_cmd(zend_execute_data *execute_data, std::string cmd) { + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count >= 1) { + zval *script = ZEND_CALL_ARG(execute_data, 1); + if (Z_TYPE_P(script) == IS_STRING) { + cmd += " \"" + std::string(Z_STRVAL_P(script)) + "\""; + if (arg_count >= 2) { + zval *args = ZEND_CALL_ARG(execute_data, 2); + if (Z_TYPE_P(args) == IS_ARRAY) { + zend_ulong index; + zval *arg; + ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(args), index, arg) { + if (Z_TYPE_P(arg) == IS_STRING) { + cmd += " " + std::string(Z_STRVAL_P(arg)); + } + if (Z_TYPE_P(arg) == IS_LONG) { + cmd += " " + std::to_string(Z_LVAL_P(arg)); + } + if (Z_TYPE_P(arg) == IS_DOUBLE) { + cmd += " " + std::to_string(Z_DVAL_P(arg)); + } + } ZEND_HASH_FOREACH_END(); + } + } + if (arg_count >= 3) { + zval *num_keys = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(num_keys) == IS_LONG) { + cmd += " " + std::to_string(Z_LVAL_P(num_keys)); + } + } + return cmd; + } + } + return ""; +} \ No newline at end of file diff --git a/src/sky_plugin_redis.h b/src/sky_plugin_redis.h index aa97ef4e..432f9c3b 100644 --- a/src/sky_plugin_redis.h +++ b/src/sky_plugin_redis.h @@ -36,6 +36,20 @@ std::string sky_plugin_redis_set_cmd(zend_execute_data *execute_data, std::strin std::string sky_plugin_redis_setex_cmd(zend_execute_data *execute_data, std::string cmd); +std::string sky_plugin_redis_multi_key_value_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_key_int_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_key_float_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_get_range_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_set_bit_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_set_range_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_psetex_cmd(zend_execute_data *execute_data, std::string cmd); + std::string sky_plugin_redis_multi_key_cmd(zend_execute_data *execute_data, std::string cmd); std::string sky_plugin_redis_uncertain_keys_cmd(zend_execute_data *execute_data, std::string cmd); @@ -44,9 +58,17 @@ std::string sky_plugin_redis_key_ttl_cmd(zend_execute_data *execute_data, std::s std::string sky_plugin_redis_bit_count_cmd(zend_execute_data *execute_data, std::string cmd); +std::string sky_plugin_redis_bit_pos_cmd(zend_execute_data *execute_data, std::string cmd); + std::string sky_plugin_redis_key_value_cmd(zend_execute_data *execute_data, std::string cmd); std::string sky_plugin_redis_todo_cmd(zend_execute_data *execute_data, std::string cmd); +std::string sky_plugin_redis_pure_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_eval_cmd(zend_execute_data *execute_data, std::string cmd); + +std::string sky_plugin_redis_select_cmd(zend_execute_data *execute_data, std::string cmd); + #endif // SKYWALKING_SKY_PLUGIN_REDIS_H From bd22c6cac174639e1344a587ad9343df992b4ee8 Mon Sep 17 00:00:00 2001 From: "bostin.wang" Date: Fri, 7 May 2021 13:10:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8Deval=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E9=94=99=E8=AF=AF=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sky_plugin_redis.cc | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/sky_plugin_redis.cc b/src/sky_plugin_redis.cc index 3dbc39e7..9e2291cc 100644 --- a/src/sky_plugin_redis.cc +++ b/src/sky_plugin_redis.cc @@ -598,6 +598,16 @@ std::string sky_plugin_redis_eval_cmd(zend_execute_data *execute_data, std::stri zval *script = ZEND_CALL_ARG(execute_data, 1); if (Z_TYPE_P(script) == IS_STRING) { cmd += " \"" + std::string(Z_STRVAL_P(script)) + "\""; + if (arg_count >= 3) { + zval *num_keys = ZEND_CALL_ARG(execute_data, 3); + if (Z_TYPE_P(num_keys) == IS_LONG) { + cmd += " " + std::to_string(Z_LVAL_P(num_keys)); + } else { + cmd += " 0"; + } + } else { + cmd += " 0"; + } if (arg_count >= 2) { zval *args = ZEND_CALL_ARG(execute_data, 2); if (Z_TYPE_P(args) == IS_ARRAY) { @@ -616,14 +626,8 @@ std::string sky_plugin_redis_eval_cmd(zend_execute_data *execute_data, std::stri } ZEND_HASH_FOREACH_END(); } } - if (arg_count >= 3) { - zval *num_keys = ZEND_CALL_ARG(execute_data, 3); - if (Z_TYPE_P(num_keys) == IS_LONG) { - cmd += " " + std::to_string(Z_LVAL_P(num_keys)); - } - } return cmd; } } return ""; -} \ No newline at end of file +}