Class: Avro::DataFile::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/avro/data_file.rb

Overview

Read files written by DataFileWriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, datum_reader) ⇒ Reader

Returns a new instance of Reader.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/avro/data_file.rb', line 215

def initialize(reader, datum_reader)
  @reader = reader
  @decoder = IO::BinaryDecoder.new(reader)
  @datum_reader = datum_reader

  # read the header: magic, meta, sync
  read_header

  @codec = DataFile.get_codec(meta['avro.codec'])

  # get ready to read
  @block_count = 0
  datum_reader.writers_schema = Schema.parse meta['avro.schema']
end

Instance Attribute Details

#block_countObject

records remaining in current block



213
214
215
# File 'lib/avro/data_file.rb', line 213

def block_count
  @block_count
end

#block_decoderObject (readonly)

The binary decoder for the contents of a block (after codec decompression)



210
211
212
# File 'lib/avro/data_file.rb', line 210

def block_decoder
  @block_decoder
end

#codecObject (readonly)

Returns the value of attribute codec.



212
213
214
# File 'lib/avro/data_file.rb', line 212

def codec
  @codec
end

#datum_readerObject (readonly)

Returns the value of attribute datum_reader.



212
213
214
# File 'lib/avro/data_file.rb', line 212

def datum_reader
  @datum_reader
end

#decoderObject (readonly)

The reader and binary decoder for the raw file stream



207
208
209
# File 'lib/avro/data_file.rb', line 207

def decoder
  @decoder
end

#file_lengthObject (readonly)

Returns the value of attribute file_length.



212
213
214
# File 'lib/avro/data_file.rb', line 212

def file_length
  @file_length
end

#metaObject (readonly)

Returns the value of attribute meta.



212
213
214
# File 'lib/avro/data_file.rb', line 212

def meta
  @meta
end

#readerObject (readonly)

The reader and binary decoder for the raw file stream



207
208
209
# File 'lib/avro/data_file.rb', line 207

def reader
  @reader
end

#sync_markerObject (readonly)

Returns the value of attribute sync_marker.



212
213
214
# File 'lib/avro/data_file.rb', line 212

def sync_marker
  @sync_marker
end

Instance Method Details

#closeObject



253
254
255
# File 'lib/avro/data_file.rb', line 253

def close
  reader.close
end

#eachObject

Iterates through each datum in this file TODO(jmhodges): handle block of length zero



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/avro/data_file.rb', line 232

def each
  loop do
    if block_count == 0
      case
      when eof?; break
      when skip_sync
        break if eof?
        read_block_header
      else
        read_block_header
      end
    end

    datum = datum_reader.read(block_decoder)
    self.block_count -= 1
    yield(datum)
  end
end

#eof?Boolean

Returns:

  • (Boolean)


251
# File 'lib/avro/data_file.rb', line 251

def eof?; reader.eof?; end