summaryrefslogtreecommitdiff
path: root/src/functions.h
diff options
context:
space:
mode:
authormeir@redislabs.com <meir@redislabs.com>2021-10-07 14:41:26 +0300
committermeir <meir@redis.com>2021-12-02 19:35:52 +0200
commitcbd463175f8b52d594fd4e6b953fa58a5db053c3 (patch)
tree2b2e4080b6b4d399eaa3053928f0193f5ddbb360 /src/functions.h
parentf21dc38a6ed3851a5e6501199e803ff0b93795cf (diff)
downloadredis-cbd463175f8b52d594fd4e6b953fa58a5db053c3.tar.gz
Redis Functions - Added redis function unit and Lua engine
Redis function unit is located inside functions.c and contains Redis Function implementation: 1. FUNCTION commands: * FUNCTION CREATE * FCALL * FCALL_RO * FUNCTION DELETE * FUNCTION KILL * FUNCTION INFO 2. Register engine In addition, this commit introduce the first engine that uses the Redis Function capabilities, the Lua engine.
Diffstat (limited to 'src/functions.h')
-rw-r--r--src/functions.h126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/functions.h b/src/functions.h
new file mode 100644
index 000000000..8675883df
--- /dev/null
+++ b/src/functions.h
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2021, Redis Ltd.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Redis nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __FUNCTIONS_H_
+#define __FUNCTIONS_H_
+
+/*
+ * functions.c unit provides the Redis Functions API:
+ * * FUNCTION CREATE
+ * * FUNCTION CALL
+ * * FUNCTION DELETE
+ * * FUNCTION KILL
+ * * FUNCTION INFO
+ *
+ * Also contains implementation for:
+ * * Save/Load function from rdb
+ * * Register engines
+ */
+
+#include "server.h"
+#include "script.h"
+#include "redismodule.h"
+
+typedef struct engine {
+ /* engine specific context */
+ void *engine_ctx;
+
+ /* Create function callback, get the engine_ctx, and function code.
+ * returns NULL on error and set sds to be the error message */
+ void* (*create)(void *engine_ctx, sds code, sds *err);
+
+ /* Invoking a function, r_ctx is an opaque object (from engine POV).
+ * The r_ctx should be used by the engine to interaction with Redis,
+ * such interaction could be running commands, set resp, or set
+ * replication mode
+ */
+ void (*call)(scriptRunCtx *r_ctx, void *engine_ctx, void *compiled_function,
+ robj **keys, size_t nkeys, robj **args, size_t nargs);
+
+ /* get current used memory by the engine */
+ size_t (*get_used_memory)(void *engine_ctx);
+
+ /* Return memory overhead for a given function,
+ * such memory is not counted as engine memory but as general
+ * structs memory that hold different information */
+ size_t (*get_function_memory_overhead)(void *compiled_function);
+
+ /* Return memory overhead for engine (struct size holding the engine)*/
+ size_t (*get_engine_memory_overhead)(void *engine_ctx);
+
+ /* free the given function */
+ void (*free_function)(void *engine_ctx, void *compiled_function);
+} engine;
+
+/* Hold information about an engine.
+ * Used on rdb.c so it must be declared here. */
+typedef struct engineInfo {
+ sds name; /* Name of the engine */
+ engine *engine; /* engine callbacks that allows to interact with the engine */
+ client *c; /* Client that is used to run commands */
+} engineInfo;
+
+/* Hold information about the specific function.
+ * Used on rdb.c so it must be declared here. */
+typedef struct functionInfo {
+ sds name; /* Function name */
+ void *function; /* Opaque object that set by the function's engine and allow it
+ to run the function, usually it's the function compiled code. */
+ engineInfo *ei; /* Pointer to the function engine */
+ sds code; /* Function code */
+ sds desc; /* Function description */
+} functionInfo;
+
+int functionsRegisterEngine(const char *engine_name, engine *engine_ctx);
+int functionsCreateWithFunctionCtx(sds function_name, sds engine_name, sds desc, sds code,
+ int replace, sds* err, functionsCtx *functions);
+void functionsCreateCommand(client *c);
+void fcallCommand(client *c);
+void fcallCommandReadOnly(client *c);
+void functionsDeleteCommand(client *c);
+void functionsKillCommand(client *c);
+void functionsStatsCommand(client *c);
+void functionsInfoCommand(client *c);
+void functionsListCommand(client *c);
+void functionsHelpCommand(client *c);
+unsigned long functionsMemory();
+unsigned long functionsMemoryOverhead();
+int functionsLoad(rio *rdb, int ver);
+unsigned long functionsNum();
+dict* functionsGet();
+functionsCtx* functionsCtxGetCurrent();
+functionsCtx* functionsCtxCreate();
+void functionsCtxFree(functionsCtx *functions_ctx);
+void functionsCtxClear(functionsCtx *functions_ctx);
+void functionsCtxSwapWithCurrent(functionsCtx *functions_ctx);
+
+int luaEngineInitEngine();
+int functionsInit();
+
+#endif /* __FUNCTIONS_H_ */