class BitBroker::Solvant

Constants

DEFAULT_CHUNK_SIZE

Attributes

chunks[R]

Public Class Methods

load_binary(dirpath, binary) click to toggle source
# File lib/bitbroker/solvant.rb, line 48
def self.load_binary(dirpath, binary)
  data = MessagePack.unpack(binary)
  offset = data['offset'] * data['chunk_size']

  File.binwrite(dirpath + data['path'], data['data'], offset)
end
new(dirpath, r_path, chunk_size = DEFAULT_CHUNK_SIZE) click to toggle source
# File lib/bitbroker/solvant.rb, line 10
def initialize(dirpath, r_path, chunk_size = DEFAULT_CHUNK_SIZE)
  @f_path = "#{dirpath}/#{r_path}"

  # Validate target file at first
  if not FileTest.exist? @f_path
    FileUtils.touch(@f_path)
  end

  # separate per chunk
  @chunks = []
  chunk_splitter(File::Stat.new(@f_path).size, chunk_size) do |offset, size|
    @chunks.push(Chunk.new({
      :r_path => r_path,
      :f_path => @f_path,
      :size => size,
      :offset => offset,
      :chunk_size => chunk_size,
    }))
  end
end

Public Instance Methods

remove() click to toggle source

This defines operations to manipulate actual Flie object on FileSystem

# File lib/bitbroker/solvant.rb, line 32
def remove
  File.unlink(@f_path)
end
upload(broker) click to toggle source
# File lib/bitbroker/solvant.rb, line 36
def upload broker
  @chunks.each do |chunk|
    broker.send_data(chunk.serialize)
  end
end
upload_to(broker, dest) click to toggle source
# File lib/bitbroker/solvant.rb, line 42
def upload_to(broker, dest)
  @chunks.each do |chunk|
    broker.send_p_data(dest, chunk.serialize)
  end
end

Private Instance Methods

chunk_splitter(total_size, chunk_size, &block) click to toggle source
# File lib/bitbroker/solvant.rb, line 56
def chunk_splitter(total_size, chunk_size, &block)
  last = total_size / chunk_size
  (0..last).each do |i|
    size = (i == last) ? total_size - chunk_size * i : chunk_size

    block.call(i, size)
  end
end