From fa41c1fc52fd4c105a52c64bf774d1a879315132 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Tue, 5 Apr 2011 14:56:56 -0700 Subject: [PATCH] Stream query... FIXED????? --- lib/diaspora/user/querying.rb | 2 +- spec/models/user/querying_spec.rb | 51 ++++++++++++++++--------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index e5b84d975..8cf573a09 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -34,7 +34,7 @@ module Diaspora posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]).offset(opts[:offset]).order(order_with_table) posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).offset(opts[:offset]).order(order_with_table) - all_posts = "(#{posts_from_others.to_sql}) UNION (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" + all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" post_ids = Post.connection.execute(all_posts).map{|r| r.first} Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table) diff --git a/spec/models/user/querying_spec.rb b/spec/models/user/querying_spec.rb index 136ac175e..49ccbc0cc 100644 --- a/spec/models/user/querying_spec.rb +++ b/spec/models/user/querying_spec.rb @@ -24,6 +24,31 @@ describe User do stream.should include(visible_post) stream.should_not include(invisible_post) end + context 'with many posts' do + before do + bob.move_contact(eve.person, bob.aspects.first, bob.aspects.create(:name => 'new aspect')) + (1..25).each do |n| + [alice, bob, eve].each do |u| + post = u.post :status_message, :text => "#{u.username} - #{n}", :to => u.aspects.first.id + post.created_at = post.created_at + n*1000*u.id + post.updated_at = post.updated_at + n*1000*u.id + post.save + end + end + end + it 'works' do #This is in one spec to save time + bob.raw_visible_posts.should == bob.raw_visible_posts(:by_members_of => bob.aspects.map{|a| a.id}) + bob.raw_visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts.map{|p| p.id}.reverse + + opts = {:limit => 40} + bob.raw_visible_posts(opts).should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})) + bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse + + opts = {:page => 2} + bob.raw_visible_posts(opts).map{|p| p.id}.should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id} + bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse + end + end end context 'with two posts' do @@ -130,33 +155,9 @@ describe User do alice.people_in_aspects([eve.aspects.first]).should == [] end end - context 'with many posts' do - before do - bob.move_contact(eve.person, bob.aspects.first, bob.aspects.create(:name => 'new aspect')) - (1..25).each do |n| - [alice, bob, eve].each do |u| - post = u.post :status_message, :text => "#{u.username} - #{n}", :to => u.aspects.first.id - post.created_at = post.created_at + n*1000*u.id - post.updated_at = post.updated_at + n*1000*u.id - post.save - end - end - end - it 'works' do #This is in one spec to save time - bob.raw_visible_posts.should == bob.raw_visible_posts(:by_members_of => bob.aspects.map{|a| a.id}) - bob.raw_visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts.map{|p| p.id}.reverse - - opts = {:limit => 40} - bob.raw_visible_posts(opts).should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})) - bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse - - opts = {:page => 2} - bob.raw_visible_posts(opts).map{|p| p.id}.should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id} - bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse - end - end end + context 'contact querying' do let(:person_one) { Factory.create :person } let(:person_two) { Factory.create :person }