Created
March 16, 2013 01:53
-
-
Save pat/5174559 to your computer and use it in GitHub Desktop.
Combined delta updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: UserAndRelationshipDelta.perform_async user.id | |
# | |
class UserAndRelationshipDelta | |
include Sidekiq::Worker | |
def perform(user_id) | |
# Set delta flags | |
User.update_all({delta: true}, {id: user_id}) | |
Relationship.update_all({delta: true}, {user_id: user_id}) | |
# Process delta indices | |
configuration = ThinkingSphinx::Configuration.instance | |
configuration.controller.index 'user_delta', 'relationship_delta' | |
# Mark non-delta user record as deleted | |
configuration.indices_for_references(:user).reject(&:delta?).each do |index| | |
ThinkingSphinx::Connection.new.execute Riddle::Query.update( | |
index.name, index.document_id_for_key(user_id), sphinx_deleted: true | |
) | |
end | |
# Mark non-delta relationship records as deleted | |
indices = configuration.indices_for_references(:relationship) | |
indices.reject(&:delta?).each do |index| | |
Relationship.where(user_id: user_id).pluck(:id).each do |id| | |
ThinkingSphinx::Connection.new.execute Riddle::Query.update( | |
index.name, index.document_id_for_key(id), sphinx_deleted: true | |
) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment