class Traject::MockReader

A mock reader, designed to do almost no work during a run to provide better benchmarking

It pulls in 20 records from the end of this file and then just returns them over and over again, up to the specified limit

Specify in a config files as follows:

require 'traject/mock_writer'
require 'traject/mock_reader'

settings do
  store "reader_class_name", "Traject::MockReader"
  store "writer_class_name", "Traject::MockWriter"
  store "mock_reader.limit", 4_000 # default is 10_000
end

Attributes

limit[RW]

Public Class Methods

new(input_stream, settings = {}) click to toggle source

@param [Ignored] input_stream (ignored) @param [Hash] settings (looks only for an integer in 'mock_reader.limit')

# File lib/traject/mock_reader.rb, line 27
def initialize(input_stream, settings = {})
  @limit = (settings["mock_reader.limit"]  || 10_000).to_i

  @records = load_ndjson(File.open(__FILE__))

  # freeze it immutable for thread safety and performance
  @records.each {|r| r.fields.freeze}
end

Public Instance Methods

each() { |records| ... } click to toggle source
# File lib/traject/mock_reader.rb, line 62
def each
  unless block_given?
    enum_for(:each)
  else
    size = @records.size
    @limit.times do |i|
      yield @records[i % size]
    end
  end
end
load_ndjson(file_io) click to toggle source

newline delimited json, assuming no internal unescaped newlines in json too!

# File lib/traject/mock_reader.rb, line 38
def load_ndjson(file_io)
  records = []

  this_file_iter = file_io.each_line


  while true
    line = this_file_iter.next
    break if /^\_\_END\_\_/.match line
  end

  begin
    while true
      json = this_file_iter.next
      next unless /\S/.match json
      records << MARC::Record.new_from_hash(JSON.parse(json))
    end
  rescue StopIteration
  end

  return records
end