class GreyscaleRecord::DataStore::Table

Attributes

name[RW]

Public Class Methods

new(name, store) click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 6
def initialize(name, store)
  @name = name
  @store = store

  # initialize the index array for later use
  @indices = {}

  # generate IDs for the records based on YAML keys
  generate_ids!

  # preemptively index the IDs
  add_index :id
end

Public Instance Methods

add_index( column ) click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 24
def add_index( column )
  return if @store.patched?
  @indices = @indices.merge( { column => Index.new(column, rows) } )
end
all() click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 20
def all
  rows.values
end
find( params = {} ) click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 29
def find( params = {} )
  return all if params.empty?
  sets = params.map do | column, values |
    if !patched? && indexed?( column )
      find_in_index column, values
    else
      GreyscaleRecord.logger.warn "You are running a query on #{@name}.#{column} which is not indexed. This will perform a table scan."
      find_in_column column, values
    end
  end

  sets.inject( sets.first ) do |result, subset|
    result & subset
  end
end

Private Instance Methods

find_in_column( column, values ) click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 59
def find_in_column( column, values )
  rows.values.select do |datum|
    Array( values ).include? datum[ column ]
  end
end
find_in_index( column, values ) click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 65
def find_in_index( column, values )
  keys = @indices[column].find( Array( values ) )
  
  keys.map do |id|
    rows[id]
  end
end
generate_ids!() click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 73
def generate_ids!
  # init IDs
  rows.each do |k, v|
    v[:id] = k
  end
end
indexed?(column) click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 55
def indexed?(column)
  @indices[column].present?
end
patched?() click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 51
def patched?
  @store.patched?
end
rows() click to toggle source
# File lib/greyscale_record/data_store/table.rb, line 47
def rows
  @store[@name]
end