Class: ActiveRecord::Turntable::Mixer
- Inherits:
-
Object
- Object
- ActiveRecord::Turntable::Mixer
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/active_record/turntable/mixer/fader.rb,
lib/active_record/turntable/mixer/fader/specified_shard.rb,
lib/active_record/turntable/mixer/fader/insert_shards_merge_result.rb,
lib/active_record/turntable/mixer/fader/select_shards_merge_result.rb,
lib/active_record/turntable/mixer/fader/update_shards_merge_result.rb,
lib/active_record/turntable/mixer/fader/calculate_shards_sum_result.rb,
lib/active_record/turntable/mixer.rb
Defined Under Namespace
Classes: Fader
Constant Summary collapse
- NOT_USED_FOR_SHARDING_OPERATORS_REGEXP =
/\A(NOT IN|IS|IS NOT|BETWEEN|LIKE|!=|<<|>>|<>|>=|<=|[*+%|&><-])\z/
Instance Method Summary collapse
- #build_fader(method_name, query, *args, &block) ⇒ Object
- #find_shard_keys(tree, table_name, shard_key) ⇒ Object
-
#initialize(proxy) ⇒ Mixer
constructor
A new instance of Mixer.
Constructor Details
#initialize(proxy) ⇒ Mixer
Returns a new instance of Mixer.
17 18 19 |
# File 'lib/active_record/turntable/mixer.rb', line 17 def initialize(proxy) @proxy = proxy end |
Instance Method Details
#build_fader(method_name, query, *args, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/active_record/turntable/mixer.rb', line 21 def build_fader(method_name, query, *args, &block) method = method_name.to_s if @proxy.shard_fixed? return SpecifiedShard.new(@proxy, { @proxy.fixed_shard => query }, method, query, *args, &block) end binds = (method == "insert") ? args[4] : args[1] binded_query = bind_sql(query, binds) begin tree = SQLTree[binded_query] rescue Exception => err logger.warn { "[ActiveRecord::Turntable] Error on Parsing SQL: #{binded_query}, on_method: #{method_name}" } raise err end case tree when SQLTree::Node::SelectQuery build_select_fader(tree, method, query, *args, &block) when SQLTree::Node::UpdateQuery, SQLTree::Node::DeleteQuery build_update_fader(tree, method, query, *args, &block) when SQLTree::Node::InsertQuery build_insert_fader(tree, method, query, *args, &block) else # send to default shard Fader::SpecifiedShard.new(@proxy, { @proxy.default_shard => query }, method, query, *args, &block) end rescue Exception => err logger.warn { "[ActiveRecord::Turntable] Error on Building Fader: #{binded_query}, on_method: #{method_name}, err: #{err}" } raise err end |
#find_shard_keys(tree, table_name, shard_key) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_record/turntable/mixer.rb', line 56 def find_shard_keys(tree, table_name, shard_key) return [] unless tree.respond_to?(:operator) case tree.operator when "OR" lkeys = find_shard_keys(tree.lhs, table_name, shard_key) rkeys = find_shard_keys(tree.rhs, table_name, shard_key) if lkeys.present? && rkeys.present? (lkeys + rkeys).uniq else [] end when "AND" lkeys = find_shard_keys(tree.lhs, table_name, shard_key) rkeys = find_shard_keys(tree.rhs, table_name, shard_key) if lkeys.present? || rkeys.present? (lkeys + rkeys).uniq else [] end when "IN", "=", "==" field = tree.lhs.respond_to?(:table) ? tree.lhs : nil if tree.rhs.is_a?(SQLTree::Node::SubQuery) if (field.try(:table) == table_name) && (field.name == shard_key) find_shard_keys(tree.rhs.where, table_name, shard_key) else [] end else values = Array(tree.rhs) if (field.try(:table) == table_name) && (field.name == shard_key) && !tree.rhs.is_a?(SQLTree::Node::SubQuery) values.map(&:value).compact else [] end end when NOT_USED_FOR_SHARDING_OPERATORS_REGEXP [] else raise ActiveRecord::Turntable::UnknownOperatorError, "[ActiveRecord::Turntable] Found Unknown SQL Operator:'#{tree.operator if tree.respond_to?(:operaor)}', Please report this bug." end end |