Class: BitBroker::Solvant
- Inherits:
-
Object
- Object
- BitBroker::Solvant
- Defined in:
- lib/bitbroker/solvant.rb
Defined Under Namespace
Classes: Chunk
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
1<<20
Instance Attribute Summary collapse
-
#chunks ⇒ Object
readonly
Returns the value of attribute chunks.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(dirpath, r_path, chunk_size = DEFAULT_CHUNK_SIZE) ⇒ Solvant
constructor
A new instance of Solvant.
-
#remove ⇒ Object
This defines operations to manipulate actual Flie object on FileSystem.
- #upload(broker) ⇒ Object
- #upload_to(broker, dest) ⇒ Object
Constructor Details
#initialize(dirpath, r_path, chunk_size = DEFAULT_CHUNK_SIZE) ⇒ Solvant
Returns a new instance of Solvant.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
Instance Attribute Details
#chunks ⇒ Object (readonly)
Returns the value of attribute chunks.
8 9 10 |
# File 'lib/bitbroker/solvant.rb', line 8 def chunks @chunks end |
Class Method Details
.load_binary(dirpath, binary) ⇒ Object
48 49 50 51 52 53 |
# 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 |
Instance Method Details
#remove ⇒ Object
This defines operations to manipulate actual Flie object on FileSystem
32 33 34 |
# File 'lib/bitbroker/solvant.rb', line 32 def remove File.unlink(@f_path) end |
#upload(broker) ⇒ Object
36 37 38 39 40 |
# 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) ⇒ Object
42 43 44 45 46 |
# 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 |