Class: Yobi::ResticOutput
- Inherits:
-
Object
- Object
- Yobi::ResticOutput
- Defined in:
- lib/yobi/restic_output.rb,
sig/yobi.rbs
Overview
:nodoc:
Defined Under Namespace
Classes: LazyList
Constant Summary collapse
- DEFAULT_TAIL_CHUNK_SIZE =
4096
Instance Attribute Summary collapse
-
#file ⇒ File
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #each_line {|line| ... } ⇒ Object
- #index ⇒ Hash[String, Array[Integer]]
-
#initialize(tail_chunk_size: DEFAULT_TAIL_CHUNK_SIZE, transform: nil) ⇒ ResticOutput
constructor
A new instance of ResticOutput.
- #inspect ⇒ Object
- #last_line ⇒ String?
- #messages(message_type = nil, &transform) ⇒ Object
- #read_line_at(offset) ⇒ String
- #stderr_lines {|line| ... } ⇒ Object
- #to_s ⇒ Object
- #write_and_parse(line, origin) ⇒ Object
Constructor Details
#initialize(tail_chunk_size: DEFAULT_TAIL_CHUNK_SIZE, transform: nil) ⇒ ResticOutput
Returns a new instance of ResticOutput.
12 13 14 15 16 17 18 19 |
# File 'lib/yobi/restic_output.rb', line 12 def initialize(tail_chunk_size: DEFAULT_TAIL_CHUNK_SIZE, transform: nil) @file = Tempfile.new("yobi-restic-output") @file.unlink @tail_chunk_size = tail_chunk_size @transform = transform @index = nil @stderr_offsets = [] end |
Instance Attribute Details
#file ⇒ File (readonly)
Returns the value of attribute file.
10 11 12 |
# File 'lib/yobi/restic_output.rb', line 10 def file @file end |
Instance Method Details
#each_line ⇒ void #each_line ⇒ Enumerator[String, void]
49 50 51 52 53 54 |
# File 'lib/yobi/restic_output.rb', line 49 def each_line(&block) return enum_for(:each_line) unless block_given? @file.rewind @file.each_line(&block) end |
#index ⇒ Hash[String, Array[Integer]]
21 22 23 |
# File 'lib/yobi/restic_output.rb', line 21 def index @index ||= build_index end |
#inspect ⇒ Object
94 95 96 |
# File 'lib/yobi/restic_output.rb', line 94 def inspect "#<#{self.class} #{@file.size} bytes>" end |
#last_line ⇒ String?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/yobi/restic_output.rb', line 30 def last_line size = @file.size return nil if size.zero? window = @tail_chunk_size loop do read_size = [window, size].min @file.seek(size - read_size) lines = @file.read(read_size).each_line.to_a lines.shift if read_size < size line = lines.reverse_each.find { |candidate| !candidate.strip.empty? } return line.strip if line return nil if read_size == size window *= 2 end end |
#messages ⇒ void #messages(message_type) ⇒ Enumerator[Hash[String, untyped], void]
56 57 58 59 60 61 62 |
# File 'lib/yobi/restic_output.rb', line 56 def ( = nil, &transform) transform ||= @transform return () unless transform offsets = ? index[] : index.values.flatten.sort LazyList.new(self, offsets, transform) end |
#read_line_at(offset) ⇒ String
25 26 27 28 |
# File 'lib/yobi/restic_output.rb', line 25 def read_line_at(offset) @file.seek(offset) @file.gets end |
#stderr_lines ⇒ void #stderr_lines ⇒ Enumerator[String, void]
64 65 66 67 68 |
# File 'lib/yobi/restic_output.rb', line 64 def stderr_lines return enum_for(:stderr_lines) unless block_given? @stderr_offsets.each { |offset| yield read_line_at(offset) } end |
#to_s ⇒ Object
89 90 91 92 |
# File 'lib/yobi/restic_output.rb', line 89 def to_s @file.rewind @file.read end |
#write_and_parse(line, origin) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/yobi/restic_output.rb', line 70 def write_and_parse(line, origin) offset = @file.pos @file.write(line) parsed = begin JSON.parse(line) rescue JSON::ParserError nil end if parsed (@index ||= Hash.new { |hash, | hash[] = [] })[parsed["message_type"]] << offset if parsed["message_type"] @transform ? @transform.call(parsed) : parsed elsif origin == :stderr @stderr_offsets << offset nil end end |