【发布时间】:2010-05-24 15:59:37
【问题描述】:
我正在尝试设计表格来建立追随者关系。
假设我有一个包含用户、主题标签和其他文本的 140char 记录流。
用户关注其他用户,也可以关注主题标签。
我在下面概述了我的设计方式,但我的设计有两个限制。我想知道其他人是否有更聪明的方法来实现相同的目标。
这个问题是
- 为每条记录复制关注者列表
- 如果添加或删除了一个新的追随者,则“全部” 记录必须更新。
代码
class HashtagFollowers(db.Model):
"""
This table contains the followers for each hashtag
"""
hashtag = db.StringProperty()
followers = db.StringListProperty()
class UserFollowers(db.Model):
"""
This table contains the followers for each user
"""
username = db.StringProperty()
followers = db.StringListProperty()
class stream(db.Model):
"""
This table contains the data stream
"""
username = db.StringProperty()
hashtag = db.StringProperty()
text = db.TextProperty()
def save(self):
"""
On each save all the followers for each hashtag and user
are added into a another table with this record as the parent
"""
super(stream, self).save()
hfs = HashtagFollowers.all().filter("hashtag =", self.hashtag).fetch(10)
for hf in hfs:
sh = streamHashtags(parent=self, followers=hf.followers)
sh.save()
ufs = UserFollowers.all().filter("username =", self.username).fetch(10)
for uf in ufs:
uh = streamUsers(parent=self, followers=uf.followers)
uh.save()
class streamHashtags(db.Model):
"""
The stream record is the parent of this record
"""
followers = db.StringListProperty()
class streamUsers(db.Model):
"""
The stream record is the parent of this record
"""
followers = db.StringListProperty()
Now, to get the stream of followed hastags
indexes = db.GqlQuery("""SELECT __key__ from streamHashtags where followers = 'myusername'""")
keys = [k,parent() for k in indexes[offset:numresults]]
return db.get(keys)
有没有更聪明的方法来做到这一点?
【问题讨论】:
标签: python database-design google-app-engine bigtable