2010-05-24 48 views
3

我想設計表來建立一個跟隨者的關係。如何在appengine中建模follower流?

說我有一個140char的記錄流,有用戶,標籤和其他文本。

用戶關注其他用戶,也可以按照標籤。

我在概述我設計下面的方式,但在我的設計中有兩個限制。我想知道其他人是否有更聰明的方法來實現相同的目標。

這樣做的問題是

  1. 追隨者清單中被複制在每個記錄
  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) 

有沒有一種更聰明的辦法做到這一點?

+0

Duplicate http://stackoverflow.com/questions/2668470/good-way-of-implementing-a-twitter-like-follower-system – 2010-05-24 18:47:13

回答

1

是的,這是扇出的問題,因爲其他人指出和Brett斯拉特金的談話應該由那些有興趣來看待。

不過,我提出2所具體的限制,即

  • 追隨者清單中被複制在每個記錄

這像他們說的是不是一個錯誤,但功能。事實上,它正是以這種方式在引擎上擴展。

  • 如果新追隨者被添加或被刪除,則'所有'記錄必須被更新。

要麼沒有做任何事情,所以未來的記錄不會被遵循。換句話說,在某個特定時間,人們不會跟隨人們的流。因此,如果在第2天取消關注,則您的關注者流仍會顯示第一天發佈的用戶記錄,但不會顯示第二天及之後的記錄。 [注意:這與twitter的做法不同]

0

你可以使用一個reference屬性,然後有一個共同的表與它的追隨者,你參考

-1

我不知道如何在谷歌應用程序引擎做到這一點,但一個數據庫模式我會考慮是:

 
Tables: 
    User -- a table of users with their attributes 
    HashTag -- a table of HashTags with their attributes 
    Follows -- a table that defines who follows whom 

Columns in the Follows table: 
    followed int,   -- the id of the followed entity (could be 
          User or Hashtag) 
    followed_is_user bit, -- whether the followed item is a User 
    followed_is_tag bit, -- whether the followed item is a HashTag 
    follower int   -- the id of the follower (this can only be 
          a User so you may want to make this a foreign 
          key on the User table) 

你也許可以凝結在兩位列進一個,但這將允許您添加用戶可以遵循在未來其他的事情。

+2

appengine數據庫實現是非常獨特的,我正在尋找一個特定的迴應。 – molicule 2010-05-24 16:54:56