Class: Daybreak::Format
- Inherits:
-
Object
- Object
- Daybreak::Format
- Defined in:
- lib/daybreak/format.rb
Overview
Database format serializer and deserializer. You can create your own implementation of this class and define your own database format!
Constant Summary collapse
- MAGIC =
Magic string of the file header
'DAYBREAK'
- VERSION =
Database file format version
1
- DELETE =
Special value size used for deleted records
(1 << 32) - 1
Instance Method Summary collapse
-
#crc32(s) ⇒ Fixnum
protected
Compute crc32 of string.
-
#dump(record) ⇒ String
Serialize record and return string.
-
#header ⇒ String
Return database header as string.
-
#parse(buf) ⇒ Array
Deserialize record from buffer.
-
#read_header(input) ⇒ Object
Read database header from input stream.
Instance Method Details
#crc32(s) ⇒ Fixnum (protected)
Compute crc32 of string
59 60 61 |
# File 'lib/daybreak/format.rb', line 59 def crc32(s) [Zlib.crc32(s, 0)].pack('N') end |
#dump(record) ⇒ String
Serialize record and return string
25 26 27 28 29 30 31 32 33 |
# File 'lib/daybreak/format.rb', line 25 def dump(record) data = if record.size == 1 [record[0].bytesize, DELETE].pack('NN') << record[0] else [record[0].bytesize, record[1].bytesize].pack('NN') << record[0] << record[1] end data << crc32(data) end |
#header ⇒ String
Return database header as string
18 19 20 |
# File 'lib/daybreak/format.rb', line 18 def header MAGIC + [VERSION].pack('n') end |
#parse(buf) ⇒ Array
Deserialize record from buffer
38 39 40 41 42 43 |
# File 'lib/daybreak/format.rb', line 38 def parse(buf) key_size, value_size = buf[0, 8].unpack('NN') data = buf.slice!(0, 8 + key_size + (value_size == DELETE ? 0 : value_size)) raise 'CRC mismatch: your data might be corrupted!' unless buf.slice!(0, 4) == crc32(data) value_size == DELETE ? [data[8, key_size]] : [data[8, key_size], data[8 + key_size, value_size]] end |
#read_header(input) ⇒ Object
Read database header from input stream
10 11 12 13 14 |
# File 'lib/daybreak/format.rb', line 10 def read_header(input) raise 'Not a Daybreak database' if input.read(MAGIC.bytesize) != MAGIC ver = input.read(2).unpack('n').first raise "Expected database version #{VERSION}, got #{ver}" if ver != VERSION end |