Module: Avro::DataFile

Defined in:
lib/avro/data_file.rb

Defined Under Namespace

Classes: DataFileError, DeflateCodec, NullCodec, Reader, SnappyCodec, Writer, ZstandardCodec

Constant Summary collapse

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

not used yet

['binary'].freeze
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



53
54
55
# File 'lib/avro/data_file.rb', line 53

def self.codecs
  @codecs
end

.get_codec(codec) ⇒ Object



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

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



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

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



57
58
59
60
61
# File 'lib/avro/data_file.rb', line 57

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