Method: Rubcask::DataFile#append

Defined in:
lib/rubcask/data_file.rb

#append(entry) ⇒ AppendResult

Note:

Calling this method will not change ‘pos` of the `file`

Append an entry at the end of the file

Parameters:

  • entry (DataEntry)

    Entry to write to the file

Returns:

  • (AppendResult)

    struct containing position and size of the entry



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubcask/data_file.rb', line 90

def append(entry)
  current_pos = @write_pos

  key_size = entry.key.bytesize
  value_size = entry.value.bytesize

  crc = Zlib.crc32([
    entry.expire_timestamp,
    key_size,
    value_size
  ].pack(HEADER_WITHOUT_CRC_FORMAT) + entry.key + entry.value)
  @write_pos += @file.write(
    [crc, entry.expire_timestamp, key_size, value_size].pack(HEADER_FORMAT),
    entry.key,
    entry.value
  )
  @file.flush
  AppendResult.new(current_pos, @write_pos - current_pos)
end