Skip to content

Commit

Permalink
write_to uses #open
Browse files Browse the repository at this point in the history
* change keyword argument `compress` -> `compressed`
  • Loading branch information
ganmacs committed Aug 30, 2016
1 parent 53e585f commit 71970b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
18 changes: 10 additions & 8 deletions lib/fluent/plugin/buffer/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,22 @@ module Decompressable
include Fluent::Plugin::Compressable

def read(**kwargs)
return super if kwargs[:compress] == :gzip

# avoid creating duplicated IO
if @chunk.is_a?(IO)
# reset io(@chunk) to read
@chunk.seek(0, IO::SEEK_SET)
decompress('', io: @chunk) # not to call IO#read
if kwargs[:compressed] == :gzip
super
else
decompress(super)
end
end

def write_to(io, **kwargs)
io.write(read(kwargs))
if kwargs[:compressed] == :gzip
super
else
open do |io_chunk|
# Avoid to load large String object when file_chunk
decompress(io_chunk.read, output_io: io)
end
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/plugin/test_buffer_file_chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def gen_chunk_path(prefix, unique_id)
c.commit

assert_equal str, c.read
assert_equal compressed_str, c.read(compress: :gzip)
assert_equal compressed_str, c.read(compressed: :gzip)

io = StringIO.new
c.write_to(io)
Expand All @@ -795,10 +795,10 @@ def gen_chunk_path(prefix, unique_id)
c.commit

assert_equal str, c.read
assert_equal compressed_str, c.read(compress: :gzip)
assert_equal compressed_str, c.read(compressed: :gzip)

io = StringIO.new
c.write_to(io, compress: :gzip)
c.write_to(io, compressed: :gzip)
assert_equal compressed_str, io.string
end
end
6 changes: 3 additions & 3 deletions test/plugin/test_buffer_memory_chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class BufferMemoryChunkTest < Test::Unit::TestCase
c.commit

assert_equal str, c.read
assert_equal compressed_str, c.read(compress: :gzip)
assert_equal compressed_str, c.read(compressed: :gzip)

io = StringIO.new
c.write_to(io)
Expand All @@ -289,10 +289,10 @@ class BufferMemoryChunkTest < Test::Unit::TestCase
c.commit

assert_equal str, c.read
assert_equal compressed_str, c.read(compress: :gzip)
assert_equal compressed_str, c.read(compressed: :gzip)

io = StringIO.new
c.write_to(io, compress: :gzip)
c.write_to(io, compressed: :gzip)
assert_equal compressed_str, io.string
end
end

0 comments on commit 71970b6

Please sign in to comment.