|
| 1 | +#include "filter_module.h" |
| 2 | +#include "fsort.h" |
| 3 | +#include "fsort_utils.h" |
| 4 | + |
| 5 | +int FSortBust_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 6 | + RedisModule_AutoMemory(ctx); |
| 7 | + |
| 8 | + if (argc <3 || argc > 4 ) { |
| 9 | + return RedisModule_WrongArity(ctx); |
| 10 | + } |
| 11 | + |
| 12 | + pthread_t tid; |
| 13 | + RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0); |
| 14 | + |
| 15 | + void **targ = RedisModule_Alloc(sizeof(void*)*3); |
| 16 | + targ[0] = bc; |
| 17 | + targ[1] = (void*)(unsigned long) argc; |
| 18 | + targ[2] = (void*)(RedisModuleString **)argv; |
| 19 | + |
| 20 | + if (pthread_create(&tid,NULL,fsort_bust_thread,targ) != 0) { |
| 21 | + RedisModule_AbortBlock(bc); |
| 22 | + return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread"); |
| 23 | + } |
| 24 | + |
| 25 | + return REDISMODULE_OK; |
| 26 | +} |
| 27 | + |
| 28 | +int FSortAggregate_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 29 | + RedisModule_AutoMemory(ctx); |
| 30 | + |
| 31 | + if (argc != 4) { |
| 32 | + return RedisModule_WrongArity(ctx); |
| 33 | + } |
| 34 | + |
| 35 | + pthread_t tid; |
| 36 | + RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0); |
| 37 | + |
| 38 | + void **targ = RedisModule_Alloc(sizeof(void*)*3); |
| 39 | + targ[0] = bc; |
| 40 | + targ[1] = (void*)(unsigned long) argc; |
| 41 | + targ[2] = (void*)(RedisModuleString **)argv; |
| 42 | + |
| 43 | + if (pthread_create(&tid,NULL,fsort_aggregate_thread,targ) != 0) { |
| 44 | + RedisModule_AbortBlock(bc); |
| 45 | + return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread"); |
| 46 | + } |
| 47 | + |
| 48 | + return REDISMODULE_OK; |
| 49 | +} |
| 50 | + |
| 51 | +int FSort_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 52 | + RedisModule_AutoMemory(ctx); |
| 53 | + |
| 54 | + if (argc < 7 ) { |
| 55 | + return RedisModule_WrongArity(ctx); |
| 56 | + } |
| 57 | + |
| 58 | + pthread_t tid; |
| 59 | + RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0); |
| 60 | + |
| 61 | + void **targ = RedisModule_Alloc(sizeof(void*)*3); |
| 62 | + targ[0] = bc; |
| 63 | + targ[1] = (void*)(unsigned long) argc; |
| 64 | + targ[2] = (void*)(RedisModuleString **)argv; |
| 65 | + |
| 66 | + if (pthread_create(&tid,NULL,fsort_fsort_thread,targ) != 0) { |
| 67 | + RedisModule_AbortBlock(bc); |
| 68 | + return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread"); |
| 69 | + } |
| 70 | + |
| 71 | + return REDISMODULE_OK; |
| 72 | +} |
| 73 | + |
| 74 | +int RedisModule_OnLoad(RedisModuleCtx *ctx) { |
| 75 | + |
| 76 | + if (RedisModule_Init(ctx, "FilterSortModule", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) { |
| 77 | + return REDISMODULE_ERR; |
| 78 | + } |
| 79 | + |
| 80 | + if (RedisModule_CreateCommand(ctx, "fsort", FSort_RedisCommand, "write", 1, 2, 1) == REDISMODULE_ERR) { |
| 81 | + return REDISMODULE_ERR; |
| 82 | + } |
| 83 | + |
| 84 | + if (RedisModule_CreateCommand(ctx, "fsortBust", FSortBust_RedisCommand, "write", 1, 1, 1) == REDISMODULE_ERR) { |
| 85 | + return REDISMODULE_ERR; |
| 86 | + } |
| 87 | + |
| 88 | + if (RedisModule_CreateCommand(ctx, "fsortaggregate", FSortAggregate_RedisCommand, "write", 1, 2, 1) == REDISMODULE_ERR) { |
| 89 | + return REDISMODULE_ERR; |
| 90 | + } |
| 91 | + |
| 92 | + return REDISMODULE_OK; |
| 93 | +} |
0 commit comments