szaszm commented on code in PR #2186:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2186#discussion_r3647142862


##########
minifi_rust/minifi_native/src/c_ffi/c_ffi_process_session.rs:
##########
@@ -0,0 +1,580 @@
+// 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
+//
+//   https://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.
+
+use super::c_ffi_flow_file::CffiFlowFile;
+use crate::MinifiError;
+use crate::api::process_session::{IoState, OutputStream};
+use crate::api::{InputStream, ProcessSession};
+use crate::c_ffi::c_ffi_primitives::{ConvertMinifiStringView, StringView};
+use crate::c_ffi::c_ffi_streams::{CffiInputStream, CffiOutputStream};
+use minifi_native_sys::{
+    minifi_input_stream, minifi_input_stream_read, minifi_input_stream_size,
+    minifi_io_status_MINIFI_IO_CANCEL, minifi_io_status_MINIFI_IO_ERROR, 
minifi_output_stream,
+    minifi_output_stream_write, minifi_process_session, 
minifi_process_session_create,
+    minifi_process_session_get, minifi_process_session_get_flow_file_attribute,
+    minifi_process_session_get_flow_file_attributes, 
minifi_process_session_get_flow_file_id,
+    minifi_process_session_read, minifi_process_session_remove,
+    minifi_process_session_set_flow_file_attribute, 
minifi_process_session_transfer,
+    minifi_process_session_write, minifi_status_MINIFI_STATUS_SUCCESS, 
minifi_string_view,
+};
+use std::ffi::{CString, c_void};
+use std::io::Read;
+use std::num::NonZeroU32;
+use std::os::raw::c_char;
+
+const _: () = {
+    if minifi_status_MINIFI_STATUS_SUCCESS != 0 {
+        panic!("minifi_status_MINIFI_STATUS_SUCCESS expected to be 0");
+    }
+};
+
+unsafe fn write_all_to_output_stream(
+    output_stream: *mut minifi_output_stream,
+    data: &[u8],
+) -> Result<i64, i64> {
+    let mut offset = 0;
+    while offset < data.len() {
+        let remaining = data.len() - offset;
+        let written = unsafe {
+            minifi_output_stream_write(
+                output_stream,
+                data.as_ptr().add(offset) as *const c_char,
+                remaining,
+            )
+        };
+        if written < 0 {
+            return Err(written);
+        }
+        if written == 0 {
+            return Err(minifi_io_status_MINIFI_IO_ERROR); // To avoid 
spinning, but not sure about it
+        }

Review Comment:
   I think this is the correct approach



##########
minifi_rust/minifi_native/src/c_ffi/c_ffi_process_session.rs:
##########
@@ -0,0 +1,580 @@
+// 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
+//
+//   https://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.
+
+use super::c_ffi_flow_file::CffiFlowFile;
+use crate::MinifiError;
+use crate::api::process_session::{IoState, OutputStream};
+use crate::api::{InputStream, ProcessSession};
+use crate::c_ffi::c_ffi_primitives::{ConvertMinifiStringView, StringView};
+use crate::c_ffi::c_ffi_streams::{CffiInputStream, CffiOutputStream};
+use minifi_native_sys::{
+    minifi_input_stream, minifi_input_stream_read, minifi_input_stream_size,
+    minifi_io_status_MINIFI_IO_CANCEL, minifi_io_status_MINIFI_IO_ERROR, 
minifi_output_stream,
+    minifi_output_stream_write, minifi_process_session, 
minifi_process_session_create,
+    minifi_process_session_get, minifi_process_session_get_flow_file_attribute,
+    minifi_process_session_get_flow_file_attributes, 
minifi_process_session_get_flow_file_id,
+    minifi_process_session_read, minifi_process_session_remove,
+    minifi_process_session_set_flow_file_attribute, 
minifi_process_session_transfer,
+    minifi_process_session_write, minifi_status_MINIFI_STATUS_SUCCESS, 
minifi_string_view,
+};
+use std::ffi::{CString, c_void};
+use std::io::Read;
+use std::num::NonZeroU32;
+use std::os::raw::c_char;
+
+const _: () = {
+    if minifi_status_MINIFI_STATUS_SUCCESS != 0 {
+        panic!("minifi_status_MINIFI_STATUS_SUCCESS expected to be 0");
+    }
+};
+
+unsafe fn write_all_to_output_stream(
+    output_stream: *mut minifi_output_stream,
+    data: &[u8],
+) -> Result<i64, i64> {
+    let mut offset = 0;
+    while offset < data.len() {
+        let remaining = data.len() - offset;
+        let written = unsafe {
+            minifi_output_stream_write(
+                output_stream,
+                data.as_ptr().add(offset) as *const c_char,
+                remaining,
+            )
+        };
+        if written < 0 {
+            return Err(written);
+        }
+        if written == 0 {
+            return Err(minifi_io_status_MINIFI_IO_ERROR); // To avoid 
spinning, but not sure about it
+        }
+        let written_usize = written as usize;
+        if written_usize > remaining {
+            return Err(minifi_io_status_MINIFI_IO_ERROR);
+        }

Review Comment:
   Shouldn't this be impossible? If so, we can panic instead of return error



##########
minifi_rust/minifi_native/src/c_ffi/c_ffi_process_session.rs:
##########
@@ -0,0 +1,610 @@
+// 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
+//
+//   https://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.
+
+use super::c_ffi_flow_file::CffiFlowFile;
+use crate::MinifiError;
+use crate::api::process_session::{IoState, OutputStream};
+use crate::api::{InputStream, ProcessSession};
+use crate::c_ffi::c_ffi_primitives::{ConvertMinifiStringView, StringView};
+use crate::c_ffi::c_ffi_streams::{CffiInputStream, CffiOutputStream};
+use minifi_native_sys::{
+    minifi_input_stream, minifi_input_stream_read, minifi_input_stream_size,
+    minifi_io_status_MINIFI_IO_CANCEL, minifi_io_status_MINIFI_IO_ERROR, 
minifi_output_stream,
+    minifi_output_stream_write, minifi_process_session, 
minifi_process_session_create,
+    minifi_process_session_get, minifi_process_session_get_flow_file_attribute,
+    minifi_process_session_get_flow_file_attributes, 
minifi_process_session_get_flow_file_id,
+    minifi_process_session_read, minifi_process_session_remove,
+    minifi_process_session_set_flow_file_attribute, 
minifi_process_session_transfer,
+    minifi_process_session_write, minifi_status_MINIFI_STATUS_SUCCESS, 
minifi_string_view,
+};
+use std::ffi::{CString, c_void};
+use std::io::Read;
+use std::num::NonZeroU32;
+use std::os::raw::c_char;
+
+const _: () = {
+    if minifi_status_MINIFI_STATUS_SUCCESS != 0 {
+        panic!("minifi_status_MINIFI_STATUS_SUCCESS expected to be 0");
+    }
+};
+
+pub struct CffiProcessSession<'a> {
+    ptr: *mut minifi_process_session,
+    // The lifetime ensures the session cannot outlive the `trigger` call.
+    _lifetime: std::marker::PhantomData<&'a ()>,
+}
+
+impl<'a> CffiProcessSession<'a> {
+    pub fn new(ptr: *mut minifi_process_session) -> Self {
+        Self {
+            ptr,
+            _lifetime: std::marker::PhantomData,
+        }
+    }
+
+    fn write_in_batches<F>(
+        &self,
+        flow_file: &CffiFlowFile,
+        produce_batch: F,
+    ) -> Result<(), MinifiError>
+    where
+        F: FnMut(&mut [u8]) -> Result<Option<usize>, MinifiError>,
+    {
+        unsafe {
+            struct State<'b, F>
+            where
+                F: FnMut(&mut [u8]) -> Result<Option<usize>, MinifiError>,
+            {
+                callback: F,
+                buffer: &'b mut [u8],
+                error: Option<MinifiError>,
+            }
+
+            let mut buffer = [0u8; 8192];
+            let mut state = State {
+                callback: produce_batch,
+                buffer: &mut buffer,
+                error: None,
+            };
+
+            unsafe extern "C" fn cb<F>(
+                user_ctx: *mut c_void,
+                output_stream: *mut minifi_output_stream,
+            ) -> i64
+            where
+                F: FnMut(&mut [u8]) -> Result<Option<usize>, MinifiError>,
+            {
+                unsafe {
+                    let state = &mut *(user_ctx as *mut State<F>);
+                    let mut overall_writes = 0;
+
+                    loop {
+                        let n = match (state.callback)(state.buffer) {
+                            Ok(Some(n)) => n,
+                            Ok(None) => break, // EOF
+                            Err(err) => {
+                                state.error = Some(err);
+                                return minifi_io_status_MINIFI_IO_ERROR;
+                            }
+                        };
+
+                        let written = minifi_output_stream_write(
+                            output_stream,
+                            state.buffer.as_ptr() as *const c_char,
+                            n,
+                        );
+
+                        if written < 0 {
+                            return minifi_io_status_MINIFI_IO_ERROR;
+                        }
+                        overall_writes += written;
+                    }

Review Comment:
   it seems correct now, thanks for the fix



##########
minifi_rust/extensions/minifi_rs_playground/features/steps/steps.py:
##########
@@ -0,0 +1,45 @@
+from behave import then, when, given
+import humanfriendly
+
+from minifi_behave.steps import checking_steps  # noqa: F401
+from minifi_behave.steps import configuration_steps  # noqa: F401
+from minifi_behave.steps import core_steps  # noqa: F401
+from minifi_behave.steps import flow_building_steps  # noqa: F401
+from minifi_behave.core.helpers import wait_for_condition
+from minifi_behave.core.minifi_test_context import MinifiTestContext
+
+
+@when("the MiNiFi instance is started without assertions")
+def minifi_starts_wo_assertions(context: MinifiTestContext):
+    context.get_or_create_default_minifi_container().deploy(context)

Review Comment:
   explaining this difference in a code comment would help future readers too



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