szaszm commented on code in PR #1504: URL: https://github.com/apache/nifi-minifi-cpp/pull/1504#discussion_r1124533781
########## extensions/lua/tests/CMakeLists.txt: ########## @@ -0,0 +1,39 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +file(GLOB EXECUTESCRIPT_LUA_TESTS "*.cpp") + +SET(EXTENSIONS_TEST_COUNT 0) + +FOREACH(testfile ${EXECUTESCRIPT_LUA_TESTS}) + get_filename_component(testfilename "${testfile}" NAME_WE) + add_executable("${testfilename}" "${testfile}") + target_include_directories(${testfilename} PRIVATE BEFORE "${CMAKE_SOURCE_DIR}/extensions/lua") + target_include_directories(${testfilename} PRIVATE BEFORE "${CMAKE_SOURCE_DIR}/extensions/standard-processors") + target_include_directories(${testfilename} PRIVATE BEFORE "${CMAKE_SOURCE_DIR}/libminifi/test/") + target_include_directories(${testfilename} SYSTEM PRIVATE BEFORE "${SOL2_INCLUDE_DIR}") + target_link_libraries(${testfilename} minifi-lua-script-extension minifi-script-extension) + target_link_libraries(${testfilename} minifi-standard-processors) Review Comment: Is this dependency on standard-processors actually used? ########## extensions/script/ExecuteScript.cpp: ########## @@ -54,94 +49,50 @@ const core::Property ExecuteScript::ModuleDirectory("Module Directory", const core::Relationship ExecuteScript::Success("success", "Script successes"); const core::Relationship ExecuteScript::Failure("failure", "Script failures"); -ScriptEngineFactory::ScriptEngineFactory(const core::Relationship& success, const core::Relationship& failure, std::shared_ptr<core::logging::Logger> logger) - : success_(success), - failure_(failure), - logger_(std::move(logger)) { -} - void ExecuteScript::initialize() { setSupportedProperties(properties()); setSupportedRelationships(relationships()); - -#ifdef PYTHON_SUPPORT - python::PythonScriptEngine::initialize(); -#endif // PYTHON_SUPPORT } void ExecuteScript::onSchedule(core::ProcessContext *context, core::ProcessSessionFactory* /*sessionFactory*/) { -#ifdef LUA_SUPPORT - auto create_engine = [this]() -> std::unique_ptr<lua::LuaScriptEngine> { - return engine_factory_.createEngine<lua::LuaScriptEngine>(); - }; - lua_script_engine_queue_ = utils::ResourceQueue<lua::LuaScriptEngine>::create(create_engine, getMaxConcurrentTasks(), std::nullopt, logger_); -#endif // LUA_SUPPORT -#ifdef PYTHON_SUPPORT - python_script_engine_ = engine_factory_.createEngine<python::PythonScriptEngine>(); -#endif // PYTHON_SUPPORT - - script_engine_ = ScriptEngineOption::parse(utils::parsePropertyWithAllowableValuesOrThrow(*context, ScriptEngine.getName(), ScriptEngineOption::values()).c_str()); - - context->getProperty(ScriptFile.getName(), script_file_); - context->getProperty(ScriptBody.getName(), script_body_); - module_directory_ = context->getProperty(ModuleDirectory); - - if (script_file_.empty() && script_body_.empty()) { - throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Either Script Body or Script File must be defined"); + static const std::unordered_map<std::string_view, std::string> executor_class_names = {{"lua", "LuaScriptExecutor"}, {"python", "PythonScriptExecutor"}}; + + if (auto script_engine_prefix = context->getProperty(ScriptEngine); script_engine_prefix && executor_class_names.contains(*script_engine_prefix)) { + const auto& executor_class_name = executor_class_names.at(*script_engine_prefix); Review Comment: A code-based lookup would me more efficient here IMO. ```suggestion const auto executor_class_lookup = [](const std::string_view script_engine_prefix) -> const char* { if (script_engine_prefix == "lua") return "LuaScriptExecutor"; if (script_engine_prefix == "python") return "PythonScriptExecutor"; return nullptr; }; if (const auto script_engine_prefix = context->getProperty(ScriptEngine); script_engine_prefix && executor_class_lookup(*script_engine_prefix)) { const char* const executor_class_name = executor_class_lookup(*script_engine_prefix); ``` ########## extensions/python/types/PyProcessSession.cpp: ########## @@ -0,0 +1,265 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "PyProcessSession.h" +#include <utility> +#include "PyException.h" +#include "PyScriptFlowFile.h" +#include "PyRelationship.h" +#include "types/PyOutputStream.h" +#include "types/PyInputStream.h" +#include "range/v3/algorithm/remove_if.hpp" + +namespace org::apache::nifi::minifi::extensions::python { + +namespace core = org::apache::nifi::minifi::core; + +PyProcessSession::PyProcessSession(std::shared_ptr<core::ProcessSession> session) + : session_(std::move(session)) { +} + +std::shared_ptr<core::FlowFile> PyProcessSession::get() { + if (!session_) { + throw std::runtime_error("Access of ProcessSession after it has been released"); + } + + auto flow_file = session_->get(); + + if (flow_file == nullptr) { + return nullptr; + } + + flow_files_.push_back(flow_file); + + return flow_file; +} + +void PyProcessSession::transfer(const std::shared_ptr<core::FlowFile>& flow_file, + const core::Relationship& relationship) { + if (!session_) { + throw std::runtime_error("Access of ProcessSession after it has been released"); + } + + if (!flow_file) { + throw std::runtime_error("Access of FlowFile after it has been released"); + } + + session_->transfer(flow_file, relationship); +} + +void PyProcessSession::read(const std::shared_ptr<core::FlowFile>& flow_file, BorrowedObject input_stream_callback) { + if (!session_) { + throw std::runtime_error("Access of ProcessSession after it has been released"); + } + + if (!flow_file) { + throw std::runtime_error("Access of FlowFile after it has been released"); + } + + session_->read(flow_file, [&input_stream_callback](const std::shared_ptr<io::InputStream>& input_stream) -> int64_t { + return Long(Callable(input_stream_callback.getAttribute("process"))(std::weak_ptr(input_stream))).asInt64(); + }); +} + +void PyProcessSession::write(const std::shared_ptr<core::FlowFile>& flow_file, BorrowedObject output_stream_callback) { + if (!session_) { + throw std::runtime_error("Access of ProcessSession after it has been released"); + } + + if (!flow_file) { + throw std::runtime_error("Access of FlowFile after it has been released"); + } + + session_->write(flow_file, [&output_stream_callback](const std::shared_ptr<io::OutputStream>& output_stream) -> int64_t { + return Long(Callable(output_stream_callback.getAttribute("process"))(std::weak_ptr(output_stream))).asInt64(); + }); +} + +std::shared_ptr<core::FlowFile> PyProcessSession::create(const std::shared_ptr<core::FlowFile>& flow_file) { + if (!session_) { + throw std::runtime_error("Access of ProcessSession after it has been released"); + } + + auto result = session_->create(flow_file); + + flow_files_.push_back(result); + return result; +} + +void PyProcessSession::remove(const std::shared_ptr<core::FlowFile>& flow_file) { + if (!session_) { + throw std::runtime_error("Access of ProcessSession after it has been released"); + } + std::shared_ptr<core::FlowFile> result; + + session_->remove(flow_file); + ranges::remove_if(flow_files_, [&flow_file](const auto& ff)-> bool { return ff == flow_file; }); Review Comment: I think we also need `erase` here to actually remove these owning pointers. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org