Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/fluent/plugin/compressable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,29 @@ def compress(data, type: :gzip, **kwargs)
io = output_io || StringIO.new
if type == :gzip
writer = Zlib::GzipWriter.new(io)
writer.write(data)
writer.finish
io.string
elsif type == :zstd
writer = Zstd::StreamWriter.new(io)
sc = Zstd::StreamingCompress.new(**kwargs)
chunk_size = kwargs[:chunk_size] || (256 * 1024)

if data.is_a?(String)
i = 0
while i < data.bytesize
io << sc.compress(data.byteslice(i, chunk_size))
i += chunk_size
end
else
data.each { |d| io << sc.compress(d) }
end

# Need to close frame to prevent unknown frame descriptor errors
io << sc.finish
io.string
else
raise ArgumentError, "Unknown compression type: #{type}"
end
writer.write(data)
writer.finish
output_io || io.string
end

# compressed_data is String like `compress(data1) + compress(data2) + ... + compress(dataN)`
Expand Down
Loading