szaszm commented on code in PR #1987: URL: https://github.com/apache/nifi-minifi-cpp/pull/1987#discussion_r2466398015
########## core-framework/c-api-framework/include/api/core/PublishedMetrics.h: ########## @@ -0,0 +1,26 @@ +/** +* 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. + */ +#pragma once + +#include <vector> +#include <string> + +namespace org::apache::nifi::minifi::api::core { + +using PublishedMetrics = std::vector<std::pair<std::string, double>>; Review Comment: YES thank you, the overcomplicated structure of metrics types has been bothering me ever since they were introduced. suggestion: use a simple aggregate struct instead of a std::pair. ########## core-framework/c-api-framework/include/api/core/FlowFile.h: ########## @@ -0,0 +1,47 @@ +/** + * + * 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. + */ +#pragma once + +#include <array> +#include <string_view> +#include "minifi-c.h" +#include "minifi-cpp/core/SpecialFlowAttribute.h" + +namespace org::apache::nifi::minifi::api::core { + +class FlowFile { + public: + explicit FlowFile(OWNED MinifiFlowFile impl): impl_(impl) {} Review Comment: since this is C++, we could replace OWNED with gsl::owner<T*> and use unique_ptr internally. ########## minifi-api/include/minifi-c/minifi-c.h: ########## @@ -0,0 +1,228 @@ +/** + * 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. + */ + +#ifndef MINIFI_API_INCLUDE_MINIFI_C_MINIFI_C_H_ +#define MINIFI_API_INCLUDE_MINIFI_C_MINIFI_C_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stddef.h> +#include <stdint.h> + +#define STRINGIFY_HELPER(X) #X +#define STRINGIFY(X) STRINGIFY_HELPER(X) + +#define MINIFI_API_MAJOR_VERSION 0 +#define MINIFI_API_MINOR_VERSION 1 +#define MINIFI_API_PATCH_VERSION 0 +#define MINIFI_API_VERSION STRINGIFY(MINIFI_API_MAJOR_VERSION) "." STRINGIFY(MINIFI_API_MINOR_VERSION) "." STRINGIFY(MINIFI_API_PATCH_VERSION) +#define MINIFI_API_VERSION_TAG "MINIFI_API_VERSION=[" MINIFI_API_VERSION "]" +#define MINIFI_NULL 0 +#define OWNED + +typedef uint32_t MinifiBool; +#define MINIFI_TRUE MinifiBool(1) +#define MINIFI_FALSE MinifiBool(0) + +typedef enum MinifiInputRequirement { + MINIFI_INPUT_REQUIRED = 0, + MINIFI_INPUT_ALLOWED = 1, + MINIFI_INPUT_FORBIDDEN = 2 +} MinifiInputRequirement; + +typedef struct MinifiStringView { + const char* data; + uint32_t length; Review Comment: this should be size_t IMO ```suggestion size_t length; ``` ########## libminifi/src/minifi-c.cpp: ########## @@ -0,0 +1,502 @@ +/** +* 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 "minifi-c/minifi-c.h" + +#include <memory> +#include <vector> + +#include "agent/agent_docs.h" +#include "core/ProcessorMetrics.h" +#include "minifi-cpp/core/Annotation.h" +#include "minifi-cpp/core/ClassLoader.h" +#include "minifi-cpp/core/ProcessContext.h" +#include "minifi-cpp/core/ProcessSession.h" +#include "minifi-cpp/core/ProcessorApi.h" +#include "minifi-cpp/core/ProcessorDescriptor.h" +#include "minifi-cpp/core/ProcessorFactory.h" +#include "minifi-cpp/core/ProcessorMetadata.h" +#include "minifi-cpp/core/PropertyValidator.h" +#include "minifi-cpp/core/logging/Logger.h" +#include "minifi-cpp/core/state/PublishedMetricProvider.h" +#include "minifi-cpp/Exception.h" +#include "minifi-cpp/core/extension/ExtensionManager.h" +#include "utils/PropertyErrors.h" +#include "minifi-cpp/agent/build_description.h" +#include "utils/CProcessor.h" + +namespace minifi = org::apache::nifi::minifi; + +namespace { + +std::string toString(MinifiStringView sv) { + return {sv.data, sv.length}; +} + +std::string_view toStringView(MinifiStringView sv) { + return {sv.data, sv.length}; +} + +minifi::core::annotation::Input toInputRequirement(MinifiInputRequirement req) { + switch (req) { + case MINIFI_INPUT_REQUIRED: return minifi::core::annotation::Input::INPUT_REQUIRED; + case MINIFI_INPUT_ALLOWED: return minifi::core::annotation::Input::INPUT_ALLOWED; + case MINIFI_INPUT_FORBIDDEN: return minifi::core::annotation::Input::INPUT_FORBIDDEN; + } + gsl_FailFast(); +} + +minifi::core::logging::LOG_LEVEL toLogLevel(MinifiLogLevel lvl) { + switch (lvl) { + case MINIFI_TRACE: return minifi::core::logging::trace; + case MINIFI_DEBUG: return minifi::core::logging::debug; + case MINIFI_INFO: return minifi::core::logging::info; + case MINIFI_WARNING: return minifi::core::logging::warn; + case MINIFI_ERROR: return minifi::core::logging::err; + case MINIFI_CRITICAL: return minifi::core::logging::critical; + case MINIFI_OFF: return minifi::core::logging::off; + } + gsl_FailFast(); +} + +minifi::core::Property createProperty(const MinifiProperty* property_description) { + gsl_Expects(property_description); + std::vector<std::string_view> allowed_values; + allowed_values.reserve(property_description->allowed_values_count); + for (size_t i = 0; i < property_description->allowed_values_count; ++i) { + allowed_values.push_back(toStringView(property_description->allowed_values_ptr[i])); + } + std::vector<std::string_view> allowed_types; + allowed_types.reserve(property_description->types_count); + for (size_t i = 0; i < property_description->types_count; ++i) { + allowed_types.push_back(toStringView(property_description->types_ptr[i])); + } + std::vector<std::string_view> dependent_properties; + dependent_properties.reserve(property_description->dependent_properties_count); + for (size_t i = 0; i < property_description->dependent_properties_count; ++i) { + dependent_properties.push_back(toStringView(property_description->dependent_properties_ptr[i])); + } + std::vector<std::pair<std::string_view, std::string_view>> exclusive_of_properties; + exclusive_of_properties.reserve(property_description->exclusive_of_properties_count); + for (size_t i = 0; i < property_description->exclusive_of_properties_count; ++i) { + exclusive_of_properties.push_back({toStringView(property_description->exclusive_of_property_names_ptr[i]), toStringView(property_description->exclusive_of_property_values_ptr[i])}); + } + std::optional<std::string_view> default_value; + if (property_description->default_value) { + default_value = toStringView(*property_description->default_value); + } + return minifi::core::Property{minifi::core::PropertyReference{ + toStringView(property_description->name), + toStringView(property_description->display_name), + toStringView(property_description->description), + static_cast<bool>(property_description->is_required), + static_cast<bool>(property_description->is_sensitive), + std::span(allowed_values), + std::span(allowed_types), + std::span(dependent_properties), + std::span(exclusive_of_properties), + default_value, + gsl::make_not_null(reinterpret_cast<const minifi::core::PropertyValidator*>(property_description->validator)), + static_cast<bool>(property_description->supports_expression_language) + }}; +} + +class CProcessorFactory : public minifi::core::ProcessorFactory { + public: + CProcessorFactory(std::string group_name, std::string class_name, minifi::utils::CProcessorClassDescription class_description) + : group_name_(std::move(group_name)), + class_name_(std::move(class_name)), + class_description_(std::move(class_description)) {} + std::unique_ptr<minifi::core::ProcessorApi> create(minifi::core::ProcessorMetadata metadata) override { + return std::make_unique<minifi::utils::CProcessor>(class_description_, metadata); + } + + [[nodiscard]] std::string getGroupName() const override { + return group_name_; + } + + [[nodiscard]] std::string getClassName() const override { + return class_name_; + } + + CProcessorFactory() = delete; + CProcessorFactory(const CProcessorFactory&) = delete; + CProcessorFactory& operator=(const CProcessorFactory&) = delete; + CProcessorFactory(CProcessorFactory&&) = delete; + CProcessorFactory& operator=(CProcessorFactory&&) = delete; + ~CProcessorFactory() override = default; + + private: + std::string group_name_; + std::string class_name_; + minifi::utils::CProcessorClassDescription class_description_; +}; + +class CExtension : public minifi::core::extension::Extension { + public: + CExtension(std::string name, MinifiBool(*initializer)(void*, MinifiConfigure), void* user_data): name_(std::move(name)), initializer_(initializer), user_data_(user_data) {} + bool initialize(const minifi::core::extension::ExtensionConfig& config) override { + gsl_Assert(initializer_); + return static_cast<bool>(initializer_(user_data_, reinterpret_cast<MinifiConfigure>(config.get()))); + } + + [[nodiscard]] const std::string& getName() const override { + return name_; + } + + private: + std::string name_; + MinifiBool(*initializer_)(void*, MinifiConfigure); Review Comment: I would introduce type aliases for most function pointer types on the API, especially if they're used multiple times. ########## core-framework/c-api-framework/include/api/core/logging/Logger.h: ########## @@ -0,0 +1,57 @@ +/** + * + * 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. + */ +#pragma once + +#include <algorithm> +#include <iostream> +#include <memory> +#include <mutex> +#include <optional> +#include <sstream> +#include <string> +#include <utility> +#include <vector> + +#include "fmt/chrono.h" +#include "fmt/ostream.h" +#include "fmt/std.h" +#include "minifi-c.h" +#include "utils/Enum.h" +#include "utils/GeneralUtils.h" +#include "utils/gsl.h" +#include "utils/SmallString.h" +#include "minifi-cpp/core/logging/Logger.h" + +namespace org::apache::nifi::minifi::api::core::logging { + +class Logger : public minifi::core::logging::Logger { + public: + explicit Logger(MinifiLogger impl): impl_(impl) {} + + void set_max_log_size(int size) override; + std::optional<std::string> get_id() override; + void log_string(minifi::core::logging::LOG_LEVEL level, std::string str) override; + bool should_log(minifi::core::logging::LOG_LEVEL level) override; + [[nodiscard]] minifi::core::logging::LOG_LEVEL level() const override; + int getMaxLogSize() override; Review Comment: does all this need to be on the api? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
