lib/s3io/read_wrapper.rb in s3io-1.0.0 vs lib/s3io/read_wrapper.rb in s3io-1.1.0

- old
+ new

@@ -2,10 +2,14 @@ def self.reader(s3object, options = {}, &block) open(s3object, 'r', options, &block) end + # This error indicates that the object was modified between being opened + # and being read. + class ReadModifiedError < IOError; end + class ReadWrapper < Wrapper # Default buffer size for line parser in bytes LINE_BUFFER_SIZE = 5 * 1024 * 1024 # MiB @@ -18,10 +22,11 @@ super(s3object) @options = { :line_buffer_size => (options[:line_buffer_size] || LINE_BUFFER_SIZE) } + @last_modified = @s3object.last_modified end # Reads data from S3 object. # # @param [Integer] bytes number of bytes to read @@ -34,10 +39,16 @@ 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 @@ -79,8 +90,7 @@ return self end alias lines each alias each_line each - end end