static void validate(RedisModuleCtx *ctx, RedisModuleCallReply *reply, RedisModuleString *password) { const char *reply_str; size_t reply_len; const char *pass; size_t pass_len; const char *crypt_pass; size_t crypt_pass_len; char crypt_buf[64]; if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_NULL) { RedisModule_ReplyWithLongLong(ctx, 0); return; } if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) { RedisModule_ReplyWithError(ctx, "WRONGTYPE Operation against a key holding the wrong kind of value"); return; } reply_str = RedisModule_CallReplyStringPtr(reply, &reply_len); pass = RedisModule_StringPtrLen(password, &pass_len); crypt_pass = crypt_rn(pass, reply_str, crypt_buf, sizeof(crypt_buf)); if (!crypt_pass) { RedisModule_ReplyWithError(ctx, "ERR hash error"); return; } crypt_pass_len = strlen(crypt_pass); if (crypt_pass_len == reply_len && !memcmp(reply_str, crypt_pass, crypt_pass_len)) { RedisModule_ReplyWithLongLong(ctx, 1); } else { RedisModule_ReplyWithLongLong(ctx, 0); } }
/* 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; }
/* 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 UniqueLenCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 2) { 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); int type = RedisModule_KeyType(key); if (type == REDISMODULE_KEYTYPE_EMPTY) { RedisModule_ReplyWithNull(ctx); return REDISMODULE_OK; } if (RedisModule_ModuleTypeGetType(key) != UniqueType) { return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } unique *unique = RedisModule_ModuleTypeGetValue(key); size_t len = uniqueLen(unique); RedisModule_ReplyWithLongLong(ctx, len); return REDISMODULE_OK; }
/* This is a simple command to test replication. Because of the "!" modified * in the RedisModule_Call() call, the two INCRs get replicated. * Also note how the ECHO is replicated in an unexpected position (check * comments the function implementation). */ int HelloRepl1_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModuleCallReply *reply; RedisModule_AutoMemory(ctx); /* This will be replicated *after* the two INCR statements, since * the Call() replication has precedence, so the actual replication * stream will be: * * MULTI * INCR foo * INCR bar * ECHO c foo * EXEC */ RedisModule_Replicate(ctx,"ECHO","c","foo"); /* Using the "!" modifier we replicate the command if it * modified the dataset in some way. */ reply = RedisModule_Call(ctx,"INCR","c!","foo"); reply = RedisModule_Call(ctx,"INCR","c!","bar"); RedisModule_ReplyWithLongLong(ctx,0); return REDISMODULE_OK; }
/* 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; }
static void PopReplyFV(RedisModuleCtx *ctx, sds skey, sds sval) { int i, n = sdslen(sval)/8; RedisModule_ReplyWithArray(ctx, 1+n); RedisModule_ReplyWithStringBuffer(ctx, skey, sdslen(skey)); long long *pvec = (long long*)sval; for (i=0; i<n; i++) { RedisModule_ReplyWithLongLong(ctx, pvec[i]); } }
/* ## FT.SUGDEL key str Delete a string from a suggestion index. ### Parameters: - key: the suggestion dictionary key. - str: the string to delete ### Returns: Integer reply: 1 if the string was found and deleted, 0 otherwise. */ int RSSuggestDelCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 3) return RedisModule_WrongArity(ctx); RedisModule_ReplicateVerbatim(ctx); RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ); int type = RedisModule_KeyType(key); if (type != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(key) != TrieType) { return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); } Trie *tree = RedisModule_ModuleTypeGetValue(key); if (!tree) { return RedisModule_ReplyWithLongLong(ctx, 0); } size_t len; const char *str = RedisModule_StringPtrLen(argv[2], &len); return RedisModule_ReplyWithLongLong(ctx, Trie_Delete(tree, str, len)); }
/* HELLO.PUSH.CALL implements RPUSH using an higher level approach, calling * a Redis command instead of working with the key in a low level way. This * approach is useful when you need to call Redis commands that are not * available as low level APIs, or when you don't need the maximum speed * possible but instead prefer implementation simplicity. */ int HelloPushCall_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]); long long len = RedisModule_CallReplyInteger(reply); RedisModule_FreeCallReply(reply); RedisModule_ReplyWithLongLong(ctx,len); 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; }
/* ## FT.SUGLEN key Get the size of an autoc-complete suggestion dictionary ### Parameters: - key: the suggestion dictionary key. ### Returns: Integer reply: the current size of the suggestion dictionary. */ int RSSuggestLenCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 2) return RedisModule_WrongArity(ctx); RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ); int type = RedisModule_KeyType(key); if (type != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(key) != TrieType) { return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); } Trie *tree = RedisModule_ModuleTypeGetValue(key); return RedisModule_ReplyWithLongLong(ctx, tree ? tree->size : 0); }
/* HELLO.PUSH.NATIVE re-implements RPUSH, and shows the low level modules API * where you can "open" keys, make low level operations, create new keys by * pushing elements into non-existing keys, and so forth. * * You'll find this command to be roughly as fast as the actual RPUSH * command. */ int HelloPushNative_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 3) return RedisModule_WrongArity(ctx); RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); RedisModule_ListPush(key,REDISMODULE_LIST_TAIL,argv[2]); size_t newlen = RedisModule_ValueLength(key); RedisModule_CloseKey(key); RedisModule_ReplyWithLongLong(ctx,newlen); return REDISMODULE_OK; }
/* ## FT.SUGGADD key string score [INCR] [PAYLOAD {payload}] Add a suggestion string to an auto-complete suggestion dictionary. This is disconnected from the index definitions, and leaves creating and updating suggestino dictionaries to the user. ### Parameters: - key: the suggestion dictionary key. - string: the suggestion string we index - score: a floating point number of the suggestion string's weight -INCR: if set, we increment the existing entry of the suggestion by the given score, instead of replacing the score. This is useful for updating the dictionary based on user queries in real time - PAYLOAD: Add a payload to the suggestion string that will be used as additional information. ### Returns: Integer reply: the current size of the suggestion dictionary. */ int RSSuggestAddCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc < 4 || argc > 7) { return RedisModule_WrongArity(ctx); } int incr = 0, rv = AC_OK; RSPayload payload = {0}; ArgsCursor ac = {0}; ArgsCursor_InitRString(&ac, argv + 4, argc - 4); while (!AC_IsAtEnd(&ac)) { const char *s = AC_GetStringNC(&ac, NULL); if (!strcasecmp(s, "INCR")) { incr = 1; } else if (!strcasecmp(s, "PAYLOAD")) { if ((rv = AC_GetString(&ac, (const char **)&payload.data, &payload.len, 0)) != AC_OK) { return RMUtil_ReplyWithErrorFmt(ctx, "Invalid payload: %s", AC_Strerror(rv)); } } else { return RMUtil_ReplyWithErrorFmt(ctx, "Unknown argument `%s`", s); } } RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE); int type = RedisModule_KeyType(key); if (type != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(key) != TrieType) { return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); } RedisModuleString *val = argv[2]; double score; if ((RedisModule_StringToDouble(argv[3], &score) != REDISMODULE_OK)) { return RedisModule_ReplyWithError(ctx, "ERR invalid score"); } /* Create an empty value object if the key is currently empty. */ Trie *tree; if (type == REDISMODULE_KEYTYPE_EMPTY) { tree = NewTrie(); RedisModule_ModuleTypeSetValue(key, TrieType, tree); } else { tree = RedisModule_ModuleTypeGetValue(key); } /* Insert the new element. */ Trie_Insert(tree, val, score, incr, &payload); RedisModule_ReplyWithLongLong(ctx, tree->size); 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; }
/* HELLOTYPE.LEN key */ int HelloTypeLen_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 2) 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); } struct HelloTypeObject *hto = RedisModule_ModuleTypeGetValue(key); RedisModule_ReplyWithLongLong(ctx,hto ? hto->len : 0); return REDISMODULE_OK; }
void SpellCheck_Reply(SpellCheckCtx *scCtx, QueryAST *q) { if (!SpellCheck_CheckTermDictsExistance(scCtx)) { return; } RedisModule_ReplyWithArray(scCtx->sctx->redisCtx, REDISMODULE_POSTPONED_ARRAY_LEN); if (scCtx->fullScoreInfo) { // sending the total number of docs for the ability to calculate score on cluster RedisModule_ReplyWithLongLong(scCtx->sctx->redisCtx, scCtx->sctx->spec->docs.size - 1); } QueryNode_ForEach(q->root, forEachCallback, scCtx, 1); RedisModule_ReplySetArrayLength(scCtx->sctx->redisCtx, scCtx->results + (scCtx->fullScoreInfo ? 1 : 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; }
static int UniquePushCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, mergefn fn) { 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, *pval; pkey = RedisModule_StringPtrLen(argv[2], &skeylen); pval = RedisModule_StringPtrLen(argv[3], &svallen); skey = sdsnewlen(pkey, skeylen); sval = sdsnewlen(pval, svallen); int n = uniquePush(unique, skey, sval, fn); if (n == -1) { RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } else { RedisModule_ReplyWithLongLong(ctx, n); } RedisModule_ReplicateVerbatim(ctx); return REDISMODULE_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; }
/* * MSISMEMBER key1 [key2 ...] member * Checks for membership in multiple sets. * Reply: Integer, the count of sets to which the member belongs. */ int MSIsMemberCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc < 3) { if (RedisModule_IsKeysPositionRequest(ctx)) /* TODO: handle this once the getkey-api allows signalling errors */ return REDISMODULE_OK; else return RedisModule_WrongArity(ctx); } if (RedisModule_IsKeysPositionRequest(ctx)) { size_t i; for (i = 1; i < argc - 1; i++) RedisModule_KeyAtPos(ctx, i); return REDISMODULE_OK; } RedisModule_AutoMemory(ctx); int iele = argc - 1; size_t count = 0; int i; for (i = 1; i < iele; i++) { RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[i], REDISMODULE_READ | REDISMODULE_WRITE); if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) continue; if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_SET) { RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); return REDISMODULE_ERR; } RedisModuleCallReply *rep = RedisModule_Call(ctx, "SISMEMBER", "ss", argv[i], argv[iele]); RMUTIL_ASSERT_NOERROR(rep) count += RedisModule_CallReplyInteger(rep); } RedisModule_ReplyWithLongLong(ctx, count); return REDISMODULE_OK; }
/* HELLO.HCOPY key srcfield dstfield * This is just an example command that sets the hash field dstfield to the * same value of srcfield. If srcfield does not exist no operation is * performed. * * The command returns 1 if the copy is performed (srcfield exists) otherwise * 0 is returned. */ int HelloHCopy_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_HASH && type != REDISMODULE_KEYTYPE_EMPTY) { return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } /* Get the old field value. */ RedisModuleString *oldval; RedisModule_HashGet(key,REDISMODULE_HASH_NONE,argv[2],&oldval,NULL); if (oldval) { RedisModule_HashSet(key,REDISMODULE_HASH_NONE,argv[3],oldval,NULL); } RedisModule_ReplyWithLongLong(ctx,oldval != NULL); return REDISMODULE_OK; }
/* HELLO.SIMPLE is among the simplest commands you can implement. * It just returns the currently selected DB id, a functionality which is * missing in Redis. The command uses two important API calls: one to * fetch the currently selected DB, the other in order to send the client * an integer reply as response. */ int HelloSimple_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_ReplyWithLongLong(ctx,RedisModule_GetSelectedDb(ctx)); return REDISMODULE_OK; }