-1

The datastore sets the entity group of a transaction automatically. I would like to find out what this is set to while I am inside the transaction. To elaborate, suppose I have the following function foo that runs in a transaction:

@ndb.transactional
def foo():
    bar1()
    bar2()

bar1 performs some actions on the datastore. It gets, puts, create entities, runs queries etc. Since bar1 is running inside a transaction all the actions that it performs, and queries it runs, must be restricted to an single entity group, as per the strictly enforced requirement of the datastore API (lets assume this is not a XG transaction).

Now in bar2 I want to programmatically determine what entity group of the transaction is. I do not have any information about what went on in bar1.

Is this possible?

Mainly I want this for testing, and understanding how and when the entity group for a transaction is set.

I could not find a way to do this from the official documentation.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
user2771609
  • 1,867
  • 1
  • 15
  • 36

1 Answers1

0

It is in the documentation. Look at the structure of keys and entity groups.

https://cloud.google.com/appengine/docs/python/datastore/entities#Python_Transactions_and_entity_groups

"an entity group is a set of entities connected through ancestry to a common root element. The organization of data into entity groups can limit what transactions can be performed:"

Entity groups are defined by the parent in the key (a single entity with no parent key has an entity group of itself).

So if you have the key you have the entity group.

Tim Hoffman
  • 12,976
  • 1
  • 17
  • 29
  • I think you misunderstood. I know the documentation describes what the entity group is and how it relates to transactions. What I want is to **programmatically** retrieve the key of **current** entity group of a running transaction. – user2771609 Sep 06 '15 at 05:15
  • 1
    Well the entity group is defined by the key of the entities you have in your transaction. If you have entities you have the key. If you have the key you know the entity group - don't you ? If you have XG (cross entity group transactions, then each of the distinct parent keys define each of the entity groups.). The key property of entity gives you the key. So what are you having a problem with ? – Tim Hoffman Sep 06 '15 at 06:29
  • Reread https://cloud.google.com/appengine/docs/python/datastore/entities#Python_Ancestor_paths on ancestor paths. It's pretty clear what the root of the entity group is. – Tim Hoffman Sep 06 '15 at 06:32
  • With all that said there is one undocumented method of a key that makes things easier - called root(). """Return the root key. This is either self or the highest parent.""" – Tim Hoffman Sep 06 '15 at 06:46
  • You are still missing the question. Let me try to elaborate. Say I have a function `foo()` that runs in a transaction. `foo` calls `bar1()` and `bar2()`. `bar1` performs some actions on the datastore. It puts, creates, runs queries, etc. These actions, by virtue of being in a transaction are limited to being in a single entity group. The datastore API ensures that. Now in **`bar2`**, without knowing anything about `bar1`, I want to retrieve what current entity group is (the root entity that `bar1` used for all its datastore operations). Does that help clarify? – user2771609 Sep 06 '15 at 15:37