Skip to content

Commit

Permalink
Optimize FeedManager#unmerge, and slightly optimize FeedManager#merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron committed Apr 4, 2017
1 parent b21f7c2 commit b1f3499
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/lib/feed_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ def trim(type, account_id)

def merge_into_timeline(from_account, into_account)
timeline_key = key(:home, into_account.id)
query = from_account.statuses.limit(MAX_ITEMS)

if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS
oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
query = query.where('id > ?', oldest_home_score)
end

redis.pipelined do
from_account.statuses.limit(MAX_ITEMS).each do |status|
query.each do |status|
next if status.direct_visibility? || filter?(:home, status, into_account)
redis.zadd(timeline_key, status.id, status.id)
end
Expand All @@ -63,8 +69,9 @@ def merge_into_timeline(from_account, into_account)

def unmerge_from_timeline(from_account, into_account)
timeline_key = key(:home, into_account.id)
oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0

from_account.statuses.select('id').find_in_batches do |statuses|
from_account.statuses.select('id').where('id > ?', oldest_home_score).find_in_batches do |statuses|
redis.pipelined do
statuses.each do |status|
redis.zrem(timeline_key, status.id)
Expand Down

0 comments on commit b1f3499

Please sign in to comment.