Class: OMF::OML::OmlIndexedTable

Inherits:
OmlTable
  • Object
show all
Defined in:
lib/omf_oml/indexed_table.rb

Overview

This table maintains the most recently added row with a unique entry in the index column.

Instance Attribute Summary collapse

Attributes inherited from OmlTable

#max_size, #name, #offset, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OmlTable

#<<, #add_row, #add_rows, create, #create_sliced_table, #data_sources, #describe, #indexed_by, #on_before_row_added, #on_content_changed, #on_row_added, #rows, #to_a

Constructor Details

#initialize(name, index_col, schema, opts = {}, &on_before_row_added) ⇒ OmlIndexedTable

index_col - Name of column to index schema - Table schema



40
41
42
43
44
45
46
47
48
# File 'lib/omf_oml/indexed_table.rb', line 40

def initialize(name, index_col, schema, opts = {}, &on_before_row_added)
  # if opts[:supress_index].nil?
    # opts[:supress_index] = true
  # end
  super name, schema, opts, &on_before_row_added
  @index_col = index_col
  @index2row = {} # each row is associated with an instance of the index
  @index = @schema.index_for_col(index_col)
end

Instance Attribute Details

#index_colObject (readonly)

Returns the value of attribute index_col.



34
35
36
# File 'lib/omf_oml/indexed_table.rb', line 34

def index_col
  @index_col
end

Class Method Details

.shadow(source_table, index_col, &on_before_row_added) ⇒ Object

Shadow an existing table and maintain an index on ‘index_col’.

source_table - Table to shadow index_col - Name of column to index on



25
26
27
28
29
30
31
32
# File 'lib/omf_oml/indexed_table.rb', line 25

def self.shadow(source_table, index_col, &on_before_row_added)
  name = "#{source_table.name}+#{index_col}"
  ix_table = self.new(name, index_col, source_table.schema, &on_before_row_added)
  source_table.on_row_added(self) do |r|
    ix_table.add_row(r)
  end
  ix_table
end

Instance Method Details

#_add_row_finally(row) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/omf_oml/indexed_table.rb', line 50

def _add_row_finally(row)
  key = row[@index]
  row_id = @index2row[key]
  unless row_id
    row_id = @rows.length
    @index2row[key] = row_id
  end
  current_row = @rows[row_id]
  return nil if current_row == row

  if current_row
    _notify_content_changed(:removed, [current_row])
  end
  @rows[row_id] = row
  return row
end