WIP convert multiStream query to evilQuery
This commit is contained in:
parent
c490224516
commit
b0ef88fb9f
3 changed files with 69 additions and 58 deletions
|
|
@ -23,6 +23,54 @@ module Diaspora
|
||||||
opts
|
opts
|
||||||
end
|
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
|
class Base
|
||||||
def initialize(user, klass)
|
def initialize(user, klass)
|
||||||
@querent = user
|
@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])
|
klass.where(:id => shareable_ids).select('DISTINCT '+klass.to_s.tableize+'.*').limit(opts[:limit]).order(opts[:order_with_table])
|
||||||
end
|
end
|
||||||
|
|
||||||
def visible_shareable_ids(klass, opts={})
|
def visible_shareables_from_cache(klass, opts)
|
||||||
opts = prep_opts(klass, opts)
|
cache = RedisCache.new(self, opts[:order_field])
|
||||||
cache = nil
|
|
||||||
|
|
||||||
if use_cache?(opts)
|
#total hax
|
||||||
cache = RedisCache.new(self, opts[:order_field])
|
if self.contacts.where(:sharing => true, :receiving => true).count > 0
|
||||||
|
cache.ensure_populated!(opts)
|
||||||
#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)
|
|
||||||
end
|
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)
|
visible_ids_from_sql(klass, opts)
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,7 @@ class Stream::Multi < Stream::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def posts
|
def posts
|
||||||
@posts ||= lambda do
|
@posts ||= Diaspora::EvilQuery::MultiStream.new(user, order, max_time, include_community_spotlight?).make_relation!
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#emits an enum of the groups which the post appeared
|
#emits an enum of the groups which the post appeared
|
||||||
|
|
@ -83,34 +79,4 @@ class Stream::Multi < Stream::Base
|
||||||
def include_community_spotlight?
|
def include_community_spotlight?
|
||||||
AppConfig[:community_spotlight].present? && user.show_community_spotlight_in_stream?
|
AppConfig[:community_spotlight].present? && user.show_community_spotlight_in_stream?
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,6 @@ class Stream::Person < Stream::Base
|
||||||
|
|
||||||
# @return [ActiveRecord::Association<Post>] AR association of posts
|
# @return [ActiveRecord::Association<Post>] AR association of posts
|
||||||
def posts
|
def posts
|
||||||
@posts ||= lambda do
|
@posts ||= user.present? ? user.posts_from(@person) : @person.posts.where(:public => true)
|
||||||
if user
|
|
||||||
posts = self.user.posts_from(@person)
|
|
||||||
else
|
|
||||||
posts = @person.posts.where(:public => true)
|
|
||||||
end
|
|
||||||
posts
|
|
||||||
end.call
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue