Browse files

Make comment count maintenance more transactional.

Move comment (re)counting for links to comment_tree.py. This should help
make comment counts more consistent. Fixes #44, fixes #60.
  • Loading branch information...
1 parent 06f4b9e commit fd02caf0355176e0034a7a8044601a1a2416dee0 Logan Hanks committed Jun 20, 2011
Showing with 28 additions and 20 deletions.
  1. +0 −1 r2/r2/controllers/api.py
  2. +28 −8 r2/r2/lib/comment_tree.py
  3. +0 −11 r2/r2/models/link.py
View
1 r2/r2/controllers/api.py
@@ -706,7 +706,6 @@ def POST_del(self, thing):
if parent_link.is_self:
recipient = Account._byID(parent_link.author_id)
- thing._delete()
delete_comment(thing)
if recipient:
View
36 r2/r2/lib/comment_tree.py
@@ -24,6 +24,7 @@
from r2.lib.utils import tup, to36
from r2.lib.db.sorts import epoch_seconds
from r2.lib.cache import sgm
+from r2.models.link import Link
MAX_ITERATIONS = 20000
@@ -57,7 +58,9 @@ def add_comments(comments):
with g.make_lock(lock_key(link_id)):
add_comments_nolock(link_id, coms)
except:
- # TODO: bare except?
+ g.log.exception(
+ 'add_comments_nolock failed for link %s, recomputing tree',
+ link_id)
# calculate it from scratch
link_comments(link_id, _update = True)
@@ -115,10 +118,6 @@ def find_parents():
if not r:
r = _parent_dict_from_tree(comment_tree)
- for comment in comments:
- cm_id = comment._id
-# print "In the olden days, I would have set %s -> %s" % (cm_id, p_id)
-
for cm_id, parent_id in new_parents.iteritems():
# print "Now, I set %s -> %s" % (cm_id, parent_id)
r[cm_id] = parent_id
@@ -129,6 +128,12 @@ def find_parents():
r[cm_id] = None
# print "And I set %s -> None" % cm_id
+ # update the link's comment count and schedule it for search reindexing
+ link = Link._byID(link_id, data = True)
+ link._incr('num_comments', len(comments))
+ from r2.lib.db.queries import changed
+ changed(link)
+
g.permacache.set(key, r)
g.permacache.set(comments_key(link_id),
@@ -168,6 +173,12 @@ def delete_comment(comment):
g.permacache.set(comments_key(comment.link_id),
(cids, comment_tree, depth, num_children))
+ # update the link's comment count and schedule it for search reindexing
+ link = Link._byID(comment.link_id, data = True)
+ link._incr('num_comments', -1)
+ from r2.lib.db.queries import changed
+ changed(link)
+
def _parent_dict_from_tree(comment_tree):
parents = {}
for parent, childs in comment_tree.iteritems():
@@ -265,7 +276,6 @@ def link_comments_and_sort(link_id, sort):
parents = _parent_dict_from_tree(cid_tree)
g.permacache.set(key, parents)
-
return cids, cid_tree, depth, num_children, parents, sorter
@@ -283,12 +293,21 @@ def link_comments(link_id, _update=False):
with g.make_lock(lock_key(link_id), timeout=180):
r = _load_link_comments(link_id)
# rebuild parent dict
- cids, cid_tree, depth, num_children = r
+ cids, cid_tree, depth, num_children, num_comments = r
+ r = r[:-1] # Remove num_comments from r; we don't need to cache it.
g.permacache.set(parent_comments_key(link_id),
_parent_dict_from_tree(cid_tree))
g.permacache.set(key, r)
+ # update the link's comment count and schedule it for search
+ # reindexing
+ link = Link._byID(link_id, data = True)
+ link.num_comments = num_comments
+ link._commit()
+ from r2.lib.db.queries import changed
+ changed(link)
+
return r
def _load_link_comments(link_id):
@@ -333,7 +352,8 @@ def _load_link_comments(link_id):
iteration_count += 1
num_children[cm_id] = num
- return cids, comment_tree, depth, num_children
+ num_comments = sum(1 for c in comments if not c._deleted)
+ return cids, comment_tree, depth, num_children, num_comments
# message conversation functions
def messages_key(user_id):
View
11 r2/r2/models/link.py
@@ -579,13 +579,6 @@ class Comment(Thing, Printable):
def _markdown(self):
pass
- def _delete(self):
- from r2.lib.db.queries import changed
-
- link = Link._byID(self.link_id, data = True)
- link._incr('num_comments', -1)
- changed(link, True)
-
@classmethod
def _new(cls, author, link, parent, body, ip):
from r2.lib.db.queries import changed
@@ -603,8 +596,6 @@ def _new(cls, author, link, parent, body, ip):
if parent:
c.parent_id = parent._id
- link._incr('num_comments', 1)
-
to = None
name = 'inbox'
if parent:
@@ -615,8 +606,6 @@ def _new(cls, author, link, parent, body, ip):
c._commit()
- changed(link, True) # only the number of comments has changed
-
inbox_rel = None
# only global admins can be message spammed.
if to and (not c._spam or to.name in g.admins):

0 comments on commit fd02caf

Please sign in to comment.