class GeoTreeModule::PtBuffer

Support for buffering new points to a file, then shuffling the points before adding them to (one or more) geotrees.

Constants

PT_SHUFFLER_

Public Class Methods

new(tree) click to toggle source

Construct an inactive buffer @param tree tree to receive the points; calls its add_buffered_point() method

when the buffer is being closed
# File lib/geotree/ptbuffer.rb, line 52
def initialize(tree)
  @tree = tree
  @buffering = false
  @buff_file = nil
  @buffered_count = 0
end
pt_hash_code(b) click to toggle source
# File lib/geotree/ptbuffer.rb, line 34
def self.pt_hash_code(b)
  buff,off = b
  c = buff[off,DATAPOINT_BYTES]
  Zlib::crc32(c)
end

Public Instance Methods

active() click to toggle source

Return true if buffer is active

# File lib/geotree/ptbuffer.rb, line 60
def active
  @buffering
end
active=(val) click to toggle source

Change buffer’s active state

# File lib/geotree/ptbuffer.rb, line 65
def active=(val)
  db = false

  if @buffering != val

    @buffering = val

    # If we were buffering the points, then close the file,
    # shuffle the points, and send them to the receiving tree(s).
    if @buff_file
      @buff_file.close

      # Sort the buffered points into a random order
      so = Sorter.new(@buff_file.path,DATAPOINT_BYTES, PT_SHUFFLER_)
      so.sort

      !db || pr(" opening chunk reader for #@buffered_count points\n")

      @buff_file.open
      @buff_file.binmode

      r = ChunkReader.new(@buff_file, 0, DATAPOINT_BYTES * @buffered_count, DATAPOINT_BYTES)
      while !r.done
        by,off = r.peek
        dp = GeoTree.read_data_point_from(by,off / INT_BYTES)
        r.read
        !db||  pr("adding data point: #{dp}\n")
        @tree.add_buffered_point(dp)
      end
      @buff_file.close
    end
    @buff_file = nil
    @buffered_count = 0
  end

end
add(data_point) click to toggle source

Add point to buffer

# File lib/geotree/ptbuffer.rb, line 103
def add(data_point)
  if !active
    @tree.add_buffered_point(data_point)
  else
    if @buffered_count == 0
      @buff_file = Tempfile.new('_geotree_')
      @buff_file.binmode
    end

    by = zero_bytes(DATAPOINT_BYTES)
    GeoTree.write_data_point(data_point, by, 0)
    nw = @buff_file.write(by)
    raise IOError if nw != by.size
    @buffered_count += 1
  end
end