<commit_before># frozen_string_literal: true

module SISFC

  class RequestGenerator

    def initialize(opts={})
      if opts.has_key? :filename
        filename = opts[:filename]
        raise ArgumentError, "File #{filename} does not exist!" unless File.exist?(filename)
        @file = File.open(filename, mode='r')
      elsif opts.has_key? :command
        command = opts[:command]
        @file = IO.popen(command)
      else
        raise ArgumentError, "Need to provide either a filename or a command!"
      end

      # throw away the first line (containing the CSV headers)
      @file.gets

      # NOTE: so far we support only sequential integer rids
      @next_rid = 0

      setup_finalizer
    end


    def generate
      # read next line from file
      line = @file.gets
      raise "End of input reached while reading request #{@next_rid}!" unless line

      # parse data
      tokens = line.split(",") # should be faster than CSV parsing
      generation_time  = tokens[0].to_f
      workflow_type_id = tokens[1].to_i
      customer_id      = tokens[2].to_i

      # increase @next_rid
      @next_rid += 1

      # return request
      {
        rid: @next_rid,
        generation_time: generation_time,
        workflow_type_id: workflow_type_id,
        customer_id: customer_id,
      }
    end


    private
      # After object destruction, make sure that the input file is closed or
      # the input command process is killed.
      def setup_finalizer
        ObjectSpace.define_finalizer(self, self.class.close_io(@file))
      end

      # Need to make this a class method, or the deallocation won't take place. See:
      # http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
      def self.close_io(file)
        Proc.new do
          if file.respond_to? :pid
            Process.kill('INT', file.pid)
          elsif
            file.close
          end
        end
      end
  end

end<commit_msg>Fix elsif->else typo.<commit_after># frozen_string_literal: true

module SISFC

  class RequestGenerator

    def initialize(opts={})
      if opts.has_key? :filename
        filename = opts[:filename]
        raise ArgumentError, "File #{filename} does not exist!" unless File.exist?(filename)
        @file = File.open(filename, mode='r')
      elsif opts.has_key? :command
        command = opts[:command]
        @file = IO.popen(command)
      else
        raise ArgumentError, "Need to provide either a filename or a command!"
      end

      # throw away the first line (containing the CSV headers)
      @file.gets

      # NOTE: so far we support only sequential integer rids
      @next_rid = 0

      setup_finalizer
    end


    def generate
      # read next line from file
      line = @file.gets
      raise "End of input reached while reading request #{@next_rid}!" unless line

      # parse data
      tokens = line.split(",") # should be faster than CSV parsing
      generation_time  = tokens[0].to_f
      workflow_type_id = tokens[1].to_i
      customer_id      = tokens[2].to_i

      # increase @next_rid
      @next_rid += 1

      # return request
      {
        rid: @next_rid,
        generation_time: generation_time,
        workflow_type_id: workflow_type_id,
        customer_id: customer_id,
      }
    end


    private
      # After object destruction, make sure that the input file is closed or
      # the input command process is killed.
      def setup_finalizer
        ObjectSpace.define_finalizer(self, self.class.close_io(@file))
      end

      # Need to make this a class method, or the deallocation won't take place. See:
      # http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
      def self.close_io(file)
        Proc.new do
          if file.respond_to? :pid
            Process.kill('INT', file.pid)
          else
            file.close
          end
        end
      end
  end

end
{'fraction_non_alphanumeric': 0.08190865016585863, 'fraction_numerical': 0.006634345496300076, 'mean_word_length': 2.1689571544058204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4588632', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1122, 'n_words': 351}