Class: FasterS3::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/faster_s3/part.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, index, part_length, extra_bytes) ⇒ Part

Returns a new instance of Part.



4
5
6
7
8
9
# File 'lib/faster_s3/part.rb', line 4

def initialize(file_path, index, part_length, extra_bytes)
  self.file_path = file_path
  self.index = index
  self.part_length = part_length
  self.extra_bytes = extra_bytes
end

Instance Attribute Details

#extra_bytesObject

Returns the value of attribute extra_bytes.



2
3
4
# File 'lib/faster_s3/part.rb', line 2

def extra_bytes
  @extra_bytes
end

#file_pathObject

Returns the value of attribute file_path.



2
3
4
# File 'lib/faster_s3/part.rb', line 2

def file_path
  @file_path
end

#indexObject

Returns the value of attribute index.



2
3
4
# File 'lib/faster_s3/part.rb', line 2

def index
  @index
end

#part_lengthObject

Returns the value of attribute part_length.



2
3
4
# File 'lib/faster_s3/part.rb', line 2

def part_length
  @part_length
end

Instance Method Details

#byte_rangeObject



15
16
17
18
19
20
# File 'lib/faster_s3/part.rb', line 15

def byte_range
  start = index * part_length
  end_pos = start + part_length + extra_bytes

  (start + existing_size)...end_pos
end

#download(s3_object) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/faster_s3/part.rb', line 26

def download(s3_object)
  return if byte_range.min == byte_range.max
  File.open(part_path, 'ab') do |file|
    s3_object.read(range: byte_range) do |chunk|
      file.write(chunk)
    end
  end
end

#existing_sizeObject



22
23
24
# File 'lib/faster_s3/part.rb', line 22

def existing_size
  File.exists?(part_path) ? File.size(part_path) : 0
end

#part_pathObject



11
12
13
# File 'lib/faster_s3/part.rb', line 11

def part_path
  "#{file_path}.part.#{index}"
end