* Removed memcaching for TagImplication (too many latent race conditions)
* Added Post.exact_tag_match to skip normalization/metatag parsing * Added DelayedJob support for tag alias/implication processing
This commit is contained in:
@@ -41,6 +41,7 @@ class Post < ActiveRecord::Base
|
||||
scope :available_for_moderation, lambda {where(["id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
|
||||
scope :hidden_from_moderation, lambda {where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
|
||||
scope :tag_match, lambda {|query| Post.tag_match_helper(query)}
|
||||
scope :exact_tag_match, lambda {|query| Post.exact_tag_match_helper(query)}
|
||||
scope :positive, where("score > 1")
|
||||
scope :negative, where("score < -1")
|
||||
search_methods :tag_match
|
||||
@@ -501,6 +502,11 @@ class Post < ActiveRecord::Base
|
||||
|
||||
relation
|
||||
end
|
||||
|
||||
def exact_tag_match_helper(q)
|
||||
arel = Post.scoped
|
||||
add_tag_string_search_relation({:related => [q].flatten, :include => [], :exclude => []}, arel)
|
||||
end
|
||||
|
||||
def tag_match_helper(q)
|
||||
unless q.is_a?(Hash)
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
class TagAlias < ActiveRecord::Base
|
||||
attr_accessor :creator_ip_addr
|
||||
after_save :update_posts
|
||||
after_save :clear_cache
|
||||
after_save :clear_remote_cache
|
||||
before_save :clear_all_cache
|
||||
after_save :update_cache
|
||||
after_destroy :clear_cache
|
||||
after_destroy :clear_remote_cache
|
||||
after_destroy :clear_all_cache
|
||||
before_validation :initialize_creator, :on => :create
|
||||
validates_presence_of :creator_id
|
||||
validates_uniqueness_of :antecedent_name
|
||||
@@ -25,8 +21,17 @@ class TagAlias < ActiveRecord::Base
|
||||
alias_hash.values.flatten.uniq
|
||||
end
|
||||
|
||||
def process!
|
||||
update_column(:status, "processing")
|
||||
update_posts
|
||||
update_column(:status, "active")
|
||||
rescue Exception => e
|
||||
update_column(:status, "error: #{e}")
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
self.creator_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
|
||||
def antecedent_tag
|
||||
@@ -44,6 +49,11 @@ class TagAlias < ActiveRecord::Base
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def clear_all_cache
|
||||
clear_cache
|
||||
clear_remote_cache
|
||||
end
|
||||
|
||||
def clear_cache
|
||||
Cache.delete("ta:#{Cache.sanitize(antecedent_name)}")
|
||||
@@ -60,13 +70,15 @@ class TagAlias < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def update_posts
|
||||
Post.tag_match(antecedent_name).find_each do |post|
|
||||
Post.exact_tag_match(antecedent_name).find_each do |post|
|
||||
escaped_antecedent_name = Regexp.escape(antecedent_name)
|
||||
fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{consequent_name} ").strip
|
||||
|
||||
post.update_attributes(
|
||||
:tag_string => fixed_tags
|
||||
)
|
||||
CurrentUser.scoped(creator, creator_ip_addr) do
|
||||
post.update_attributes(
|
||||
:tag_string => fixed_tags
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,43 +1,19 @@
|
||||
class TagImplication < ActiveRecord::Base
|
||||
before_save :clear_cache
|
||||
before_save :update_descendant_names
|
||||
after_save :update_descendant_names_for_parent
|
||||
after_save :update_cache
|
||||
after_save :update_posts
|
||||
after_destroy :clear_cache
|
||||
after_destroy :clear_remote_cache
|
||||
belongs_to :creator, :class_name => "User"
|
||||
before_validation :initialize_creator, :on => :create
|
||||
validates_presence_of :creator_id
|
||||
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
|
||||
validate :absence_of_circular_relation
|
||||
|
||||
module CacheMethods
|
||||
def clear_cache
|
||||
Cache.delete("ti:#{Cache.sanitize(antecedent_name)}")
|
||||
@descendants = nil
|
||||
end
|
||||
|
||||
def clear_remote_cache
|
||||
Danbooru.config.other_server_hosts.each do |server|
|
||||
Net::HTTP.delete(URI.parse("http://#{server}/tag_implications/#{id}/cache"))
|
||||
end
|
||||
end
|
||||
|
||||
def update_cache
|
||||
descendant_names_array
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
module DescendantMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
# assumes names are normalized
|
||||
def with_descendants(names)
|
||||
names + Cache.get_multi(names.flatten, "ti") do |name|
|
||||
([name] + where(["antecedent_name = ?", name]).all.map {|x| x.descendant_names_array}).flatten
|
||||
end.values.flatten.uniq
|
||||
(names + where("antecedent_name in (?)", names).map(&:descendant_names_array)).flatten.uniq
|
||||
end
|
||||
end
|
||||
|
||||
@@ -55,9 +31,7 @@ class TagImplication < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def descendant_names_array
|
||||
Cache.get("ti:#{Cache.sanitize(antecedent_name)}") do
|
||||
descendant_names.split(/ /)
|
||||
end
|
||||
descendant_names.split(/ /)
|
||||
end
|
||||
|
||||
def update_descendant_names
|
||||
@@ -93,12 +67,20 @@ class TagImplication < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
include CacheMethods
|
||||
include DescendantMethods
|
||||
include ParentMethods
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
self.creator_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
|
||||
def process!
|
||||
update_column(:status, "processing")
|
||||
update_posts
|
||||
update_column(:status, "active")
|
||||
rescue Exception => e
|
||||
update_column(:status, "error: #{e}")
|
||||
end
|
||||
|
||||
def absence_of_circular_relation
|
||||
@@ -110,12 +92,14 @@ class TagImplication < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def update_posts
|
||||
Post.tag_match(antecedent_name).find_each do |post|
|
||||
Post.exact_tag_match(antecedent_name).find_each do |post|
|
||||
escaped_antecedent_name = Regexp.escape(antecedent_name)
|
||||
fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{antecedent_name} #{descendant_names} ").strip
|
||||
post.update_attributes(
|
||||
:tag_string => fixed_tags
|
||||
)
|
||||
CurrentUser.scoped(creator, creator_ip_addr) do
|
||||
post.update_attributes(
|
||||
:tag_string => fixed_tags
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user