Norio Akagi
Norio Akagi

Reputation: 725

Google Cloud Datastore - is it possible to use a transaction for a single root entity?

I'm new to Google CloudDatastore and reading a document.
(Note: we don't plan to use Google AppEngine, just DataStore only.)

According to the document, DataStore supports transaction but

If you want to use queries within a transaction,
your data must be organized into entity groups in such a way
that you can specify ancestor filters that will match the right data.

So I thought as long as I want to use transaction, I am forced to create some parent key and set it as an ancestor. And all entities under the parent have a limitation that update and transaction can be only performed once per second.

However, I also see a very simple example of insert here: https://cloud.google.com/datastore/docs/concepts/entities#datastore-insert-python

with client.transaction():
    incomplete_key = client.key('Task')

    task = datastore.Entity(key=incomplete_key)

    task.update({
        'category': 'Personal',
        'done': False,
        'priority': 4,
        'description': 'Learn Cloud Datastore'
    })

    client.put(task)

It doesn't specify a parent and use a single root entity inside a transaction, does it ? Even about examples in Transaction page only the one for "read-only transaction" explicitly specifies a parent. Do other ones simply omit a parent while it actually exists?

I'm wondering I can use transaction without an entity group (= without a big performance degrade) if I can specify a key of a root entity, but there is no such description in the document...

I'd appreciate if someone can clarify the behavior. Thanks.

Upvotes: 1

Views: 740

Answers (2)

Dan McGrath
Dan McGrath

Reputation: 42018

Transactions across multiple entity groups is indeed allowed (with a limit of 25 entity groups per documentation)

If you want to use queries within a transaction,

Note this key sentence in the text you quoted. It is saying any 'queries' you want to issue inside of a transaction need to be ancestor queries. This is because non-ancestor queries are eventually consistent, so it would be impossible for the transaction engine to reason about any state changes and hence not know when to fail or succeed the transaction. It is not saying you cannot to do transactions across entity groups.

It doesn't specify a parent and use a single root entity inside a transaction, does it ?

I think this is the other source of confusion. Only children entities have parents specified to denote which entity group they are in. When no parent is specified, then the entity in question is a root entity (it's parent is root). Another way of saying this is every root entity is it's own entity group.

Upvotes: 2

TheAddonDepot
TheAddonDepot

Reputation: 8964

Technically the task entity in your description constitutes an entity group even though it has no child entities. The max number of entity groups allowed is 25 so if you try to create more than 25 top-level entities using this pattern your queries will fail.

The way I avoid performance hits is to use multiple entity groups. I structure my datastore so that I have multiple root entities and try to limit multiple transactions within an entity group.

Upvotes: 0

Related Questions