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


##########
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;
+                    }
+                    overall_writes
+                }
+            }
+
+            let status = minifi_process_session_write(
+                self.ptr,
+                flow_file.get_ptr(),
+                Some(cb::<F>),
+                &mut state as *mut _ as *mut c_void,
+            );
+
+            if let Some(err) = state.error.take() {
+                return Err(err);
+            }
+
+            match status {
+                #[allow(non_upper_case_globals)]
+                minifi_status_MINIFI_STATUS_SUCCESS => Ok(()),
+                error_code => Err(MinifiError::StatusError((
+                    "minifi_process_session_write".into(),
+                    NonZeroU32::new_unchecked(error_code),
+                ))),
+            }
+        }
+    }
+}
+
+impl<'a> ProcessSession for CffiProcessSession<'a> {
+    type FlowFile = CffiFlowFile<'a>; // FlowFile shouldn't outlive the Session
+
+    fn create(&mut self) -> Result<Self::FlowFile, MinifiError> {
+        let ff_ptr = unsafe { minifi_process_session_create(self.ptr, 
std::ptr::null_mut()) };
+        if ff_ptr.is_null() {
+            Err(MinifiError::UnknownError)
+        } else {
+            Ok(CffiFlowFile::new(ff_ptr))
+        }
+    }
+
+    fn get(&mut self) -> Option<Self::FlowFile> {
+        let ff_ptr = unsafe { minifi_process_session_get(self.ptr) };
+        if ff_ptr.is_null() {
+            None
+        } else {
+            Some(CffiFlowFile::new(ff_ptr))
+        }
+    }
+
+    fn transfer(&self, flow_file: Self::FlowFile, relationship: &str) -> 
Result<(), MinifiError> {
+        let c_relationship = CString::new(relationship)?;
+        unsafe {
+            match minifi_process_session_transfer(
+                self.ptr,
+                flow_file.get_ptr(),
+                minifi_string_view {
+                    data: c_relationship.as_ptr(),
+                    length: c_relationship.as_bytes().len(),
+                },
+            ) {
+                #[allow(non_upper_case_globals)]
+                minifi_status_MINIFI_STATUS_SUCCESS => Ok(()),
+                err_code => Err(MinifiError::StatusError((
+                    "minifi_process_session_transfer".into(),
+                    NonZeroU32::new_unchecked(err_code),
+                ))),
+            }
+        }
+    }
+
+    fn remove(&mut self, flow_file: Self::FlowFile) -> Result<(), MinifiError> 
{
+        unsafe {
+            match minifi_process_session_remove(self.ptr, flow_file.get_ptr()) 
{
+                #[allow(non_upper_case_globals)]
+                minifi_status_MINIFI_STATUS_SUCCESS => Ok(()),
+                err_code => Err(MinifiError::StatusError((
+                    "minifi_process_session_remove".into(),
+                    NonZeroU32::new_unchecked(err_code),
+                ))),
+            }
+        }
+    }
+
+    fn set_attribute(
+        &self,
+        flow_file: &mut Self::FlowFile,
+        attr_key: &str,
+        attr_value: &str,
+    ) -> Result<(), MinifiError> {
+        unsafe {
+            let attr_key_string_view = StringView::new(attr_key);
+            let attr_value_string_view = StringView::new(attr_value);
+            match minifi_process_session_set_flow_file_attribute(
+                self.ptr,
+                flow_file.get_ptr(),
+                attr_key_string_view.as_raw(),
+                &attr_value_string_view.as_raw(),
+            ) {
+                #[allow(non_upper_case_globals)]
+                minifi_status_MINIFI_STATUS_SUCCESS => Ok(()),
+                err_code => Err(MinifiError::StatusError((
+                    format!(
+                        "minifi_process_session_set_flow_file_attribute({}, 
{})",
+                        attr_key, attr_value
+                    )
+                    .into(),
+                    NonZeroU32::new_unchecked(err_code),
+                ))),
+            }
+        }
+    }
+
+    fn get_attribute(&self, flow_file: &Self::FlowFile, attr_key: &str) -> 
Option<String> {
+        let mut attr_value: Option<String> = None;
+        unsafe {
+            unsafe extern "C" fn cb(
+                rs_attr_value: *mut c_void,
+                minifi_attr_value: minifi_string_view,
+            ) {
+                unsafe {
+                    let result_target = &mut *(rs_attr_value as *mut 
Option<String>);
+                    *result_target = minifi_attr_value.as_str().map(|s| 
s.to_owned()).ok()
+                }
+            }
+
+            let attr_key_string_view = StringView::new(attr_key);
+            minifi_process_session_get_flow_file_attribute(
+                self.ptr,
+                flow_file.get_ptr(),
+                attr_key_string_view.as_raw(),
+                Some(cb),
+                &mut attr_value as *mut _ as *mut c_void,
+            );
+        }
+        attr_value
+    }
+
+    fn on_attributes<F: FnMut(&str, &str)>(

Review Comment:
   
https://github.com/apache/nifi-minifi-cpp/pull/2186/changes/12e0d803102feef8f8c3b2756865dccf0703a25b#diff-96aebac58eee80359110dca0bef2f59cb05dedbdb363db2e7d63fad26980133fR46



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