class Cequel::Schema::TableReader

A TableReader interprets table data from the cassandra driver into a table descriptor (read: `Table`).

Constants

COLLECTION_TYPE_PATTERN
COMPOSITE_TYPE_PATTERN
REVERSED_TYPE_PATTERN

Attributes

indexes[R]
table[R]

@return [Table] object representation of the table defined in the

database
table_data[R]

Public Class Methods

new(table_data) click to toggle source

@param keyspace [Metal::Keyspace] keyspace to read the table from @param table_name [Symbol] name of the table to read @private

# File lib/cequel/schema/table_reader.rb, line 52
def initialize(table_data)
  @table_data = table_data
  @table = Table.new(table_data.name,
                     Cassandra::MaterializedView === table_data)
end
read(keyspace, table_name) click to toggle source

Read the schema defined in the database for a given table and return a {Table} instance

@param (see initialize) @return (see read)

# File lib/cequel/schema/table_reader.rb, line 27
def read(keyspace, table_name)
  table_data = fetch_raw_keyspace(keyspace).table(table_name.to_s)
  (fail NoSuchTableError) if table_data.blank?

  new(table_data).call
end

Protected Class Methods

def_property(name, option_method = name, coercion = ->(val, _table_data){ val } click to toggle source
# File lib/cequel/schema/table_reader.rb, line 131
def self.def_property(name,
                      option_method = name,
                      coercion = ->(val, _table_data){ val })

  @@prop_extractors << ->(table, table_data) {
    raw_prop_val = table_data.options.public_send(option_method)
    prop_val = coercion.call(raw_prop_val,table_data)

    table.add_property TableProperty.build(name, prop_val)
  }
end
fetch_raw_keyspace(keyspace) click to toggle source
# File lib/cequel/schema/table_reader.rb, line 36
def fetch_raw_keyspace(keyspace)
  cluster = keyspace.cluster
  cluster.refresh_schema

  (fail NoSuchKeyspaceError, "No such keyspace #{keyspace.name}") unless
    cluster.has_keyspace?(keyspace.name)

  cluster.keyspace(keyspace.name)
end

Public Instance Methods

call() click to toggle source

Read table schema from the database

@return [Table] object representation of table in the database, or

`nil` if no table by given name exists

@api private

# File lib/cequel/schema/table_reader.rb, line 66
def call
  return nil if table_data.blank?

  read_partition_keys
  read_clustering_columns
  read_indexes
  read_data_columns
  read_properties
  read_table_settings

  table
end

Protected Instance Methods

index_name(column_info) click to toggle source
# File lib/cequel/schema/table_reader.rb, line 178
def index_name(column_info)
  if idx_name = indexes[column_info.name]
    idx_name.to_sym
  else
    nil
  end
end
interpret_column(c) click to toggle source
# File lib/cequel/schema/table_reader.rb, line 115
def interpret_column(c)
  case c.type
  when Cassandra::Types::Simple
    DataColumn.new(c.name.to_sym, type(c.type), index_name(c))
  when Cassandra::Types::List
    List.new(c.name.to_sym, type(c.type.value_type))
  when Cassandra::Types::Set
    Set.new(c.name.to_sym, type(c.type.value_type))
  when Cassandra::Types::Map
    Map.new(c.name.to_sym, type(c.type.key_type), type(c.type.value_type))
  else
    fail "Unsupported type #{c.type.inspect}"
  end
end
read_clustering_columns() click to toggle source
# File lib/cequel/schema/table_reader.rb, line 90
def read_clustering_columns
  table_data.clustering_columns
    .each do |c|
      table.add_column ClusteringColumn.new(c.name.to_sym, type(c.type), c.order)
    end
end
read_data_columns() click to toggle source
# File lib/cequel/schema/table_reader.rb, line 106
def read_data_columns
  ((table_data.each_column - table_data.partition_key) - table_data.clustering_columns)
    .each do |c|
      next if table.has_column?(c.name.to_sym)

      table.add_column interpret_column(c)
    end
end
read_indexes() click to toggle source
# File lib/cequel/schema/table_reader.rb, line 97
def read_indexes
  @indexes = if table_data.respond_to?(:each_index)
               Hash[table_data.each_index.map{|i| [i.target, i.name]}]
             else
               # materialized view
               {}
             end
end
read_partition_keys() click to toggle source
# File lib/cequel/schema/table_reader.rb, line 83
def read_partition_keys
  table_data.partition_key.each do |k|
    table.add_column PartitionKey.new(k.name.to_sym, type(k.type))
  end

end
read_properties() click to toggle source
# File lib/cequel/schema/table_reader.rb, line 164
def read_properties
  @@prop_extractors.each do |extractor|
    extractor.call(table, table_data)
  end
end
read_table_settings() click to toggle source
# File lib/cequel/schema/table_reader.rb, line 170
def read_table_settings
  table.compact_storage = table_data.options.compact_storage?
end
type(type_info) click to toggle source
# File lib/cequel/schema/table_reader.rb, line 174
def type(type_info)
  ::Cequel::Type[type_info.kind]
end