pass compression level into zstandard compressor
MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -530,6 +530,9 @@ except ImportError:
cpdef zstandard_write_block(object fo, bytes block_bytes, compression_level):
"""Write block in "zstandard" codec."""
cdef bytearray tmp = bytearray()
+ if compression_level is not None:
+ data = zstd.ZstdCompressor(level=compression_level).compress(block_bytes)
+ else:
data = zstd.ZstdCompressor().compress(block_bytes)
write_long(tmp, len(data))
fo.write(tmp)

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -367,6 +367,9 @@ else:
def zstandard_write_block(encoder, block_bytes, compression_level):
"""Write block in "zstandard" codec."""
+ if compression_level is not None:
+ data = zstd.ZstdCompressor(level=compression_level).compress(block_bytes)
+ else:
data = zstd.ZstdCompressor().compress(block_bytes)
encoder.write_long(len(data))
encoder._fo.write(data)

MODIFY
tests/test_compression.py
tests/test_compression.py
@@ -130,7 +130,8 @@ def test_unsupported_codec():
list(fastavro.reader(modified_file))
-def test_compression_level():
+@pytest.mark.parametrize("codec", ["deflate", "zstandard"])
+def test_compression_level(codec):
"""https://github.com/fastavro/fastavro/issues/377"""
schema = {
"doc": "A weather reading.",
@@ -152,7 +153,7 @@ def test_compression_level():
]
file = BytesIO()
- fastavro.writer(file, schema, records, codec="deflate", codec_compression_level=9)
+ fastavro.writer(file, schema, records, codec=codec, codec_compression_level=9)
file.seek(0)
out_records = list(fastavro.reader(file))

pass compression level into zstandard compressor