From b0ef88fb9f2e067775f5fe53b56fe6a3f2ac247a Mon Sep 17 00:00:00 2001 From: Dennis Collinson Date: Wed, 25 Jan 2012 19:10:05 -0800 Subject: [PATCH] WIP convert multiStream query to evilQuery --- lib/diaspora/user/querying.rb | 82 ++++++++++++++++++++++++++++------- lib/stream/multi.rb | 36 +-------------- lib/stream/person.rb | 9 +--- 3 files changed, 69 insertions(+), 58 deletions(-) diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 7abf66fc0..2385501c6 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -23,6 +23,54 @@ module Diaspora opts end + class MultiStream + def initialize(user, order, max_time, include_spotlight) + @user = user + @order = order + @max_time = max_time + @aspect_ids = aspect_ids! + @include_spotlight = include_spotlight + end + + def aspect_ids! + @user.aspects.map(&:id) + end + + def make_relation! + post_ids = aspects_post_ids + followed_tags_post_ids + mentioned_post_ids + post_ids += community_spotlight_post_ids if @include_spotlight + Post.where(:id => post_ids) + end + + def aspects_post_ids + @aspects_post_ids ||= @user.visible_shareable_ids(Post, :limit => 15, :order => "#{@order} DESC", :max_time => @max_time, :all_aspects? => true, :by_members_of => @aspect_ids) + end + + def followed_tags_post_ids + @followed_tags_ids ||= ids(StatusMessage.public_tag_stream(tag_ids)) + end + + def mentioned_post_ids + @mentioned_post_ids ||= ids(StatusMessage.where_person_is_mentioned(@user.person)) + end + + def community_spotlight_post_ids + @community_spotlight_post_ids ||= ids(Post.all_public.where(:author_id => community_spotlight_person_ids)) + end + + def community_spotlight_person_ids + @community_spotlight_person_ids ||= Person.community_spotlight.select('id').map{|x| x.id} + end + + def tag_ids + @user.followed_tags.map{|x| x.id} + end + + def ids(query) + Post.connection.select_values(query.for_a_stream(@max_time, @order).select('posts.id').to_sql) + end + end + class Base def initialize(user, klass) @querent = user @@ -121,26 +169,30 @@ module Diaspora klass.where(:id => shareable_ids).select('DISTINCT '+klass.to_s.tableize+'.*').limit(opts[:limit]).order(opts[:order_with_table]) end - def visible_shareable_ids(klass, opts={}) - opts = prep_opts(klass, opts) - cache = nil + def visible_shareables_from_cache(klass, opts) + cache = RedisCache.new(self, opts[:order_field]) - if use_cache?(opts) - cache = RedisCache.new(self, opts[:order_field]) - - #total hax - if self.contacts.where(:sharing => true, :receiving => true).count > 0 - cache.ensure_populated!(opts) - end - - name = klass.to_s.downcase - shareable_ids = cache.send(name+"_ids", opts[:max_time], opts[:limit] +1) + #total hax + if self.contacts.where(:sharing => true, :receiving => true).count > 0 + cache.ensure_populated!(opts) end - if perform_db_query?(shareable_ids, cache, opts) + name = klass.to_s.downcase + "_ids" + cached_ids = cache.send(name, opts[:max_time], opts[:limit] +1) + + if perform_db_query?(cached_ids, cache, opts) visible_ids_from_sql(klass, opts) else - shareable_ids + cached_ids + end + end + + def visible_shareable_ids(klass, opts={}) + opts = prep_opts(klass, opts) + if use_cache?(opts) + visible_shareables_from_cache(klass, opts) + else + visible_ids_from_sql(klass, opts) end end diff --git a/lib/stream/multi.rb b/lib/stream/multi.rb index 0dc9efaa1..b1944e6ea 100644 --- a/lib/stream/multi.rb +++ b/lib/stream/multi.rb @@ -16,11 +16,7 @@ class Stream::Multi < Stream::Base end def posts - @posts ||= lambda do - post_ids = aspects_post_ids + followed_tags_post_ids + mentioned_post_ids - post_ids += community_spotlight_post_ids if include_community_spotlight? - Post.where(:id => post_ids) - end.call + @posts ||= Diaspora::EvilQuery::MultiStream.new(user, order, max_time, include_community_spotlight?).make_relation! end #emits an enum of the groups which the post appeared @@ -83,34 +79,4 @@ class Stream::Multi < Stream::Base def include_community_spotlight? AppConfig[:community_spotlight].present? && user.show_community_spotlight_in_stream? end - - def aspects_post_ids - @aspects_post_ids ||= user.visible_shareable_ids(Post, :limit => 15, :order => "#{order} DESC", :max_time => max_time, :all_aspects? => true, :by_members_of => aspect_ids) - end - - def followed_tags_post_ids - @followed_tags_ids ||= ids(StatusMessage.public_tag_stream(tag_ids)) - end - - def mentioned_post_ids - @mentioned_post_ids ||= ids(StatusMessage.where_person_is_mentioned(user.person)) - end - - def community_spotlight_post_ids - @community_spotlight_post_ids ||= ids(Post.all_public.where(:author_id => community_spotlight_person_ids)) - end - - #worthless helpers - def community_spotlight_person_ids - @community_spotlight_person_ids ||= Person.community_spotlight.select('id').map{|x| x.id} - end - - def tag_ids - user.followed_tags.map{|x| x.id} - end - - def ids(query) - Post.connection.select_values(query.for_a_stream(max_time, order).select('posts.id').to_sql) - end - end diff --git a/lib/stream/person.rb b/lib/stream/person.rb index 87c2442f3..efbe9e22a 100644 --- a/lib/stream/person.rb +++ b/lib/stream/person.rb @@ -13,13 +13,6 @@ class Stream::Person < Stream::Base # @return [ActiveRecord::Association] AR association of posts def posts - @posts ||= lambda do - if user - posts = self.user.posts_from(@person) - else - posts = @person.posts.where(:public => true) - end - posts - end.call + @posts ||= user.present? ? user.posts_from(@person) : @person.posts.where(:public => true) end end