Storing a directed graph in google appengine datastore

强颜欢笑 提交于 2019-12-03 03:43:38

Here's the simplest way:

class Vertex(db.Model):
  outedges = db.ListProperty(db.Key)
  # Other information about the vertex here

Now you can explore the graph without any queries at all - just call db.get on 1 or more keys to retrieve the relevant vertices:

# Get the first referenced vertex
vertex2 = db.get(vertex1.outedges[0])

# Get all referenced vertices
vertices = db.get(vertex1.outedges)

Depending on the number of vertex/links you might want to just use lists instead of creating a bunch of new entities. Check the friends graph problems described in the second half of this video from Google IO 2009: http://www.youtube.com/watch?v=AgaL6NGpkB8

If you think the number of vertex is high enough you can just create a Vertex model with a list that represents the connections.

Considering you're using the google app engine it would be best if you stored the information in separate tables:

One for the vertices, one for the links from a vertex (as you already said) and an additional one where the paths are already precomputed.

GAE works best if the information you store is denormalized so you don't have to do any calculations on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!