2 * Copyright (c) 2018-2019 Nokia.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
25 #include <sys/types.h>
29 /* ---------------- Defines common between core and modules --------------- */
34 /* Error status return values. */
35 #define REDISMODULE_OK 0
36 #define REDISMODULE_ERR 1
39 #define REDISMODULE_APIVER_1 1
41 /* API flags and constants */
42 #define REDISMODULE_READ (1<<0)
43 #define REDISMODULE_WRITE (1<<1)
45 #define REDISMODULE_LIST_HEAD 0
46 #define REDISMODULE_LIST_TAIL 1
49 #define REDISMODULE_KEYTYPE_EMPTY 0
50 #define REDISMODULE_KEYTYPE_STRING 1
51 #define REDISMODULE_KEYTYPE_LIST 2
52 #define REDISMODULE_KEYTYPE_HASH 3
53 #define REDISMODULE_KEYTYPE_SET 4
54 #define REDISMODULE_KEYTYPE_ZSET 5
55 #define REDISMODULE_KEYTYPE_MODULE 6
58 #define REDISMODULE_REPLY_UNKNOWN -1
59 #define REDISMODULE_REPLY_STRING 0
60 #define REDISMODULE_REPLY_ERROR 1
61 #define REDISMODULE_REPLY_INTEGER 2
62 #define REDISMODULE_REPLY_ARRAY 3
63 #define REDISMODULE_REPLY_NULL 4
65 /* Postponed array length. */
66 #define REDISMODULE_POSTPONED_ARRAY_LEN -1
69 #define REDISMODULE_NO_EXPIRE -1
71 /* Sorted set API flags. */
72 #define REDISMODULE_ZADD_XX (1<<0)
73 #define REDISMODULE_ZADD_NX (1<<1)
74 #define REDISMODULE_ZADD_ADDED (1<<2)
75 #define REDISMODULE_ZADD_UPDATED (1<<3)
76 #define REDISMODULE_ZADD_NOP (1<<4)
79 #define REDISMODULE_HASH_NONE 0
80 #define REDISMODULE_HASH_NX (1<<0)
81 #define REDISMODULE_HASH_XX (1<<1)
82 #define REDISMODULE_HASH_CFIELDS (1<<2)
83 #define REDISMODULE_HASH_EXISTS (1<<3)
85 /* Context Flags: Info about the current context returned by RM_GetContextFlags */
87 /* The command is running in the context of a Lua script */
88 #define REDISMODULE_CTX_FLAGS_LUA 0x0001
89 /* The command is running inside a Redis transaction */
90 #define REDISMODULE_CTX_FLAGS_MULTI 0x0002
91 /* The instance is a master */
92 #define REDISMODULE_CTX_FLAGS_MASTER 0x0004
93 /* The instance is a slave */
94 #define REDISMODULE_CTX_FLAGS_SLAVE 0x0008
95 /* The instance is read-only (usually meaning it's a slave as well) */
96 #define REDISMODULE_CTX_FLAGS_READONLY 0x0010
97 /* The instance is running in cluster mode */
98 #define REDISMODULE_CTX_FLAGS_CLUSTER 0x0020
99 /* The instance has AOF enabled */
100 #define REDISMODULE_CTX_FLAGS_AOF 0x0040 //
101 /* The instance has RDB enabled */
102 #define REDISMODULE_CTX_FLAGS_RDB 0x0080 //
103 /* The instance has Maxmemory set */
104 #define REDISMODULE_CTX_FLAGS_MAXMEMORY 0x0100
105 /* Maxmemory is set and has an eviction policy that may delete keys */
106 #define REDISMODULE_CTX_FLAGS_EVICT 0x0200
109 /* A special pointer that we can use between the core and the module to signal
110 * field deletion, and that is impossible to be a valid pointer. */
111 #define REDISMODULE_HASH_DELETE ((RedisModuleString*)(long)1)
113 /* Error messages. */
114 #define REDISMODULE_ERRORMSG_WRONGTYPE "WRONGTYPE Operation against a key holding the wrong kind of value"
116 #define REDISMODULE_POSITIVE_INFINITE (1.0/0.0)
117 #define REDISMODULE_NEGATIVE_INFINITE (-1.0/0.0)
119 #define REDISMODULE_NOT_USED(V) ((void) V)
121 /* ------------------------- End of common defines ------------------------ */
126 #ifndef REDISMODULE_CORE
130 typedef long long mstime_t;
132 /* Incomplete structures for compiler checks but opaque access. */
133 typedef struct RedisModuleCtx RedisModuleCtx;
134 typedef struct RedisModuleKey RedisModuleKey;
135 typedef struct RedisModuleString RedisModuleString;
136 typedef struct RedisModuleCallReply RedisModuleCallReply;
137 typedef struct RedisModuleIO RedisModuleIO;
138 typedef struct RedisModuleType RedisModuleType;
139 typedef struct RedisModuleDigest RedisModuleDigest;
140 typedef struct RedisModuleBlockedClient RedisModuleBlockedClient;
142 //typedef int (*RedisModuleCmdFunc) (RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
144 typedef void *(*RedisModuleTypeLoadFunc)(RedisModuleIO *rdb, int encver);
145 typedef void (*RedisModuleTypeSaveFunc)(RedisModuleIO *rdb, void *value);
146 typedef void (*RedisModuleTypeRewriteFunc)(RedisModuleIO *aof, RedisModuleString *key, void *value);
147 typedef size_t (*RedisModuleTypeMemUsageFunc)(const void *value);
148 typedef void (*RedisModuleTypeDigestFunc)(RedisModuleDigest *digest, void *value);
149 typedef void (*RedisModuleTypeFreeFunc)(void *value);
151 #define REDISMODULE_TYPE_METHOD_VERSION 1
152 typedef struct RedisModuleTypeMethods {
154 RedisModuleTypeLoadFunc rdb_load;
155 RedisModuleTypeSaveFunc rdb_save;
156 RedisModuleTypeRewriteFunc aof_rewrite;
157 RedisModuleTypeMemUsageFunc mem_usage;
158 RedisModuleTypeDigestFunc digest;
159 RedisModuleTypeFreeFunc free;
160 } RedisModuleTypeMethods;
162 #define REDISMODULE_GET_API(name) \
163 RedisModule_GetApi("RedisModule_" #name, ((void **)&RedisModule_ ## name))
165 #define REDISMODULE_API_FUNC(x) (*x)
170 typedef struct redisObject {
176 /* This structure represents a module inside the system. */
178 void *handle; /* Module dlopen() handle. */
179 char *name; /* Module name. */
180 int ver; /* Module version. We use just progressive integers. */
181 int apiver; /* Module API version as requested during initialization.*/
182 //list *types; /* Module data types. */
184 typedef struct RedisModule RedisModule;
186 //static dict *modules; /* Hash table of modules. SDS -> RedisModule ptr.*/
188 /* Entries in the context->amqueue array, representing objects to free
189 * when the callback returns. */
190 struct AutoMemEntry {
195 /* AutMemEntry type field values. */
196 #define REDISMODULE_AM_KEY 0
197 #define REDISMODULE_AM_STRING 1
198 #define REDISMODULE_AM_REPLY 2
199 #define REDISMODULE_AM_FREED 3 /* Explicitly freed by user already. */
201 /* The pool allocator block. Redis Modules can allocate memory via this special
202 * allocator that will automatically release it all once the callback returns.
203 * This means that it can only be used for ephemeral allocations. However
204 * there are two advantages for modules to use this API:
206 * 1) The memory is automatically released when the callback returns.
207 * 2) This allocator is faster for many small allocations since whole blocks
208 * are allocated, and small pieces returned to the caller just advancing
209 * the index of the allocation.
211 * Allocations are always rounded to the size of the void pointer in order
212 * to always return aligned memory chunks. */
214 #define REDISMODULE_POOL_ALLOC_MIN_SIZE (1024*8)
215 #define REDISMODULE_POOL_ALLOC_ALIGN (sizeof(void*))
217 typedef struct RedisModulePoolAllocBlock {
220 struct RedisModulePoolAllocBlock *next;
222 } RedisModulePoolAllocBlock;
224 /* This structure represents the context in which Redis modules operate.
225 * Most APIs module can access, get a pointer to the context, so that the API
226 * implementation can hold state across calls, or remember what to free after
227 * the call and so forth.
229 * Note that not all the context structure is always filled with actual values
230 * but only the fields needed in a given context. */
232 struct RedisModuleBlockedClient;
234 struct RedisModuleCtx {
235 void *getapifuncptr; /* NOTE: Must be the first field. */
236 struct RedisModule *module; /* Module reference. */
237 //client *client; /* Client calling a command. */
238 struct RedisModuleBlockedClient *blocked_client; /* Blocked client for
239 thread safe context. */
240 struct AutoMemEntry *amqueue; /* Auto memory queue of objects to free. */
241 int amqueue_len; /* Number of slots in amqueue. */
242 int amqueue_used; /* Number of used slots in amqueue. */
243 int flags; /* REDISMODULE_CTX_... flags. */
244 void **postponed_arrays; /* To set with RM_ReplySetArrayLength(). */
245 int postponed_arrays_count; /* Number of entries in postponed_arrays. */
246 void *blocked_privdata; /* Privdata set when unblocking a client. */
248 /* Used if there is the REDISMODULE_CTX_KEYS_POS_REQUEST flag set. */
252 struct RedisModulePoolAllocBlock *pa_head;
254 typedef struct RedisModuleCtx RedisModuleCtx;
256 #define REDISMODULE_CTX_INIT {(void*)(unsigned long)&RM_GetApi, NULL, NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL, NULL, 0, NULL}
257 #define REDISMODULE_CTX_MULTI_EMITTED (1<<0)
258 #define REDISMODULE_CTX_AUTO_MEMORY (1<<1)
259 #define REDISMODULE_CTX_KEYS_POS_REQUEST (1<<2)
260 #define REDISMODULE_CTX_BLOCKED_REPLY (1<<3)
261 #define REDISMODULE_CTX_BLOCKED_TIMEOUT (1<<4)
262 #define REDISMODULE_CTX_THREAD_SAFE (1<<5)
264 /* This represents a Redis key opened with RM_OpenKey(). */
265 struct RedisModuleKey {
268 robj *key; /* Key name object. */
269 robj *value; /* Value object, or NULL if the key was not found. */
270 void *iter; /* Iterator. */
271 int mode; /* Opening mode. */
274 uint32_t ztype; /* REDISMODULE_ZSET_RANGE_* */
275 //zrangespec zrs; /* Score range. */
276 //zlexrangespec zlrs; /* Lex range. */
277 uint32_t zstart; /* Start pos for positional ranges. */
278 uint32_t zend; /* End pos for positional ranges. */
279 void *zcurrent; /* Zset iterator current node. */
280 int zer; /* Zset iterator end reached flag
281 (true if end was reached). */
283 typedef struct RedisModuleKey RedisModuleKey;
285 /* RedisModuleKey 'ztype' values. */
286 #define REDISMODULE_ZSET_RANGE_NONE 0 /* This must always be 0. */
287 #define REDISMODULE_ZSET_RANGE_LEX 1
288 #define REDISMODULE_ZSET_RANGE_SCORE 2
289 #define REDISMODULE_ZSET_RANGE_POS 3
291 /* Function pointer type of a function representing a command inside
294 typedef int (*RedisModuleCmdFunc) (RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
297 /* This struct holds the information about a command registered by a module.*/
298 struct RedisModuleCommandProxy {
299 struct RedisModule *module;
300 RedisModuleCmdFunc func;
301 struct redisCommand *rediscmd;
303 typedef struct RedisModuleCommandProxy RedisModuleCommandProxy;
305 #define REDISMODULE_REPLYFLAG_NONE 0
306 #define REDISMODULE_REPLYFLAG_TOPARSE (1<<0) /* Protocol must be parsed. */
307 #define REDISMODULE_REPLYFLAG_NESTED (1<<1) /* Nested reply object. No proto
310 /* Reply of RM_Call() function. The function is filled in a lazy
311 * way depending on the function called on the reply structure. By default
312 * only the type, proto and protolen are filled. */
313 typedef struct RedisModuleCallReply {
315 int type; /* REDISMODULE_REPLY_... */
316 int flags; /* REDISMODULE_REPLYFLAG_... */
317 size_t len; /* Len of strings or num of elements of arrays. */
318 char *proto; /* Raw reply protocol. An SDS string at top-level object. */
319 size_t protolen;/* Length of protocol. */
321 const char *str; /* String pointer for string and error replies. This
322 does not need to be freed, always points inside
323 a reply->proto buffer of the reply object or, in
324 case of array elements, of parent reply objects. */
325 long long ll; /* Reply value for integer reply. */
326 struct RedisModuleCallReply *array; /* Array of sub-reply elements. */
328 } RedisModuleCallReply;
330 /* Structure representing a blocked client. We get a pointer to such
331 * an object when blocking from modules. */
332 typedef struct RedisModuleBlockedClient {
333 //client *client; /* Pointer to the blocked client. or NULL if the client
334 // was destroyed during the life of this object. */
335 RedisModule *module; /* Module blocking the client. */
336 RedisModuleCmdFunc reply_callback; /* Reply callback on normal completion.*/
337 RedisModuleCmdFunc timeout_callback; /* Reply callback on timeout. */
338 void (*free_privdata)(void *); /* privdata cleanup callback. */
339 void *privdata; /* Module private data that may be used by the reply
340 or timeout callback. It is set via the
341 RedisModule_UnblockClient() API. */
342 //client *reply_client; /* Fake client used to accumulate replies
343 // in thread safe contexts. */
344 int dbid; /* Database number selected by the original client. */
345 } RedisModuleBlockedClient;
347 #define RedisModuleString robj
354 int RedisModule_CreateCommand(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep);
355 int RedisModule_WrongArity(RedisModuleCtx *ctx);
356 int RedisModule_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll);
357 void *RedisModule_OpenKey(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode);
358 RedisModuleCallReply *RedisModule_Call(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...);
359 void RedisModule_FreeCallReply(RedisModuleCallReply *reply);
360 int RedisModule_CallReplyType(RedisModuleCallReply *reply);
361 long long RedisModule_CallReplyInteger(RedisModuleCallReply *reply);
362 const char *RedisModule_StringPtrLen(const RedisModuleString *str, size_t *len);
363 int RedisModule_ReplyWithError(RedisModuleCtx *ctx, const char *err);
364 int RedisModule_ReplyWithString(RedisModuleCtx *ctx, RedisModuleString *str);
365 int RedisModule_ReplyWithNull(RedisModuleCtx *ctx);
366 int RedisModule_ReplyWithCallReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply);
367 const char *RedisModule_CallReplyStringPtr(RedisModuleCallReply *reply, size_t *len);
368 RedisModuleString *RedisModule_CreateStringFromCallReply(RedisModuleCallReply *reply);
369 int RedisModule_StringToLongLong(const RedisModuleString *str, long long *ll);
371 int RedisModule_KeyType(RedisModuleKey *kp);
372 void RedisModule_CloseKey(RedisModuleKey *kp);
374 /* This is included inline inside each Redis module. */
375 int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver);
377 size_t RedisModule_CallReplyLength(RedisModuleCallReply *reply);
378 RedisModuleCallReply *RedisModule_CallReplyArrayElement(RedisModuleCallReply *reply, size_t idx);
379 int RedisModule_ReplyWithArray(RedisModuleCtx *ctx, long len);
382 /* Things only defined for the modules core, not exported to modules
383 * including this file. */
387 /* Things only defined for the modules core, not exported to modules
388 * including this file. */
389 #define RedisModuleString robj
391 #endif /* REDISMODULE_CORE */
392 #endif /* REDISMOUDLE_H */