summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/modules/hellodict.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/src/modules/hellodict.c')
-rw-r--r--examples/redis-unstable/src/modules/hellodict.c111
1 files changed, 0 insertions, 111 deletions
diff --git a/examples/redis-unstable/src/modules/hellodict.c b/examples/redis-unstable/src/modules/hellodict.c
deleted file mode 100644
index 00c4964..0000000
--- a/examples/redis-unstable/src/modules/hellodict.c
+++ /dev/null
@@ -1,111 +0,0 @@
1/* Hellodict -- An example of modules dictionary API
2 *
3 * This module implements a volatile key-value store on top of the
4 * dictionary exported by the Redis modules API.
5 *
6 * -----------------------------------------------------------------------------
7 *
8 * Copyright (c) 2018-Present, Redis Ltd.
9 * All rights reserved.
10 *
11 * Licensed under your choice of (a) the Redis Source Available License 2.0
12 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
13 * GNU Affero General Public License v3 (AGPLv3).
14 */
15
16#include "../redismodule.h"
17#include <stdio.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include <string.h>
21
22static RedisModuleDict *Keyspace;
23
24/* HELLODICT.SET <key> <value>
25 *
26 * Set the specified key to the specified value. */
27int cmd_SET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
28 if (argc != 3) return RedisModule_WrongArity(ctx);
29 RedisModule_DictSet(Keyspace,argv[1],argv[2]);
30 /* We need to keep a reference to the value stored at the key, otherwise
31 * it would be freed when this callback returns. */
32 RedisModule_RetainString(NULL,argv[2]);
33 return RedisModule_ReplyWithSimpleString(ctx, "OK");
34}
35
36/* HELLODICT.GET <key>
37 *
38 * Return the value of the specified key, or a null reply if the key
39 * is not defined. */
40int cmd_GET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
41 if (argc != 2) return RedisModule_WrongArity(ctx);
42 RedisModuleString *val = RedisModule_DictGet(Keyspace,argv[1],NULL);
43 if (val == NULL) {
44 return RedisModule_ReplyWithNull(ctx);
45 } else {
46 return RedisModule_ReplyWithString(ctx, val);
47 }
48}
49
50/* HELLODICT.KEYRANGE <startkey> <endkey> <count>
51 *
52 * Return a list of matching keys, lexicographically between startkey
53 * and endkey. No more than 'count' items are emitted. */
54int cmd_KEYRANGE(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
55 if (argc != 4) return RedisModule_WrongArity(ctx);
56
57 /* Parse the count argument. */
58 long long count;
59 if (RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK) {
60 return RedisModule_ReplyWithError(ctx,"ERR invalid count");
61 }
62
63 /* Seek the iterator. */
64 RedisModuleDictIter *iter = RedisModule_DictIteratorStart(
65 Keyspace, ">=", argv[1]);
66
67 /* Reply with the matching items. */
68 char *key;
69 size_t keylen;
70 long long replylen = 0; /* Keep track of the emitted array len. */
71 RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_LEN);
72 while((key = RedisModule_DictNextC(iter,&keylen,NULL)) != NULL) {
73 if (replylen >= count) break;
74 if (RedisModule_DictCompare(iter,"<=",argv[2]) == REDISMODULE_ERR)
75 break;
76 RedisModule_ReplyWithStringBuffer(ctx,key,keylen);
77 replylen++;
78 }
79 RedisModule_ReplySetArrayLength(ctx,replylen);
80
81 /* Cleanup. */
82 RedisModule_DictIteratorStop(iter);
83 return REDISMODULE_OK;
84}
85
86/* This function must be present on each Redis module. It is used in order to
87 * register the commands into the Redis server. */
88int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
89 REDISMODULE_NOT_USED(argv);
90 REDISMODULE_NOT_USED(argc);
91
92 if (RedisModule_Init(ctx,"hellodict",1,REDISMODULE_APIVER_1)
93 == REDISMODULE_ERR) return REDISMODULE_ERR;
94
95 if (RedisModule_CreateCommand(ctx,"hellodict.set",
96 cmd_SET,"write deny-oom",1,1,0) == REDISMODULE_ERR)
97 return REDISMODULE_ERR;
98
99 if (RedisModule_CreateCommand(ctx,"hellodict.get",
100 cmd_GET,"readonly",1,1,0) == REDISMODULE_ERR)
101 return REDISMODULE_ERR;
102
103 if (RedisModule_CreateCommand(ctx,"hellodict.keyrange",
104 cmd_KEYRANGE,"readonly",1,1,0) == REDISMODULE_ERR)
105 return REDISMODULE_ERR;
106
107 /* Create our global dictionary. Here we'll set our keys and values. */
108 Keyspace = RedisModule_CreateDict(NULL);
109
110 return REDISMODULE_OK;
111}