Class: ExternalSortModule::ChunkWriter

Inherits:
Chunk
  • Object
show all
Defined in:
lib/geotree/externalsort.rb

Overview

Chunk subclass that performs streaming writing to target with sliding window

Instance Method Summary collapse

Methods inherited from Chunk

#done, #set_chunk_size

Constructor Details

#initialize(target_file, target_offset, target_length, element_size, chunk_size = MAX_CHUNK_SIZE_) ⇒ ChunkWriter

Returns a new instance of ChunkWriter.



146
147
148
# File 'lib/geotree/externalsort.rb', line 146

def initialize(target_file, target_offset, target_length, element_size, chunk_size = MAX_CHUNK_SIZE_)
  super(target_file,target_offset,target_length,element_size, chunk_size)
end

Instance Method Details

#write(src_buffer, src_offset = 0) ⇒ Object

Write an element to the target

Parameters:

  • src_buffer

    source of element

  • src_offset (defaults to: 0)

    offset into source

Raises:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/geotree/externalsort.rb', line 154

def write(src_buffer, src_offset = 0)
  raise IllegalStateException if done
  raise ArgumentError if (src_buffer.size - src_offset < @element_size)

  if @buffer_offset == @buffer.length
    max_size = @max_chunk_size
    chunk_size = [@target_end_offset - @target_offset, max_size].min
    @buffer = zero_bytes(chunk_size)
    @buffer_offset = 0
  end

  @buffer[@buffer_offset,@element_size] = src_buffer[src_offset,@element_size]
  @buffer_offset += @element_size

  # If buffer is now full, flush to target
  if @buffer_offset == @buffer.size
    f = @target_file
    f.pos = @target_offset
    bytes_written = f.write(@buffer)
    raise IOError if @buffer.size != bytes_written
    @target_offset += bytes_written
  end
end