Class: SmartS3Sync::FileTable

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_s3_sync/file_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, prefix = nil) ⇒ FileTable

Returns a new instance of FileTable.



5
6
7
8
9
10
# File 'lib/smart_s3_sync/file_table.rb', line 5

def initialize(root, prefix=nil)
  @map    = {} # map of hashes to file destinations
  @files  = [] # single list of files to keep
  @root   = File.expand_path(root) # root file destination
  @prefix =  prefix ? prefix.gsub(/(?:^\/)|(?:\/$)/,'') : ''
end

Instance Method Details

#copy!(fog_dir, sync_options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/smart_s3_sync/file_table.rb', line 20

def copy!(fog_dir, sync_options={})
  @map.sort_by do |(k, target)|
    1_000_000_000 * (target.local_source.nil? ? 0 : -1) -
        1_000_000 * target.destinations.length +
      1/1_048_576 * target.size
  end.each do |(k, target)|
    target.copy!(fog_dir, sync_options)
  end
end

#keep?(filename) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/smart_s3_sync/file_table.rb', line 30

def keep?(filename)
  @files.include?(filename)
end

#push(fog_file) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/smart_s3_sync/file_table.rb', line 12

def push(fog_file)
  digest = hash_key(fog_file) # pull cloud calculated hex digest from file
  @map[digest] ||= FileTarget.new(digest, fog_file.key, fog_file.content_length) # grab or create target
  destination_filename = File.expand_path(strip_prefix(fog_file.key), @root) # calculate local path
  @files.push destination_filename # add local path to global list of files to keep
  @map[digest].add_destination(destination_filename) # and add local path to the target
end

#to_copyObject



34
35
36
# File 'lib/smart_s3_sync/file_table.rb', line 34

def to_copy
  @_tc ||= @map.select {|key, target| target.destinations.length > 0 }.map{|x, y| y }
end

#to_downloadObject



38
39
40
# File 'lib/smart_s3_sync/file_table.rb', line 38

def to_download
  @_td ||= to_copy.select {|target| target.local_source.nil? }
end