Class: StructuredDataToSql::Json::CountingIO

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_data_to_sql/json/record_streamer.rb

Overview

IO wrapper reporting consumed bytes; Oj.sc_parse drives custom IO-like objects through readpartial.

Instance Method Summary collapse

Constructor Details

#initialize(io, &on_bytes) ⇒ CountingIO

Returns a new instance of CountingIO.



435
436
437
438
# File 'lib/structured_data_to_sql/json/record_streamer.rb', line 435

def initialize(io, &on_bytes)
  @io = io
  @on_bytes = on_bytes
end

Instance Method Details

#__raw_io__Object

Exposed so RecordStreamer can reach the underlying IO for seek-based NDJSON probe/rewind without going through the counting wrapper.



442
443
444
# File 'lib/structured_data_to_sql/json/record_streamer.rb', line 442

def __raw_io__
  @io
end

#each_line(&block) ⇒ Object

Delegates each_line to the underlying IO, reporting bytes as each line is yielded so progress tracking still works during NDJSON iteration.



467
468
469
470
471
472
473
474
# File 'lib/structured_data_to_sql/json/record_streamer.rb', line 467

def each_line(&block)
  return enum_for(:each_line) unless block

  @io.each_line do |line|
    @on_bytes.call(line.bytesize)
    block.call(line)
  end
end

#read(length = nil, out_buffer = nil) ⇒ Object



459
460
461
462
463
# File 'lib/structured_data_to_sql/json/record_streamer.rb', line 459

def read(length = nil, out_buffer = nil)
  chunk = out_buffer ? @io.read(length, out_buffer) : @io.read(length)
  @on_bytes.call(chunk.bytesize) if chunk
  chunk
end

#readpartial(max_length, out_buffer = nil) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/structured_data_to_sql/json/record_streamer.rb', line 446

def readpartial(max_length, out_buffer = nil)
  chunk =
    (
      if out_buffer
        @io.readpartial(max_length, out_buffer)
      else
        @io.readpartial(max_length)
      end
    )
  @on_bytes.call(chunk.bytesize) if chunk
  chunk
end