Class: DbCharmer::Sharding::Method::DbBlockGroupMap

Inherits:
Object
  • Object
show all
Includes:
DbBlockGroupMapBase
Defined in:
lib/db_charmer/sharding/method/db_block_group_map.rb

Defined Under Namespace

Classes: Group, Shard

Instance Attribute Summary

Attributes included from DbBlockGroupMapBase

#block_size, #connection, #connection_name, #groups_table, #map_table, #name, #shards_table

Instance Method Summary collapse

Methods included from DbBlockGroupMapBase

#allocate_new_block_for_key, #block_end_for_key, #block_for_key, #block_start_for_key, #clear_group_info_cache, #clear_shard_info_cache, #create_shard_database, #drop_all_shard_databases, #drop_shard_database, #get_cached_block, #group_class, #group_info_by_id, #initialize, #least_loaded_group, #prepare_shard_models, #set_cached_block, #shard_class, #shard_connection_config_no_dbname, #shard_connections, #shard_for_key, #shard_info_by_group_id, #shard_info_by_id

Instance Method Details

#create_group(shard_id, open, enabled) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/db_charmer/sharding/method/db_block_group_map.rb', line 79

def create_group(shard_id, open, enabled)
  # Prepare model
  prepare_shard_models

  # Create the record
  Group.create! do |group|
    group.shard_id = shard_id
    group.open     = open
    group.enabled  = enabled
  end
end

#create_shard(params) ⇒ Object




60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/db_charmer/sharding/method/db_block_group_map.rb', line 60

def create_shard(params)
  params = params.symbolize_keys
  [ :db_host, :db_port, :db_user, :db_pass, :db_name_prefix ].each do |arg|
    raise ArgumentError, "Missing required parameter: #{arg}" unless params[arg]
  end

  # Prepare model
  prepare_shard_models

  # Create the record
  Shard.create! do |shard|
    shard.db_host = params[:db_host]
    shard.db_port = params[:db_port]
    shard.db_user = params[:db_user]
    shard.db_pass = params[:db_pass]
    shard.db_name_prefix = params[:db_name_prefix]
  end
end

#group_database_name(shard, group_id) ⇒ Object



55
56
57
# File 'lib/db_charmer/sharding/method/db_block_group_map.rb', line 55

def group_database_name(shard, group_id)
  "%s_%05d" % [ shard.db_name_prefix, group_id ]
end

#shard_connection_config(shard, group_id) ⇒ Object


Create configuration (use mapping connection as a template)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/db_charmer/sharding/method/db_block_group_map.rb', line 36

def shard_connection_config(shard, group_id)
  # Format connection name
  shard_name = "db_charmer_db_block_group_map_#{name}_s%d_g%d" % [ shard.id, group_id]

  # Here we get the mapping connection's configuration
  # They do not expose configs so we hack in and get the instance var
  # FIXME: Find a better way, maybe move config method to our ar extenstions
  connection.instance_variable_get(:@config).clone.merge(
    # Name for the connection factory
    :connection_name => shard_name,
    # Connection params
    :host => shard.db_host,
    :port => shard.db_port,
    :username => shard.db_user,
    :password => shard.db_pass,
    :database => group_database_name(shard, group_id)
  )
end