Module: Avro::DataFile

Defined in:
lib/avro/data_file.rb

Defined Under Namespace

Classes: DataFileError, DeflateCodec, NullCodec, Reader, Writer

Constant Summary collapse

VERSION =
1
MAGIC =
"Obj" + [VERSION].pack('c')
MAGIC_SIZE =
MAGIC.size
SYNC_SIZE =
16
SYNC_INTERVAL =
1000 * SYNC_SIZE
META_SCHEMA =
Schema.parse('{"type": "map", "values": "bytes"}')
VALID_ENCODINGS =

not used yet

['binary']
VALID_CODECS =

TODO this constant won’t be updated if you register another codec. Deprecated in favor of Avro::DataFile::codecs

DataFile.codecs.keys

Class Method Summary collapse

Class Method Details

.codecsObject



51
52
53
# File 'lib/avro/data_file.rb', line 51

def self.codecs
  @codecs
end

.get_codec(codec) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/avro/data_file.rb', line 61

def self.get_codec(codec)
  codec ||= 'null'
  if codec.respond_to?(:compress) && codec.respond_to?(:decompress)
    codec # it's a codec instance
  elsif codec.is_a?(Class)
    codec.new # it's a codec class
  elsif @codecs.include?(codec.to_s)
    @codecs[codec.to_s] # it's a string or symbol (codec name)
  else
    raise DataFileError, "Unknown codec: #{codec.inspect}"
  end
end

.open(file_path, mode = 'r', schema = nil, codec = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/avro/data_file.rb', line 31

def self.open(file_path, mode='r', schema=nil, codec=nil)
  schema = Avro::Schema.parse(schema) if schema
  case mode
  when 'w'
    unless schema
      raise DataFileError, "Writing an Avro file requires a schema."
    end
    io = open_writer(File.open(file_path, 'wb'), schema, codec)
  when 'r'
    io = open_reader(File.open(file_path, 'rb'), schema)
  else
    raise DataFileError, "Only modes 'r' and 'w' allowed. You gave #{mode.inspect}."
  end

  yield io if block_given?
  io
ensure
  io.close if block_given? && io
end

.register_codec(codec) ⇒ Object



55
56
57
58
59
# File 'lib/avro/data_file.rb', line 55

def self.register_codec(codec)
  @codecs ||= {}
  codec = codec.new if !codec.respond_to?(:codec_name) && codec.is_a?(Class)
  @codecs[codec.codec_name.to_s] = codec
end