class Bio::MAF::ThreadedChunkReaderWrapper

Attributes

cr[R]
pos[R]

Public Class Methods

new(cr, buffer_size=64) click to toggle source
# File lib/bio/maf/parser.rb, line 99
def initialize(cr, buffer_size=64)
  @cr = cr
  @buffer = java.util.concurrent.LinkedBlockingQueue.new(buffer_size)
  @eof_reached = false
  @first_seq_read = false
end

Public Instance Methods

f() click to toggle source
# File lib/bio/maf/parser.rb, line 112
def f
  cr.f
end
read_ahead() click to toggle source

Read ahead into queue.

# File lib/bio/maf/parser.rb, line 117
def read_ahead
  # n = 0
  begin
    until f.eof?
      chunk = cr.read_chunk
      c_pos = cr.pos
      @buffer.put([c_pos, chunk])
    end
    @buffer.put(:eof)
    # @eof_reached = true
  rescue Exception
    @read_ahead_ex = $!
    LOG.error $!
    @buffer.put($!)
  end
end
read_chunk() click to toggle source
# File lib/bio/maf/parser.rb, line 134
def read_chunk
  if ! @first_seq_read
    # this is the first read_chunk call to read the header
    # not necessarily indicative of sequential access
    @first_seq_read = true
    chunk = cr.read_chunk
    @pos = cr.pos
    return chunk
  elsif @read_ahead_ex
    raise @read_ahead_ex
  elsif @eof_reached
    return nil
  else
    start_read_ahead if @read_thread.nil?
    e = @buffer.take
    case
    when e == :eof
      @eof_reached = nil
      return nil
    when e.is_a?(Exception)
      raise e
    else
      c_pos, chunk = e
      @pos = c_pos
      return chunk
    end
  end
end
read_chunk_at(*args) click to toggle source
# File lib/bio/maf/parser.rb, line 163
def read_chunk_at(*args)
  cr.read_chunk_at(*args)
end
start_read_ahead() click to toggle source

Spawn a read-ahead thread. Called from {#initialize}.

# File lib/bio/maf/parser.rb, line 107
def start_read_ahead
  LOG.debug { "Starting read-ahead thread." }
  @read_thread = Thread.new { read_ahead }
end