Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Speed up ShardTemplate comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Stu Hood committed Feb 17, 2012
1 parent 3cd456c commit 89b80bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
21 changes: 15 additions & 6 deletions lib/gizzard/shard_template.rb
Expand Up @@ -12,34 +12,43 @@ class ShardTemplate
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
@type_name = ShardTemplate.type_to_type_name(type)
end

def self.type_to_type_name(type)
if (dot_index = type.rindex('.'))
type[dot_index+1 .. -1]
else
type
end
end

def self.concrete?(type)
!Shard::VIRTUAL_SHARD_TYPES.include? type.split('.').last
!Shard::VIRTUAL_SHARD_TYPES.include?(type_to_type_name(type))
end

def concrete?
self.class.concrete? type
!Shard::VIRTUAL_SHARD_TYPES.include? @type_name
end

def replicating?
Shard::REPLICATING_SHARD_TYPES.include? type.split('.').last
Shard::REPLICATING_SHARD_TYPES.include? @type_name
end

def valid_copy_source?
!Shard::INVALID_COPY_TYPES.include? type.split('.').last
!Shard::INVALID_COPY_TYPES.include? @type_name
end

def identifier
concrete? ? "#{type}/#{host}" : type.to_s
end

def table_name_suffix
Shard::SHARD_SUFFIXES[type.split('.').last]
Shard::SHARD_SUFFIXES[@type_name]
end

def shard_tag
Shard::SHARD_TAGS[type.split('.').last]
Shard::SHARD_TAGS[@type_name]
end

def host
Expand Down
14 changes: 6 additions & 8 deletions lib/gizzard/transformation.rb
Expand Up @@ -82,20 +82,18 @@ def noop?
end

def eql?(o)
o.is_a?(self.class) &&
from.eql?(o.from) &&
to.eql?(o.to) &&
copy_dest_wrapper.eql?(o.copy_dest_wrapper)
o.is_a?(self.class) && (self <=> o) == 0
end

def <=>(o)
to_a = lambda {|t| [t.from, t.to, t.copy_dest_wrapper] }

to_a.call(self) <=> to_a.call(o)
if ((cmp = self.from <=> o.from) != 0); return cmp end
if ((cmp = self.to <=> o.to) != 0); return cmp end
self.copy_dest_wrapper <=> o.copy_dest_wrapper
end

def hash
from.hash + to.hash + copy_dest_wrapper.hash
return @hash if @hash
@hash = from.hash + to.hash + copy_dest_wrapper.hash
end

# create a map of empty phase lists
Expand Down

0 comments on commit 89b80bd

Please sign in to comment.