From 898bd0ed332332886e3f99f9f592a5a568f23b48 Mon Sep 17 00:00:00 2001 From: slzhou Date: Tue, 21 Feb 2023 21:24:09 +0800 Subject: [PATCH] feat: script open function accept env item --- include/libs/function/taosudf.h | 8 ++++++-- source/libs/function/src/udfd.c | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/include/libs/function/taosudf.h b/include/libs/function/taosudf.h index 4e33bf633c..1ef4f59b2c 100644 --- a/include/libs/function/taosudf.h +++ b/include/libs/function/taosudf.h @@ -261,6 +261,10 @@ typedef int32_t (*TUdfAggMergeFunc)(SUdfInterBuf *inputBuf1, SUdfInterBuf *input typedef int32_t (*TUdfAggFinishFunc)(SUdfInterBuf *buf, SUdfInterBuf *resultData); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +typedef struct SScriptUdfEnvItem{ + char *name; + char *value; +} SScriptUdfEnvItem; typedef enum EUdfFuncType { UDF_FUNC_TYPE_SCALAR = 1, @@ -291,8 +295,8 @@ typedef int32_t (*TScriptUdfInitFunc)(SScriptUdfInfo *info, void **pUdfCtx); typedef int32_t (*TScriptUdfDestoryFunc)(void *udfCtx); // the following function is for open/close script plugin. -typedef int32_t (*TScriptOpenFunc)(void *scriptCtx); -typedef int32_t (*TScriptCloseFunc)(void *scriptCtx); +typedef int32_t (*TScriptOpenFunc)(SScriptUdfEnvItem* items, int numItems); +typedef int32_t (*TScriptCloseFunc)(); #ifdef __cplusplus } diff --git a/source/libs/function/src/udfd.c b/source/libs/function/src/udfd.c index d2a61d74e2..ed5f87647f 100644 --- a/source/libs/function/src/udfd.c +++ b/source/libs/function/src/udfd.c @@ -48,9 +48,9 @@ typedef struct SUdfCPluginCtx { TUdfDestroyFunc destroyFunc; } SUdfCPluginCtx; -int32_t udfdCPluginOpen(void *scriptCtx) { return 0; } +int32_t udfdCPluginOpen(SScriptUdfEnvItem *items, int numItems) { return 0; } -int32_t udfdCPluginClose(void *scriptCtx) { return 0; } +int32_t udfdCPluginClose() { return 0; } int32_t udfdCPluginUdfInit(SScriptUdfInfo *udf, void **pUdfCtx) { int32_t err = 0; @@ -308,6 +308,9 @@ void udfdInitializeCPlugin(SUdfScriptPlugin *plugin) { plugin->udfAggProcFunc = udfdCPluginUdfAggProc; plugin->udfAggMergeFunc = udfdCPluginUdfAggMerge; plugin->udfAggFinishFunc = udfdCPluginUdfAggFinish; + + SScriptUdfEnvItem items[0]; + plugin->openFunc(items, 0); return; } @@ -343,19 +346,45 @@ void udfdInitializePythonPlugin(SUdfScriptPlugin *plugin) { fnError("can not load python plugin. lib path %s", plugin->libPath); return; } + if (plugin->openFunc) { + SScriptUdfEnvItem items[] ={{"PYTHONPATH", tsUdfdLdLibPath}}; + plugin->openFunc(items, 1); + } plugin->libLoaded = true; return; } void udfdDeinitCPlugin(SUdfScriptPlugin *plugin) { + if (plugin->closeFunc) { + plugin->closeFunc(); + } + plugin->openFunc = NULL; + plugin->closeFunc = NULL; + plugin->udfInitFunc = NULL; + plugin->udfDestroyFunc = NULL; + plugin->udfScalarProcFunc = NULL; + plugin->udfAggStartFunc = NULL; + plugin->udfAggProcFunc = NULL; + plugin->udfAggMergeFunc = NULL; + plugin->udfAggFinishFunc = NULL; return; } void udfdDeinitPythonPlugin(SUdfScriptPlugin *plugin) { + plugin->closeFunc(); uv_dlclose(&plugin->lib); if (plugin->libLoaded) { plugin->libLoaded = false; } + plugin->openFunc = NULL; + plugin->closeFunc = NULL; + plugin->udfInitFunc = NULL; + plugin->udfDestroyFunc = NULL; + plugin->udfScalarProcFunc = NULL; + plugin->udfAggStartFunc = NULL; + plugin->udfAggProcFunc = NULL; + plugin->udfAggMergeFunc = NULL; + plugin->udfAggFinishFunc = NULL; } void udfdInitScriptPlugins() { -- GitLab