Search code examples
pythongoogle-app-enginetransactionsbigtable

How is Model.get_or_insert() an ancestor transaction in app engine


I don't understand how google app engine for python's Model.get_or_insert(key_name, **kwds) method works.

From my understanding, all datastore operations inside a transaction must be ancestor queries. However, I don't see how the documentation for Model.get_or_insert(key_name, **kwds) is an ancestor query. For the following example, since 'parent' is not specified in the arguments for the last line, wouldn't the result of parent=kwds.get('parent') in the second line be None, thus negating the query as an ancestor query?:

def txn(key_name, **kwds):
    entity = Story.get_by_key_name(key_name, parent=kwds.get('parent')) # parent is not defined?
    if entity is None:
        entity = Story(key_name=key_name, **kwds)
        entity.put()
    return entity

def get_or_insert(key_name, **kwargs):
    return db.run_in_transaction(txn, key_name, **kwargs)

get_or_insert('some key', title="The Three Little Pigs")  # no parent specified

(link to documentation)

I'm not trying to split hairs here, I'm just having trouble understanding how to properly use transactions in my own application and was looking to this documentation to augment my understanding.

Thanks so much for your help!


Solution

  • You need ancestor queries to guarantee atomicity in a transaction that involves multiple entities. All the entities in the transaction need to share the same ancestor. Model.get_or_insert() only retrieves one entity, which obviously shares the same ancestor with itself.

    Key paths/ancestors determine how data is stored in the App Engine datastore. Entities with a common ancestor are stored on the same database shard, making it easier to implement atomic transactions without synchronizing between shards.

    If you're using the HRD, which you probably are at this point, Cross Group transactions are available too, although I'm not too certain on how they work behind the scenes. http://code.google.com/appengine/docs/python/datastore/overview.html#Cross_Group_Transactions