lordgamez commented on code in PR #2201: URL: https://github.com/apache/nifi-minifi-cpp/pull/2201#discussion_r3757113711
########## extensions/lmdb/LmdbStream.cpp: ########## @@ -0,0 +1,119 @@ +/** + * + * 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 <cstring> +#include <algorithm> +#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; } + + 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; + } + + dirty_ = false; + return true; +} Review Comment: Fixed in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc ########## extensions/lmdb/LmdbStream.cpp: ########## @@ -0,0 +1,119 @@ +/** + * + * 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 <cstring> +#include <algorithm> +#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; } + + 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; + } + + dirty_ = false; + return true; +} + +void LmdbStream::seek(size_t offset) { + offset_ = offset; +} + +size_t LmdbStream::tell() const { + return offset_; +} + +size_t LmdbStream::write(const uint8_t* value, size_t size) { + if (!write_enable_) { return STREAM_ERROR; } + if (size != 0 && IsNullOrEmpty(value)) { return STREAM_ERROR; } Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc ########## extensions/lmdb/LmdbContentRepository.h: ########## @@ -0,0 +1,87 @@ +/** + * 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 <memory> +#include <string> +#include <array> +#include <string_view> + +#include "core/BufferedContentSession.h" +#include "core/ContentRepository.h" +#include "core/ClassName.h" +#include "core/logging/LoggerFactory.h" +#include "minifi-cpp/core/PropertyDefinition.h" +#include "minifi-cpp/utils/Export.h" +#include "minifi-cpp/utils/Id.h" +#include "lmdb.h" + +namespace org::apache::nifi::minifi::core::repository { + +class LmdbContentRepository : public core::ContentRepositoryImpl { + public: + explicit LmdbContentRepository(std::string_view name = className<LmdbContentRepository>(), const utils::Identifier& uuid = {}) + : core::ContentRepositoryImpl(name, uuid) {} + + ~LmdbContentRepository() override { Review Comment: Good point, updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc ########## extensions/lmdb/LmdbContentRepository.h: ########## @@ -0,0 +1,87 @@ +/** + * 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 <memory> +#include <string> +#include <array> +#include <string_view> + +#include "core/BufferedContentSession.h" +#include "core/ContentRepository.h" +#include "core/ClassName.h" +#include "core/logging/LoggerFactory.h" +#include "minifi-cpp/core/PropertyDefinition.h" +#include "minifi-cpp/utils/Export.h" +#include "minifi-cpp/utils/Id.h" +#include "lmdb.h" + +namespace org::apache::nifi::minifi::core::repository { + +class LmdbContentRepository : public core::ContentRepositoryImpl { + public: + explicit LmdbContentRepository(std::string_view name = className<LmdbContentRepository>(), const utils::Identifier& uuid = {}) + : core::ContentRepositoryImpl(name, uuid) {} + + ~LmdbContentRepository() override { + stop(); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc ########## extensions/lmdb/LmdbContentRepository.h: ########## @@ -0,0 +1,87 @@ +/** + * 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 <memory> +#include <string> +#include <array> +#include <string_view> + +#include "core/BufferedContentSession.h" +#include "core/ContentRepository.h" +#include "core/ClassName.h" +#include "core/logging/LoggerFactory.h" +#include "minifi-cpp/core/PropertyDefinition.h" +#include "minifi-cpp/utils/Export.h" +#include "minifi-cpp/utils/Id.h" +#include "lmdb.h" + +namespace org::apache::nifi::minifi::core::repository { + +class LmdbContentRepository : public core::ContentRepositoryImpl { + public: + explicit LmdbContentRepository(std::string_view name = className<LmdbContentRepository>(), const utils::Identifier& uuid = {}) + : core::ContentRepositoryImpl(name, uuid) {} + + ~LmdbContentRepository() override { + stop(); + if (lmdb_env_) { + mdb_dbi_close(lmdb_env_, lmdb_handle_); + mdb_env_close(lmdb_env_); + } + } Review Comment: Agreed, this was extracted in the `LmdbWrapper` class in https://github.com/apache/nifi-minifi-cpp/pull/2203 As it would be a bit complicated changing both PRs to move that here and we will probably handle and merge both PRs close together or at least in the same release I would keep that change in the follow up PR for now. ########## extensions/lmdb/LmdbContentRepository.h: ########## @@ -0,0 +1,87 @@ +/** + * 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 <memory> +#include <string> +#include <array> +#include <string_view> + +#include "core/BufferedContentSession.h" +#include "core/ContentRepository.h" +#include "core/ClassName.h" +#include "core/logging/LoggerFactory.h" +#include "minifi-cpp/core/PropertyDefinition.h" +#include "minifi-cpp/utils/Export.h" +#include "minifi-cpp/utils/Id.h" +#include "lmdb.h" + +namespace org::apache::nifi::minifi::core::repository { + +class LmdbContentRepository : public core::ContentRepositoryImpl { + public: + explicit LmdbContentRepository(std::string_view name = className<LmdbContentRepository>(), const utils::Identifier& uuid = {}) + : core::ContentRepositoryImpl(name, uuid) {} + + ~LmdbContentRepository() override { + stop(); + if (lmdb_env_) { + mdb_dbi_close(lmdb_env_, lmdb_handle_); + mdb_env_close(lmdb_env_); + } + } + + EXTENSIONAPI static constexpr auto Properties = std::array<core::PropertyReference, 0>{}; + EXTENSIONAPI static constexpr bool SupportsDynamicProperties = false; + EXTENSIONAPI static constexpr bool SupportsDynamicRelationships = false; + + class Session : public BufferedContentSession { + public: + explicit Session(std::shared_ptr<ContentRepository> repository); + + void commit() override; + }; + + std::shared_ptr<ContentSession> createSession() override; + bool initialize(const std::shared_ptr<minifi::Configure>& configuration) override; + std::shared_ptr<io::BaseStream> write(const minifi::ResourceClaim& claim, bool append = false) override; Review Comment: Good point, updated to use explicit parameter for all repository writes removing default value in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc ########## extensions/lmdb/LmdbContentRepository.cpp: ########## @@ -0,0 +1,288 @@ +/** + * 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"); } Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc ########## extensions/lmdb/LmdbContentRepository.h: ########## @@ -0,0 +1,87 @@ +/** + * 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 <memory> +#include <string> +#include <array> +#include <string_view> + +#include "core/BufferedContentSession.h" +#include "core/ContentRepository.h" +#include "core/ClassName.h" +#include "core/logging/LoggerFactory.h" +#include "minifi-cpp/core/PropertyDefinition.h" +#include "minifi-cpp/utils/Export.h" +#include "minifi-cpp/utils/Id.h" +#include "lmdb.h" + +namespace org::apache::nifi::minifi::core::repository { + +class LmdbContentRepository : public core::ContentRepositoryImpl { + public: + explicit LmdbContentRepository(std::string_view name = className<LmdbContentRepository>(), const utils::Identifier& uuid = {}) + : core::ContentRepositoryImpl(name, uuid) {} + + ~LmdbContentRepository() override { + stop(); + if (lmdb_env_) { + mdb_dbi_close(lmdb_env_, lmdb_handle_); + mdb_env_close(lmdb_env_); + } + } + + EXTENSIONAPI static constexpr auto Properties = std::array<core::PropertyReference, 0>{}; + EXTENSIONAPI static constexpr bool SupportsDynamicProperties = false; + EXTENSIONAPI static constexpr bool SupportsDynamicRelationships = false; + + class Session : public BufferedContentSession { + public: + explicit Session(std::shared_ptr<ContentRepository> repository); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/2201/commits/f0f3414be0d37ff0f9ecadc88893e83fda6620bc -- 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]
