lib/daybreak/record.rb in daybreak-0.1.2 vs lib/daybreak/record.rb in daybreak-0.1.3
- old
+ new
@@ -7,11 +7,10 @@
# Thrown when there is a CRC mismatch between the data from the disk
# and what was written to disk previously.
class CorruptDataError < Exception; end
extend self
- extend Locking
# The mask a record uses to check for deletion.
DELETION_MASK = 1 << 31
# Read a record from an open io source, check the CRC, and set <tt>@key</tt>
@@ -26,24 +25,21 @@
s << crc_string(s)
end
# Create a new record to read from IO.
# @param [#read] io an IO instance to read from
- def read(io)
- lock io do
- record = []
- masked = read32(io)
- # Read the record's key bytes
- record << io.read(masked & (DELETION_MASK - 1)) <<
- # Read the record's value bytes
- io.read(read32(io)) <<
- # Set the deletion flag
- ((masked & DELETION_MASK) != 0)
- crc = io.read(4)
- raise CorruptDataError, 'CRC mismatch' unless crc == crc_string(key_data_string(record))
- record
- end
+ def deserialize(buf)
+ record = []
+ masked = read32(buf)
+ # Read the record's key bytes
+ record << buf.slice!(0, masked & (DELETION_MASK - 1)) <<
+ # Read the record's value bytes
+ buf.slice!(0, read32(buf)) <<
+ # Set the deletion flag
+ ((masked & DELETION_MASK) != 0)
+ raise CorruptDataError, 'CRC mismatch' unless buf.slice!(0, 4) == crc_string(key_data_string(record))
+ record
end
private
# Return the deletion flag plus two length prefixed cells
@@ -57,11 +53,10 @@
def part(data, length)
[length].pack('N') << data
end
- def read32(io)
- raw = io.read(4)
- raw.unpack('N')[0]
+ def read32(buf)
+ buf.slice!(0, 4).unpack('N')[0]
end
end
end