フォロワー関係を構築するためのテーブルを設計しようとしています。
ユーザー、ハッシュタグ、その他のテキストを含む140文字のレコードのストリームがあるとします。
ユーザーは他のユーザーをフォローし、ハッシュタグをフォローすることもできます。
これを設計した方法の概要を以下に示しますが、設計には2つの制限があります。他の人が同じ目標を達成するためのより賢い方法を持っているかどうか疑問に思いました。
これに関する問題は
- フォロワーのリストは、レコードごとにコピーされます
- 新しいフォロワーが追加または削除された場合は、「すべての」レコードを更新する必要があります。
コード
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)
これを行うためのよりスマートな方法はありますか?