lordgamez commented on code in PR #2201: URL: https://github.com/apache/nifi-minifi-cpp/pull/2201#discussion_r3475105042
########## thirdparty/lmdb/add-cmake-file.patch: ########## @@ -0,0 +1,29 @@ +lmdb only comes with a Makefile which only works on Linux so we create our own CMakeLists.txt file to be platform independent + +diff -rupN orig/libraries/liblmdb/CMakeLists.txt patched/libraries/liblmdb/CMakeLists.txt +--- orig/libraries/liblmdb/CMakeLists.txt 2026-06-12 13:02:00.868592927 +0200 ++++ patched/libraries/liblmdb/CMakeLists.txt 2026-06-12 11:51:51.369241534 +0200 +@@ -0,0 +1,23 @@ ++cmake_minimum_required(VERSION 3.25) ++project(lmdb C) ++ ++if(MSVC) ++ add_compile_options(/W3 /WX-) ++else() ++ add_compile_options(-W -Wall -Wno-unused-parameter -Wbad-function-cast -Wuninitialized -O2 -g) Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/92f698e12994676f394a3cfdb0ea53573a490652 ########## extensions/lmdb/LmdbStream.cpp: ########## @@ -0,0 +1,116 @@ +/** + * + * 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 "LmdbStream.h" + +#include <string> +#include <utility> + +#include "io/validation.h" + +namespace org::apache::nifi::minifi::io { + +LmdbStream::LmdbStream(std::string path, MDB_env* lmdb_env, MDB_dbi* lmdb_handle, bool write_enable) + : BaseStreamImpl(), + path_(std::move(path)), + write_enable_(write_enable), + lmdb_env_(lmdb_env), + lmdb_handle_(lmdb_handle), + exists_(loadValue()) {} + +bool LmdbStream::loadValue() { + MDB_val key{path_.size(), const_cast<char*>(path_.data())}; + MDB_val value{}; + + MDB_txn* txn = nullptr; + if (const int rc = mdb_txn_begin(lmdb_env_, nullptr, MDB_RDONLY, &txn); rc != MDB_SUCCESS) { + logger_->log_error("Failed to begin LMDB read transaction in loadValue: {}", mdb_strerror(rc)); + return false; + } + auto guard = gsl::finally([txn] { mdb_txn_abort(txn); }); + + const auto rc = mdb_get(txn, *lmdb_handle_, &key, &value); + if (rc == MDB_SUCCESS) { + value_ = std::string(static_cast<char*>(value.mv_data), value.mv_size); + return true; + } else if (rc != MDB_NOTFOUND) { + logger_->log_error("Failed to get value from LMDB database: {}", mdb_strerror(rc)); + } + return false; +} + +void LmdbStream::close() { + commit(); +} + +bool LmdbStream::commit() { + if (!write_enable_ || !dirty_) { return false; } + dirty_ = false; + + MDB_txn* txn = nullptr; + auto rc = mdb_txn_begin(lmdb_env_, nullptr, 0, &txn); + if (rc != MDB_SUCCESS) { + logger_->log_error("Failed to begin LMDB transaction in close: {}", mdb_strerror(rc)); + return false; + } + + MDB_val key{path_.size(), const_cast<char*>(path_.data())}; + MDB_val val{value_.size(), const_cast<char*>(value_.data())}; + rc = mdb_put(txn, *lmdb_handle_, &key, &val, 0); + if (rc != MDB_SUCCESS) { + logger_->log_error("Failed to put value in LMDB database during close: {}", mdb_strerror(rc)); + mdb_txn_abort(txn); + return false; + } + + rc = mdb_txn_commit(txn); + if (rc != MDB_SUCCESS) { + logger_->log_error("Failed to commit LMDB transaction during close: {}", mdb_strerror(rc)); + return false; + } + return true; +} Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/92f698e12994676f394a3cfdb0ea53573a490652 ########## extensions/lmdb/LmdbStream.cpp: ########## @@ -0,0 +1,116 @@ +/** + * + * 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 "LmdbStream.h" + +#include <string> +#include <utility> + Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/92f698e12994676f394a3cfdb0ea53573a490652 ########## extensions/lmdb/LmdbContentRepository.cpp: ########## @@ -0,0 +1,283 @@ +/** + * 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 "LmdbContentRepository.h" + +#include <filesystem> +#include <iterator> +#include <memory> +#include <string> +#include <utility> +#include <vector> + +#include "LmdbStream.h" +#include "core/Resource.h" +#include "lmdb.h" +#include "minifi-cpp/Exception.h" +#include "minifi-cpp/utils/gsl.h" +#include "utils/Locations.h" + +namespace org::apache::nifi::minifi::core::repository { + +LmdbContentRepository::Session::Session(std::shared_ptr<ContentRepository> repository) : BufferedContentSession(std::move(repository)) {} + +void LmdbContentRepository::Session::commit() { + auto lmdb_content_repository = std::dynamic_pointer_cast<LmdbContentRepository>(repository_); + if (!lmdb_content_repository) { throw Exception(REPOSITORY_EXCEPTION, "Session's repository is not an LmdbContentRepository"); } + + const auto writeResource = [&lmdb_content_repository](const std::shared_ptr<ResourceClaim>& resource_claim, const std::shared_ptr<io::BaseStream>& stream, bool is_append) { + auto outStream = lmdb_content_repository->write(*resource_claim, is_append); + if (outStream == nullptr) { throw Exception(REPOSITORY_EXCEPTION, "Couldn't open the underlying resource for write: " + resource_claim->getContentFullPath()); } + const auto size = stream->size(); + if (outStream->write(stream->getBuffer()) != size) { + throw Exception(REPOSITORY_EXCEPTION, "Failed to write " + std::string(is_append ? "appended" : "new") + " resource: " + resource_claim->getContentFullPath()); + } + auto lmdb_out_stream = std::dynamic_pointer_cast<io::LmdbStream>(outStream); + if (lmdb_out_stream == nullptr) { throw Exception(REPOSITORY_EXCEPTION, "Couldn't cast output stream to LmdbStream for commit: " + resource_claim->getContentFullPath()); } + if (!lmdb_out_stream->commit()) { throw Exception(REPOSITORY_EXCEPTION, "Failed to commit " + std::string(is_append ? "appended" : "new") + " resource: " + resource_claim->getContentFullPath()); } + }; + + for (const auto& resource : managed_resources_) { + writeResource(resource.first, resource.second, false); + } + + for (const auto& resource : append_state_) { + writeResource(resource.first, resource.second.stream, true); + } + + managed_resources_.clear(); + append_state_.clear(); +} + +bool LmdbContentRepository::initialize(const std::shared_ptr<minifi::Configure>& configuration) { + if (const int rc = mdb_env_create(&lmdb_env_)) { + logger_->log_error("Failed to create LMDB environment: {}", mdb_strerror(rc)); + return false; + } + + // Reserve virtual address space for the DB file (max size it can grow to) + const auto max_db_size = configuration->get(Configure::nifi_content_repository_lmdb_max_db_size) | utils::andThen([](auto max_db_size_str) -> std::optional<uint64_t> { + if (max_db_size_str.empty()) { return std::nullopt; } + return parsing::parseDataSize(max_db_size_str) | utils::orThrow(fmt::format("{} was set to invalid value: '{}'", Configure::nifi_content_repository_lmdb_max_db_size, max_db_size_str)); + }) | utils::orElse([] { + // Default to 10 GB if the property is not set + return std::make_optional<uint64_t>(10ULL * 1024 * 1024 * 1024); + }); + + if (!max_db_size) { + logger_->log_error("Invalid max DB size configuration for LMDB Content Repository"); + mdb_env_close(lmdb_env_); + lmdb_env_ = nullptr; + return false; + } + + logger_->log_info("Setting LMDB max DB size to {} bytes", *max_db_size); + mdb_env_set_mapsize(lmdb_env_, gsl::narrow<size_t>(*max_db_size)); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/92f698e12994676f394a3cfdb0ea53573a490652 ########## extensions/lmdb/CMakeLists.txt: ########## @@ -0,0 +1,37 @@ +# +# 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. +# + +if (NOT (ENABLE_ALL OR ENABLE_LMDB)) + return() +endif() + +include(LMDB) + +include(${CMAKE_SOURCE_DIR}/extensions/ExtensionHeader.txt) + +file(GLOB SOURCES "*.cpp") + +add_minifi_library(minifi-lmdb SHARED ${SOURCES}) + +target_include_directories(minifi-lmdb PUBLIC ${LMDB_INCLUDE_DIR}) +target_link_libraries(minifi-lmdb PUBLIC lmdb) +target_link_libraries(minifi-lmdb PUBLIC minifi-api minifi-extension-framework) +target_link_libraries(minifi-lmdb PRIVATE $<LINK_ONLY:core-minifi>) + +register_extension(minifi-lmdb "LMDB" LMDB "This Enables persistent provenance, flowfile, and content repositories using LMDB" "extensions/lmdb/tests") Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/92f698e12994676f394a3cfdb0ea53573a490652 -- 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]
