/* HELLOTYPE.RANGE key first count */ int HelloTypeRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 4) return RedisModule_WrongArity(ctx); RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); int type = RedisModule_KeyType(key); if (type != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(key) != HelloType) { return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } long long first, count; if (RedisModule_StringToLongLong(argv[2],&first) != REDISMODULE_OK || RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK || first < 0 || count < 0) { return RedisModule_ReplyWithError(ctx, "ERR invalid first or count parameters"); } struct HelloTypeObject *hto = RedisModule_ModuleTypeGetValue(key); struct HelloTypeNode *node = hto ? hto->head : NULL; RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN); long long arraylen = 0; while(node && count--) { RedisModule_ReplyWithLongLong(ctx,node->value); arraylen++; node = node->next; } RedisModule_ReplySetArrayLength(ctx,arraylen); return REDISMODULE_OK; }
int GMThumbnailCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 4) { return RedisModule_WrongArity(ctx); } // init auto memory for created strings RedisModule_AutoMemory(ctx); // open the key RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE); // If key doesn't exist then return immediately if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) { RedisModule_ReplyWithError(ctx, "empty key"); return REDISMODULE_OK; } // Validate key is a string if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING) { RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); return REDISMODULE_ERR; } // Validate inputs are valid unsigned ints long long width; if (RedisModule_StringToLongLong(argv[2], &width) == REDISMODULE_ERR || width <= 0) { RedisModule_ReplyWithError(ctx, "Invalid arguments"); return REDISMODULE_ERR; } long long height; if (RedisModule_StringToLongLong(argv[3], &height) == REDISMODULE_ERR || height <= 0) { RedisModule_ReplyWithError(ctx, "Invalid arguments"); return REDISMODULE_ERR; } // Get access to the image size_t key_len; char *buf = RedisModule_StringDMA(key, &key_len, REDISMODULE_READ); if (!buf) { RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); return REDISMODULE_ERR; } struct GoImgThumbnail_return r = GoImgThumbnail(buf, key_len, width, height); if (r.r3 != 0) { RedisModule_ReplyWithError(ctx, r.r2); free(r.r0); free(r.r2); return REDISMODULE_ERR; } int result = UpdateTransformedImage(ctx, key, r.r0, r.r1); free(r.r0); free(r.r2); return result; }
/* HELLOTYPE.INSERT key value */ int HelloTypeInsert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 3) return RedisModule_WrongArity(ctx); RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); int type = RedisModule_KeyType(key); if (type != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(key) != HelloType) { return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } long long value; if ((RedisModule_StringToLongLong(argv[2],&value) != REDISMODULE_OK)) { return RedisModule_ReplyWithError(ctx,"ERR invalid value: must be a signed 64 bit integer"); } /* Create an empty value object if the key is currently empty. */ struct HelloTypeObject *hto; if (type == REDISMODULE_KEYTYPE_EMPTY) { hto = createHelloTypeObject(); RedisModule_ModuleTypeSetValue(key,HelloType,hto); } else { hto = RedisModule_ModuleTypeGetValue(key); } /* Insert the new element. */ HelloTypeInsert(hto,value); RedisModule_ReplyWithLongLong(ctx,hto->len); RedisModule_ReplicateVerbatim(ctx); return REDISMODULE_OK; }
/* HELLO.LEFTPAD str len ch * This is an implementation of the infamous LEFTPAD function, that * was at the center of an issue with the npm modules system in March 2016. * * LEFTPAD is a good example of using a Redis Modules API called * "pool allocator", that was a famous way to allocate memory in yet another * open source project, the Apache web server. * * The concept is very simple: there is memory that is useful to allocate * only in the context of serving a request, and must be freed anyway when * the callback implementing the command returns. So in that case the module * does not need to retain a reference to these allocations, it is just * required to free the memory before returning. When this is the case the * module can call RedisModule_PoolAlloc() instead, that works like malloc() * but will automatically free the memory when the module callback returns. * * Note that PoolAlloc() does not necessarily require AutoMemory to be * active. */ int HelloLeftPad_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ long long padlen; if (argc != 4) return RedisModule_WrongArity(ctx); if ((RedisModule_StringToLongLong(argv[2],&padlen) != REDISMODULE_OK) || (padlen< 0)) { return RedisModule_ReplyWithError(ctx,"ERR invalid padding length"); } size_t strlen, chlen; const char *str = RedisModule_StringPtrLen(argv[1], &strlen); const char *ch = RedisModule_StringPtrLen(argv[3], &chlen); /* If the string is already larger than the target len, just return * the string itself. */ if (strlen >= padlen) return RedisModule_ReplyWithString(ctx,argv[1]); /* Padding must be a single character in this simple implementation. */ if (chlen != 1) return RedisModule_ReplyWithError(ctx, "ERR padding must be a single char"); /* Here we use our pool allocator, for our throw-away allocation. */ padlen -= strlen; char *buf = RedisModule_PoolAlloc(ctx,padlen+strlen); for (size_t j = 0; j < padlen; j++) buf[j] = *ch; memcpy(buf+padlen,str,strlen); RedisModule_ReplyWithStringBuffer(ctx,buf,padlen+strlen); return REDISMODULE_OK; }
/* Another command to show replication. In this case, we call * RedisModule_ReplicateVerbatim() to mean we want just the command to be * propagated to slaves / AOF exactly as it was called by the user. * * This command also shows how to work with string objects. * It takes a list, and increments all the elements (that must have * a numerical value) by 1, returning the sum of all the elements * as reply. * * Usage: HELLO.REPL2 <list-key> */ int HelloRepl2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 2) return RedisModule_WrongArity(ctx); RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST) return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); size_t listlen = RedisModule_ValueLength(key); long long sum = 0; /* Rotate and increment. */ while(listlen--) { RedisModuleString *ele = RedisModule_ListPop(key,REDISMODULE_LIST_TAIL); long long val; if (RedisModule_StringToLongLong(ele,&val) != REDISMODULE_OK) val = 0; val++; sum += val; RedisModuleString *newele = RedisModule_CreateStringFromLongLong(ctx,val); RedisModule_ListPush(key,REDISMODULE_LIST_HEAD,newele); } RedisModule_ReplyWithLongLong(ctx,sum); RedisModule_ReplicateVerbatim(ctx); return REDISMODULE_OK; }
int UniquePushIVCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc < 4) { return RedisModule_WrongArity(ctx); } RedisModule_AutoMemory(ctx); // open the key and make sure it is indeed a Hash and not empty RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE); int type = RedisModule_KeyType(key); if (type != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(key) != UniqueType) { return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } unique *unique; if (type == REDISMODULE_KEYTYPE_EMPTY) { unique = uniqueCreate(); RedisModule_ModuleTypeSetValue(key,UniqueType,unique); } else { unique = RedisModule_ModuleTypeGetValue(key); } sds skey, sval; size_t skeylen, svallen; const char *pkey; unsigned int lvec = argc-3; pkey = RedisModule_StringPtrLen(argv[2], &skeylen); svallen = lvec *8; skey = sdsnewlen(pkey, skeylen); sval = sdsnewlen(NULL, svallen); long long *vec = (long long*)sval; int i; for (i=3; i<argc; i++) { if (REDISMODULE_OK != RedisModule_StringToLongLong(argv[i], vec+i-3)) { return RedisModule_WrongArity(ctx); } } int n = uniquePush(unique, skey, sval, merge_int64); if (n == -1) { RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } else { RedisModule_ReplyWithLongLong(ctx, n); } RedisModule_ReplicateVerbatim(ctx); return REDISMODULE_OK; }
/* HELLO.RAND.ARRAY <count> * Shows how to generate arrays as commands replies. * It just outputs <count> random numbers. */ int HelloRandArray_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 2) return RedisModule_WrongArity(ctx); long long count; if (RedisModule_StringToLongLong(argv[1],&count) != REDISMODULE_OK || count < 0) return RedisModule_ReplyWithError(ctx,"ERR invalid count"); /* To reply with an array, we call RedisModule_ReplyWithArray() followed * by other "count" calls to other reply functions in order to generate * the elements of the array. */ RedisModule_ReplyWithArray(ctx,count); while(count--) RedisModule_ReplyWithLongLong(ctx,rand()); return REDISMODULE_OK; }
/* HELLO.MORE.EXPIRE key milliseconds. * * If they key has already an associated TTL, extends it by "milliseconds" * milliseconds. Otherwise no operation is performed. */ int HelloMoreExpire_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 3) return RedisModule_WrongArity(ctx); mstime_t addms, expire; if (RedisModule_StringToLongLong(argv[2],&addms) != REDISMODULE_OK) return RedisModule_ReplyWithError(ctx,"ERR invalid expire time"); RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); expire = RedisModule_GetExpire(key); if (expire != REDISMODULE_NO_EXPIRE) { expire += addms; RedisModule_SetExpire(key,expire); } return RedisModule_ReplyWithSimpleString(ctx,"OK"); }
/* HELLO.LIST.SPLICE srclist dstlist count * Moves 'count' elements from the tail of 'srclist' to the head of * 'dstlist'. If less than count elements are available, it moves as much * elements as possible. */ int HelloListSplice_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 4) return RedisModule_WrongArity(ctx); RedisModuleKey *srckey = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); RedisModuleKey *dstkey = RedisModule_OpenKey(ctx,argv[2], REDISMODULE_READ|REDISMODULE_WRITE); /* Src and dst key must be empty or lists. */ if ((RedisModule_KeyType(srckey) != REDISMODULE_KEYTYPE_LIST && RedisModule_KeyType(srckey) != REDISMODULE_KEYTYPE_EMPTY) || (RedisModule_KeyType(dstkey) != REDISMODULE_KEYTYPE_LIST && RedisModule_KeyType(dstkey) != REDISMODULE_KEYTYPE_EMPTY)) { RedisModule_CloseKey(srckey); RedisModule_CloseKey(dstkey); return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } long long count; if ((RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK) || (count < 0)) { RedisModule_CloseKey(srckey); RedisModule_CloseKey(dstkey); return RedisModule_ReplyWithError(ctx,"ERR invalid count"); } while(count-- > 0) { RedisModuleString *ele; ele = RedisModule_ListPop(srckey,REDISMODULE_LIST_TAIL); if (ele == NULL) break; RedisModule_ListPush(dstkey,REDISMODULE_LIST_HEAD,ele); RedisModule_FreeString(ctx,ele); } size_t len = RedisModule_ValueLength(srckey); RedisModule_CloseKey(srckey); RedisModule_CloseKey(dstkey); RedisModule_ReplyWithLongLong(ctx,len); return REDISMODULE_OK; }