Class: ExternalSortModule::ChunkRandomAccess

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

Overview

A subclass of Chunk that does not use a sliding window, and instead can contain the entire target length; includes methods for accessing target elements in arbitrary (non-streaming) order

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Chunk

#done, #set_chunk_size

Constructor Details

#initialize(target_file, target_offset, target_length, element_size) ⇒ ChunkRandomAccess

Construct chunk, and read the complete targeted bytes to the buffer

Raises:

  • (IOError)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/geotree/externalsort.rb', line 58

def initialize(target_file, target_offset, target_length, element_size)
  super(target_file,target_offset,target_length,element_size,target_length)

  @num_elements = target_length / element_size

  chunk_size = target_length

  f = @target_file
  f.pos = @target_offset
  @buffer = f.read(chunk_size)
  raise IOError if !@buffer || @buffer.size != chunk_size
end

Instance Attribute Details

#num_elementsObject (readonly)

Returns the value of attribute num_elements.



55
56
57
# File 'lib/geotree/externalsort.rb', line 55

def num_elements
  @num_elements
end

Instance Method Details

#element(index) ⇒ Object

Get element from chunk

Parameters:

  • index

    of element,

Raises:

  • (ArgumentError)


73
74
75
76
77
# File 'lib/geotree/externalsort.rb', line 73

def element(index)
  raise ArgumentError if index < 0 || index >= num_elements
  off = index * @element_size
  [@buffer,off]
end

#replace_buffer_with(b) ⇒ Object

Replace existing buffer

Raises:

  • (IllegalArgumentException)


80
81
82
83
# File 'lib/geotree/externalsort.rb', line 80

def replace_buffer_with(b)
  raise IllegalArgumentException if b.size != @buffer.size
  @buffer = b
end

#writeObject

Write buffer to target

Raises:

  • (IOError)


86
87
88
89
90
91
# File 'lib/geotree/externalsort.rb', line 86

def write
  f = @target_file
  f.pos = @target_end_offset - @target_length
  bytes_written = f.write(@buffer)
  raise IOError if @buffer.size != bytes_written
end