Module: CassandraModel::CompositeRecordStatic

Extended by:
Forwardable
Defined in:
lib/cassandra_model/composite_record_static.rb

Constant Summary collapse

PK_MUTEX =
Mutex.new
CK_MUTEX =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#clustering_columnsObject



14
15
16
# File 'lib/cassandra_model/composite_record_static.rb', line 14

def clustering_columns
  table_data.composite_clustering_columns ||= internal_clustering_columns.map { |column| trimmed_column(column, /^ck_/, composite_ck_map) || column }
end

#columnsObject



26
27
28
# File 'lib/cassandra_model/composite_record_static.rb', line 26

def columns
  table_data.composite_columns ||= composite_columns.each { |column| define_attribute(column) }
end

#composite_ck_mapObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cassandra_model/composite_record_static.rb', line 42

def composite_ck_map
  unless table_data.composite_ck_map
    CK_MUTEX.synchronize do
      return table_data.composite_ck_map if table_data.composite_ck_map

      table_data.composite_ck_map = {}
      columns
    end
  end
  table_data.composite_ck_map
end

#composite_defaultsObject



54
55
56
# File 'lib/cassandra_model/composite_record_static.rb', line 54

def composite_defaults
  table_data.internal_defaults ||= build_composite_map
end

#composite_pk_mapObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cassandra_model/composite_record_static.rb', line 30

def composite_pk_map
  unless table_data.composite_pk_map
    PK_MUTEX.synchronize do
      return table_data.composite_pk_map if table_data.composite_pk_map

      table_data.composite_pk_map = {}
      columns
    end
  end
  table_data.composite_pk_map
end

#generate_composite_defaults(column_defaults, truth_table) ⇒ Object



58
59
60
# File 'lib/cassandra_model/composite_record_static.rb', line 58

def generate_composite_defaults(column_defaults, truth_table)
  table_config.composite_defaults = truth_table.map { |row| column_defaults.except(*row) }
end

#generate_composite_defaults_from_inquirer(inquirer) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/cassandra_model/composite_record_static.rb', line 62

def generate_composite_defaults_from_inquirer(inquirer)
  table_config.composite_defaults = inquirer.composite_rows.map do |row|
    row.inject({}) do |memo, column|
      memo.merge!(column => inquirer.column_defaults[column])
    end
  end
end

#normalized_attributes(attributes) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/cassandra_model/composite_record_static.rb', line 99

def normalized_attributes(attributes)
  attributes = super(attributes)

  attributes.inject({}) do |memo, (column, value)|
    memo.merge!(normalized_column(column) => value)
  end
end

#normalized_column(column) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/cassandra_model/composite_record_static.rb', line 89

def normalized_column(column)
  column = super(column)

  if column =~ /^rk_/ || column =~ /^ck_/
    mapped_column(column)
  else
    column
  end
end

#partition_keyObject



10
11
12
# File 'lib/cassandra_model/composite_record_static.rb', line 10

def partition_key
  table_data.composite_partition_key ||= internal_partition_key.map { |column| trimmed_column(column, /^rk_/, composite_pk_map) || column }
end

#primary_keyObject



18
19
20
21
22
23
24
# File 'lib/cassandra_model/composite_record_static.rb', line 18

def primary_key
  table_data.composite_primary_key ||= (internal_partition_key + internal_clustering_columns).map do |column|
    trimmed_column(column, /^rk_/, composite_pk_map) ||
        trimmed_column(column, /^ck_/, composite_ck_map) ||
        column
  end.uniq
end

#restriction_attributes(restriction) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cassandra_model/composite_record_static.rb', line 77

def restriction_attributes(restriction)
  updated_restriction = restriction.inject({}) do |memo, (key, value)|
    updated_key = key_for_where_params(key)
    memo.merge!(updated_key => value)
  end

  missing_keys = Set.new(internal_partition_key - updated_restriction.keys)
  default_clause = composite_defaults.find { |row| (missing_keys ^ row.keys).empty? }
  updated_restriction.merge!(default_clause) if default_clause
  updated_restriction
end

#select_column(column) ⇒ Object



111
112
113
# File 'lib/cassandra_model/composite_record_static.rb', line 111

def select_column(column)
  has_field?(column) ? column : mapped_column(column)
end

#select_columns(columns) ⇒ Object



107
108
109
# File 'lib/cassandra_model/composite_record_static.rb', line 107

def select_columns(columns)
  columns.map { |column| select_column(column) }
end

#shard_keyObject



70
71
72
73
74
75
# File 'lib/cassandra_model/composite_record_static.rb', line 70

def shard_key
  table_data.composite_shard_key ||= begin
    column = super
    column =~ /^rk_/ ? composite_pk_map[column] : column
  end
end