How to use High Replication Datastore

2019-04-11 19:38发布

Okay, I have watched the video and read the articles in the App Engine documentation (including Using the High Replication Datastore). However I am still completely confused on the practical usage of it. I understand the benefits (from the video) and they sound great. But what I am lacking is a few practical examples. There are plenty of master/slave examples on the web, but very little illustrating (with proper documentation) the high replication datastore. The guestbook code example used in the Using the High Replication Datastore article illustrates the ancestor key by adding a new functionality that the previous guestbook code example does not have (seems you can change guestbook). This just adds to the confusion.

I often use djangoforms on GAE and I was wondering if someone can help me translate all these queries into high replication datastore compatible queries (let's forget for a moment the discussion that not all queries necessarily need to be high replication datastore compatible queries and focus on the example itself).

UPDATE: with high replication datastore compatible queries I refer to queries that always return the latest data and not potential stale data. Using entity groups seems to be the way to go here but as mentioned before, I don't have many practical code examples of how to do this, so that is what I am looking for!

So the queries in this article are:

The main recurring query in this article is:

query = db.GqlQuery("SELECT * FROM Item ORDER BY name")

which we will translate to:

query = Item.all().order('name')  // datastore request

validating the form happens like:

data = ItemForm(data=self.request.POST)
if data.is_valid():
    # Save the data, and redirect to the view page
    entity = data.save(commit=False)
    entity.added_by = users.get_current_user()
    entity.put()  // datastore request

and getting the latest entry from the datastore for populating a form happens like:

id = int(self.request.get('id'))
item = Item.get(db.Key.from_path('Item', id))  // datastore request
data = ItemForm(data=self.request.POST, instance=item)

So what do I/we need to do to make all these datastore requests compatible with the high replication datastore?

One last thing that is also not clear to me. Using ancestor keys, does this have any impact on the model in datastore. For example, in the guestbook code example they use:

def guestbook_key(guestbook_name=None):
  return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')

However 'Guestbook' does not exist in the model, so how can you use 'db.Key.from_path' on this and why would this work? Does this change how data is stored in the datastore which I need to keep into account when retrieving the data (e.g. does it add another field I should exclude from showing when using djangoforms)?

Like I said before, this is confusing me a lot and your help is greatly appreciated!

2条回答
何必那么认真
2楼-- · 2019-04-11 20:04

The changes to use the HRD are not in how queries are made, but in what guarantees are made about what data you get back. The example you give:

query = db.GqlQuery("SELECT * FROM Item ORDER BY name")

will work in the HRD as well. The catch (basically) is that this kind of query (using either this syntax, or the Item.all() form) can return objects slightly out-of-date. This is probably not a big deal with the guestbook.

Note that if you're getting an object by key directly, it will never be out-of-date. It's only for queries that you can see this issue. You can avoid this problem with queries by placing all the entities that need to be consistent in a single entity group. Note that this limits the rate at which you can write to the entity group.

In answer to your follow-up question, "Guestbook" is the name of the entity.

查看更多
在下西门庆
3楼-- · 2019-04-11 20:25

I'm not sure why you think you need to change your queries at all. The documentation that you link to clearly states:

The back end changes, but the datastore API does not change at all. You'll use the same programming interfaces no matter which datastore you're using.

The point of that page is just to say that queries may be out of sync if you don't use entity groups. Your final code snippet is just an example of that - the string 'Guestbook' is exactly an ancestor key. I don't understand why you think it needs to exist in the model. Once again, this is unchanged from the non-HR datastore - it has always been the case that keys are built up from paths, which can consist of arbitrary strings. You probably need to reread the documentation on entity groups and keys.

查看更多
登录 后发表回答