class Daybreak::Format

Database format serializer and deserializer. You can create your own implementation of this class and define your own database format! @api public

Constants

DELETE

Special value size used for deleted records

MAGIC

Magic string of the file header

VERSION

Database file format version

Public Instance Methods

dump(record) click to toggle source

Serialize record and return string @param [Array] record an array with [key, value] or [key] if the record is deleted @return [String] serialized record

# 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() click to toggle source

Return database header as string @return [String] database file header

# File lib/daybreak/format.rb, line 18
def header
  MAGIC + [VERSION].pack('n')
end
parse(buf) { |value_size == DELETE ? [data : [data, data| ... } click to toggle source

Deserialize records from buffer, and yield them. @param [String] buf the buffer to read from @yield [Array] blk deserialized record [key, value] or [key] if the record is deleted @return [Fixnum] number of records

# File lib/daybreak/format.rb, line 39
def parse(buf)
  n, count = 0, 0
  while n < buf.size
    key_size, value_size = buf[n, 8].unpack('NN')
    data_size = key_size + 8
    data_size += value_size if value_size != DELETE
    data = buf[n, data_size]
    n += data_size
    raise 'CRC mismatch: your data might be corrupted!' unless buf[n, 4] == crc32(data)
    n += 4
    yield(value_size == DELETE ? [data[8, key_size]] : [data[8, key_size], data[8 + key_size, value_size]])
    count += 1
  end
  count
end
read_header(input) click to toggle source

Read database header from input stream @param [#read] input the input stream @return void

# 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

Protected Instance Methods

crc32(s) click to toggle source

Compute crc32 of string @param [String] s a string @return [Fixnum]

# File lib/daybreak/format.rb, line 69
def crc32(s)
  [Zlib.crc32(s, 0)].pack('N')
end