Module: Tros::DataFile
- Defined in:
- lib/tros/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.bytesize
- SYNC_SIZE =
16
- SYNC_INTERVAL =
4000 * SYNC_SIZE
- META_SCHEMA =
Schema.parse('{"type": "map", "values": "bytes"}')
- VALID_ENCODINGS =
['binary']
- VALID_CODECS =
TODO this constant won’t be updated if you register another codec. Deprecated in favor of Tros::DataFile::codecs
DataFile.codecs.keys
Class Method Summary
collapse
Class Method Details
.codecs ⇒ Object
51
52
53
|
# File 'lib/tros/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/tros/data_file.rb', line 61
def self.get_codec(codec)
codec ||= 'null'
if codec.respond_to?(:compress) && codec.respond_to?(:decompress)
codec elsif codec.is_a?(Class)
codec.new elsif @codecs.include?(codec.to_s)
@codecs[codec.to_s] 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/tros/data_file.rb', line 31
def self.open(file_path, mode='r', schema=nil, codec=nil)
schema = Tros::Schema.parse(schema) if schema
case mode
when 'w'
unless schema
raise DataFileError, "Writing an Tros 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/tros/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
|