mirror of
https://github.com/fluent/fluentd-ui.git
synced 2025-08-11 00:37:06 +02:00
45 lines
878 B
Ruby
45 lines
878 B
Ruby
class FileReverseReader
|
|
attr_reader :io, :step
|
|
|
|
def initialize(io, step = 1024 * 1024 * 1)
|
|
@io = io
|
|
@step = step
|
|
end
|
|
|
|
def each_line(&block)
|
|
io.seek(0, IO::SEEK_END)
|
|
buf = ""
|
|
loop do
|
|
if reach_start_of_file?
|
|
last_pos = io.pos
|
|
io.seek(0, IO::SEEK_SET)
|
|
buf.insert(0, io.read(last_pos))
|
|
split_each_line(buf, &block)
|
|
break
|
|
end
|
|
|
|
io.seek(-1 * step, IO::SEEK_CUR)
|
|
buf.insert(0, io.read(step))
|
|
io.seek(-1 * step, IO::SEEK_CUR)
|
|
next if buf[$/].nil?
|
|
gap = buf.index($/)
|
|
buf.gsub!(/\A.*?\n/, "")
|
|
split_each_line(buf, &block)
|
|
buf = ""
|
|
io.seek(gap, IO::SEEK_CUR)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def split_each_line(buf, &block)
|
|
buf.split($/).reverse.each do |line|
|
|
block.call(line)
|
|
end
|
|
end
|
|
|
|
def reach_start_of_file?
|
|
step >= io.pos
|
|
end
|
|
end
|