Class: Bio::MAF::ThreadedChunkReaderWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/maf/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cr, buffer_size = 64) ⇒ ThreadedChunkReaderWrapper

Returns a new instance of ThreadedChunkReaderWrapper.



99
100
101
102
103
104
# 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

Instance Attribute Details

#crObject (readonly)



97
98
99
# File 'lib/bio/maf/parser.rb', line 97

def cr
  @cr
end

#posObject (readonly)



97
98
99
# File 'lib/bio/maf/parser.rb', line 97

def pos
  @pos
end

Instance Method Details

#fObject



112
113
114
# File 'lib/bio/maf/parser.rb', line 112

def f
  cr.f
end

#read_aheadObject

Read ahead into queue.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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_chunkObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object



163
164
165
# File 'lib/bio/maf/parser.rb', line 163

def read_chunk_at(*args)
  cr.read_chunk_at(*args)
end

#start_read_aheadObject

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



107
108
109
110
# 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