mirror of
https://github.com/fluencelabs/redis
synced 2025-06-13 01:01:22 +00:00
Add module api for looking into INFO fields
- Add RM_GetServerInfo and friends - Add auto memory for new opaque struct - Add tests for new APIs other minor fixes: - add const in various char pointers - requested_section in modulesCollectInfo was actually not sds but char* - extract new string2d out of getDoubleFromObject for code reuse Add module API for
This commit is contained in:
@ -29,6 +29,51 @@ void InfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
|
||||
|
||||
}
|
||||
|
||||
int info_get(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, char field_type)
|
||||
{
|
||||
if (argc != 3 && argc != 4) {
|
||||
RedisModule_WrongArity(ctx);
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
int err = REDISMODULE_OK;
|
||||
const char *section, *field;
|
||||
section = RedisModule_StringPtrLen(argv[1], NULL);
|
||||
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);
|
||||
if (err==REDISMODULE_OK)
|
||||
RedisModule_ReplyWithLongLong(ctx, ll);
|
||||
} else if (field_type=='d') {
|
||||
double d = RedisModule_ServerInfoGetFieldDouble(ctx, info, field, &err);
|
||||
if (err==REDISMODULE_OK)
|
||||
RedisModule_ReplyWithDouble(ctx, d);
|
||||
} else {
|
||||
RedisModuleString *str = RedisModule_ServerInfoGetField(ctx, info, field);
|
||||
if (str) {
|
||||
RedisModule_ReplyWithString(ctx, str);
|
||||
RedisModule_FreeString(ctx, str);
|
||||
} else
|
||||
err=REDISMODULE_ERR;
|
||||
}
|
||||
if (err!=REDISMODULE_OK)
|
||||
RedisModule_ReplyWithError(ctx, "not found");
|
||||
RedisModule_FreeServerInfo(ctx, info);
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
|
||||
int info_gets(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
||||
return info_get(ctx, argv, argc, 's');
|
||||
}
|
||||
|
||||
int info_geti(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
||||
return info_get(ctx, argv, argc, 'i');
|
||||
}
|
||||
|
||||
int info_getd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
||||
return info_get(ctx, argv, argc, 'd');
|
||||
}
|
||||
|
||||
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
||||
REDISMODULE_NOT_USED(argv);
|
||||
REDISMODULE_NOT_USED(argc);
|
||||
@ -37,5 +82,12 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
||||
|
||||
if (RedisModule_RegisterInfoFunc(ctx, InfoFunc) == REDISMODULE_ERR) return REDISMODULE_ERR;
|
||||
|
||||
if (RedisModule_CreateCommand(ctx,"info.gets", info_gets,"",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)
|
||||
return REDISMODULE_ERR;
|
||||
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
|
Reference in New Issue
Block a user