Class: S3io::ReadWrapper

Inherits:
Wrapper show all
Includes:
Enumerable
Defined in:
lib/s3io/read_wrapper.rb

Constant Summary collapse

LINE_BUFFER_SIZE =

Default buffer size for line parser in bytes

5 * 1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wrapper

#close

Constructor Details

#initialize(s3object, options = {}) ⇒ ReadWrapper

Returns a new instance of ReadWrapper.



21
22
23
24
25
26
27
28
# File 'lib/s3io/read_wrapper.rb', line 21

def initialize(s3object, options = {})
  super(s3object)

  @options = {
    :line_buffer_size => (options[:line_buffer_size] || LINE_BUFFER_SIZE)
  }
  @last_modified = @s3object.last_modified
end

Instance Attribute Details

#posObject

Current byte position in S3 object



19
20
21
# File 'lib/s3io/read_wrapper.rb', line 19

def pos
  @pos
end

Instance Method Details

#each(separator = $/) {|line| ... } ⇒ Object Also known as: lines, each_line

Iterates over S3 object lines.

Parameters:

  • separator (String) (defaults to: $/)

    line separator string

Yields:

  • (line)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/s3io/read_wrapper.rb', line 67

def each(separator = $/)
  return enum_for(:each, separator) unless block_given?

  line = ''
  newline_pos = nil

  # Either trying to parse the remainder or reading some more data
  while newline_pos || !(buffer = read(@options[:line_buffer_size])).empty?
    prev_newline_pos = newline_pos || 0
    newline_pos = buffer.index(separator, prev_newline_pos)

    if newline_pos
      line << buffer[prev_newline_pos..newline_pos]
      newline_pos += 1
      yield line
      line = ''
    else
      line << buffer[prev_newline_pos..-1]
    end
  end

  # Flush the remainder if body doesn't end with separator
  yield line unless line.empty?

  return self
end

#eof?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/s3io/read_wrapper.rb', line 55

def eof?
  @pos >= @s3object.content_length
end

#read(bytes = nil) ⇒ Object

Reads data from S3 object.

Parameters:

  • bytes (Integer) (defaults to: nil)

    number of bytes to read



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/s3io/read_wrapper.rb', line 33

def read(bytes = nil)
  content_length = @s3object.content_length

  return '' if (@pos >= content_length) || (bytes == 0)

  bytes ||= content_length

  upper_bound = @pos + bytes - 1
  upper_bound = (content_length - 1) if upper_bound >= content_length

  data = @s3object.read :range => @pos..upper_bound

  last_modified = @s3object.last_modified
  unless last_modified == @last_modified
    fail ReadModifiedError, "S3 object #{@s3object.key} was updated during read, last_modified=#{last_modified.to_s} (was #{@last_modified.to_s})"
  end

  @pos = upper_bound + 1

  return data
end

#rewindObject

Rewinds position to the very beginning of S3 object.



60
61
62
# File 'lib/s3io/read_wrapper.rb', line 60

def rewind
  @pos = 0
end