martinzink commented on code in PR #1504:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1504#discussion_r1124673200


##########
extensions/python/types/PyProcessSession.cpp:
##########
@@ -0,0 +1,265 @@
+/**
+ *
+ * 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 "PyProcessSession.h"
+#include <utility>
+#include "PyException.h"
+#include "PyScriptFlowFile.h"
+#include "PyRelationship.h"
+#include "types/PyOutputStream.h"
+#include "types/PyInputStream.h"
+#include "range/v3/algorithm/remove_if.hpp"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+namespace core = org::apache::nifi::minifi::core;
+
+PyProcessSession::PyProcessSession(std::shared_ptr<core::ProcessSession> 
session)
+    : session_(std::move(session)) {
+}
+
+std::shared_ptr<core::FlowFile> PyProcessSession::get() {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been 
released");
+  }
+
+  auto flow_file = session_->get();
+
+  if (flow_file == nullptr) {
+    return nullptr;
+  }
+
+  flow_files_.push_back(flow_file);
+
+  return flow_file;
+}
+
+void PyProcessSession::transfer(const std::shared_ptr<core::FlowFile>& 
flow_file,
+                                const core::Relationship& relationship) {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been 
released");
+  }
+
+  if (!flow_file) {
+    throw std::runtime_error("Access of FlowFile after it has been released");
+  }
+
+  session_->transfer(flow_file, relationship);
+}
+
+void PyProcessSession::read(const std::shared_ptr<core::FlowFile>& flow_file, 
BorrowedObject input_stream_callback) {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been 
released");
+  }
+
+  if (!flow_file) {
+    throw std::runtime_error("Access of FlowFile after it has been released");
+  }
+
+  session_->read(flow_file, [&input_stream_callback](const 
std::shared_ptr<io::InputStream>& input_stream) -> int64_t {
+    return 
Long(Callable(input_stream_callback.getAttribute("process"))(std::weak_ptr(input_stream))).asInt64();
+  });
+}
+
+void PyProcessSession::write(const std::shared_ptr<core::FlowFile>& flow_file, 
BorrowedObject output_stream_callback) {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been 
released");
+  }
+
+  if (!flow_file) {
+    throw std::runtime_error("Access of FlowFile after it has been released");
+  }
+
+  session_->write(flow_file, [&output_stream_callback](const 
std::shared_ptr<io::OutputStream>& output_stream) -> int64_t {
+    return 
Long(Callable(output_stream_callback.getAttribute("process"))(std::weak_ptr(output_stream))).asInt64();
+  });
+}
+
+std::shared_ptr<core::FlowFile> PyProcessSession::create(const 
std::shared_ptr<core::FlowFile>& flow_file) {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been 
released");
+  }
+
+  auto result = session_->create(flow_file);
+
+  flow_files_.push_back(result);
+  return result;
+}
+
+void PyProcessSession::remove(const std::shared_ptr<core::FlowFile>& 
flow_file) {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been 
released");
+  }
+  std::shared_ptr<core::FlowFile> result;
+
+  session_->remove(flow_file);
+  ranges::remove_if(flow_files_, [&flow_file](const auto& ff)-> bool { return 
ff == flow_file; });

Review Comment:
   good catch, fixed it in 
https://github.com/apache/nifi-minifi-cpp/pull/1504/commits/71a33853c6b5b71e668be9587b052f016388fdc0#diff-c6e6761718cb79a8c101319d280e709c261cf4231184df8fe7c88e68fc432e6dR111



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