Skip to content

Commit

Permalink
Garbage collector: Improve performance
Browse files Browse the repository at this point in the history
Run the cleanup for those readers only, who have at least one single
read_marks. Belongs to #64
  • Loading branch information
ledermann committed Oct 31, 2015
1 parent 1ea90de commit 7d26c80
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/unread/garbage_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(readable_class)

def run!
ReadMark.reader_classes.each do |reader_class|
reader_class.reader_scope.find_each do |reader|
readers_to_cleanup(reader_class).each do |reader|
if oldest_timestamp = readable_class.read_scope(reader).
unread_by(reader).
minimum(readable_class.readable_options[:on])
Expand All @@ -23,6 +23,17 @@ def run!
end

private
# Not for every reader a cleanup is needed.
# Look for those readers with at least one single read mark
def readers_to_cleanup(reader_class)
reader_class.
reader_scope.
joins(:read_marks).
where(:read_marks => { :readable_type => readable_class.base_class.name }).
group('read_marks.reader_type, read_marks.reader_id').
having('COUNT(read_marks.id) > 1')
end

def update_read_marks_for_user(reader, timestamp)
ReadMark.transaction do
# Delete markers OLDER than the given timestamp
Expand Down

0 comments on commit 7d26c80

Please sign in to comment.