infotest.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. RedisModule_InfoAddSection(ctx, "unsafe");
  19. RedisModule_InfoBeginDictField(ctx, "unsafe:field");
  20. RedisModule_InfoAddFieldLongLong(ctx, "value", 1);
  21. RedisModule_InfoEndDictField(ctx);
  22. if (for_crash_report) {
  23. RedisModule_InfoAddSection(ctx, "Klingon");
  24. RedisModule_InfoAddFieldCString(ctx, "one", "wa’");
  25. RedisModule_InfoAddFieldCString(ctx, "two", "cha’");
  26. RedisModule_InfoAddFieldCString(ctx, "three", "wej");
  27. }
  28. }
  29. int info_get(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, char field_type)
  30. {
  31. if (argc != 3 && argc != 4) {
  32. RedisModule_WrongArity(ctx);
  33. return REDISMODULE_OK;
  34. }
  35. int err = REDISMODULE_OK;
  36. const char *section, *field;
  37. section = RedisModule_StringPtrLen(argv[1], NULL);
  38. field = RedisModule_StringPtrLen(argv[2], NULL);
  39. RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, section);
  40. if (field_type=='i') {
  41. long long ll = RedisModule_ServerInfoGetFieldSigned(info, field, &err);
  42. if (err==REDISMODULE_OK)
  43. RedisModule_ReplyWithLongLong(ctx, ll);
  44. } else if (field_type=='u') {
  45. unsigned long long ll = (unsigned long long)RedisModule_ServerInfoGetFieldUnsigned(info, field, &err);
  46. if (err==REDISMODULE_OK)
  47. RedisModule_ReplyWithLongLong(ctx, ll);
  48. } else if (field_type=='d') {
  49. double d = RedisModule_ServerInfoGetFieldDouble(info, field, &err);
  50. if (err==REDISMODULE_OK)
  51. RedisModule_ReplyWithDouble(ctx, d);
  52. } else if (field_type=='c') {
  53. const char *str = RedisModule_ServerInfoGetFieldC(info, field);
  54. if (str)
  55. RedisModule_ReplyWithCString(ctx, str);
  56. } else {
  57. RedisModuleString *str = RedisModule_ServerInfoGetField(ctx, info, field);
  58. if (str) {
  59. RedisModule_ReplyWithString(ctx, str);
  60. RedisModule_FreeString(ctx, str);
  61. } else
  62. err=REDISMODULE_ERR;
  63. }
  64. if (err!=REDISMODULE_OK)
  65. RedisModule_ReplyWithError(ctx, "not found");
  66. RedisModule_FreeServerInfo(ctx, info);
  67. return REDISMODULE_OK;
  68. }
  69. int info_gets(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  70. return info_get(ctx, argv, argc, 's');
  71. }
  72. int info_getc(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  73. return info_get(ctx, argv, argc, 'c');
  74. }
  75. int info_geti(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  76. return info_get(ctx, argv, argc, 'i');
  77. }
  78. int info_getu(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  79. return info_get(ctx, argv, argc, 'u');
  80. }
  81. int info_getd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  82. return info_get(ctx, argv, argc, 'd');
  83. }
  84. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  85. REDISMODULE_NOT_USED(argv);
  86. REDISMODULE_NOT_USED(argc);
  87. if (RedisModule_Init(ctx,"infotest",1,REDISMODULE_APIVER_1)
  88. == REDISMODULE_ERR) return REDISMODULE_ERR;
  89. if (RedisModule_RegisterInfoFunc(ctx, InfoFunc) == REDISMODULE_ERR) return REDISMODULE_ERR;
  90. if (RedisModule_CreateCommand(ctx,"info.gets", info_gets,"",0,0,0) == REDISMODULE_ERR)
  91. return REDISMODULE_ERR;
  92. if (RedisModule_CreateCommand(ctx,"info.getc", info_getc,"",0,0,0) == REDISMODULE_ERR)
  93. return REDISMODULE_ERR;
  94. if (RedisModule_CreateCommand(ctx,"info.geti", info_geti,"",0,0,0) == REDISMODULE_ERR)
  95. return REDISMODULE_ERR;
  96. if (RedisModule_CreateCommand(ctx,"info.getu", info_getu,"",0,0,0) == REDISMODULE_ERR)
  97. return REDISMODULE_ERR;
  98. if (RedisModule_CreateCommand(ctx,"info.getd", info_getd,"",0,0,0) == REDISMODULE_ERR)
  99. return REDISMODULE_ERR;
  100. return REDISMODULE_OK;
  101. }