From 88f66362af13f953ecef9a7eb110f545a40574cc Mon Sep 17 00:00:00 2001 From: Dustin Rodrigues Date: Sat, 18 Nov 2017 14:45:52 -0500 Subject: [PATCH] MINIFICPP-307 - support text mode in GenerateFlowFile MINIFICPP-307 - fix linter issues, typo --- libminifi/src/processors/GenerateFlowFile.cpp | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/libminifi/src/processors/GenerateFlowFile.cpp b/libminifi/src/processors/GenerateFlowFile.cpp index 3741a8f3a4..32ca26b224 100644 --- a/libminifi/src/processors/GenerateFlowFile.cpp +++ b/libminifi/src/processors/GenerateFlowFile.cpp @@ -45,6 +45,8 @@ core::Property GenerateFlowFile::BatchSize("Batch Size", "The number of FlowFile core::Property GenerateFlowFile::DataFormat("Data Format", "Specifies whether the data should be Text or Binary", GenerateFlowFile::DATA_FORMAT_BINARY); core::Property GenerateFlowFile::UniqueFlowFiles("Unique FlowFiles", "If true, each FlowFile that is generated will be unique. If false, a random value will be generated and all FlowFiles", "true"); core::Relationship GenerateFlowFile::Success("success", "success operational on the flow record"); +const unsigned int TEXT_LEN = 90; +static const char TEXT_CHARS[TEXT_LEN+1] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_=+/?.,';:\"?<>\n\t "; void GenerateFlowFile::initialize() { // Set the supported properties @@ -64,6 +66,7 @@ void GenerateFlowFile::onTrigger(core::ProcessContext *context, core::ProcessSes int64_t batchSize = 1; bool uniqueFlowFile = true; int64_t fileSize = 1024; + bool textData = false; std::string value; if (context->getProperty(FileSize.getName(), value)) { @@ -72,6 +75,9 @@ void GenerateFlowFile::onTrigger(core::ProcessContext *context, core::ProcessSes if (context->getProperty(BatchSize.getName(), value)) { core::Property::StringToInt(value, batchSize); } + if (context->getProperty(DataFormat.getName(), value)) { + textData = (value == GenerateFlowFile::DATA_FORMAT_TEXT); + } if (context->getProperty(UniqueFlowFiles.getName(), value)) { org::apache::nifi::minifi::utils::StringUtils::StringToBool(value, uniqueFlowFile); } @@ -84,10 +90,17 @@ void GenerateFlowFile::onTrigger(core::ProcessContext *context, core::ProcessSes uint64_t dataSize = fileSize; GenerateFlowFile::WriteCallback callback(data, dataSize); char *current = data; - for (int i = 0; i < fileSize; i += sizeof(int)) { - int randValue = random(); - *(reinterpret_cast(current)) = randValue; - current += sizeof(int); + if (textData) { + for (int i = 0; i < fileSize; i++) { + int randValue = random(); + data[i] = TEXT_CHARS[randValue % TEXT_LEN]; + } + } else { + for (int i = 0; i < fileSize; i += sizeof(int)) { + int randValue = random(); + *(reinterpret_cast(current)) = randValue; + current += sizeof(int); + } } for (int i = 0; i < batchSize; i++) { // For each batch @@ -101,14 +114,21 @@ void GenerateFlowFile::onTrigger(core::ProcessContext *context, core::ProcessSes delete[] data; } else { if (!_data) { - // We have not create the unique data yet + // We have not created the unique data yet _data = new char[fileSize]; _dataSize = fileSize; char *current = _data; - for (int i = 0; i < fileSize; i += sizeof(int)) { - int randValue = random(); - *(reinterpret_cast(current)) = randValue; - current += sizeof(int); + if (textData) { + for (int i = 0; i < fileSize; i++) { + int randValue = random(); + _data[i] = TEXT_CHARS[randValue % TEXT_LEN]; + } + } else { + for (int i = 0; i < fileSize; i += sizeof(int)) { + int randValue = random(); + *(reinterpret_cast(current)) = randValue; + current += sizeof(int); + } } } GenerateFlowFile::WriteCallback callback(_data, _dataSize);