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.



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

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



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

def block_count
  @block_count
end

#block_decoderObject (readonly)

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



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

def block_decoder
  @block_decoder
end

#codecObject (readonly)

Returns the value of attribute codec.



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

def codec
  @codec
end

#datum_readerObject (readonly)

Returns the value of attribute datum_reader.



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

def datum_reader
  @datum_reader
end

#decoderObject (readonly)

The reader and binary decoder for the raw file stream



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

def decoder
  @decoder
end

#file_lengthObject (readonly)

Returns the value of attribute file_length.



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

def file_length
  @file_length
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

#readerObject (readonly)

The reader and binary decoder for the raw file stream



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

def reader
  @reader
end

#sync_markerObject (readonly)

Returns the value of attribute sync_marker.



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

def sync_marker
  @sync_marker
end

Instance Method Details

#closeObject



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

def close
  reader.close
end

#eachObject

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



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

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)


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

def eof?; reader.eof?; end