MCPcopy Create free account
hub / github.com/dpkp/kafka-python / lz4_encode_old_kafka

Function lz4_encode_old_kafka

kafka/codec.py:259–288  ·  view source on GitHub ↗

Encode payload for 0.8/0.9 brokers -- requires an incorrect header checksum.

(payload)

Source from the content-addressed store, hash-verified

257
258
259def lz4_encode_old_kafka(payload):
260 """Encode payload for 0.8/0.9 brokers -- requires an incorrect header checksum."""
261 if xxhash is None:
262 raise RuntimeError('pip install xxhash for lz4 encoding with 0.8/0.9 brokers')
263 data = lz4_encode(payload)
264 header_size = 7
265 flg = data[4]
266 if not isinstance(flg, int):
267 flg = ord(flg)
268
269 content_size_bit = ((flg >> 3) & 1)
270 if content_size_bit:
271 # Old kafka does not accept the content-size field
272 # so we need to discard it and reset the header flag
273 flg -= 8
274 data = bytearray(data)
275 data[4] = flg
276 data = bytes(data)
277 payload = data[header_size+8:]
278 else:
279 payload = data[header_size:]
280
281 # This is the incorrect hc
282 hc = xxhash.xxh32(data[0:header_size-1]).digest()[-2:-1] # pylint: disable-msg=no-member
283
284 return b''.join([
285 data[0:header_size-1],
286 hc,
287 payload
288 ])
289
290
291def lz4_decode_old_kafka(payload):

Callers 2

test_lz4_oldFunction · 0.90
_maybe_compressMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_lz4_oldFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…