1*572c4311Sfengbojiang /* Hellodict -- An example of modules dictionary API
2*572c4311Sfengbojiang  *
3*572c4311Sfengbojiang  * This module implements a volatile key-value store on top of the
4*572c4311Sfengbojiang  * dictionary exported by the Redis modules API.
5*572c4311Sfengbojiang  *
6*572c4311Sfengbojiang  * -----------------------------------------------------------------------------
7*572c4311Sfengbojiang  *
8*572c4311Sfengbojiang  * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
9*572c4311Sfengbojiang  * All rights reserved.
10*572c4311Sfengbojiang  *
11*572c4311Sfengbojiang  * Redistribution and use in source and binary forms, with or without
12*572c4311Sfengbojiang  * modification, are permitted provided that the following conditions are met:
13*572c4311Sfengbojiang  *
14*572c4311Sfengbojiang  *   * Redistributions of source code must retain the above copyright notice,
15*572c4311Sfengbojiang  *     this list of conditions and the following disclaimer.
16*572c4311Sfengbojiang  *   * Redistributions in binary form must reproduce the above copyright
17*572c4311Sfengbojiang  *     notice, this list of conditions and the following disclaimer in the
18*572c4311Sfengbojiang  *     documentation and/or other materials provided with the distribution.
19*572c4311Sfengbojiang  *   * Neither the name of Redis nor the names of its contributors may be used
20*572c4311Sfengbojiang  *     to endorse or promote products derived from this software without
21*572c4311Sfengbojiang  *     specific prior written permission.
22*572c4311Sfengbojiang  *
23*572c4311Sfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24*572c4311Sfengbojiang  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*572c4311Sfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*572c4311Sfengbojiang  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27*572c4311Sfengbojiang  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*572c4311Sfengbojiang  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*572c4311Sfengbojiang  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30*572c4311Sfengbojiang  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31*572c4311Sfengbojiang  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32*572c4311Sfengbojiang  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33*572c4311Sfengbojiang  * POSSIBILITY OF SUCH DAMAGE.
34*572c4311Sfengbojiang  */
35*572c4311Sfengbojiang 
36*572c4311Sfengbojiang #define REDISMODULE_EXPERIMENTAL_API
37*572c4311Sfengbojiang #include "../redismodule.h"
38*572c4311Sfengbojiang #include <stdio.h>
39*572c4311Sfengbojiang #include <stdlib.h>
40*572c4311Sfengbojiang #include <ctype.h>
41*572c4311Sfengbojiang #include <string.h>
42*572c4311Sfengbojiang 
43*572c4311Sfengbojiang static RedisModuleDict *Keyspace;
44*572c4311Sfengbojiang 
45*572c4311Sfengbojiang /* HELLODICT.SET <key> <value>
46*572c4311Sfengbojiang  *
47*572c4311Sfengbojiang  * Set the specified key to the specified value. */
cmd_SET(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)48*572c4311Sfengbojiang int cmd_SET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
49*572c4311Sfengbojiang     if (argc != 3) return RedisModule_WrongArity(ctx);
50*572c4311Sfengbojiang     RedisModule_DictSet(Keyspace,argv[1],argv[2]);
51*572c4311Sfengbojiang     /* We need to keep a reference to the value stored at the key, otherwise
52*572c4311Sfengbojiang      * it would be freed when this callback returns. */
53*572c4311Sfengbojiang     RedisModule_RetainString(NULL,argv[2]);
54*572c4311Sfengbojiang     return RedisModule_ReplyWithSimpleString(ctx, "OK");
55*572c4311Sfengbojiang }
56*572c4311Sfengbojiang 
57*572c4311Sfengbojiang /* HELLODICT.GET <key>
58*572c4311Sfengbojiang  *
59*572c4311Sfengbojiang  * Return the value of the specified key, or a null reply if the key
60*572c4311Sfengbojiang  * is not defined. */
cmd_GET(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)61*572c4311Sfengbojiang int cmd_GET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
62*572c4311Sfengbojiang     if (argc != 2) return RedisModule_WrongArity(ctx);
63*572c4311Sfengbojiang     RedisModuleString *val = RedisModule_DictGet(Keyspace,argv[1],NULL);
64*572c4311Sfengbojiang     if (val == NULL) {
65*572c4311Sfengbojiang         return RedisModule_ReplyWithNull(ctx);
66*572c4311Sfengbojiang     } else {
67*572c4311Sfengbojiang         return RedisModule_ReplyWithString(ctx, val);
68*572c4311Sfengbojiang     }
69*572c4311Sfengbojiang }
70*572c4311Sfengbojiang 
71*572c4311Sfengbojiang /* HELLODICT.KEYRANGE <startkey> <endkey> <count>
72*572c4311Sfengbojiang  *
73*572c4311Sfengbojiang  * Return a list of matching keys, lexicographically between startkey
74*572c4311Sfengbojiang  * and endkey. No more than 'count' items are emitted. */
cmd_KEYRANGE(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)75*572c4311Sfengbojiang int cmd_KEYRANGE(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
76*572c4311Sfengbojiang     if (argc != 4) return RedisModule_WrongArity(ctx);
77*572c4311Sfengbojiang 
78*572c4311Sfengbojiang     /* Parse the count argument. */
79*572c4311Sfengbojiang     long long count;
80*572c4311Sfengbojiang     if (RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK) {
81*572c4311Sfengbojiang         return RedisModule_ReplyWithError(ctx,"ERR invalid count");
82*572c4311Sfengbojiang     }
83*572c4311Sfengbojiang 
84*572c4311Sfengbojiang     /* Seek the iterator. */
85*572c4311Sfengbojiang     RedisModuleDictIter *iter = RedisModule_DictIteratorStart(
86*572c4311Sfengbojiang         Keyspace, ">=", argv[1]);
87*572c4311Sfengbojiang 
88*572c4311Sfengbojiang     /* Reply with the matching items. */
89*572c4311Sfengbojiang     char *key;
90*572c4311Sfengbojiang     size_t keylen;
91*572c4311Sfengbojiang     long long replylen = 0; /* Keep track of the amitted array len. */
92*572c4311Sfengbojiang     RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);
93*572c4311Sfengbojiang     while((key = RedisModule_DictNextC(iter,&keylen,NULL)) != NULL) {
94*572c4311Sfengbojiang         if (replylen >= count) break;
95*572c4311Sfengbojiang         if (RedisModule_DictCompare(iter,"<=",argv[2]) == REDISMODULE_ERR)
96*572c4311Sfengbojiang             break;
97*572c4311Sfengbojiang         RedisModule_ReplyWithStringBuffer(ctx,key,keylen);
98*572c4311Sfengbojiang         replylen++;
99*572c4311Sfengbojiang     }
100*572c4311Sfengbojiang     RedisModule_ReplySetArrayLength(ctx,replylen);
101*572c4311Sfengbojiang 
102*572c4311Sfengbojiang     /* Cleanup. */
103*572c4311Sfengbojiang     RedisModule_DictIteratorStop(iter);
104*572c4311Sfengbojiang     return REDISMODULE_OK;
105*572c4311Sfengbojiang }
106*572c4311Sfengbojiang 
107*572c4311Sfengbojiang /* This function must be present on each Redis module. It is used in order to
108*572c4311Sfengbojiang  * register the commands into the Redis server. */
RedisModule_OnLoad(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)109*572c4311Sfengbojiang int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
110*572c4311Sfengbojiang     REDISMODULE_NOT_USED(argv);
111*572c4311Sfengbojiang     REDISMODULE_NOT_USED(argc);
112*572c4311Sfengbojiang 
113*572c4311Sfengbojiang     if (RedisModule_Init(ctx,"hellodict",1,REDISMODULE_APIVER_1)
114*572c4311Sfengbojiang         == REDISMODULE_ERR) return REDISMODULE_ERR;
115*572c4311Sfengbojiang 
116*572c4311Sfengbojiang     if (RedisModule_CreateCommand(ctx,"hellodict.set",
117*572c4311Sfengbojiang         cmd_SET,"write deny-oom",1,1,0) == REDISMODULE_ERR)
118*572c4311Sfengbojiang         return REDISMODULE_ERR;
119*572c4311Sfengbojiang 
120*572c4311Sfengbojiang     if (RedisModule_CreateCommand(ctx,"hellodict.get",
121*572c4311Sfengbojiang         cmd_GET,"readonly",1,1,0) == REDISMODULE_ERR)
122*572c4311Sfengbojiang         return REDISMODULE_ERR;
123*572c4311Sfengbojiang 
124*572c4311Sfengbojiang     if (RedisModule_CreateCommand(ctx,"hellodict.keyrange",
125*572c4311Sfengbojiang         cmd_KEYRANGE,"readonly",1,1,0) == REDISMODULE_ERR)
126*572c4311Sfengbojiang         return REDISMODULE_ERR;
127*572c4311Sfengbojiang 
128*572c4311Sfengbojiang     /* Create our global dictionray. Here we'll set our keys and values. */
129*572c4311Sfengbojiang     Keyspace = RedisModule_CreateDict(NULL);
130*572c4311Sfengbojiang 
131*572c4311Sfengbojiang     return REDISMODULE_OK;
132*572c4311Sfengbojiang }
133