How to model social graph like flockDB in Google App Engine
FlockDB provides a very nice way to model a social graph: There are only two entities in the system, User and Friendship. where User represents vertices of the graph and friendship represents edges between two users. Like this:
User(id, first_name, last_name, birth_data, registration_timestamp)
Friendship(id, user1_id, user2_id, depth, other friendship params ...)
There are a number of advantages of this approach
- You can specify params for friendship like friend, close friend, in a relationship
- You can choose whether your friendship are d开发者_如何学编程irectional or not.
My question is How to implement this with DataStore provided by Google AppEngine. here is my first attempt, but i dont understand how really to assign keys to the friendship entities.
class User(db.Model):
name = db.StringProperty()
age = db.IntegerProperty()
registeration_ts = db.DateTimeProperty(auto_now_add=True)
class FriendShip(db.Model):
user1 = db.Key() // key to some user in user1
user2 = db.Key() // key to some user in user2
creation_ts = db.DateTimeProperty(auto_now_add=True)
updated_ts = db.DateTimeProperty(auto_now_add=True)
one way would be to model like this:
class User(db.Model):
name = db.StringProperty()
friends = db.SelfReferenceProperty(collection_name="subscribers")
This would be a simple model to represent a user and friends. But i think it will have two disadvatnages:
- Since friendship is not an entity, we can't assign properties to it
- since now a collection is maintained, i don't have idea how fast it will to query like fetching friends of friends.
Instead of db.Key()
(which is a value, not a property class), use db.ReferenceProperty(User)
.
精彩评论