Exemple #1
0
void RMUtil_SCAN(RedisModuleCtx *ctx, const char *pattern, RedisModuleString **keys, size_t *key_count) {
    int scan_idx = 0;               /* SCAN cursor. */
    size_t total_keys = 0;          /* Number of keys retrieved. */
    RedisModuleCallReply *reply;

    /* Consume SCAN */
    do {
        reply = RedisModule_Call(ctx, "SCAN", "lcc", scan_idx, "MATCH", pattern);

        /* First element is the scan cursor, 0 indicates end of SCAN. */
        RedisModuleCallReply *element = RedisModule_CallReplyArrayElement(reply, 0);
        scan_idx = RedisModule_CallReplyInteger(element);

        /* Process SCAN results. */
        RedisModuleCallReply *scan_results = RedisModule_CallReplyArrayElement(reply, 1);
        /* Number of elements in replay. */
        size_t keys_count = RedisModule_CallReplyLength(scan_results);

        /* Extract SCAN result elements. */
        for(int idx = 0; idx < keys_count && *key_count > total_keys; idx++) {
            element = RedisModule_CallReplyArrayElement(scan_results, idx);
            RedisModuleString *key = RedisModule_CreateStringFromCallReply(element);
            keys[total_keys] = key;
            total_keys++;
        }
        RedisModule_FreeCallReply(reply);
    } while(scan_idx != 0);

    /* Update number of stores fetched. */
    *key_count = total_keys;
}
Exemple #2
0
RMUtilInfo *RMUtil_HGetAll(RedisModuleCtx *ctx, RedisModuleString *id) {
    RedisModuleCallReply *reply = RedisModule_Call(ctx, "HGETALL", "s", id);
    if(RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) {
        printf("ERROE expecting an array \n");
        return NULL;
    }
    
    size_t reply_len = RedisModule_CallReplyLength(reply);
    RMUtilInfo *hgetall = malloc(sizeof(RMUtilInfo));
    hgetall->numEntries = reply_len/2;
    hgetall->entries = malloc(sizeof(RMUtilInfoEntry) * hgetall->numEntries);
    
    // Consume HGETALL
    for(int idx = 0; idx < reply_len; idx+=2) {
        RedisModuleCallReply *subreply;
        subreply = RedisModule_CallReplyArrayElement(reply, idx);

        size_t len;
        char *key = strdup(RedisModule_CallReplyStringPtr(subreply, &len));
        key[len] = 0;
        hgetall->entries[idx/2].key = key;
        
        subreply = RedisModule_CallReplyArrayElement(reply, idx+1);
        char *val = strdup(RedisModule_CallReplyStringPtr(subreply, &len));
        val[len] = 0;
        hgetall->entries[idx/2].val = val;
    }

    RedisModule_FreeCallReply(reply);
    return hgetall;
}
Exemple #3
0
/* HELLO.PUSH.CALL2
 * This is exaxctly as HELLO.PUSH.CALL, but shows how we can reply to the
 * client using directly a reply object that Call() returned. */
int HelloPushCall2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
    if (argc != 3) return RedisModule_WrongArity(ctx);

    RedisModuleCallReply *reply;

    reply = RedisModule_Call(ctx,"RPUSH","ss",argv[1],argv[2]);
    RedisModule_ReplyWithCallReply(ctx,reply);
    RedisModule_FreeCallReply(reply);
    return REDISMODULE_OK;
}
Exemple #4
0
/* HELLO.LIST.SUM.LEN returns the total length of all the items inside
 * a Redis list, by using the high level Call() API.
 * This command is an example of the array reply access. */
int HelloListSumLen_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
    if (argc != 2) return RedisModule_WrongArity(ctx);

    RedisModuleCallReply *reply;

    reply = RedisModule_Call(ctx,"LRANGE","sll",argv[1],(long long)0,(long long)-1);
    size_t strlen = 0;
    size_t items = RedisModule_CallReplyLength(reply);
    size_t j;
    for (j = 0; j < items; j++) {
        RedisModuleCallReply *ele = RedisModule_CallReplyArrayElement(reply,j);
        strlen += RedisModule_CallReplyLength(ele);
    }
    RedisModule_FreeCallReply(reply);
    RedisModule_ReplyWithLongLong(ctx,strlen);
    return REDISMODULE_OK;
}