keyspecs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "redismodule.h"
  2. #define UNUSED(V) ((void) V)
  3. int kspec_legacy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  4. UNUSED(argv);
  5. UNUSED(argc);
  6. RedisModule_ReplyWithSimpleString(ctx, "OK");
  7. return REDISMODULE_OK;
  8. }
  9. int kspec_complex1(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  10. UNUSED(argv);
  11. UNUSED(argc);
  12. RedisModule_ReplyWithSimpleString(ctx, "OK");
  13. return REDISMODULE_OK;
  14. }
  15. int kspec_complex2(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  16. UNUSED(argv);
  17. UNUSED(argc);
  18. RedisModule_ReplyWithSimpleString(ctx, "OK");
  19. return REDISMODULE_OK;
  20. }
  21. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  22. REDISMODULE_NOT_USED(argv);
  23. REDISMODULE_NOT_USED(argc);
  24. int spec_id;
  25. if (RedisModule_Init(ctx, "keyspecs", 1, REDISMODULE_APIVER_1)== REDISMODULE_ERR)
  26. return REDISMODULE_ERR;
  27. /* Test legacy range "gluing" */
  28. if (RedisModule_CreateCommand(ctx,"kspec.legacy",kspec_legacy,"",0,0,0) == REDISMODULE_ERR)
  29. return REDISMODULE_ERR;
  30. if (RedisModule_AddCommandKeySpec(ctx,"kspec.legacy","read",&spec_id) == REDISMODULE_ERR)
  31. return REDISMODULE_ERR;
  32. if (RedisModule_SetCommandKeySpecBeginSearchIndex(ctx,"kspec.legacy",spec_id,1) == REDISMODULE_ERR)
  33. return REDISMODULE_ERR;
  34. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.legacy",spec_id,0,1,0) == REDISMODULE_ERR)
  35. return REDISMODULE_ERR;
  36. if (RedisModule_AddCommandKeySpec(ctx,"kspec.legacy","write",&spec_id) == REDISMODULE_ERR)
  37. return REDISMODULE_ERR;
  38. if (RedisModule_SetCommandKeySpecBeginSearchIndex(ctx,"kspec.legacy",spec_id,2) == REDISMODULE_ERR)
  39. return REDISMODULE_ERR;
  40. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.legacy",spec_id,0,1,0) == REDISMODULE_ERR)
  41. return REDISMODULE_ERR;
  42. /* First is legacy, rest are new specs */
  43. if (RedisModule_CreateCommand(ctx,"kspec.complex1",kspec_complex1,"",1,1,1) == REDISMODULE_ERR)
  44. return REDISMODULE_ERR;
  45. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex1","write",&spec_id) == REDISMODULE_ERR)
  46. return REDISMODULE_ERR;
  47. if (RedisModule_SetCommandKeySpecBeginSearchKeyword(ctx,"kspec.complex1",spec_id,"STORE",2) == REDISMODULE_ERR)
  48. return REDISMODULE_ERR;
  49. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.complex1",spec_id,0,1,0) == REDISMODULE_ERR)
  50. return REDISMODULE_ERR;
  51. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex1","read",&spec_id) == REDISMODULE_ERR)
  52. return REDISMODULE_ERR;
  53. if (RedisModule_SetCommandKeySpecBeginSearchKeyword(ctx,"kspec.complex1",spec_id,"KEYS",2) == REDISMODULE_ERR)
  54. return REDISMODULE_ERR;
  55. if (RedisModule_SetCommandKeySpecFindKeysKeynum(ctx,"kspec.complex1",spec_id,0,1,1) == REDISMODULE_ERR)
  56. return REDISMODULE_ERR;
  57. /* First is not legacy, more than STATIC_KEYS_SPECS_NUM specs */
  58. if (RedisModule_CreateCommand(ctx,"kspec.complex2",kspec_complex2,"",0,0,0) == REDISMODULE_ERR)
  59. return REDISMODULE_ERR;
  60. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex2","write",&spec_id) == REDISMODULE_ERR)
  61. return REDISMODULE_ERR;
  62. if (RedisModule_SetCommandKeySpecBeginSearchKeyword(ctx,"kspec.complex2",spec_id,"STORE",5) == REDISMODULE_ERR)
  63. return REDISMODULE_ERR;
  64. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.complex2",spec_id,0,1,0) == REDISMODULE_ERR)
  65. return REDISMODULE_ERR;
  66. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex2","read",&spec_id) == REDISMODULE_ERR)
  67. return REDISMODULE_ERR;
  68. if (RedisModule_SetCommandKeySpecBeginSearchIndex(ctx,"kspec.complex2",spec_id,1) == REDISMODULE_ERR)
  69. return REDISMODULE_ERR;
  70. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.complex2",spec_id,0,1,0) == REDISMODULE_ERR)
  71. return REDISMODULE_ERR;
  72. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex2","read",&spec_id) == REDISMODULE_ERR)
  73. return REDISMODULE_ERR;
  74. if (RedisModule_SetCommandKeySpecBeginSearchIndex(ctx,"kspec.complex2",spec_id,2) == REDISMODULE_ERR)
  75. return REDISMODULE_ERR;
  76. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.complex2",spec_id,0,1,0) == REDISMODULE_ERR)
  77. return REDISMODULE_ERR;
  78. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex2","write",&spec_id) == REDISMODULE_ERR)
  79. return REDISMODULE_ERR;
  80. if (RedisModule_SetCommandKeySpecBeginSearchIndex(ctx,"kspec.complex2",spec_id,3) == REDISMODULE_ERR)
  81. return REDISMODULE_ERR;
  82. if (RedisModule_SetCommandKeySpecFindKeysKeynum(ctx,"kspec.complex2",spec_id,0,1,1) == REDISMODULE_ERR)
  83. return REDISMODULE_ERR;
  84. if (RedisModule_AddCommandKeySpec(ctx,"kspec.complex2","write",&spec_id) == REDISMODULE_ERR)
  85. return REDISMODULE_ERR;
  86. if (RedisModule_SetCommandKeySpecBeginSearchKeyword(ctx,"kspec.complex2",spec_id,"MOREKEYS",5) == REDISMODULE_ERR)
  87. return REDISMODULE_ERR;
  88. if (RedisModule_SetCommandKeySpecFindKeysRange(ctx,"kspec.complex2",spec_id,-1,1,0) == REDISMODULE_ERR)
  89. return REDISMODULE_ERR;
  90. return REDISMODULE_OK;
  91. }