mol*_*ule 3 python google-app-engine database-design bigtable
我正在尝试设计表来建立一个跟随者关系.
假设我有一个包含用户,主题标签和其他文本的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)
Run Code Online (Sandbox Code Playgroud)
有更聪明的方法吗?
您想要解决的问题称为扇出问题.
来自Google App Engine团队的Brett Slatkin为App Engine上的扇出问题提供了一个高效/可扩展的解决方案.你可以在这里找到一个关于这个话题的视频:
http://code.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html
| 归档时间: |
|
| 查看次数: |
950 次 |
| 最近记录: |