Class: LogParser::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/log_parser/buffer.rb

Overview

Log buffers provide line after line from a given source, and only store as many lines as necessary:

* Read lines from `log` lazily, i.e. only if {find_index} or {[]} are called.
* Drop lines that are no longer needed, i.e. when {forward} is called.

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ Buffer

Creates a new buffer that reads lines from the given source.

Parameters:

  • log (Array<String>, IO, StringIO)

    Where to read log lines from. Can be either an array of ‘String`, an `IO` (e.g. `STDIN`), or a `StringIO`.



15
16
17
18
19
20
21
# File 'lib/log_parser/buffer.rb', line 15

def initialize(log)
  @buffer = []
  @stream = nil

  @buffer = log if log.is_a?(Array)
  @stream = log if log.is_a?(IO) || log.is_a?(StringIO)
end

Instance Method Details

#[](offset, length = nil) ⇒ String+

Retrieves the next ‘length` many log lines starting from index `offset`.

Parameters:

  • offset (Integer)

    The first index to retrieve. Note: Indices are relative to the start of the buffer, not the start of the log!

  • length (Integer) (defaults to: nil)

    The number of elements to retrieve.

Returns:

  • (String, Array<String>)

    If ‘length` is set, returns the array of lines with indices in `(offset..offset+length-1)`. Otherwise, returns the line with index `offset`.



84
85
86
87
88
89
90
91
92
# File 'lib/log_parser/buffer.rb', line 84

def [](offset, length = nil)
  base_length = length || 1
  while offset + base_length > @buffer.size
    return (length.nil? ? nil : @buffer[offset, @buffer.size]) if stream_is_done?
    @buffer.push(@stream.readline.rstrip)
  end

  length.nil? ? @buffer[offset] : @buffer[offset, length]
end

#buffer_sizeInteger

The current size of this buffer, that is the number of lines currently stored.

Returns:

  • (Integer)


33
34
35
# File 'lib/log_parser/buffer.rb', line 33

def buffer_size
  @buffer.size
end

#closevoid

This method returns an undefined value.

Closes the ‘IO` this buffer reads from, if any.



115
116
117
# File 'lib/log_parser/buffer.rb', line 115

def close
  @stream.close unless @stream.nil? || @stream.closed?
end

#empty?true, false

Determines whether there are more lines in this log (buffer).

Returns:

  • (true, false)


26
27
28
# File 'lib/log_parser/buffer.rb', line 26

def empty?
  @buffer.empty? && stream_is_done?
end

#find_index(starting_from = 0) {|line| ... } ⇒ Integer?

Finds the first index of an element that fulfills a predicate.

Parameters:

  • starting_from (Integer) (defaults to: 0)

    The first index to check. That is, indices ‘(0..starting_from - 1)` are skipped. Note: Indices are relative to the start of the buffer, not the start of the log!

Yields:

  • Invokes a block as predicate over lines.

Yield Parameters:

  • line (String)

    A line of the log.

Yield Returns:

  • (true, false)

    ‘true` if (and only if) `line` fulfills the predicate.

Returns:

  • (Integer, nil)

    The first index of an element that fulfills the predicate, if any; otherwise, ‘nil`.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/log_parser/buffer.rb', line 61

def find_index(starting_from = 0)
  index = starting_from
  element = self[index]

  until element.nil?
    return index if yield(element)
    index += 1
    element = self[index]
  end

  nil
end

#firstString?

The first available line, if any.

Note: Will read from the source if necessary.

Returns:

  • (String, nil)


42
43
44
# File 'lib/log_parser/buffer.rb', line 42

def first
  self[0]
end

#forward(offset = 1) ⇒ void

This method returns an undefined value.

Moves the front of this buffer forwards by ‘offset` elements.

That is, the following code returns ‘true`: “`ruby before = buffer buffer.forward after = buffer.first before == after “`

Parameters:

  • offset (Integer) (defaults to: 1)

    The number of lines to drop.



107
108
109
110
# File 'lib/log_parser/buffer.rb', line 107

def forward(offset = 1)
  self[offset]
  @buffer.slice!(0, offset)
end