Commit 01082334 by jimabramson

Remove / consolidate queries and adjust indexing to optimize user api

performance.
parent f8cedf4d
...@@ -3,7 +3,6 @@ require_relative 'content' ...@@ -3,7 +3,6 @@ require_relative 'content'
class Comment < Content class Comment < Content
include Mongoid::Tree include Mongoid::Tree
include Mongo::Voteable
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::MagicCounterCache include Mongoid::MagicCounterCache
......
...@@ -2,7 +2,6 @@ require_relative 'content' ...@@ -2,7 +2,6 @@ require_relative 'content'
class CommentThread < Content class CommentThread < Content
include Mongo::Voteable
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::TaggableWithContext include Mongoid::TaggableWithContext
include Mongoid::TaggableWithContext::AggregationStrategy::RealTime include Mongoid::TaggableWithContext::AggregationStrategy::RealTime
......
class Content class Content
include Mongoid::Document include Mongoid::Document
include Mongo::Voteable
field :visible, type: Boolean, default: true field :visible, type: Boolean, default: true
field :abuse_flaggers, type: Array, default: [] field :abuse_flaggers, type: Array, default: []
......
...@@ -9,6 +9,7 @@ class Subscription ...@@ -9,6 +9,7 @@ class Subscription
index({subscriber_id: 1, source_id: 1, source_type: 1}) index({subscriber_id: 1, source_id: 1, source_type: 1})
index({subscriber_id: 1, source_type: 1}) index({subscriber_id: 1, source_type: 1})
index({subscriber_id: 1}) index({subscriber_id: 1})
index({source_id: 1, source_type: 1}, {background: true})
def to_hash def to_hash
as_document.slice(*%w[subscriber_id source_id source_type]) as_document.slice(*%w[subscriber_id source_id source_type])
......
...@@ -23,47 +23,27 @@ class User ...@@ -23,47 +23,27 @@ class User
validates_uniqueness_of :username validates_uniqueness_of :username
validates_uniqueness_of :email validates_uniqueness_of :email
index external_id: 1 index( {external_id: 1}, {unique: true, background: true} )
def subscriptions_as_source def subscriptions_as_source
Subscription.where(source_id: id.to_s, source_type: self.class.to_s) Subscription.where(source_id: id.to_s, source_type: self.class.to_s)
end end
def subscriptions_as_subscriber
Subscription.where(subscriber_id: id.to_s)
end
def subscribed_thread_ids def subscribed_thread_ids
Subscription.where(subscriber_id: id.to_s, source_type: "CommentThread").only(:source_id).map(&:source_id) Subscription.where(subscriber_id: id.to_s, source_type: "CommentThread").only(:source_id).map(&:source_id)
end end
def subscribed_commentable_ids
subscriptions_as_subscriber.where(source_type: "Commentable").only(:source_id).map(&:source_id)
end
def subscribed_user_ids
subscriptions_as_subscriber.where(source_type: "User").only(:source_id).map(&:source_id)
end
def subscribed_threads def subscribed_threads
CommentThread.where(:id.in => subscribed_thread_ids) CommentThread.where(:id.in => subscribed_thread_ids)
end end
def subscribed_commentables
Commentable.find(*subscribed_commentable_ids).only(:id).map(&:id)
end
def subscribed_users
subscribed_user_ids.map {|id| User.find(id)}
end
def to_hash(params={}) def to_hash(params={})
hash = as_document.slice(*%w[username external_id]) hash = as_document.slice(*%w[username external_id])
if params[:complete] if params[:complete]
hash = hash.merge("subscribed_thread_ids" => subscribed_thread_ids, hash = hash.merge("subscribed_thread_ids" => subscribed_thread_ids,
"subscribed_commentable_ids" => subscribed_commentable_ids, "subscribed_commentable_ids" => [], # not used by comment client. To be removed once removed from comment client.
"subscribed_user_ids" => subscribed_user_ids, "subscribed_user_ids" => [], # ditto.
"follower_ids" => subscriptions_as_source.only(:subscriber_id).map(&:subscriber_id), "follower_ids" => [], # ditto.
"id" => id, "id" => id,
"upvoted_ids" => upvoted_ids, "upvoted_ids" => upvoted_ids,
"downvoted_ids" => downvoted_ids, "downvoted_ids" => downvoted_ids,
...@@ -81,11 +61,11 @@ class User ...@@ -81,11 +61,11 @@ class User
end end
def upvoted_ids def upvoted_ids
Comment.up_voted_by(self).map(&:id) + CommentThread.up_voted_by(self).map(&:id) Content.up_voted_by(self).map(&:id)
end end
def downvoted_ids def downvoted_ids
Comment.down_voted_by(self).map(&:id) + CommentThread.down_voted_by(self).map(&:id) Content.down_voted_by(self).map(&:id)
end end
def followers def followers
...@@ -115,8 +95,6 @@ class User ...@@ -115,8 +95,6 @@ class User
include ::NewRelic::Agent::MethodTracer include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash add_method_tracer :to_hash
add_method_tracer :subscribed_thread_ids add_method_tracer :subscribed_thread_ids
add_method_tracer :subscribed_commentable_ids
add_method_tracer :subscribed_user_ids
add_method_tracer :upvoted_ids add_method_tracer :upvoted_ids
add_method_tracer :downvoted_ids add_method_tracer :downvoted_ids
......
db.users.dropIndex({ external_id: 1 }) // drop the non-unique one
db.users.ensureIndex({ external_id: 1 }, { unique: true, background: true })
db.subscriptions.ensureIndex({ source_id: 1, source_type: 1 }, { background: true })
db.users.dropIndex({ external_id: 1 }) // drop the unique one
db.users.ensureIndex({ external_id: 1 }, { background: true })
db.subscriptions.dropIndex({ source_id: 1, source_type: 1 })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment