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


##########
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;
+}
+
+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; }
+  value_.append(reinterpret_cast<const char*>(value), size);

Review Comment:
   LMDB does not have an append function when writing a value like RocksDB's 
`Merge` function, so instead of rereading the original value, appending to it, 
then writing back the new value, all the writes are buffered until the stream 
is closed, that's when the actual write and commit happens. Currently all 
content repository streams are used either for write-only or read-only use 
cases, so there should be no use case where reads and writes are mixed. This 
should be addressed in a separate PR to change the content repository interface 
to use separate OutputStream and InputStream types for reads and writes to 
enforce this, which would also result in separate LmdbInputStream and 
LmdbOutputStream types (same for RocksDB).



-- 
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