Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Add 'insert_auto_ids' support to 'Batch'.#515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6078fc1d5d28c9de78a696f12f165c10b10File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -72,6 +72,7 @@ def __init__(self, dataset_id=None, connection=None): | ||
| 'a dataset ID set.') | ||
| self._mutation = datastore_pb.Mutation() | ||
| self._auto_id_entities = [] | ||
| @property | ||
| def dataset_id(self): | ||
| @@ -107,6 +108,30 @@ def mutation(self): | ||
| """ | ||
| return self._mutation | ||
| def add_auto_id_entity(self, entity): | ||
| """Adds an entity to the list of entities to update with IDs. | ||
| When an entity has a partial key, calling ``save()`` adds an | ||
| insert_auto_id entry in the mutation. In order to make sure we | ||
| update the Entity once the transaction is committed, we need to | ||
| keep track of which entities to update (and the order is | ||
| important). | ||
| When you call ``save()`` on an entity inside a transaction, if | ||
| the entity has a partial key, it adds itself to the list of | ||
| entities to be updated once the transaction is committed by | ||
| calling this method. | ||
| :type entity: :class:`gcloud.datastore.entity.Entity` | ||
| :param entity: The entity to be updated with a completed key. | ||
| :raises: ValueError if the entity's key is alread completed. | ||
| """ | ||
| if not entity.key.is_partial: | ||
| raise ValueError("Entity has a completed key") | ||
| self._auto_id_entities.append(entity) | ||
| def put(self, entity): | ||
| """Remember an entity's state to be saved during ``commit``. | ||
| @@ -137,6 +162,9 @@ def put(self, entity): | ||
| self.dataset_id, key_pb, properties, | ||
| exclude_from_indexes=exclude, mutation=self.mutation) | ||
| if entity.key.is_partial: | ||
| self._auto_id_entities.append(entity) | ||
| def delete(self, key): | ||
| """Remember a key to be deleted durring ``commit``. | ||
| @@ -159,7 +187,15 @@ def commit(self): | ||
| however it can be called explicitly if you don't want to use a | ||
| context manager. | ||
| """ | ||
| self.connection.commit(self._dataset_id, self.mutation) | ||
| response = self.connection.commit(self._dataset_id, self.mutation) | ||
| # If the back-end returns without error, we are guaranteed that | ||
| # the response's 'insert_auto_id_key' will match (length and order) | ||
| # the request's 'insert_auto_id` entities, which are derived from | ||
| # our '_auto_id_entities' (no partial success). | ||
| for new_key_pb, entity in zip(response.insert_auto_id_key, | ||
| self._auto_id_entities): | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| new_id = new_key_pb.path_element[-1].id | ||
| entity.key = entity.key.completed_key(new_id) | ||
| def __enter__(self): | ||
| return self | ||
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.