Module: NoBrainer::Document::TableConfig::ClassMethods

Defined in:
lib/no_brainer/document/table_config.rb

Instance Method Summary collapse

Instance Method Details

#_set_table_config(options) ⇒ Object



30
31
32
33
34
35
# File 'lib/no_brainer/document/table_config.rb', line 30

def _set_table_config(options)
  raise "table_config() must be used at the parent class, not a subclass" unless is_root_class?

  options.assert_valid_keys(*VALID_TABLE_CONFIG_OPTIONS)
  self.table_config_options.merge!(options)
end

#rebalanceObject



60
61
62
63
# File 'lib/no_brainer/document/table_config.rb', line 60

def rebalance
  NoBrainer.run { rql_table.rebalance }
  true
end

#rql_tableObject



43
44
45
# File 'lib/no_brainer/document/table_config.rb', line 43

def rql_table
  RethinkDB::RQL.new.table(table_name)
end

#store_in(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/no_brainer/document/table_config.rb', line 17

def store_in(options)
  if options[:table]
    STDERR.puts "[NoBrainer] `store_in(table: ...)' has been removed. Use `table_config(name: ...)' instead."
    options[:name] = options.delete(:table)
  end

  if options[:database] || options[:db]
    raise "`store_in(db: ...)' has been removed. Use `run_with(db: ...)' instead."
  end

  table_config(options)
end

#sync_indexes(options = {}) ⇒ Object



85
86
87
# File 'lib/no_brainer/document/table_config.rb', line 85

def sync_indexes(options={})
  NoBrainer::Document::Index::Synchronizer.new(self).sync_indexes(options)
end

#sync_schema(options = {}) ⇒ Object



89
90
91
92
# File 'lib/no_brainer/document/table_config.rb', line 89

def sync_schema(options={})
  sync_table_config(options)
  sync_indexes(options)
end

#sync_table_config(options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/no_brainer/document/table_config.rb', line 78

def sync_table_config(options={})
  c = table_create_options
  table_config.update!(c.slice(:durability, :primary_key, :write_acks))
  NoBrainer.run { rql_table.reconfigure(c.slice(:shards, :replicas, :primary_replica_tag, :nonvoting_replica_tags)) }
  true
end

#table_config(options = {}) ⇒ Object



47
48
49
50
# File 'lib/no_brainer/document/table_config.rb', line 47

def table_config(options={})
  return _set_table_config(options) unless options.empty?
  NoBrainer::System::TableConfig.new_from_db(NoBrainer.run { rql_table.config })
end

#table_create_optionsObject



69
70
71
72
73
74
75
76
# File 'lib/no_brainer/document/table_config.rb', line 69

def table_create_options
  NoBrainer::Config.table_options
    .merge(table_config_options)
    .merge(:name => table_name)
    .merge(:primary_key => lookup_field_alias(pk_name))
    .reverse_merge(:durability => 'hard')
    .reduce({}) { |h,(k,v)| h[k] = v.is_a?(Symbol) ? v.to_s : v; h } # symbols -> strings
end

#table_nameObject



37
38
39
40
41
# File 'lib/no_brainer/document/table_config.rb', line 37

def table_name
  name = table_config_options[:name]
  name = name.call if name.is_a?(Proc)
  (name || root_class.name.tableize.gsub('/', '__')).to_s
end

#table_statsObject



56
57
58
# File 'lib/no_brainer/document/table_config.rb', line 56

def table_stats
  NoBrainer::System::Stats.where(:db => NoBrainer.current_db, :table => table_name).to_a
end

#table_statusObject



52
53
54
# File 'lib/no_brainer/document/table_config.rb', line 52

def table_status
  NoBrainer::System::TableConfig.new_from_db(NoBrainer.run { rql_table.status })
end

#table_waitObject



65
66
67
# File 'lib/no_brainer/document/table_config.rb', line 65

def table_wait
  NoBrainer.run { rql_table.wait }
end