Add base version of Redis modules
[ric-plt/dbaas.git] / redismodule / tst / mock / include / redismodule.h
1 /*
2  * Copyright (c) 2018-2019 Nokia.
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16
17 #ifndef REDISMODULE_H
18 #define REDISMODULE_H
19
20 #include <sys/types.h>
21 #include <stdint.h>
22 #include <stdio.h>
23
24 /* ---------------- Defines common between core and modules --------------- */
25
26
27
28
29 /* Error status return values. */
30 #define REDISMODULE_OK 0
31 #define REDISMODULE_ERR 1
32
33 /* API versions. */
34 #define REDISMODULE_APIVER_1 1
35
36 /* API flags and constants */
37 #define REDISMODULE_READ (1<<0)
38 #define REDISMODULE_WRITE (1<<1)
39
40 #define REDISMODULE_LIST_HEAD 0
41 #define REDISMODULE_LIST_TAIL 1
42
43 /* Key types. */
44 #define REDISMODULE_KEYTYPE_EMPTY 0
45 #define REDISMODULE_KEYTYPE_STRING 1
46 #define REDISMODULE_KEYTYPE_LIST 2
47 #define REDISMODULE_KEYTYPE_HASH 3
48 #define REDISMODULE_KEYTYPE_SET 4
49 #define REDISMODULE_KEYTYPE_ZSET 5
50 #define REDISMODULE_KEYTYPE_MODULE 6
51
52 /* Reply types. */
53 #define REDISMODULE_REPLY_UNKNOWN -1
54 #define REDISMODULE_REPLY_STRING 0
55 #define REDISMODULE_REPLY_ERROR 1
56 #define REDISMODULE_REPLY_INTEGER 2
57 #define REDISMODULE_REPLY_ARRAY 3
58 #define REDISMODULE_REPLY_NULL 4
59
60 /* Postponed array length. */
61 #define REDISMODULE_POSTPONED_ARRAY_LEN -1
62
63 /* Expire */
64 #define REDISMODULE_NO_EXPIRE -1
65
66 /* Sorted set API flags. */
67 #define REDISMODULE_ZADD_XX      (1<<0)
68 #define REDISMODULE_ZADD_NX      (1<<1)
69 #define REDISMODULE_ZADD_ADDED   (1<<2)
70 #define REDISMODULE_ZADD_UPDATED (1<<3)
71 #define REDISMODULE_ZADD_NOP     (1<<4)
72
73 /* Hash API flags. */
74 #define REDISMODULE_HASH_NONE       0
75 #define REDISMODULE_HASH_NX         (1<<0)
76 #define REDISMODULE_HASH_XX         (1<<1)
77 #define REDISMODULE_HASH_CFIELDS    (1<<2)
78 #define REDISMODULE_HASH_EXISTS     (1<<3)
79
80 /* Context Flags: Info about the current context returned by RM_GetContextFlags */
81
82 /* The command is running in the context of a Lua script */
83 #define REDISMODULE_CTX_FLAGS_LUA 0x0001
84 /* The command is running inside a Redis transaction */
85 #define REDISMODULE_CTX_FLAGS_MULTI 0x0002
86 /* The instance is a master */
87 #define REDISMODULE_CTX_FLAGS_MASTER 0x0004
88 /* The instance is a slave */
89 #define REDISMODULE_CTX_FLAGS_SLAVE 0x0008
90 /* The instance is read-only (usually meaning it's a slave as well) */
91 #define REDISMODULE_CTX_FLAGS_READONLY 0x0010
92 /* The instance is running in cluster mode */
93 #define REDISMODULE_CTX_FLAGS_CLUSTER 0x0020
94 /* The instance has AOF enabled */
95 #define REDISMODULE_CTX_FLAGS_AOF 0x0040 //
96 /* The instance has RDB enabled */
97 #define REDISMODULE_CTX_FLAGS_RDB 0x0080 //
98 /* The instance has Maxmemory set */
99 #define REDISMODULE_CTX_FLAGS_MAXMEMORY 0x0100
100 /* Maxmemory is set and has an eviction policy that may delete keys */
101 #define REDISMODULE_CTX_FLAGS_EVICT 0x0200
102
103
104 /* A special pointer that we can use between the core and the module to signal
105  * field deletion, and that is impossible to be a valid pointer. */
106 #define REDISMODULE_HASH_DELETE ((RedisModuleString*)(long)1)
107
108 /* Error messages. */
109 #define REDISMODULE_ERRORMSG_WRONGTYPE "WRONGTYPE Operation against a key holding the wrong kind of value"
110
111 #define REDISMODULE_POSITIVE_INFINITE (1.0/0.0)
112 #define REDISMODULE_NEGATIVE_INFINITE (-1.0/0.0)
113
114 #define REDISMODULE_NOT_USED(V) ((void) V)
115
116 /* ------------------------- End of common defines ------------------------ */
117
118
119
120
121 #ifndef REDISMODULE_CORE
122
123
124
125 typedef long long mstime_t;
126
127 /* Incomplete structures for compiler checks but opaque access. */
128 typedef struct RedisModuleCtx RedisModuleCtx;
129 typedef struct RedisModuleKey RedisModuleKey;
130 typedef struct RedisModuleString RedisModuleString;
131 typedef struct RedisModuleCallReply RedisModuleCallReply;
132 typedef struct RedisModuleIO RedisModuleIO;
133 typedef struct RedisModuleType RedisModuleType;
134 typedef struct RedisModuleDigest RedisModuleDigest;
135 typedef struct RedisModuleBlockedClient RedisModuleBlockedClient;
136
137 //typedef int (*RedisModuleCmdFunc) (RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
138
139 typedef void *(*RedisModuleTypeLoadFunc)(RedisModuleIO *rdb, int encver);
140 typedef void (*RedisModuleTypeSaveFunc)(RedisModuleIO *rdb, void *value);
141 typedef void (*RedisModuleTypeRewriteFunc)(RedisModuleIO *aof, RedisModuleString *key, void *value);
142 typedef size_t (*RedisModuleTypeMemUsageFunc)(const void *value);
143 typedef void (*RedisModuleTypeDigestFunc)(RedisModuleDigest *digest, void *value);
144 typedef void (*RedisModuleTypeFreeFunc)(void *value);
145
146 #define REDISMODULE_TYPE_METHOD_VERSION 1
147 typedef struct RedisModuleTypeMethods {
148     uint64_t version;
149     RedisModuleTypeLoadFunc rdb_load;
150     RedisModuleTypeSaveFunc rdb_save;
151     RedisModuleTypeRewriteFunc aof_rewrite;
152     RedisModuleTypeMemUsageFunc mem_usage;
153     RedisModuleTypeDigestFunc digest;
154     RedisModuleTypeFreeFunc free;
155 } RedisModuleTypeMethods;
156
157 #define REDISMODULE_GET_API(name) \
158     RedisModule_GetApi("RedisModule_" #name, ((void **)&RedisModule_ ## name))
159
160 #define REDISMODULE_API_FUNC(x) (*x)
161
162 #if 1
163
164
165 typedef struct redisObject {
166     unsigned type:4;
167     unsigned encoding:4;
168     int refcount;
169     void *ptr;
170 } robj;
171 /* This structure represents a module inside the system. */
172 struct RedisModule {
173     void *handle;   /* Module dlopen() handle. */
174     char *name;     /* Module name. */
175     int ver;        /* Module version. We use just progressive integers. */
176     int apiver;     /* Module API version as requested during initialization.*/
177     //list *types;    /* Module data types. */
178 };
179 typedef struct RedisModule RedisModule;
180
181 //static dict *modules; /* Hash table of modules. SDS -> RedisModule ptr.*/
182
183 /* Entries in the context->amqueue array, representing objects to free
184  * when the callback returns. */
185 struct AutoMemEntry {
186     void *ptr;
187     int type;
188 };
189
190 /* AutMemEntry type field values. */
191 #define REDISMODULE_AM_KEY 0
192 #define REDISMODULE_AM_STRING 1
193 #define REDISMODULE_AM_REPLY 2
194 #define REDISMODULE_AM_FREED 3 /* Explicitly freed by user already. */
195
196 /* The pool allocator block. Redis Modules can allocate memory via this special
197  * allocator that will automatically release it all once the callback returns.
198  * This means that it can only be used for ephemeral allocations. However
199  * there are two advantages for modules to use this API:
200  *
201  * 1) The memory is automatically released when the callback returns.
202  * 2) This allocator is faster for many small allocations since whole blocks
203  *    are allocated, and small pieces returned to the caller just advancing
204  *    the index of the allocation.
205  *
206  * Allocations are always rounded to the size of the void pointer in order
207  * to always return aligned memory chunks. */
208
209 #define REDISMODULE_POOL_ALLOC_MIN_SIZE (1024*8)
210 #define REDISMODULE_POOL_ALLOC_ALIGN (sizeof(void*))
211
212 typedef struct RedisModulePoolAllocBlock {
213     uint32_t size;
214     uint32_t used;
215     struct RedisModulePoolAllocBlock *next;
216     char memory[];
217 } RedisModulePoolAllocBlock;
218
219 /* This structure represents the context in which Redis modules operate.
220  * Most APIs module can access, get a pointer to the context, so that the API
221  * implementation can hold state across calls, or remember what to free after
222  * the call and so forth.
223  *
224  * Note that not all the context structure is always filled with actual values
225  * but only the fields needed in a given context. */
226
227 struct RedisModuleBlockedClient;
228
229 struct RedisModuleCtx {
230     void *getapifuncptr;            /* NOTE: Must be the first field. */
231     struct RedisModule *module;     /* Module reference. */
232     //client *client;                 /* Client calling a command. */
233     struct RedisModuleBlockedClient *blocked_client; /* Blocked client for
234                                                         thread safe context. */
235     struct AutoMemEntry *amqueue;   /* Auto memory queue of objects to free. */
236     int amqueue_len;                /* Number of slots in amqueue. */
237     int amqueue_used;               /* Number of used slots in amqueue. */
238     int flags;                      /* REDISMODULE_CTX_... flags. */
239     void **postponed_arrays;        /* To set with RM_ReplySetArrayLength(). */
240     int postponed_arrays_count;     /* Number of entries in postponed_arrays. */
241     void *blocked_privdata;         /* Privdata set when unblocking a client. */
242
243     /* Used if there is the REDISMODULE_CTX_KEYS_POS_REQUEST flag set. */
244     int *keys_pos;
245     int keys_count;
246
247     struct RedisModulePoolAllocBlock *pa_head;
248 };
249 typedef struct RedisModuleCtx RedisModuleCtx;
250
251 #define REDISMODULE_CTX_INIT {(void*)(unsigned long)&RM_GetApi, NULL, NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL, NULL, 0, NULL}
252 #define REDISMODULE_CTX_MULTI_EMITTED (1<<0)
253 #define REDISMODULE_CTX_AUTO_MEMORY (1<<1)
254 #define REDISMODULE_CTX_KEYS_POS_REQUEST (1<<2)
255 #define REDISMODULE_CTX_BLOCKED_REPLY (1<<3)
256 #define REDISMODULE_CTX_BLOCKED_TIMEOUT (1<<4)
257 #define REDISMODULE_CTX_THREAD_SAFE (1<<5)
258
259 /* This represents a Redis key opened with RM_OpenKey(). */
260 struct RedisModuleKey {
261     RedisModuleCtx *ctx;
262     //redisDb *db;
263     robj *key;      /* Key name object. */
264     robj *value;    /* Value object, or NULL if the key was not found. */
265     void *iter;     /* Iterator. */
266     int mode;       /* Opening mode. */
267
268     /* Zset iterator. */
269     uint32_t ztype;         /* REDISMODULE_ZSET_RANGE_* */
270     //zrangespec zrs;         /* Score range. */
271     //zlexrangespec zlrs;     /* Lex range. */
272     uint32_t zstart;        /* Start pos for positional ranges. */
273     uint32_t zend;          /* End pos for positional ranges. */
274     void *zcurrent;         /* Zset iterator current node. */
275     int zer;                /* Zset iterator end reached flag
276                                (true if end was reached). */
277 };
278 typedef struct RedisModuleKey RedisModuleKey;
279
280 /* RedisModuleKey 'ztype' values. */
281 #define REDISMODULE_ZSET_RANGE_NONE 0       /* This must always be 0. */
282 #define REDISMODULE_ZSET_RANGE_LEX 1
283 #define REDISMODULE_ZSET_RANGE_SCORE 2
284 #define REDISMODULE_ZSET_RANGE_POS 3
285
286 /* Function pointer type of a function representing a command inside
287  * a Redis module. */
288
289 typedef int (*RedisModuleCmdFunc) (RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
290
291
292 /* This struct holds the information about a command registered by a module.*/
293 struct RedisModuleCommandProxy {
294     struct RedisModule *module;
295     RedisModuleCmdFunc func;
296     struct redisCommand *rediscmd;
297 };
298 typedef struct RedisModuleCommandProxy RedisModuleCommandProxy;
299
300 #define REDISMODULE_REPLYFLAG_NONE 0
301 #define REDISMODULE_REPLYFLAG_TOPARSE (1<<0) /* Protocol must be parsed. */
302 #define REDISMODULE_REPLYFLAG_NESTED (1<<1)  /* Nested reply object. No proto
303                                                 or struct free. */
304
305 /* Reply of RM_Call() function. The function is filled in a lazy
306  * way depending on the function called on the reply structure. By default
307  * only the type, proto and protolen are filled. */
308 typedef struct RedisModuleCallReply {
309     RedisModuleCtx *ctx;
310     int type;       /* REDISMODULE_REPLY_... */
311     int flags;      /* REDISMODULE_REPLYFLAG_...  */
312     size_t len;     /* Len of strings or num of elements of arrays. */
313     char *proto;    /* Raw reply protocol. An SDS string at top-level object. */
314     size_t protolen;/* Length of protocol. */
315     union {
316         const char *str; /* String pointer for string and error replies. This
317                             does not need to be freed, always points inside
318                             a reply->proto buffer of the reply object or, in
319                             case of array elements, of parent reply objects. */
320         long long ll;    /* Reply value for integer reply. */
321         struct RedisModuleCallReply *array; /* Array of sub-reply elements. */
322     } val;
323 } RedisModuleCallReply;
324
325 /* Structure representing a blocked client. We get a pointer to such
326  * an object when blocking from modules. */
327 typedef struct RedisModuleBlockedClient {
328     //client *client;  /* Pointer to the blocked client. or NULL if the client
329      //                   was destroyed during the life of this object. */
330     RedisModule *module;    /* Module blocking the client. */
331     RedisModuleCmdFunc reply_callback; /* Reply callback on normal completion.*/
332     RedisModuleCmdFunc timeout_callback; /* Reply callback on timeout. */
333     void (*free_privdata)(void *);       /* privdata cleanup callback. */
334     void *privdata;     /* Module private data that may be used by the reply
335                            or timeout callback. It is set via the
336                            RedisModule_UnblockClient() API. */
337     //client *reply_client;           /* Fake client used to accumulate replies
338    //                                    in thread safe contexts. */
339     int dbid;           /* Database number selected by the original client. */
340 } RedisModuleBlockedClient;
341
342 #define RedisModuleString robj
343
344 #endif
345
346
347
348
349 int RedisModule_CreateCommand(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep);
350 int RedisModule_WrongArity(RedisModuleCtx *ctx);
351 int RedisModule_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll);
352 void *RedisModule_OpenKey(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode);
353 RedisModuleCallReply *RedisModule_Call(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...);
354 void RedisModule_FreeCallReply(RedisModuleCallReply *reply);
355 int RedisModule_CallReplyType(RedisModuleCallReply *reply);
356 long long RedisModule_CallReplyInteger(RedisModuleCallReply *reply);
357 const char *RedisModule_StringPtrLen(const RedisModuleString *str, size_t *len);
358 int RedisModule_ReplyWithError(RedisModuleCtx *ctx, const char *err);
359 int RedisModule_ReplyWithString(RedisModuleCtx *ctx, RedisModuleString *str);
360 int RedisModule_ReplyWithNull(RedisModuleCtx *ctx);
361 int RedisModule_ReplyWithCallReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply);
362 const char *RedisModule_CallReplyStringPtr(RedisModuleCallReply *reply, size_t *len);
363 RedisModuleString *RedisModule_CreateStringFromCallReply(RedisModuleCallReply *reply);
364
365 int RedisModule_KeyType(RedisModuleKey *kp);
366 void RedisModule_CloseKey(RedisModuleKey *kp);
367
368 /* This is included inline inside each Redis module. */
369 int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver);
370
371 size_t RedisModule_CallReplyLength(RedisModuleCallReply *reply);
372 RedisModuleCallReply *RedisModule_CallReplyArrayElement(RedisModuleCallReply *reply, size_t idx);
373 int RedisModule_ReplyWithArray(RedisModuleCtx *ctx, long len);
374
375
376 /* Things only defined for the modules core, not exported to modules
377  * including this file. */
378
379 #else
380
381 /* Things only defined for the modules core, not exported to modules
382  * including this file. */
383 #define RedisModuleString robj
384
385 #endif /* REDISMODULE_CORE */
386 #endif /* REDISMOUDLE_H */