From deebed23e16c2cab9e0362e94bca8545d6e33596 Mon Sep 17 00:00:00 2001 From: Oran Agra Date: Mon, 4 Nov 2019 07:57:52 +0200 Subject: [PATCH] Adding RM_ServerInfoGetFieldC --- src/module.c | 14 ++++++++++---- src/redismodule.h | 6 ++++-- tests/modules/infotest.c | 14 ++++++++++++-- tests/unit/moduleapi/infotest.tcl | 1 + 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/module.c b/src/module.c index ce85c479d..fea66f293 100644 --- a/src/module.c +++ b/src/module.c @@ -5534,11 +5534,17 @@ RedisModuleString *RM_ServerInfoGetField(RedisModuleCtx *ctx, RedisModuleServerI return o; } +/* Similar to RM_ServerInfoGetField, but returns a char* which should not be freed but the caller. */ +const char *RM_ServerInfoGetFieldC(RedisModuleServerInfoData *data, const char* field) { + sds val = raxFind(data->rax, (unsigned char *)field, strlen(field)); + if (val == raxNotFound) return NULL; + return val; +} + /* Get the value of a field from data collected with RM_GetServerInfo(). If the * field is not found, or is not numerical, return value will be 0, and the * optional out_err argument will be set to REDISMODULE_ERR. */ -long long RM_ServerInfoGetFieldNumerical(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field, int *out_err) { - UNUSED(ctx); +long long RM_ServerInfoGetFieldNumerical(RedisModuleServerInfoData *data, const char* field, int *out_err) { long long ll; sds val = raxFind(data->rax, (unsigned char *)field, strlen(field)); if (val == raxNotFound) { @@ -5556,8 +5562,7 @@ long long RM_ServerInfoGetFieldNumerical(RedisModuleCtx *ctx, RedisModuleServerI /* Get the value of a field from data collected with RM_GetServerInfo(). If the * field is not found, or is not a double, return value will be 0, and the * optional out_err argument will be set to REDISMODULE_ERR. */ -double RM_ServerInfoGetFieldDouble(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field, int *out_err) { - UNUSED(ctx); +double RM_ServerInfoGetFieldDouble(RedisModuleServerInfoData *data, const char* field, int *out_err) { double dbl; sds val = raxFind(data->rax, (unsigned char *)field, strlen(field)); if (val == raxNotFound) { @@ -6862,6 +6867,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(GetServerInfo); REGISTER_API(FreeServerInfo); REGISTER_API(ServerInfoGetField); + REGISTER_API(ServerInfoGetFieldC); REGISTER_API(ServerInfoGetFieldNumerical); REGISTER_API(ServerInfoGetFieldDouble); REGISTER_API(GetClientInfoById); diff --git a/src/redismodule.h b/src/redismodule.h index d923a1b8a..0c289848f 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -506,8 +506,9 @@ int REDISMODULE_API_FUNC(RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx * RedisModuleServerInfoData *REDISMODULE_API_FUNC(RedisModule_GetServerInfo)(RedisModuleCtx *ctx, const char *section); void REDISMODULE_API_FUNC(RedisModule_FreeServerInfo)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data); RedisModuleString *REDISMODULE_API_FUNC(RedisModule_ServerInfoGetField)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field); -long long REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldNumerical)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field, int *out_err); -double REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldDouble)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field, int *out_err); +const char *REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldC)(RedisModuleServerInfoData *data, const char* field); +long long REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldNumerical)(RedisModuleServerInfoData *data, const char* field, int *out_err); +double REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldDouble)(RedisModuleServerInfoData *data, const char* field, int *out_err); int REDISMODULE_API_FUNC(RedisModule_SubscribeToServerEvent)(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback); RedisModuleBlockedClient *REDISMODULE_API_FUNC(RedisModule_BlockClientOnKeys)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata); void REDISMODULE_API_FUNC(RedisModule_SignalKeyAsReady)(RedisModuleCtx *ctx, RedisModuleString *key); @@ -713,6 +714,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int REDISMODULE_GET_API(GetServerInfo); REDISMODULE_GET_API(FreeServerInfo); REDISMODULE_GET_API(ServerInfoGetField); + REDISMODULE_GET_API(ServerInfoGetFieldC); REDISMODULE_GET_API(ServerInfoGetFieldNumerical); REDISMODULE_GET_API(ServerInfoGetFieldDouble); REDISMODULE_GET_API(GetClientInfoById); diff --git a/tests/modules/infotest.c b/tests/modules/infotest.c index 460ebb9c0..a21fefffc 100644 --- a/tests/modules/infotest.c +++ b/tests/modules/infotest.c @@ -41,13 +41,17 @@ int info_get(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, char field field = RedisModule_StringPtrLen(argv[2], NULL); RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, section); if (field_type=='i') { - long long ll = RedisModule_ServerInfoGetFieldNumerical(ctx, info, field, &err); + long long ll = RedisModule_ServerInfoGetFieldNumerical(info, field, &err); if (err==REDISMODULE_OK) RedisModule_ReplyWithLongLong(ctx, ll); } else if (field_type=='d') { - double d = RedisModule_ServerInfoGetFieldDouble(ctx, info, field, &err); + double d = RedisModule_ServerInfoGetFieldDouble(info, field, &err); if (err==REDISMODULE_OK) RedisModule_ReplyWithDouble(ctx, d); + } else if (field_type=='c') { + const char *str = RedisModule_ServerInfoGetFieldC(info, field); + if (str) + RedisModule_ReplyWithCString(ctx, str); } else { RedisModuleString *str = RedisModule_ServerInfoGetField(ctx, info, field); if (str) { @@ -66,6 +70,10 @@ int info_gets(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return info_get(ctx, argv, argc, 's'); } +int info_getc(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + return info_get(ctx, argv, argc, 'c'); +} + int info_geti(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return info_get(ctx, argv, argc, 'i'); } @@ -84,6 +92,8 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) if (RedisModule_CreateCommand(ctx,"info.gets", info_gets,"",0,0,0) == REDISMODULE_ERR) return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx,"info.getc", info_getc,"",0,0,0) == REDISMODULE_ERR) + return REDISMODULE_ERR; if (RedisModule_CreateCommand(ctx,"info.geti", info_geti,"",0,0,0) == REDISMODULE_ERR) return REDISMODULE_ERR; if (RedisModule_CreateCommand(ctx,"info.getd", info_getd,"",0,0,0) == REDISMODULE_ERR) diff --git a/tests/unit/moduleapi/infotest.tcl b/tests/unit/moduleapi/infotest.tcl index a42681345..225798dd5 100644 --- a/tests/unit/moduleapi/infotest.tcl +++ b/tests/unit/moduleapi/infotest.tcl @@ -13,6 +13,7 @@ start_server {tags {"modules"}} { test {module reading info} { # check string, integer and float fields assert_equal [r info.gets replication role] "master" + assert_equal [r info.getc replication role] "master" assert_equal [r info.geti stats expired_keys] 0 assert_equal [r info.getd stats expired_stale_perc] 0