infotest.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "redismodule.h"
  2. #include <string.h>
  3. void InfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
  4. RedisModule_InfoAddSection(ctx, "");
  5. RedisModule_InfoAddFieldLongLong(ctx, "global", -2);
  6. RedisModule_InfoAddFieldULongLong(ctx, "uglobal", (unsigned long long)-2);
  7. RedisModule_InfoAddSection(ctx, "Spanish");
  8. RedisModule_InfoAddFieldCString(ctx, "uno", "one");
  9. RedisModule_InfoAddFieldLongLong(ctx, "dos", 2);
  10. RedisModule_InfoAddSection(ctx, "Italian");
  11. RedisModule_InfoAddFieldLongLong(ctx, "due", 2);
  12. RedisModule_InfoAddFieldDouble(ctx, "tre", 3.3);
  13. RedisModule_InfoAddSection(ctx, "keyspace");
  14. RedisModule_InfoBeginDictField(ctx, "db0");
  15. RedisModule_InfoAddFieldLongLong(ctx, "keys", 3);
  16. RedisModule_InfoAddFieldLongLong(ctx, "expires", 1);
  17. RedisModule_InfoEndDictField(ctx);
  18. if (for_crash_report) {
  19. RedisModule_InfoAddSection(ctx, "Klingon");
  20. RedisModule_InfoAddFieldCString(ctx, "one", "wa’");
  21. RedisModule_InfoAddFieldCString(ctx, "two", "cha’");
  22. RedisModule_InfoAddFieldCString(ctx, "three", "wej");
  23. }
  24. }
  25. int info_get(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, char field_type)
  26. {
  27. if (argc != 3 && argc != 4) {
  28. RedisModule_WrongArity(ctx);
  29. return REDISMODULE_OK;
  30. }
  31. int err = REDISMODULE_OK;
  32. const char *section, *field;
  33. section = RedisModule_StringPtrLen(argv[1], NULL);
  34. field = RedisModule_StringPtrLen(argv[2], NULL);
  35. RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, section);
  36. if (field_type=='i') {
  37. long long ll = RedisModule_ServerInfoGetFieldSigned(info, field, &err);
  38. if (err==REDISMODULE_OK)
  39. RedisModule_ReplyWithLongLong(ctx, ll);
  40. } else if (field_type=='u') {
  41. unsigned long long ll = (unsigned long long)RedisModule_ServerInfoGetFieldUnsigned(info, field, &err);
  42. if (err==REDISMODULE_OK)
  43. RedisModule_ReplyWithLongLong(ctx, ll);
  44. } else if (field_type=='d') {
  45. double d = RedisModule_ServerInfoGetFieldDouble(info, field, &err);
  46. if (err==REDISMODULE_OK)
  47. RedisModule_ReplyWithDouble(ctx, d);
  48. } else if (field_type=='c') {
  49. const char *str = RedisModule_ServerInfoGetFieldC(info, field);
  50. if (str)
  51. RedisModule_ReplyWithCString(ctx, str);
  52. } else {
  53. RedisModuleString *str = RedisModule_ServerInfoGetField(ctx, info, field);
  54. if (str) {
  55. RedisModule_ReplyWithString(ctx, str);
  56. RedisModule_FreeString(ctx, str);
  57. } else
  58. err=REDISMODULE_ERR;
  59. }
  60. if (err!=REDISMODULE_OK)
  61. RedisModule_ReplyWithError(ctx, "not found");
  62. RedisModule_FreeServerInfo(ctx, info);
  63. return REDISMODULE_OK;
  64. }
  65. int info_gets(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  66. return info_get(ctx, argv, argc, 's');
  67. }
  68. int info_getc(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  69. return info_get(ctx, argv, argc, 'c');
  70. }
  71. int info_geti(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  72. return info_get(ctx, argv, argc, 'i');
  73. }
  74. int info_getu(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  75. return info_get(ctx, argv, argc, 'u');
  76. }
  77. int info_getd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  78. return info_get(ctx, argv, argc, 'd');
  79. }
  80. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  81. REDISMODULE_NOT_USED(argv);
  82. REDISMODULE_NOT_USED(argc);
  83. if (RedisModule_Init(ctx,"infotest",1,REDISMODULE_APIVER_1)
  84. == REDISMODULE_ERR) return REDISMODULE_ERR;
  85. if (RedisModule_RegisterInfoFunc(ctx, InfoFunc) == REDISMODULE_ERR) return REDISMODULE_ERR;
  86. if (RedisModule_CreateCommand(ctx,"info.gets", info_gets,"",0,0,0) == REDISMODULE_ERR)
  87. return REDISMODULE_ERR;
  88. if (RedisModule_CreateCommand(ctx,"info.getc", info_getc,"",0,0,0) == REDISMODULE_ERR)
  89. return REDISMODULE_ERR;
  90. if (RedisModule_CreateCommand(ctx,"info.geti", info_geti,"",0,0,0) == REDISMODULE_ERR)
  91. return REDISMODULE_ERR;
  92. if (RedisModule_CreateCommand(ctx,"info.getu", info_getu,"",0,0,0) == REDISMODULE_ERR)
  93. return REDISMODULE_ERR;
  94. if (RedisModule_CreateCommand(ctx,"info.getd", info_getd,"",0,0,0) == REDISMODULE_ERR)
  95. return REDISMODULE_ERR;
  96. return REDISMODULE_OK;
  97. }