1 | /* |
2 | * Copyright (c) 2021, Redis Ltd. |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright notice, |
9 | * this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * * Neither the name of Redis nor the names of its contributors may be used |
14 | * to endorse or promote products derived from this software without |
15 | * specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #ifndef __FUNCTIONS_H_ |
31 | #define __FUNCTIONS_H_ |
32 | |
33 | /* |
34 | * functions.c unit provides the Redis Functions API: |
35 | * * FUNCTION CREATE |
36 | * * FUNCTION CALL |
37 | * * FUNCTION DELETE |
38 | * * FUNCTION KILL |
39 | * * FUNCTION INFO |
40 | * |
41 | * Also contains implementation for: |
42 | * * Save/Load function from rdb |
43 | * * Register engines |
44 | */ |
45 | |
46 | #include "server.h" |
47 | #include "script.h" |
48 | #include "redismodule.h" |
49 | |
50 | typedef struct functionLibInfo functionLibInfo; |
51 | |
52 | typedef struct engine { |
53 | /* engine specific context */ |
54 | void *engine_ctx; |
55 | |
56 | /* Create function callback, get the engine_ctx, and function code. |
57 | * returns NULL on error and set sds to be the error message */ |
58 | int (*create)(void *engine_ctx, functionLibInfo *li, sds code, sds *err); |
59 | |
60 | /* Invoking a function, r_ctx is an opaque object (from engine POV). |
61 | * The r_ctx should be used by the engine to interaction with Redis, |
62 | * such interaction could be running commands, set resp, or set |
63 | * replication mode |
64 | */ |
65 | void (*call)(scriptRunCtx *r_ctx, void *engine_ctx, void *compiled_function, |
66 | robj **keys, size_t nkeys, robj **args, size_t nargs); |
67 | |
68 | /* get current used memory by the engine */ |
69 | size_t (*get_used_memory)(void *engine_ctx); |
70 | |
71 | /* Return memory overhead for a given function, |
72 | * such memory is not counted as engine memory but as general |
73 | * structs memory that hold different information */ |
74 | size_t (*get_function_memory_overhead)(void *compiled_function); |
75 | |
76 | /* Return memory overhead for engine (struct size holding the engine)*/ |
77 | size_t (*get_engine_memory_overhead)(void *engine_ctx); |
78 | |
79 | /* free the given function */ |
80 | void (*free_function)(void *engine_ctx, void *compiled_function); |
81 | } engine; |
82 | |
83 | /* Hold information about an engine. |
84 | * Used on rdb.c so it must be declared here. */ |
85 | typedef struct engineInfo { |
86 | sds name; /* Name of the engine */ |
87 | engine *engine; /* engine callbacks that allows to interact with the engine */ |
88 | client *c; /* Client that is used to run commands */ |
89 | } engineInfo; |
90 | |
91 | /* Hold information about the specific function. |
92 | * Used on rdb.c so it must be declared here. */ |
93 | typedef struct functionInfo { |
94 | sds name; /* Function name */ |
95 | void *function; /* Opaque object that set by the function's engine and allow it |
96 | to run the function, usually it's the function compiled code. */ |
97 | functionLibInfo* li; /* Pointer to the library created the function */ |
98 | sds desc; /* Function description */ |
99 | uint64_t f_flags; /* Function flags */ |
100 | } functionInfo; |
101 | |
102 | /* Hold information about the specific library. |
103 | * Used on rdb.c so it must be declared here. */ |
104 | struct functionLibInfo { |
105 | sds name; /* Library name */ |
106 | dict *functions; /* Functions dictionary */ |
107 | engineInfo *ei; /* Pointer to the function engine */ |
108 | sds code; /* Library code */ |
109 | }; |
110 | |
111 | int functionsRegisterEngine(const char *engine_name, engine *engine_ctx); |
112 | sds functionsCreateWithLibraryCtx(sds code, int replace, sds* err, functionsLibCtx *lib_ctx); |
113 | unsigned long functionsMemory(); |
114 | unsigned long functionsMemoryOverhead(); |
115 | unsigned long functionsNum(); |
116 | unsigned long functionsLibNum(); |
117 | dict* functionsLibGet(); |
118 | size_t functionsLibCtxfunctionsLen(functionsLibCtx *functions_ctx); |
119 | functionsLibCtx* functionsLibCtxGetCurrent(); |
120 | functionsLibCtx* functionsLibCtxCreate(); |
121 | void functionsLibCtxClearCurrent(int async); |
122 | void functionsLibCtxFree(functionsLibCtx *lib_ctx); |
123 | void functionsLibCtxClear(functionsLibCtx *lib_ctx); |
124 | void functionsLibCtxSwapWithCurrent(functionsLibCtx *lib_ctx); |
125 | |
126 | int functionLibCreateFunction(sds name, void *function, functionLibInfo *li, sds desc, uint64_t f_flags, sds *err); |
127 | |
128 | int luaEngineInitEngine(); |
129 | int functionsInit(); |
130 | |
131 | #endif /* __FUNCTIONS_H_ */ |
132 | |