lordgamez commented on code in PR #2201:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2201#discussion_r3757114950


##########
extensions/lmdb/tests/LmdbContentSessionTests.cpp:
##########
@@ -0,0 +1,301 @@
+/**
+ *
+ * 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 <array>
+#include <memory>
+#include <string>
+
+#include "LmdbContentRepository.h"
+#include "catch2/matchers/catch_matchers_string.hpp"
+#include "core/repository/VolatileContentRepository.h"
+#include "unit/Catch.h"
+#include "unit/TestBase.h"
+#include "utils/ConfigurationUtils.h"
+
+namespace org::apache::nifi::minifi::test {
+
+class LmdbContentSessionController : public TestController {
+ public:
+  LmdbContentSessionController() : 
content_repository_(std::make_shared<core::repository::LmdbContentRepository>())
 {
+    auto content_repo_path = createTempDirectory();
+    auto config = std::make_shared<ConfigureImpl>();
+    config->set(Configure::nifi_dbcontent_repository_directory_default, 
content_repo_path.string());
+    content_repository_->initialize(config);
+  }
+
+  ~LmdbContentSessionController() override { log.reset(); }
+
+  LmdbContentSessionController(LmdbContentSessionController&&) = delete;
+  LmdbContentSessionController(const LmdbContentSessionController&) = delete;
+  LmdbContentSessionController& operator=(LmdbContentSessionController&&) = 
delete;
+  LmdbContentSessionController& operator=(const LmdbContentSessionController&) 
= delete;
+
+  std::shared_ptr<core::ContentRepository> content_repository_;
+};
+
+namespace {
+
+std::shared_ptr<io::OutputStream> operator<<(std::shared_ptr<io::OutputStream> 
stream, const std::string& str) {
+  REQUIRE(stream->write(reinterpret_cast<const uint8_t*>(str.data()), 
str.length()) == str.length());
+  return stream;
+}
+
+std::shared_ptr<io::InputStream> operator>>(std::shared_ptr<io::InputStream> 
stream, std::string& str) {
+  str.clear();
+  std::array<std::byte, utils::configuration::DEFAULT_BUFFER_SIZE> buffer{};
+  while (true) {
+    const auto ret = stream->read(buffer);
+    REQUIRE_FALSE(io::isError(ret));
+    if (ret == 0) { break; }
+    str += std::string{reinterpret_cast<char*>(buffer.data()), ret};
+  }
+  return stream;
+}
+
+void requireNoCreate(const std::shared_ptr<ResourceClaim>&) {
+  REQUIRE(false);
+}
+
+std::shared_ptr<ResourceClaim> makeCommittedClaim(const 
std::shared_ptr<core::ContentRepository>& content_repository, const 
std::string& content) {
+  auto session = content_repository->createSession();
+  auto claim = session->create();
+  session->write(claim) << content;
+  session->commit();
+  return claim;
+}
+
+}  // namespace
+
+TEST_CASE("Writing a previously committed claim from a new session throws", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+  auto old_claim = makeCommittedClaim(controller.content_repository_, "data");
+
+  auto session = controller.content_repository_->createSession();
+  REQUIRE_THROWS(session->write(old_claim));
+}
+
+TEST_CASE("Reading a previously committed claim from a new session succeeds", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+  auto old_claim = makeCommittedClaim(controller.content_repository_, "data");
+
+  auto session = controller.content_repository_->createSession();
+  REQUIRE_NOTHROW(session->read(old_claim));
+}
+
+TEST_CASE("Reading a claim after appending to it in the same session throws", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+  auto old_claim = makeCommittedClaim(controller.content_repository_, "data");
+
+  auto session = controller.content_repository_->createSession();
+  session->append(old_claim, 4, requireNoCreate) << "-addendum";
+  REQUIRE_THROWS(session->read(old_claim));
+}
+
+TEST_CASE("Append with on_copy callback produces a new claim with concatenated 
content when the append lock is held", "[lmdb]") {
+  LmdbContentSessionController controller;
+  auto old_claim = makeCommittedClaim(controller.content_repository_, "data");
+
+  auto blocking_session = controller.content_repository_->createSession();
+  blocking_session->append(old_claim, 4, requireNoCreate) << "-addendum";
+
+  std::shared_ptr<ResourceClaim> copied_claim;
+  {
+    auto other_session = controller.content_repository_->createSession();
+    other_session->append(old_claim, 4, [&](auto new_claim) { copied_claim = 
std::move(new_claim); }) << "-some extra content";
+    other_session->commit();
+  }
+  REQUIRE(copied_claim);
+
+  std::string read_content;
+  read_content.resize(controller.content_repository_->size(*copied_claim));
+  
controller.content_repository_->read(*copied_claim)->read(as_writable_bytes(std::span(read_content)));
+  REQUIRE(read_content == "data-some extra content");
+}
+
+TEST_CASE("Session commits a write to a new claim", "[lmdb]") {
+  LmdbContentSessionController controller;
+
+  std::shared_ptr<ResourceClaim> claim;
+  {
+    auto session = controller.content_repository_->createSession();
+    claim = session->create();
+    session->write(claim) << "hello content!";
+
+    std::string buffered_content;
+    session->read(claim) >> buffered_content;
+    REQUIRE(buffered_content == "hello content!");
+
+    session->commit();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*claim) >> content;
+  REQUIRE(content == "hello content!");
+}
+
+TEST_CASE("Session commits multiple appends from offsets on a new claim", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+
+  std::shared_ptr<ResourceClaim> claim;
+  {
+    auto session = controller.content_repository_->createSession();
+    claim = session->create();
+    session->append(claim, 0, requireNoCreate) << "beginning";
+    session->append(claim, 9, requireNoCreate) << "-end";
+    session->commit();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*claim) >> content;
+  REQUIRE(content == "beginning-end");
+}
+
+TEST_CASE("Session commits a write followed by an append on a new claim", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+
+  std::shared_ptr<ResourceClaim> claim;
+  {
+    auto session = controller.content_repository_->createSession();
+    claim = session->create();
+    session->write(claim) << "first";
+    session->append(claim, 5, requireNoCreate) << "-last";
+    session->commit();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*claim) >> content;
+  REQUIRE(content == "first-last");
+}
+
+TEST_CASE("Session commits a write that overwrites an earlier write on the 
same claim", "[lmdb]") {
+  LmdbContentSessionController controller;
+
+  std::shared_ptr<ResourceClaim> claim;
+  {
+    auto session = controller.content_repository_->createSession();
+    claim = session->create();
+    session->write(claim) << "beginning";
+    session->write(claim) << "overwritten";
+    session->commit();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*claim) >> content;
+  REQUIRE(content == "overwritten");
+}
+
+TEST_CASE("Session commits an append to a previously committed claim", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+  auto old_claim = makeCommittedClaim(controller.content_repository_, "data");
+
+  {
+    auto session = controller.content_repository_->createSession();
+    session->append(old_claim, 4, requireNoCreate) << "-addendum";
+    session->commit();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*old_claim) >> content;
+  REQUIRE(content == "data-addendum");
+}
+
+TEST_CASE("Session rollback discards new claims", "[lmdb]") {
+  LmdbContentSessionController controller;
+  std::shared_ptr<core::ContentRepository> content_repository = 
controller.content_repository_;
+
+  std::shared_ptr<ResourceClaim> claim;
+  {
+    auto session = content_repository->createSession();
+    claim = session->create();
+    session->write(claim) << "discarded";
+    session->rollback();
+  }
+  REQUIRE_FALSE(content_repository->exists(*claim));
+}
+
+TEST_CASE("Session rollback leaves a previously committed claim unchanged", 
"[lmdb]") {
+  LmdbContentSessionController controller;
+  auto old_claim = makeCommittedClaim(controller.content_repository_, "data");
+
+  {
+    auto session = controller.content_repository_->createSession();
+    session->append(old_claim, 4, requireNoCreate) << "-addendum";
+    session->rollback();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*old_claim) >> content;
+  REQUIRE(content == "data");
+}
+
+TEST_CASE("Buffered session reads buffered content before commit", "[lmdb]") {
+  LmdbContentSessionController controller;
+
+  auto session = controller.content_repository_->createSession();
+  auto claim = session->create();
+  session->write(claim) << "uncommitted-data";
+
+  std::string content;
+  session->read(claim) >> content;
+  REQUIRE(content == "uncommitted-data");
+
+  REQUIRE_FALSE(controller.content_repository_->exists(*claim));
+}
+
+TEST_CASE("Empty new claim commit does not throw", "[lmdb]") {
+  LmdbContentSessionController controller;
+
+  auto session = controller.content_repository_->createSession();
+  auto claim = session->create();
+  REQUIRE_NOTHROW(session->commit());
+
+  REQUIRE(controller.content_repository_->exists(*claim));
+  REQUIRE(controller.content_repository_->size(*claim) == 0);
+}
+
+TEST_CASE("Rollback after a commit on a different session leaves committed 
content untouched", "[lmdb]") {
+  LmdbContentSessionController controller;
+
+  std::shared_ptr<ResourceClaim> claim;
+  {
+    auto first_session = controller.content_repository_->createSession();
+    claim = first_session->create();
+    first_session->write(claim) << "committed";
+    first_session->commit();
+  }
+
+  {
+    auto rolled_back_session = controller.content_repository_->createSession();
+    rolled_back_session->append(claim, 9, requireNoCreate) << "-rolled-back";
+    rolled_back_session->rollback();
+  }
+
+  std::string content;
+  controller.content_repository_->read(*claim) >> content;
+  REQUIRE(content == "committed");
+}
+
+TEST_CASE("LmdbContentRepository::Session commit throws when underlying 
repository is not LmdbContentRepository", "[lmdb]") {
+  auto unrelated_repository = 
std::make_shared<core::repository::VolatileContentRepository>();
+  unrelated_repository->initialize(std::make_shared<ConfigureImpl>());
+
+  core::repository::LmdbContentRepository::Session 
session(unrelated_repository);
+  REQUIRE_THROWS_WITH(session.commit(), 
Catch::Matchers::ContainsSubstring("Session's repository is not an 
LmdbContentRepository"));
+}

Review Comment:
   Removed 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"); }
+
+  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);
+  if (const auto rc = mdb_env_set_mapsize(lmdb_env_, 
gsl::narrow<size_t>(*max_db_size)); rc != MDB_SUCCESS) {
+    logger_->log_error("Failed to set LMDB map size: {}", mdb_strerror(rc));
+    mdb_env_close(lmdb_env_);
+    lmdb_env_ = nullptr;
+    return false;
+  }
+
+  const auto working_dir = utils::getMinifiDir();
+
+  std::string value;
+  if 
(configuration->get(Configure::nifi_dbcontent_repository_directory_default, 
value) && !value.empty()) {
+    directory_ = value;
+  } else {
+    directory_ = (working_dir / "lmdbcontentrepository").string();
+  }
+
+  if (std::filesystem::exists(directory_)) {
+    logger_->log_info("Using existing LMDB Content Repository directory at 
{}", directory_);
+  } else {
+    logger_->log_info("Creating LMDB Content Repository directory at {}", 
directory_);
+    if (!std::filesystem::create_directories(directory_)) {
+      logger_->log_error("Failed to create LMDB Content Repository directory 
at {}", directory_);
+      mdb_env_close(lmdb_env_);
+      lmdb_env_ = nullptr;
+      return false;
+    }
+  }
+
+  if (const int rc = mdb_env_open(lmdb_env_, directory_.c_str(), MDB_NOTLS, 
0664)) {
+    logger_->log_error("Failed to open LMDB environment: {}", 
mdb_strerror(rc));
+    mdb_env_close(lmdb_env_);
+    lmdb_env_ = nullptr;
+    return false;
+  }
+
+  MDB_txn* init_txn = nullptr;
+  if (const int rc = mdb_txn_begin(lmdb_env_, nullptr, 0, &init_txn); rc != 
MDB_SUCCESS) {
+    logger_->log_error("Failed to begin LMDB transaction during initialize: 
{}", mdb_strerror(rc));
+    mdb_env_close(lmdb_env_);
+    lmdb_env_ = nullptr;
+    return false;
+  }
+  if (const int rc = mdb_dbi_open(init_txn, nullptr, 0, &lmdb_handle_); rc != 
MDB_SUCCESS) {
+    logger_->log_error("Failed to open LMDB database: {}", mdb_strerror(rc));
+    mdb_txn_abort(init_txn);
+    mdb_env_close(lmdb_env_);
+    lmdb_env_ = nullptr;
+    return false;
+  }
+
+  if (const int rc = mdb_txn_commit(init_txn); rc != MDB_SUCCESS) {
+    logger_->log_error("Failed to commit LMDB transaction during initialize: 
{}", mdb_strerror(rc));
+    mdb_env_close(lmdb_env_);
+    lmdb_env_ = nullptr;
+    return false;
+  }
+
+  return true;
+}
+
+void LmdbContentRepository::start() {}
+void LmdbContentRepository::stop() {}
+
+std::shared_ptr<ContentSession> LmdbContentRepository::createSession() {
+  return std::make_shared<Session>(sharedFromThis<ContentRepository>());

Review Comment:
   Updated 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"); }
+
+  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);

Review Comment:
   Good idea, 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]

Reply via email to