DataToken FileManagerStrategy::read()

in file_management/src/file_upload/file_manager_strategy.cpp [300:333]


DataToken FileManagerStrategy::read(std::string &data) {
  std::lock_guard<std::mutex> read_lock(active_read_file_mutex_);
  if (active_read_file_.empty()) {
    active_read_file_ = getFileToRead();
    // if the file is still empty, return an empty token.
    if (active_read_file_.empty()) {
      return 0;
    }
    active_read_file_stream_ = std::make_unique<std::ifstream>(active_read_file_);
  }
  AWS_LOG_DEBUG(__func__, "Reading from active log file: %s", active_read_file_.c_str());
  DataToken token;
  if (token_store_->isTokenAvailable(active_read_file_)) {
    FileTokenInfo file_token = token_store_->popAvailableToken(active_read_file_);
    active_read_file_stream_->seekg(file_token.position_);
  }
  int position = active_read_file_stream_->tellg();
  auto file_size = active_read_file_stream_->seekg(0, std::ifstream::end).tellg();
  active_read_file_stream_->seekg(position, std::ifstream::beg);
  std::getline(*active_read_file_stream_, data);
  int next_position = active_read_file_stream_->tellg();
  token = token_store_->createToken(active_read_file_, position, next_position >= file_size);

  if (next_position >= file_size) {
    auto file_loc = std::find(stored_files_.begin(), stored_files_.end(), active_read_file_);
    if (file_loc != stored_files_.end()) {
      stored_files_.erase(file_loc);
    }
    active_read_file_.clear();
    active_read_file_stream_ = nullptr;

  }
  return token;
}