Github user phrocker commented on a diff in the pull request:

    https://github.com/apache/nifi-minifi-cpp/pull/116#discussion_r125020742
  
    --- Diff: libminifi/src/processors/TailFile.cpp ---
    @@ -222,27 +233,52 @@ void TailFile::onTrigger(core::ProcessContext 
*context, core::ProcessSession *se
       checkRollOver(fileLocation, fileName);
       std::string fullPath = fileLocation + "/" + _currentTailFileName;
       struct stat statbuf;
    +
       if (stat(fullPath.c_str(), &statbuf) == 0) {
         if (statbuf.st_size <= this->_currentTailFilePosition) {
    -      // there are no new input for the current tail fil
    +      // there are no new input for the current tail file
           context->yield();
           return;
         }
    -    std::shared_ptr<FlowFileRecord> flowFile = 
std::static_pointer_cast<FlowFileRecord>(session->create());
    -    if (!flowFile)
    -      return;
    -    std::size_t found = _currentTailFileName.find_last_of(".");
    -    std::string baseName = _currentTailFileName.substr(0, found);
    -    std::string extension = _currentTailFileName.substr(found + 1);
    -    flowFile->updateKeyedAttribute(PATH, fileLocation);
    -    flowFile->addKeyedAttribute(ABSOLUTE_PATH, fullPath);
    -    session->import(fullPath, flowFile, true, 
this->_currentTailFilePosition);
    -    session->transfer(flowFile, Success);
    -    logger_->log_info("TailFile %s for %d bytes", 
_currentTailFileName.c_str(), flowFile->getSize());
    -    std::string logName = baseName + "." + 
std::to_string(_currentTailFilePosition) + "-" + 
std::to_string(_currentTailFilePosition + flowFile->getSize()) + "." + 
extension;
    -    flowFile->updateKeyedAttribute(FILENAME, logName);
    -    this->_currentTailFilePosition += flowFile->getSize();
    -    storeState();
    +
    +      std::size_t found = _currentTailFileName.find_last_of(".");
    +      std::string baseName = _currentTailFileName.substr(0, found);
    +      std::string extension = _currentTailFileName.substr(found + 1);
    +
    +    if (!this->_delimiter.empty()) {
    +      char delim = this->_delimiter.c_str()[0];
    +      std::shared_ptr<std::vector<std::shared_ptr<FlowFileRecord>>> 
flowFiles = std::make_shared<std::vector<std::shared_ptr<FlowFileRecord>>>();
    +      session->import(fullPath, flowFiles, true, 
this->_currentTailFilePosition, delim);
    +      logger_->log_info("%d flowfiles were received from TailFile input", 
flowFiles->size());
    +
    +      for(std::shared_ptr<FlowFileRecord> ffr : *flowFiles) {
    +        logger_->log_info("TailFile %s for %d bytes", 
_currentTailFileName.c_str(), ffr->getSize());
    +        std::string logName = baseName + "." + 
std::to_string(_currentTailFilePosition) + "-" + 
std::to_string(_currentTailFilePosition + ffr->getSize()) + "." + extension;
    +          ffr->updateKeyedAttribute(PATH, fileLocation);
    +          ffr->addKeyedAttribute(ABSOLUTE_PATH, fullPath);
    +        ffr->updateKeyedAttribute(FILENAME, logName);
    +          session->transfer(ffr, Success);
    +        this->_currentTailFilePosition += ffr->getSize() + 1;    //TODO: 
Why am I having to add +1 here to get the output correct as expected????
    --- End diff --
    
    This is because the getline call of ifstream extracts the delimiter, but 
discards it. Thus when you create a flowfile, the size lacks the delimited 
byte. As a result you need to increment here to ensure the positional reference 
matches the input stream. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to