From 9bfdd5e5ee4a0c3b040a4214b0b89676b4089445 Mon Sep 17 00:00:00 2001 From: spez Date: Tue, 30 Sep 2008 10:59:23 -0700 Subject: [PATCH] precompute toplinks --- r2/r2/controllers/listingcontroller.py | 5 +---- r2/r2/lib/db/queries.py | 11 ++++++++++- r2/r2/models/subreddit.py | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/r2/r2/controllers/listingcontroller.py b/r2/r2/controllers/listingcontroller.py index ecb996624..b95faf610 100644 --- a/r2/r2/controllers/listingcontroller.py +++ b/r2/r2/controllers/listingcontroller.py @@ -260,10 +260,7 @@ class ToplinksController(ListingController): title_text = _('top scoring links') def query(self): - q = Link._query(Link.c.top_link == True, - sort = desc('_hot'), - *c.site.query_rules()) - return q + return c.site.get_links('toplinks', 'all') @validate(VSrMask('srs')) def GET_listing(self, **env): diff --git a/r2/r2/lib/db/queries.py b/r2/r2/lib/db/queries.py index 95c1c34ed..03144454b 100644 --- a/r2/r2/lib/db/queries.py +++ b/r2/r2/lib/db/queries.py @@ -18,7 +18,8 @@ db_sorts = dict(hot = (desc, '_hot'), new = (desc, '_date'), top = (desc, '_score'), controversial = (desc, '_controversy'), - old = (asc, '_date')) + old = (asc, '_date'), + toplinks = (desc, '_hot')) def db_sort(sort): cls, col = db_sorts[sort] @@ -179,6 +180,10 @@ def get_links(sr, sort, time): """General link query for a subreddit.""" q = Link._query(Link.c.sr_id == sr._id, sort = db_sort(sort)) + + if sort == 'toplinks': + q._filter(Link.c.top_link == True) + if time != 'all': q._filter(db_times[time]) return make_results(q) @@ -298,6 +303,7 @@ def new_link(link): results = all_queries(get_links, sr, ('hot', 'new', 'old'), ['all']) results.extend(all_queries(get_links, sr, ('top', 'controversial'), db_times.keys())) results.append(get_submitted(author, 'new', 'all')) + results.append(get_links(sr, 'toplinks', 'all')) if link._deleted: add_queries(results, delete_item = link) @@ -328,6 +334,7 @@ def new_vote(vote): sr = item.subreddit_slow results = all_queries(get_links, sr, ('hot', 'new'), ['all']) results.extend(all_queries(get_links, sr, ('top', 'controversial'), db_times.keys())) + results.append(get_links(sr, 'toplinks', 'all')) add_queries(results) #must update both because we don't know if it's a changed vote @@ -366,6 +373,8 @@ def add_all_srs(): for sr in fetch_things2(q): add_queries(all_queries(get_links, sr, ('hot', 'new', 'old'), ['all'])) add_queries(all_queries(get_links, sr, ('top', 'controversial'), db_times.keys())) + add_queries([get_links(sr, 'toplinks', 'all')]) + def update_user(user): if isinstance(user, str): diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 6694f65b4..8a9b730c0 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -509,6 +509,8 @@ class DefaultSR(FakeSubreddit): else: q = Link._query(Link.c.sr_id == sr_ids, sort = queries.db_sort(sort)) + if sort == 'toplinks': + q._filter(Link.c.top_link == True) if time != 'all': q._filter(queries.db_times[time]) return q