incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexandre Leray <newslett...@alexandreleray.com>
Subject Implementing a threaded comment application with map/reduce
Date Tue, 18 May 2010 00:24:53 GMT
Hi,

I just started learning couchdb (so far so good!); please forgive me if 
I'm not crystal clear in my explanations!

I'm trying to implement a threaded commenting system in couch, much like 
http://code.google.com/p/django-threadedcomments/ does for django/sql.

I wrote some dummy python code to explain what it is suppose to achieve 
(visible in full color here:
http://dpaste.com/195870/):

class Comment:
     """
     This represent a comment.
     With Couchdb it would be a document with 2 fields:
         object_id: the id of the commented object
         content: the content of the comment
     and of course the _id of the document.
     So a comment could be attached to any object given its id
     (be the object a comment itself or not)
     """
     def __init__(self, id, object_id, content):
         self.id = id
         self.object_id = object_id
         self.content = content

# Let's make a fake list of comment.
# The first arg is the id, the second the id of the target object
# and the third is the content of the comment
comments = [
     Comment(2, 1, "This is a first comment on some object"),
     Comment(3, 1, "A second comment on some object"),
     Comment(4, 2, "A comment on the first comment"),
     Comment(5, 2, "A second comment on the first comment"),
     Comment(6, 3, "A comment on the second comment on some object"),
]

def recurse (object_id):
     """
     recursively fetches an object comments.
     Returns a json of nested comment objects/documents
     """
     comment_list = [item for item in comments if item.object_id == 
object_id]
     if len(comment_list) == 0:
         return ""
     else:
         for c in comment_list:
             return """
                 {
                     "id" : "%s",
                     "object_id" : "%s",
                     "content" : "%s",
                     "children": [
                         %s
                     ],
                  },
             """ % (c.id, c.object_id, c.content, recurse(c.id))

# get the comments for the object/document with the id 1
print recurse(1)


How could I implement such a thing using map/reduce?

Thanks,

Alexandre Leray



Mime
View raw message