szaszm commented on code in PR #1751: URL: https://github.com/apache/nifi-minifi-cpp/pull/1751#discussion_r1682907319
########## libminifi/src/utils/file/AssetManager.cpp: ########## @@ -0,0 +1,189 @@ +/** + * 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 "utils/file/AssetManager.h" +#include "utils/file/FileUtils.h" +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "core/logging/LoggerFactory.h" +#include "utils/Hash.h" + +#undef GetObject // windows.h #defines GetObject = GetObjectA or GetObjectW, which conflicts with rapidjson + +namespace org::apache::nifi::minifi::utils::file { + +AssetManager::AssetManager(const Configure& configuration) + : root_(configuration.get(Configure::nifi_asset_directory).value_or((configuration.getHome() / "asset").string())), + logger_(core::logging::LoggerFactory<AssetManager>::getLogger()) { + refreshState(); +} + +void AssetManager::refreshState() { + std::lock_guard lock(mtx_); + state_.clear(); + if (!utils::file::FileUtils::exists(root_)) { + std::filesystem::create_directories(root_); + } + if (!utils::file::FileUtils::exists(root_ / ".state")) { + std::ofstream{root_ / ".state", std::ios::binary} << R"({"digest": "", "assets": {}})"; + } + rapidjson::Document doc; + + std::string file_content = utils::file::get_content(root_ / ".state"); + + rapidjson::ParseResult res = doc.Parse(file_content.c_str(), file_content.size()); + if (!res) { + logger_->log_error("Failed to parse asset '.state' file, not a valid json file"); + return; + } + if (!doc.IsObject()) { + logger_->log_error("Asset '.state' file is malformed"); + return; + } + if (!doc.HasMember("digest")) { + logger_->log_error("Asset '.state' file is malformed, missing 'digest'"); + return; + } + if (!doc["digest"].IsString()) { + logger_->log_error("Asset '.state' file is malformed, 'digest' is not a string"); + return; + } + if (!doc.HasMember("assets")) { + logger_->log_error("Asset '.state' file is malformed, missing 'assets'"); + return; + } + if (!doc["assets"].IsObject()) { + logger_->log_error("Asset '.state' file is malformed, 'assets' is not an object"); + return; + } + + + AssetLayout new_state; + new_state.digest = std::string{doc["digest"].GetString(), doc["digest"].GetStringLength()}; + + for (auto& [id, entry] : doc["assets"].GetObject()) { + if (!entry.IsObject()) { + logger_->log_error("Asset '.state' file is malformed, 'assets.{}' is not an object", std::string_view{id.GetString(), id.GetStringLength()}); + return; + } + AssetDescription description; + description.id = std::string{id.GetString(), id.GetStringLength()}; + if (!entry.HasMember("path") || !entry["path"].IsString()) { + logger_->log_error("Asset '.state' file is malformed, 'assets.{}.path' does not exist or is not a string", std::string_view{id.GetString(), id.GetStringLength()}); + return; + } + description.path = std::string{entry["path"].GetString(), entry["path"].GetStringLength()}; + if (!entry.HasMember("url") || !entry["url"].IsString()) { + logger_->log_error("Asset '.state' file is malformed, 'assets.{}.url' does not exist or is not a string", std::string_view{id.GetString(), id.GetStringLength()}); + return; + } + description.url = std::string{entry["url"].GetString(), entry["url"].GetStringLength()}; + + if (utils::file::FileUtils::exists(root_ / description.id)) { + new_state.assets.insert(std::move(description)); + } else { + logger_->log_error("Asset '.state' file contains entry '{}' that does not exist on the filesystem at '{}'", + std::string_view{id.GetString(), id.GetStringLength()}, (root_ / description.id).string()); + } + } + state_ = std::move(new_state); +} + +std::string AssetManager::hash() const { + std::lock_guard lock(mtx_); + return state_.digest.empty() ? "null" : state_.digest; +} + +nonstd::expected<void, std::string> AssetManager::sync( + const org::apache::nifi::minifi::utils::file::AssetLayout& layout, + const std::function<nonstd::expected<std::vector<std::byte>, std::string>(std::string_view /*url*/)>& fetch) { + std::lock_guard lock(mtx_); + org::apache::nifi::minifi::utils::file::AssetLayout new_state{ Review Comment: There is no need to use fully qualified names, when we're in the same namespace. ########## minifi_main/MiNiFiMain.cpp: ########## @@ -397,11 +398,13 @@ int main(int argc, char **argv) { .sensitive_properties_encryptor = utils::crypto::EncryptionProvider::createSensitivePropertiesEncryptor(minifiHome) }, nifi_configuration_class_name); - std::vector<std::shared_ptr<core::RepositoryMetricsSource>> repo_metric_sources{prov_repo, flow_repo, content_repo}; - auto metrics_publisher_store = std::make_unique<minifi::state::MetricsPublisherStore>(configure, repo_metric_sources, flow_configuration); + auto asset_manager = std::make_shared<utils::file::AssetManager>(*configure); Review Comment: Could we do this without sharing ownership? Maybe main could own it. If not, could you explain in a comment why? -- 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]
