def pack_into(self, buff, offset):
"""Serialize and write to ``buff`` starting at offset ``offset``.
Intentionally follows the pattern of ``struct.pack_into``
:param buff: The buffer to write into
:param offset: The offset to start the write at
"""
# NB a length of 0 means an empty string, whereas -1 means null
len_key = -1 if self.partition_key is None else len(self.partition_key)
len_value = -1 if self.value is None else len(self.value)
fmt = '!BBi%dsi%ds' % (max(len_key, 0), max(len_value, 0))
args = (self.MAGIC,
self.compression_type,
len_key,
self.partition_key or b"",
len_value,
self.value or b"")
struct.pack_into(fmt, buff, offset + 4, *args)
fmt_size = struct.calcsize(fmt)
data = buffer(buff[(offset + 4):(offset + 4 + fmt_size)])
crc = crc32(data) & 0xffffffff
struct.pack_into('!I', buff, offset, crc)
评论列表
文章目录