Class: Spark::Serializer::AutoBatched
- Defined in:
- lib/spark/serializer/auto_batched.rb
Overview
AutoBatched serializator
Batch size is computed automatically. Simillar to Python’s AutoBatchedSerializer.
Constant Summary collapse
- MAX_RATIO =
10
Instance Attribute Summary
Attributes inherited from Batched
Instance Method Summary collapse
- #batched? ⇒ Boolean
- #dump_to_io(data, io) ⇒ Object
-
#initialize(serializer, best_size = 65536) ⇒ AutoBatched
constructor
A new instance of AutoBatched.
- #name ⇒ Object
- #unbatch! ⇒ Object
Methods inherited from Batched
#dump, #load, #load_from_io, #to_s
Methods inherited from Base
#==, #check_each, #error, #inspect, #load_from_file, #load_from_io, #to_s
Constructor Details
#initialize(serializer, best_size = 65536) ⇒ AutoBatched
Returns a new instance of AutoBatched.
12 13 14 15 16 17 |
# File 'lib/spark/serializer/auto_batched.rb', line 12 def initialize(serializer, best_size=65536) @serializer = serializer @best_size = best_size.to_i error('Batch size must be greater than 1') if @best_size < 2 end |
Instance Method Details
#batched? ⇒ Boolean
19 20 21 |
# File 'lib/spark/serializer/auto_batched.rb', line 19 def batched? true end |
#dump_to_io(data, io) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/spark/serializer/auto_batched.rb', line 30 def dump_to_io(data, io) check_each(data) # Only Array have .slice data = data.to_a index = 0 batch = 2 max = @best_size * MAX_RATIO loop do chunk = data.slice(index, batch) if chunk.nil? || chunk.empty? break end serialized = @serializer.dump(chunk) io.write_string(serialized) index += batch size = serialized.bytesize if size < @best_size batch *= 2 elsif size > max && batch > 1 batch /= 2 end end io.flush end |
#name ⇒ Object
26 27 28 |
# File 'lib/spark/serializer/auto_batched.rb', line 26 def name "AutoBatched(#{@best_size})" end |
#unbatch! ⇒ Object
23 24 |
# File 'lib/spark/serializer/auto_batched.rb', line 23 def unbatch! end |