szaszm commented on code in PR #2186: URL: https://github.com/apache/nifi-minifi-cpp/pull/2186#discussion_r3623099216
########## minifi_rust/extensions/minifi_rs_playground/src/processors/generate_flow_file.rs: ########## @@ -0,0 +1,196 @@ +// 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 minifi_native::macros::ComponentIdentifier; +use minifi_native::{ + GetProperty, Logger, MinifiError, OnTriggerResult, ProcessContext, ProcessSession, Schedule, + Trigger, +}; +use rand::RngExt; +use rand::distr::Alphanumeric; +use std::cmp::PartialEq; + +mod properties; +mod relationships; + +#[derive(Debug, PartialEq)] +enum Mode { + UniqueBytes, + UniqueText, + NotUniqueBytes, + NotUniqueText, + CustomText, + Empty, +} + +#[derive(Debug, ComponentIdentifier)] +pub(crate) struct GenerateFlowFileRs { + mode: Mode, + batch_size: u64, + file_size: u64, + data_generated_during_on_schedule: Vec<u8>, +} + +impl Schedule for GenerateFlowFileRs { + fn schedule<P: GetProperty, L: Logger>(context: &P, _logger: &L) -> Result<Self, MinifiError> + where + Self: Sized, + { + let is_unique = context + .get_bool_property(&properties::UNIQUE_FLOW_FILES)? + .expect("Required property"); + let is_text = context + .get_property(&properties::DATA_FORMAT)? + .expect("Required property") + .as_str() + == "Text"; + let has_custom_text = context.get_property(&properties::CUSTOM_TEXT)?.is_some(); + + let file_size = context + .get_size_property(&properties::FILE_SIZE)? + .expect("Required property"); + let batch_size = context + .get_u64_property(&properties::BATCH_SIZE)? + .expect("Required property"); + + let mode = Self::get_mode(is_unique, is_text, has_custom_text, file_size); + let data_generated_during_on_schedule = + if mode == Mode::NotUniqueText || mode == Mode::NotUniqueBytes { + let mut data = vec![0; file_size as usize]; + Self::generate_data(&mut data, is_text); + data + } else { + vec![] + }; + + Ok(Self { + mode, + batch_size, + file_size, + data_generated_during_on_schedule, + }) + } +} + +impl GenerateFlowFileRs { + fn is_unique(&self) -> bool { + match self.mode { + Mode::UniqueBytes => true, + Mode::UniqueText => true, + Mode::NotUniqueBytes => false, + Mode::NotUniqueText => false, + Mode::CustomText => false, + Mode::Empty => false, + } + } + + fn is_text(&self) -> bool { + match self.mode { + Mode::UniqueBytes => false, + Mode::UniqueText => true, + Mode::NotUniqueBytes => false, + Mode::NotUniqueText => true, + Mode::CustomText => true, + Mode::Empty => false, + } + } + + fn get_mode(is_unique: bool, is_text: bool, has_custom_text: bool, file_size: u64) -> Mode { + if is_text && !is_unique && has_custom_text { + return Mode::CustomText; + } + + if file_size == 0 { + return Mode::Empty; + } + + match (is_unique, is_text) { + (true, true) => Mode::UniqueText, + (true, false) => Mode::UniqueBytes, + (false, true) => Mode::NotUniqueText, + (false, false) => Mode::NotUniqueBytes, + } + } + + fn generate_data(data: &mut [u8], text_data: bool) { + let mut rng = rand::rng(); + + if text_data { + for byte in data.iter_mut() { + *byte = rng.sample(Alphanumeric); + } + } else { + rng.fill(data); + } + } +} + +impl Trigger for GenerateFlowFileRs { + fn trigger<PC, PS, L>( + &self, + context: &mut PC, + session: &mut PS, + _logger: &L, + ) -> Result<OnTriggerResult, MinifiError> + where + PC: ProcessContext, + PS: ProcessSession<FlowFile = PC::FlowFile>, + { + let non_unique_data_buffer: &[u8]; + let custom_text_for_batch: Option<String>; + + if self.mode == Mode::CustomText { + // CustomText mode must have the Custom Text property set at + // trigger time — falling back to `data_generated_during_on_schedule` + // (which is empty for this mode) would silently produce empty + // flow files. + custom_text_for_batch = Some( + context + .get_property(&properties::CUSTOM_TEXT, None)? Review Comment: do Expression Language expressions get evaluated in get_property? ########## minifi_rust/extensions/minifi_rs_playground/src/lib.rs: ########## @@ -0,0 +1,60 @@ +// 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. + +mod controller_services; +mod processors; + +use crate::controller_services::dog_controller_service::DogControllerRs; +use crate::controller_services::duck_controller_service::DuckControllerRs; +use crate::controller_services::dummy_controller_service::DummyControllerService; +use crate::controller_services::lorem_ipsum_controller_service::LoremIpsumControllerService; +use crate::processors::asciify_german::AsciifyGerman; +use crate::processors::count_actual_logging::CountActualLogging; +use crate::processors::duplicate_text::DuplicateStreamText; +use crate::processors::generate_flow_file::GenerateFlowFileRs; +use crate::processors::get_file::GetFileRs; +use crate::processors::kamikaze_processor::KamikazeProcessorRs; +use crate::processors::log_attribute::LogAttributeRs; +use crate::processors::lorem_ipsum_cs_user::LoremIpsumCSUser; +use crate::processors::put_file::PutFileRs; +use crate::processors::zoo_processor::ZooProcessorRs; + +use minifi_native::{ + ComplexProcessorType, FlowFileSourceProcessorType, FlowFileStreamTransformProcessorType, + FlowFileTransformProcessorType, MultiThreaded, SingleThreaded, +}; + +minifi_native::declare_minifi_extension!( +processors: [ Review Comment: I think we should indent the contents within `declare_minifi_extension!()` -- 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]
