Class: Gizzard::ShardTemplate
- Inherits:
-
Object
- Object
- Gizzard::ShardTemplate
- Includes:
- Comparable
- Defined in:
- lib/gizzard/shard_template.rb
Constant Summary collapse
- ABSTRACT_HOST =
"localhost"
- DEFAULT_WEIGHT =
1
Instance Attribute Summary collapse
-
#dest_type ⇒ Object
readonly
Returns the value of attribute dest_type.
-
#source_type ⇒ Object
readonly
Returns the value of attribute source_type.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Similarity/Equality.
- #children ⇒ Object
- #concrete? ⇒ Boolean
- #concrete_descendants ⇒ Object
- #contains_shard_type?(other) ⇒ Boolean
- #copy_sources ⇒ Object
- #descendants ⇒ Object (also: #flatten)
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #host ⇒ Object
- #identifier ⇒ Object
-
#initialize(type, host, weight, source_type, dest_type, children) ⇒ ShardTemplate
constructor
A new instance of ShardTemplate.
- #inspect ⇒ Object (also: #to_s)
- #link_eql?(other) ⇒ Boolean
- #replicating? ⇒ Boolean
- #shard_eql?(other) ⇒ Boolean
- #shard_tag ⇒ Object
- #shared_host?(other) ⇒ Boolean
- #table_name_suffix ⇒ Object
- #to_complex_config ⇒ Object
- #to_config ⇒ Object
- #to_config_struct ⇒ Object
- #to_shard(table_prefix, translations = {}) ⇒ Object
-
#to_shard_id(table_prefix, translations = {}) ⇒ Object
Concretization.
- #to_shard_info(table_prefix, translations = {}) ⇒ Object
- #to_simple_config(tag = "") ⇒ Object
- #valid_copy_source? ⇒ Boolean
Constructor Details
#initialize(type, host, weight, source_type, dest_type, children) ⇒ ShardTemplate
Returns a new instance of ShardTemplate.
12 13 14 15 |
# File 'lib/gizzard/shard_template.rb', line 12 def initialize(type, host, weight, source_type, dest_type, children) @type, @host, @weight, @source_type, @dest_type, @children = type, host, weight, source_type || '', dest_type || '', children end |
Instance Attribute Details
#dest_type ⇒ Object (readonly)
Returns the value of attribute dest_type.
10 11 12 |
# File 'lib/gizzard/shard_template.rb', line 10 def dest_type @dest_type end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type.
10 11 12 |
# File 'lib/gizzard/shard_template.rb', line 10 def source_type @source_type end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/gizzard/shard_template.rb', line 10 def type @type end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
10 11 12 |
# File 'lib/gizzard/shard_template.rb', line 10 def weight @weight end |
Class Method Details
.concrete?(type) ⇒ Boolean
17 18 19 |
# File 'lib/gizzard/shard_template.rb', line 17 def self.concrete?(type) !Shard::VIRTUAL_SHARD_TYPES.include? type.split('.').last end |
.configure(options) ⇒ Object
212 213 214 215 |
# File 'lib/gizzard/shard_template.rb', line 212 def configure() @@options ||= {} @@options.merge!() end |
.options ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/gizzard/shard_template.rb', line 217 def @@options ||= {} @@options[:replicating] ||= "ReplicatingShard" #@@options[:source_type] ||= "BIGINT UNSIGNED" #@@options[:dest_type] ||= "BIGINT UNSIGNED" @@options end |
.parse(string) ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/gizzard/shard_template.rb', line 225 def parse(string) if [:simple] parse_simple(string) else parse_complex(string) end end |
Instance Method Details
#<=>(other) ⇒ Object
Similarity/Equality
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gizzard/shard_template.rb', line 99 def <=>(other) raise ArgumentError, "other is not a ShardTemplate" unless other.is_a? ShardTemplate to_a = lambda {|t| [t.host, t.type, t.source_type.to_s, t.dest_type.to_s, t.weight] } if (cmp = to_a.call(self) <=> to_a.call(other)) == 0 children <=> other.children else cmp end end |
#children ⇒ Object
55 56 57 |
# File 'lib/gizzard/shard_template.rb', line 55 def children @children.sort { |a, b| b <=> a } end |
#concrete? ⇒ Boolean
21 22 23 |
# File 'lib/gizzard/shard_template.rb', line 21 def concrete? self.class.concrete? type end |
#concrete_descendants ⇒ Object
65 66 67 |
# File 'lib/gizzard/shard_template.rb', line 65 def concrete_descendants descendants.select {|t| t.concrete? } end |
#contains_shard_type?(other) ⇒ Boolean
142 143 144 |
# File 'lib/gizzard/shard_template.rb', line 142 def contains_shard_type?(other) descendants.map {|d| d.type }.include? other end |
#copy_sources ⇒ Object
69 70 71 72 |
# File 'lib/gizzard/shard_template.rb', line 69 def copy_sources return [] unless self.valid_copy_source? self.concrete? ? [self] : children.inject([]) {|a, c| a.concat c.copy_sources } end |
#descendants ⇒ Object Also known as: flatten
59 60 61 |
# File 'lib/gizzard/shard_template.rb', line 59 def descendants [self].concat children.map {|c| c.descendants }.flatten end |
#eql?(other) ⇒ Boolean
111 112 113 114 |
# File 'lib/gizzard/shard_template.rb', line 111 def eql?(other) return false unless other.is_a? ShardTemplate (self <=> other) == 0 end |
#hash ⇒ Object
146 147 148 |
# File 'lib/gizzard/shard_template.rb', line 146 def hash weight.hash + host.hash + type.hash + children.hash end |
#host ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/gizzard/shard_template.rb', line 45 def host if concrete? @host elsif replicating? ABSTRACT_HOST else children.first.host end end |
#identifier ⇒ Object
33 34 35 |
# File 'lib/gizzard/shard_template.rb', line 33 def identifier concrete? ? "#{type}/#{host}" : type.to_s end |
#inspect ⇒ Object Also known as: to_s
74 75 76 |
# File 'lib/gizzard/shard_template.rb', line 74 def inspect to_config end |
#link_eql?(other) ⇒ Boolean
123 124 125 126 127 128 |
# File 'lib/gizzard/shard_template.rb', line 123 def link_eql?(other) raise ArgumentError, "other is not a ShardTemplate" unless other.is_a? ShardTemplate to_a = lambda {|t| [t.host, t.type, t.source_type.to_s, t.dest_type.to_s, t.weight] } to_a.call(self) == to_a.call(other) end |
#replicating? ⇒ Boolean
25 26 27 |
# File 'lib/gizzard/shard_template.rb', line 25 def replicating? Shard::REPLICATING_SHARD_TYPES.include? type.split('.').last end |
#shard_eql?(other) ⇒ Boolean
116 117 118 119 120 121 |
# File 'lib/gizzard/shard_template.rb', line 116 def shard_eql?(other) raise ArgumentError, "other is not a ShardTemplate" unless other.is_a? ShardTemplate to_a = lambda {|t| [t.host, t.type, t.source_type.to_s, t.dest_type.to_s] } to_a.call(self) == to_a.call(other) end |
#shard_tag ⇒ Object
41 42 43 |
# File 'lib/gizzard/shard_template.rb', line 41 def shard_tag Shard::SHARD_TAGS[type.split('.').last] end |
#shared_host?(other) ⇒ Boolean
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gizzard/shard_template.rb', line 130 def shared_host?(other) raise ArgumentError, "other is not a ShardTemplate" unless other.is_a? ShardTemplate self.concrete_descendants.each do |s| other.concrete_descendants.each do |o| return true if s.shard_eql? o end end false end |
#table_name_suffix ⇒ Object
37 38 39 |
# File 'lib/gizzard/shard_template.rb', line 37 def table_name_suffix Shard::SHARD_SUFFIXES[type.split('.').last] end |
#to_complex_config ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/gizzard/shard_template.rb', line 183 def to_complex_config if children.empty? config_definition else child_defs = children.map {|c| c.to_complex_config } child_defs_s = child_defs.length == 1 ? child_defs.first : "(#{child_defs.join(", ")})" "#{config_definition} -> #{child_defs_s}" end end |
#to_config ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/gizzard/shard_template.rb', line 175 def to_config if ShardTemplate.[:simple] to_simple_config else to_complex_config end end |
#to_config_struct ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/gizzard/shard_template.rb', line 166 def to_config_struct if children.empty? config_definition else child_defs = children.map {|c| c.to_config_struct } { config_definition => (child_defs.length == 1 ? child_defs.first : child_defs) } end end |
#to_shard(table_prefix, translations = {}) ⇒ Object
91 92 93 94 95 |
# File 'lib/gizzard/shard_template.rb', line 91 def to_shard(table_prefix, translations = {}) Shard.new(to_shard_info(table_prefix, translations), children.map {|c| c.to_shard(table_prefix, translations) }, weight) end |
#to_shard_id(table_prefix, translations = {}) ⇒ Object
Concretization
81 82 83 84 85 |
# File 'lib/gizzard/shard_template.rb', line 81 def to_shard_id(table_prefix, translations = {}) table_prefix = [table_prefix, table_name_suffix].compact.join('_') shard_id = ShardId.new(host, table_prefix) translations[shard_id] || shard_id end |
#to_shard_info(table_prefix, translations = {}) ⇒ Object
87 88 89 |
# File 'lib/gizzard/shard_template.rb', line 87 def to_shard_info(table_prefix, translations = {}) ShardInfo.new(to_shard_id(table_prefix, translations), type, source_type, dest_type, 0) end |
#to_simple_config(tag = "") ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/gizzard/shard_template.rb', line 193 def to_simple_config(tag="") if children.empty? && concrete? tag += "+#{shard_tag}" if shard_tag "#{host}#{tag if !tag.empty?}" elsif !children.empty? if replicating? children.map {|c| c.to_simple_config }.join(", ") else children.map {|c| c.to_simple_config("#{tag}#{'+' + shard_tag.to_s if shard_tag}")} end else "" end end |
#valid_copy_source? ⇒ Boolean
29 30 31 |
# File 'lib/gizzard/shard_template.rb', line 29 def valid_copy_source? !Shard::INVALID_COPY_TYPES.include? type.split('.').last end |